DockerExecuteAction extends AbstractTestAction { public DockerCommand<?> getCommand() { return command; } DockerExecuteAction(Builder builder); @Override void doExecute(TestContext context); DockerCommand<?> getCommand(); DockerClient getDockerClient(); String getExpectedCommandResult(); static final String DEFAULT_JSON_MESSAGE_VALIDATOR; }
@Test public void testInfo() throws Exception { InfoCmd command = Mockito.mock(InfoCmd.class); com.github.dockerjava.api.model.Info result = new com.github.dockerjava.api.model.Info(); reset(dockerClient, command); when(dockerClient.infoCmd()).thenReturn(command); when(command.exec()).thenReturn(result); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new Info()) .build(); action.execute(context); Assert.assertEquals(action.getCommand().getCommandResult(), result); }
@Test public void testPing() throws Exception { PingCmd command = Mockito.mock(PingCmd.class); reset(dockerClient, command); when(dockerClient.pingCmd()).thenReturn(command); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new Ping()) .build(); action.execute(context); Assert.assertEquals(((ResponseItem)action.getCommand().getCommandResult()).getStatus(), "success"); }
@Test public void testVersion() throws Exception { VersionCmd command = Mockito.mock(VersionCmd.class); com.github.dockerjava.api.model.Version result = new com.github.dockerjava.api.model.Version(); reset(dockerClient, command); when(dockerClient.versionCmd()).thenReturn(command); when(command.exec()).thenReturn(result); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new Version()) .build(); action.execute(context); Assert.assertEquals(action.getCommand().getCommandResult(), result); }
@Test public void testCreate() throws Exception { CreateContainerCmd command = Mockito.mock(CreateContainerCmd.class); CreateContainerResponse response = new CreateContainerResponse(); response.setId(UUID.randomUUID().toString()); reset(dockerClient, command); when(dockerClient.createContainerCmd("image_create")).thenReturn(command); when(command.withName("my_container")).thenReturn(command); when(command.exec()).thenReturn(response); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ContainerCreate() .image("image_create") .name("myContainer")) .build(); action.execute(context); Assert.assertEquals(action.getCommand().getCommandResult(), response); Assert.assertEquals(context.getVariable(DockerMessageHeaders.CONTAINER_ID), response.getId()); }
@Test public void testCreateNoName() throws Exception { CreateContainerCmd command = Mockito.mock(CreateContainerCmd.class); InspectContainerCmd inspectCommand = Mockito.mock(InspectContainerCmd.class); InspectContainerResponse inspectResponse = Mockito.mock(InspectContainerResponse.class); CreateContainerResponse response = new CreateContainerResponse(); response.setId(UUID.randomUUID().toString()); reset(dockerClient, command, inspectCommand, inspectResponse); when(dockerClient.createContainerCmd("image_create")).thenReturn(command); when(dockerClient.inspectContainerCmd(response.getId())).thenReturn(inspectCommand); when(command.exec()).thenReturn(response); when(inspectCommand.exec()).thenReturn(inspectResponse); when(inspectResponse.getName()).thenReturn("/my_container"); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ContainerCreate().image("image_create")) .build(); action.execute(context); Assert.assertEquals(action.getCommand().getCommandResult(), response); Assert.assertEquals(context.getVariable(DockerMessageHeaders.CONTAINER_ID), response.getId()); Assert.assertEquals(context.getVariable(DockerMessageHeaders.CONTAINER_NAME), "my_container"); }
@Test public void testInspectContainer() throws Exception { InspectContainerCmd command = Mockito.mock(InspectContainerCmd.class); InspectContainerResponse response = new InspectContainerResponse(); reset(dockerClient, command); when(dockerClient.inspectContainerCmd("container_inspect")).thenReturn(command); when(command.exec()).thenReturn(response); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ContainerInspect().container("container_inspect")) .build(); action.execute(context); Assert.assertEquals(action.getCommand().getCommandResult(), response); }
@Test public void testInspectImage() throws Exception { InspectImageCmd command = Mockito.mock(InspectImageCmd.class); InspectImageResponse response = new InspectImageResponse(); reset(dockerClient, command); when(dockerClient.inspectImageCmd("image_inspect")).thenReturn(command); when(command.exec()).thenReturn(response); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ImageInspect().image("image_inspect")) .build(); action.execute(context); Assert.assertEquals(action.getCommand().getCommandResult(), response); }
@Test public void testRemoveContainer() throws Exception { RemoveContainerCmd command = Mockito.mock(RemoveContainerCmd.class); reset(dockerClient, command); when(dockerClient.removeContainerCmd("container_inspect")).thenReturn(command); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ContainerRemove().container("container_inspect")) .build(); action.execute(context); Assert.assertEquals(((ResponseItem)action.getCommand().getCommandResult()).getStatus(), "success"); }
@Test public void testRemoveImage() throws Exception { RemoveImageCmd command = Mockito.mock(RemoveImageCmd.class); reset(dockerClient, command); when(dockerClient.removeImageCmd("image_remove")).thenReturn(command); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ImageRemove().image("image_remove")) .build(); action.execute(context); Assert.assertEquals(((ResponseItem)action.getCommand().getCommandResult()).getStatus(), "success"); }
@Test public void testStartContainer() throws Exception { StartContainerCmd command = Mockito.mock(StartContainerCmd.class); reset(dockerClient, command); when(dockerClient.startContainerCmd("container_start")).thenReturn(command); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ContainerStart().container("container_start")) .build(); action.execute(context); Assert.assertEquals(((ResponseItem)action.getCommand().getCommandResult()).getStatus(), "success"); }
@Test public void testStopContainer() throws Exception { StopContainerCmd command = Mockito.mock(StopContainerCmd.class); reset(dockerClient, command); when(dockerClient.stopContainerCmd("container_stop")).thenReturn(command); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ContainerStop().container("container_stop")) .build(); action.execute(context); Assert.assertEquals(((ResponseItem)action.getCommand().getCommandResult()).getStatus(), "success"); }
@Test public void testWaitContainer() throws Exception { WaitContainerCmd command = Mockito.mock(WaitContainerCmd.class); final WaitResponse responseItem = Mockito.mock(WaitResponse.class); reset(dockerClient, command); when(dockerClient.waitContainerCmd("container_wait")).thenReturn(command); doAnswer(new Answer<WaitContainerResultCallback>() { @Override public WaitContainerResultCallback answer(InvocationOnMock invocation) throws Throwable { WaitContainerResultCallback resultCallback = (WaitContainerResultCallback) invocation.getArguments()[0]; resultCallback.onNext(responseItem); resultCallback.onComplete(); return resultCallback; } }).when(command).exec(any(WaitContainerResultCallback.class)); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ContainerWait().container("container_wait")) .build(); action.execute(context); Assert.assertEquals(((WaitResponse)action.getCommand().getCommandResult()).getStatusCode(), new Integer(0)); }
@Test public void testPullImage() throws Exception { PullImageCmd command = Mockito.mock(PullImageCmd.class); final PullResponseItem responseItem = Mockito.mock(PullResponseItem.class); reset(dockerClient, command, responseItem); when(dockerClient.pullImageCmd("image_pull")).thenReturn(command); when(responseItem.isPullSuccessIndicated()).thenReturn(true); when(command.withTag("image_tag")).thenReturn(command); doAnswer(new Answer<PullImageResultCallback>() { @Override public PullImageResultCallback answer(InvocationOnMock invocation) throws Throwable { PullImageResultCallback resultCallback = (PullImageResultCallback) invocation.getArguments()[0]; resultCallback.onNext(responseItem); resultCallback.onComplete(); return resultCallback; } }).when(command).exec(any(PullImageResultCallback.class)); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ImagePull().image("image_pull").tag("image_tag")) .build(); action.execute(context); Assert.assertEquals(action.getCommand().getCommandResult(), responseItem); }
@Test public void testBuildImage() throws Exception { BuildImageCmd command = Mockito.mock(BuildImageCmd.class); final BuildResponseItem responseItem = Mockito.mock(BuildResponseItem.class); reset(dockerClient, command, responseItem); when(dockerClient.buildImageCmd()).thenReturn(command); when(responseItem.isBuildSuccessIndicated()).thenReturn(true); when(responseItem.getImageId()).thenReturn("new_image"); when(command.withDockerfile(any(File.class))).thenReturn(command); when(command.withTag("latest")).thenReturn(command); doAnswer(new Answer<BuildImageResultCallback>() { @Override public BuildImageResultCallback answer(InvocationOnMock invocation) throws Throwable { BuildImageResultCallback resultCallback = (BuildImageResultCallback) invocation.getArguments()[0]; resultCallback.onNext(responseItem); resultCallback.onComplete(); return resultCallback; } }).when(command).exec(any(BuildImageResultCallback.class)); DockerExecuteAction action = new DockerExecuteAction.Builder() .client(client) .command(new ImageBuild() .dockerFile(new ClassPathResource("com/consol/citrus/docker/Dockerfile")) .tag("new_image:latest")) .build(); action.execute(context); Assert.assertEquals(action.getCommand().getCommandResult(), responseItem); Assert.assertEquals(context.getVariable(DockerMessageHeaders.IMAGE_ID), "new_image"); }