DefaultDockerClientFacade extends BaseDockerClientFacade { @Nonnull @Override public NewAgentHolderInfo createAgent(@Nonnull CreateAgentParameters createAgentParameters) { DockerCloudUtils.requireNonNull(createAgentParameters, "Agent creation parameters cannot be null."); try { Node agentHolderSpec = createAgentParameters.getAgentHolderSpec(); String resolvedImage = createAgentParameters. getImageName(). orElse(createAgentParameters.getAgentHolderSpec().getAsString("Image", null)); if (resolvedImage == null) { throw new DockerClientFacadeException("Failed to determine image name."); } pullIfRequired(resolvedImage, createAgentParameters.getPullStrategy(), createAgentParameters.getPullStatusListener(), createAgentParameters.getRegistryCredentials()); EditableNode editableContainerSpec = agentHolderSpec.editNode(); Node imageInspect = client.inspectImage(resolvedImage); pinContainerImage(editableContainerSpec, imageInspect); clearExistingPluginProperties(editableContainerSpec, imageInspect); applyEnv(editableContainerSpec, createAgentParameters.getEnv()); applyLabels(editableContainerSpec, createAgentParameters.getLabels()); Node containerNode = client.createContainer(editableContainerSpec.saveNode(), null); String id = containerNode.getAsString("Id"); List<String> warnings = containerNode.getArray("Warnings", Node.EMPTY_ARRAY).getArrayValues(). stream().map(Node::getAsString).collect(Collectors.toList()); Node inspectNode = client.inspectContainer(id); String containerName = inspectNode.getAsString("Name"); if (containerName.startsWith("/")) { containerName = containerName.substring(1); } return new NewAgentHolderInfo(id, containerName, resolvedImage, warnings); } catch (NodeProcessingException e) { throw new DockerClientFacadeException("Failed to setup agent container.", e); } } DefaultDockerClientFacade(@Nonnull DockerClient client); @Nonnull @Override NewAgentHolderInfo createAgent(@Nonnull CreateAgentParameters createAgentParameters); @Override String startAgent(@Nonnull String containerId); @Override String restartAgent(@Nonnull String containerId); @Nonnull @Override List<AgentHolderInfo> listAgentHolders(@Nonnull String labelFilter, @Nonnull String valueFilter); @Override boolean terminateAgentContainer(@Nonnull String containerId, @Nonnull Duration timeout, boolean removeContainer); @Nonnull @Override CharSequence getLogs(@Nonnull String containerId); @Nonnull @Override StreamHandler streamLogs(@Nonnull String containerId); @Override boolean supportQueryingLogs(); static final String CONTAINER_RUNNING_STATE; }
@Test public void createAgentMustReturnAgentId() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); dockerClient.localImage("resolved-image", "latest"); NewAgentHolderInfo agentInfo = facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); assertThat(agentInfo.getId()).isEqualTo(dockerClient.getContainers().get(0).getId()); }
@Test public void createAgentMustReturnAgentName() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); dockerClient.localImage("resolved-image", "latest"); NewAgentHolderInfo agentInfo = facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); assertThat(agentInfo.getName()).isEqualTo(dockerClient.getContainers().get(0).getName()); }
@Test public void createAgentMustReturnResolvedImageName() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); dockerClient.localImage("resolved-image", "latest"); NewAgentHolderInfo agentInfo = facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); assertThat(agentInfo.getResolvedImage()).isEqualTo("resolved-image:latest"); }
@Test public void createAgentMustReturnWarnings() { DockerClientFacade facade = createFacade(dockerClient); dockerClient .localImage("resolved-image", "latest") .containerCreationWarning("foo") .containerCreationWarning("bar"); NewAgentHolderInfo agentInfo = facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); assertThat(agentInfo.getWarnings()).isEqualTo(listOf("foo", "bar")); }
@Test public void sourceImageIdIsSet() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); TestImage img = dockerClient.newLocalImage("resolved-image", "latest"); String containerId = facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")).getId(); List<Container> containers = dockerClient.getContainers(); assertThat(containers).hasSize(1).first().matches(container -> container.getId().equals(containerId)); Container container = containers.get(0); assertThat(container.getLabels()).isEqualTo(mapOf(pair(DockerCloudUtils.SOURCE_IMAGE_ID_LABEL, img.getId()))); }
@Test public void trimPreviousManagedLabels() { TestImage img = dockerClient.newLocalImage("resolved-image", "latest"); img.label(DockerCloudUtils.NS_PREFIX + "foo1", "bar1"); img.label("foo2", "bar2"); DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); Container container = dockerClient.getContainers().get(0); assertThat(container.getLabels()).containsEntry(DockerCloudUtils.NS_PREFIX + "foo1", "") .containsEntry("foo2", "bar2"); }
@Test public void trimPreviousManagedEnv() { TestImage img = dockerClient.newLocalImage("resolved-image", "latest"); img.env(DockerCloudUtils.ENV_PREFIX + "FOO1", "bar1"); img.env("FOO2", "bar2"); DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); Container container = dockerClient.getContainers().get(0); assertThat(container.getEnv()).containsEntry(DockerCloudUtils.ENV_PREFIX + "FOO1", "") .containsEntry("FOO2", "bar2"); }
@Test public void successfulPull() { TestImage img = dockerClient.newRegistryImage("resolved-image", "latest"); DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). pullStrategy(PullStrategy.PULL)); assertThat(dockerClient.getLocalImages()).hasSize(1).containsExactly(img); }
@Test(expected = DockerClientFacadeException.class) public void failedPull() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). pullStrategy(PullStrategy.PULL)); }
@Test public void pullListener() { dockerClient.newRegistryImage("resolved-image", "latest").pullProgress("layer1", "Pulling fs layer", null, null) .pullProgress("layer1", "Downloading", 0, 100).pullProgress("layer2", "Pulling fs layer", null, null) .pullProgress("layer2", "Downloading", 0, new BigInteger("10000000000")) .pullProgress("layer1", "Downloading", 50, 100).pullProgress("layer1", "Downloading", 100, 100) .pullProgress("layer1", "Pull complete", null, null) .pullProgress("layer2", "Downloading", new BigInteger("2500000000"), new BigInteger("10000000000")) .pullProgress("layer2", "Downloading", new BigInteger("10000000000"), new BigInteger("10000000000")) .pullProgress("layer2", "Pull complete", null, null); DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); List<ListenerInvocation> invocations = new ArrayList<>(); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). pullStrategy(PullStrategy.PULL). pullStatusListener((status, layer, percent) -> invocations.add(new ListenerInvocation(status, layer, percent)))); assertThat(invocations).isEqualTo( Arrays.asList(new ListenerInvocation( "Pulling fs layer","layer1", PullStatusListener.NO_PROGRESS), new ListenerInvocation( "Downloading","layer1", 0), new ListenerInvocation( "Pulling fs layer","layer2", PullStatusListener.NO_PROGRESS), new ListenerInvocation( "Downloading","layer2", 0), new ListenerInvocation( "Downloading","layer1", 50), new ListenerInvocation( "Downloading","layer1", 100), new ListenerInvocation( "Pull complete","layer1", PullStatusListener.NO_PROGRESS), new ListenerInvocation( "Downloading","layer2", 25), new ListenerInvocation( "Downloading","layer2", 100), new ListenerInvocation( "Pull complete","layer2", PullStatusListener.NO_PROGRESS))); }
@Test public void pullListenerUnexpectedProgressReporting() { dockerClient.newRegistryImage("resolved-image", "latest") .pullProgress("layer", "Downloading", 100, 0) .pullProgress("layer", "Downloading", 100, 50) .pullProgress("layer", "Downloading", -50, 100).pullProgress("layer", "Downloading", -50, -100); DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); List<ListenerInvocation> invocations = new ArrayList<>(); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). pullStrategy(PullStrategy.PULL). pullStatusListener((status, layer, percent) -> invocations.add(new ListenerInvocation(status, layer, percent)))); assertThat(invocations).isEqualTo( Arrays.asList(new ListenerInvocation("Downloading", "layer", PullStatusListener.NO_PROGRESS), new ListenerInvocation("Downloading", "layer", PullStatusListener.NO_PROGRESS), new ListenerInvocation("Downloading", "layer", PullStatusListener.NO_PROGRESS), new ListenerInvocation("Downloading", "layer", PullStatusListener.NO_PROGRESS))); }
@Test public void containerLabels() { dockerClient.localImage("resolved-image", "latest"); DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). label("key1", "value1"). label("key2", "value2")); List<Container> containers = dockerClient.getContainers(); assertThat(containers).hasSize(1); assertThat(containers.get(0).getLabels()).containsEntry("key1", "value1").containsEntry("key2", "value2"); }
@Test public void containerEnv() { dockerClient.localImage("resolved-image", "latest"); DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); Map<String, String> env = mapOf(pair("key1", "value1"), pair("key2", "value2")); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). env("key1", "value1"). env("key2", "value2")); List<Container> containers = dockerClient.getContainers(); assertThat(containers).hasSize(1); assertThat(containers.get(0).getEnv()).isEqualTo(env); }