DefaultDockerClientFacade extends BaseDockerClientFacade { @Nonnull @Override public List<AgentHolderInfo> listAgentHolders(@Nonnull String labelFilter, @Nonnull String valueFilter) { DockerCloudUtils.requireNonNull(labelFilter, "Label filter key cannot be null."); DockerCloudUtils.requireNonNull(valueFilter, "Label filter value cannot be null."); Node containerNodes = client.listContainersWithLabel(Collections.singletonMap(labelFilter, valueFilter)); try { return containerNodes.getArrayValues().stream().filter(containerNode -> { String sourceImageId = containerNode.getObject("Labels", Node.EMPTY_OBJECT). getAsString(DockerCloudUtils.SOURCE_IMAGE_ID_LABEL, null); String currentImageId = containerNode.getAsString("ImageID"); return sourceImageId != null && sourceImageId.equals(currentImageId); }).map(this::parseContainerInfo).collect(Collectors.toList()); } catch (NodeProcessingException e) { throw new DockerClientFacadeException("Failed to list agent containers.", 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 listAgentContainers() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); TestImage img = dockerClient.newLocalImage("image-1", "latest"); assertThat(facade.listAgentHolders("foo", "bar")).isEmpty(); Container container = new Container(). label("foo", "bar"). label(DockerCloudUtils.SOURCE_IMAGE_ID_LABEL, img.getId()). name("the_container_name"). image(img). creationTimestamp(TestUtils.TEST_INSTANT). running(true); dockerClient.container(container); List<AgentHolderInfo> agentHolders = facade. listAgentHolders("foo", "bar"); assertThat(agentHolders).hasSize(1); AgentHolderInfo agentHolder = agentHolders.get(0); assertThat(agentHolder.getId()).isEqualTo(container.getId()); assertThat(agentHolder.getTaskId()).isEqualTo(container.getId()); assertThat(agentHolder.getName()).isEqualTo("the_container_name"); assertThat(agentHolder.getStateMsg()).isEqualTo(DefaultDockerClientFacade.CONTAINER_RUNNING_STATE); assertThat(agentHolder.getLabels()).isEqualTo( mapOf(pair("foo", "bar"), pair(DockerCloudUtils.SOURCE_IMAGE_ID_LABEL, img.getId()))); assertThat(agentHolder.getCreationTimestamp()).isEqualTo(TestUtils.TEST_INSTANT); assertThat(agentHolder.isRunning()).isTrue(); }
@Test public void listAgentContainersMustListMultipleContainers() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); TestImage image1 = dockerClient.newLocalImage("image-1", "latest"); TestImage image2 = dockerClient.newLocalImage("image-2", "latest"); assertThat(facade.listAgentHolders("foo", "bar")).isEmpty(); Container validContainer1 = new Container(). label("foo", "bar"). label(DockerCloudUtils.SOURCE_IMAGE_ID_LABEL, image1.getId()). image(image1); Container validContainer2 = new Container(). label("foo", "bar"). label(DockerCloudUtils.SOURCE_IMAGE_ID_LABEL, image2.getId()). image(image2); dockerClient.container(validContainer1).container(validContainer2); List<String> containerIds = facade.listAgentHolders("foo", "bar").stream(). map(AgentHolderInfo::getId). collect(Collectors.toList()); assertThat(containerIds).containsExactlyInAnyOrder(validContainer1.getId(), validContainer2.getId()); }
@Test public void listAgentContainersMustIgnoreContainersWithWrongLabel() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); TestImage image1 = dockerClient.newLocalImage("image-1", "latest"); assertThat(facade.listAgentHolders("foo", "bar")).isEmpty(); Container wrongLabelValue = new Container(). label("foo", "baz"). label(DockerCloudUtils.SOURCE_IMAGE_ID_LABEL, image1.getId()). image(image1); dockerClient.container(wrongLabelValue); List<String> containerIds = facade.listAgentHolders("foo", "bar").stream(). map(AgentHolderInfo::getId). collect(Collectors.toList()); assertThat(containerIds).isEmpty(); }
@Test public void listAgentContainersMustIgnoreContainersWithNonMatchingSourceId() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); TestImage image1 = dockerClient.newLocalImage("image-1", "latest"); TestImage image2 = dockerClient.newLocalImage("image-2", "latest"); assertThat(facade.listAgentHolders("foo", "bar")).isEmpty(); Container sourceImageIdNotMatching = new Container(). label("foo", "bar"). label(DockerCloudUtils.SOURCE_IMAGE_ID_LABEL, image1.getId()). image(image2); dockerClient.container(sourceImageIdNotMatching); List<String> containerIds = facade.listAgentHolders("foo", "bar").stream(). map(AgentHolderInfo::getId). collect(Collectors.toList()); assertThat(containerIds).isEmpty(); }
@Test public void listAgentContainersSourceImageIdNotSet() { DefaultDockerClientFacade facade = new DefaultDockerClientFacade(dockerClient); TestImage image = dockerClient.newLocalImage("image-1", "latest"); Container container = new Container(). label("foo", "bar"). image(image); dockerClient.container(container); List<AgentHolderInfo> containers = facade.listAgentHolders("foo", "bar"); assertThat(containers).isEmpty(); }