@Test
public void testGroupPolicies()
    throws IOException, ContainerExecutionException {

  // Generate new policy files each containing one grant
  File openSocketPolicyFile =
      File.createTempFile("openSocket", "policy", baseTestDirectory);
  File classLoaderPolicyFile =
      File.createTempFile("createClassLoader", "policy", baseTestDirectory);
  Permission socketPerm = new SocketPermission("localhost:0", "listen");
  Permission runtimePerm = new RuntimePermission("createClassLoader");
  StringBuilder socketPermString = new StringBuilder();
  Formatter openSocketPolicyFormatter = new Formatter(socketPermString);
  openSocketPolicyFormatter.format(SOCKET_PERMISSION_FORMAT,
      socketPerm.getClass().getName(), socketPerm.getName(),
      socketPerm.getActions());
  FileWriter socketPermWriter = new FileWriter(openSocketPolicyFile);
  socketPermWriter.write(socketPermString.toString());
  socketPermWriter.close();
  StringBuilder classLoaderPermString = new StringBuilder();
  Formatter classLoaderPolicyFormatter = new Formatter(classLoaderPermString);
  classLoaderPolicyFormatter.format(RUNTIME_PERMISSION_FORMAT,
      runtimePerm.getClass().getName(), runtimePerm.getName());
  FileWriter classLoaderPermWriter = new FileWriter(classLoaderPolicyFile);
  classLoaderPermWriter.write(classLoaderPermString.toString());
  classLoaderPermWriter.close();
  conf.set(YarnConfiguration.YARN_CONTAINER_SANDBOX_POLICY_GROUP_PREFIX +
      WHITELIST_GROUP, openSocketPolicyFile.toString());
  conf.set(YarnConfiguration.YARN_CONTAINER_SANDBOX_POLICY_GROUP_PREFIX +
      NORMAL_GROUP, classLoaderPolicyFile.toString());
  String[] inputCommand = {"$JAVA_HOME/bin/java jar MyJob.jar"};
  List<String> commands = Arrays.asList(inputCommand);
  runtimeContextBuilder.setExecutionAttribute(USER, WHITELIST_USER);
  runtimeContextBuilder.setExecutionAttribute(CONTAINER_RUN_CMDS, commands);
  runtime.prepareContainer(runtimeContextBuilder.build());
  //pull generated policy from cmd
  Matcher policyMatches = Pattern.compile(POLICY_APPEND_FLAG + "=?([^ ]+)")
      .matcher(commands.get(0));
  policyMatches.find();
  String generatedPolicy = policyMatches.group(1);
  //Test that generated policy file has included both policies
  Assert.assertTrue(
      Files.readAllLines(Paths.get(generatedPolicy)).contains(
          classLoaderPermString.toString().split("\n")[1]));
  Assert.assertTrue(
      Files.readAllLines(Paths.get(generatedPolicy)).contains(
          socketPermString.toString().split("\n")[1]));
}