DockerCommandExecutor { public static String executeDockerCommand(DockerCommand dockerCommand, String containerId, Map<String, String> env, PrivilegedOperationExecutor privilegedOperationExecutor, boolean disableFailureLogging, Context nmContext) throws ContainerExecutionException { PrivilegedOperation dockerOp = dockerCommand.preparePrivilegedOperation( dockerCommand, containerId, env, nmContext); if (disableFailureLogging) { dockerOp.disableFailureLogging(); } LOG.debug("Running docker command: {}", dockerCommand); try { String result = privilegedOperationExecutor .executePrivilegedOperation(null, dockerOp, null, env, true, false); if (result != null && !result.isEmpty()) { result = result.trim(); } return result; } catch (PrivilegedOperationException e) { throw new ContainerExecutionException("Docker operation failed", e.getExitCode(), e.getOutput(), e.getErrorOutput()); } } private  DockerCommandExecutor(); static String executeDockerCommand(DockerCommand dockerCommand,
      String containerId, Map<String, String> env,
      PrivilegedOperationExecutor privilegedOperationExecutor,
      boolean disableFailureLogging, Context nmContext); static DockerContainerStatus getContainerStatus(String containerId,
      PrivilegedOperationExecutor privilegedOperationExecutor,
      Context nmContext); static DockerContainerStatus parseContainerStatus(
      String containerStatusStr); static boolean isStoppable(DockerContainerStatus containerStatus); static boolean isKillable(DockerContainerStatus containerStatus); static boolean isRemovable(DockerContainerStatus containerStatus); static boolean isStartable(DockerContainerStatus containerStatus);  }
@Test public void testExecuteDockerCommand() throws Exception { DockerStopCommand dockerStopCommand = new DockerStopCommand(MOCK_CONTAINER_ID); DockerCommandExecutor.executeDockerCommand(dockerStopCommand, cId.toString(), env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType.RUN_DOCKER_CMD.name(), ops.get(0).getOperationType().name()); }
@Test public void testExecuteDockerRm() throws Exception { DockerRmCommand dockerCommand = new DockerRmCommand(MOCK_CONTAINER_ID, null); DockerCommandExecutor.executeDockerCommand(dockerCommand, MOCK_CONTAINER_ID, env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); PrivilegedOperation privOp = ops.get(0); List<String> args = privOp.getArguments(); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType. REMOVE_DOCKER_CONTAINER.name(), privOp.getOperationType().name()); assertEquals(1, args.size()); assertEquals(MOCK_CONTAINER_ID, args.get(0)); }
@Test public void testExecuteDockerRmWithCgroup() throws Exception { DockerRmCommand dockerCommand = new DockerRmCommand(MOCK_CONTAINER_ID, MOCK_CGROUP_HIERARCHY); DockerCommandExecutor.executeDockerCommand(dockerCommand, MOCK_CONTAINER_ID, env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); PrivilegedOperation privOp = ops.get(0); List<String> args = privOp.getArguments(); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType. REMOVE_DOCKER_CONTAINER.name(), privOp.getOperationType().name()); assertEquals(2, args.size()); assertEquals(MOCK_CGROUP_HIERARCHY, args.get(0)); assertEquals(MOCK_CONTAINER_ID, args.get(1)); }
@Test public void testExecuteDockerStop() throws Exception { DockerStopCommand dockerCommand = new DockerStopCommand(MOCK_CONTAINER_ID); DockerCommandExecutor.executeDockerCommand(dockerCommand, MOCK_CONTAINER_ID, env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); List<String> dockerCommands = getValidatedDockerCommands(ops); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType.RUN_DOCKER_CMD.name(), ops.get(0).getOperationType().name()); assertEquals(3, dockerCommands.size()); assertEquals("[docker-command-execution]", dockerCommands.get(0)); assertEquals(" docker-command=stop", dockerCommands.get(1)); assertEquals(" name=" + MOCK_CONTAINER_ID, dockerCommands.get(2)); }
@Test public void testExecuteDockerPull() throws Exception { DockerPullCommand dockerCommand = new DockerPullCommand(MOCK_IMAGE_NAME); DockerCommandExecutor.executeDockerCommand(dockerCommand, MOCK_CONTAINER_ID, env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); List<String> dockerCommands = getValidatedDockerCommands(ops); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType.RUN_DOCKER_CMD.name(), ops.get(0).getOperationType().name()); assertEquals(3, dockerCommands.size()); assertEquals("[docker-command-execution]", dockerCommands.get(0)); assertEquals(" docker-command=pull", dockerCommands.get(1)); assertEquals(" image=" + MOCK_IMAGE_NAME, dockerCommands.get(2)); }
@Test public void testExecuteDockerLoad() throws Exception { DockerLoadCommand dockerCommand = new DockerLoadCommand(MOCK_LOCAL_IMAGE_NAME); DockerCommandExecutor.executeDockerCommand(dockerCommand, MOCK_CONTAINER_ID, env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); List<String> dockerCommands = getValidatedDockerCommands(ops); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType.RUN_DOCKER_CMD.name(), ops.get(0).getOperationType().name()); assertEquals(3, dockerCommands.size()); assertEquals("[docker-command-execution]", dockerCommands.get(0)); assertEquals(" docker-command=load", dockerCommands.get(1)); assertEquals(" image=" + MOCK_LOCAL_IMAGE_NAME, dockerCommands.get(2)); }
@Test public void testExecuteDockerKillSIGQUIT() throws Exception { DockerKillCommand dockerKillCommand = new DockerKillCommand(MOCK_CONTAINER_ID) .setSignal(ContainerExecutor.Signal.QUIT.name()); DockerCommandExecutor.executeDockerCommand(dockerKillCommand, MOCK_CONTAINER_ID, env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); List<String> dockerCommands = getValidatedDockerCommands(ops); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType.RUN_DOCKER_CMD.name(), ops.get(0).getOperationType().name()); assertEquals(4, dockerCommands.size()); assertEquals("[docker-command-execution]", dockerCommands.get(0)); assertEquals(" docker-command=kill", dockerCommands.get(1)); assertEquals(" name=" + MOCK_CONTAINER_ID, dockerCommands.get(2)); assertEquals(" signal=" + ContainerExecutor.Signal.QUIT.name(), dockerCommands.get(3)); }
@Test public void testExecuteDockerKillSIGKILL() throws Exception { DockerKillCommand dockerKillCommand = new DockerKillCommand(MOCK_CONTAINER_ID) .setSignal(ContainerExecutor.Signal.KILL.name()); DockerCommandExecutor.executeDockerCommand(dockerKillCommand, MOCK_CONTAINER_ID, env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); List<String> dockerCommands = getValidatedDockerCommands(ops); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType.RUN_DOCKER_CMD.name(), ops.get(0).getOperationType().name()); assertEquals(4, dockerCommands.size()); assertEquals("[docker-command-execution]", dockerCommands.get(0)); assertEquals(" docker-command=kill", dockerCommands.get(1)); assertEquals(" name=" + MOCK_CONTAINER_ID, dockerCommands.get(2)); assertEquals(" signal=" + ContainerExecutor.Signal.KILL.name(), dockerCommands.get(3)); }
@Test public void testExecuteDockerKillSIGTERM() throws Exception { DockerKillCommand dockerKillCommand = new DockerKillCommand(MOCK_CONTAINER_ID) .setSignal(ContainerExecutor.Signal.TERM.name()); DockerCommandExecutor.executeDockerCommand(dockerKillCommand, MOCK_CONTAINER_ID, env, mockExecutor, false, nmContext); List<PrivilegedOperation> ops = MockPrivilegedOperationCaptor .capturePrivilegedOperations(mockExecutor, 1, true); List<String> dockerCommands = getValidatedDockerCommands(ops); assertEquals(1, ops.size()); assertEquals(PrivilegedOperation.OperationType.RUN_DOCKER_CMD.name(), ops.get(0).getOperationType().name()); assertEquals(4, dockerCommands.size()); assertEquals("[docker-command-execution]", dockerCommands.get(0)); assertEquals(" docker-command=kill", dockerCommands.get(1)); assertEquals(" name=" + MOCK_CONTAINER_ID, dockerCommands.get(2)); assertEquals(" signal=" + ContainerExecutor.Signal.TERM.name(), dockerCommands.get(3)); }