PrivilegedOperationExecutor { public static PrivilegedOperation squashCGroupOperations (List<PrivilegedOperation> ops) throws PrivilegedOperationException { if (ops.size() == 0) { return null; } StringBuffer finalOpArg = new StringBuffer(PrivilegedOperation .CGROUP_ARG_PREFIX); boolean noTasks = true; for (PrivilegedOperation op : ops) { if (!op.getOperationType() .equals(PrivilegedOperation.OperationType.ADD_PID_TO_CGROUP)) { LOG.warn("Unsupported operation type: " + op.getOperationType()); throw new PrivilegedOperationException("Unsupported operation type:" + op.getOperationType()); } List<String> args = op.getArguments(); if (args.size() != 1) { LOG.warn("Invalid number of args: " + args.size()); throw new PrivilegedOperationException("Invalid number of args: " + args.size()); } String arg = args.get(0); String tasksFile = StringUtils.substringAfter(arg, PrivilegedOperation.CGROUP_ARG_PREFIX); if (tasksFile == null || tasksFile.isEmpty()) { LOG.warn("Invalid argument: " + arg); throw new PrivilegedOperationException("Invalid argument: " + arg); } if (tasksFile.equals(PrivilegedOperation.CGROUP_ARG_NO_TASKS)) { continue; } if (noTasks == false) { finalOpArg.append(PrivilegedOperation.LINUX_FILE_PATH_SEPARATOR); finalOpArg.append(tasksFile); } else { finalOpArg.append(tasksFile); noTasks = false; } } if (noTasks) { finalOpArg.append(PrivilegedOperation.CGROUP_ARG_NO_TASKS); } PrivilegedOperation finalOp = new PrivilegedOperation( PrivilegedOperation.OperationType.ADD_PID_TO_CGROUP, finalOpArg .toString()); return finalOp; } private  PrivilegedOperationExecutor(Configuration conf); static String getContainerExecutorExecutablePath(Configuration conf); static PrivilegedOperationExecutor getInstance(Configuration conf); String[] getPrivilegedOperationExecutionCommand(List<String>
      prefixCommands,
      PrivilegedOperation operation); String executePrivilegedOperation(List<String> prefixCommands,
      PrivilegedOperation operation, File workingDir,
      Map<String, String> env, boolean grabOutput, boolean inheritParentEnv); String executePrivilegedOperation(PrivilegedOperation operation,
      boolean grabOutput); static PrivilegedOperation squashCGroupOperations(List<PrivilegedOperation> ops);  }
@Test public void testSquashCGroupOperationsWithInvalidOperations() { List<PrivilegedOperation> ops = new ArrayList<>(); ops.add(opTasksNone); ops.add(opDisallowed); try { PrivilegedOperationExecutor.squashCGroupOperations(ops); Assert.fail("Expected squash operation to fail with an exception!"); } catch (PrivilegedOperationException e) { LOG.info("Caught expected exception : " + e); } ops.clear(); ops.add(opTasksNone); ops.add(opTasksInvalid); try { PrivilegedOperationExecutor.squashCGroupOperations(ops); Assert.fail("Expected squash operation to fail with an exception!"); } catch (PrivilegedOperationException e) { LOG.info("Caught expected exception : " + e); } }
@Test public void testSquashCGroupOperationsWithValidOperations() { List<PrivilegedOperation> ops = new ArrayList<>(); ops.clear(); ops.add(opTasks1); ops.add(opTasksNone); ops.add(opTasks2); ops.add(opTasks3); try { PrivilegedOperation op = PrivilegedOperationExecutor .squashCGroupOperations(ops); String expected = new StringBuffer (PrivilegedOperation.CGROUP_ARG_PREFIX) .append(cGroupTasks1).append(PrivilegedOperation .LINUX_FILE_PATH_SEPARATOR) .append(cGroupTasks2).append(PrivilegedOperation .LINUX_FILE_PATH_SEPARATOR) .append(cGroupTasks3).toString(); Assert.assertEquals(1, op.getArguments().size()); Assert.assertEquals(expected, op.getArguments().get(0)); } catch (PrivilegedOperationException e) { LOG.info("Caught unexpected exception : " + e); Assert.fail("Caught unexpected exception: " + e); } }