SwarmDockerClientFacade extends BaseDockerClientFacade { @Nonnull @Override public NewAgentHolderInfo createAgent(@Nonnull CreateAgentParameters createAgentParameters) { DockerCloudUtils.requireNonNull(createAgentParameters, "Agent creation parameters cannot be null."); if (createAgentParameters.getPullStrategy() != PullStrategy.NO_PULL) { throw new DockerClientFacadeException("Pull strategy " + createAgentParameters.getPullStrategy() + " is not" + " supported when in swarm mode."); } EditableNode editableServiceSpec = createAgentParameters.getAgentHolderSpec().editNode(); Optional<String> imageName = createAgentParameters.getImageName(); editableServiceSpec.getOrCreateObject("Mode"). getOrCreateObject("Replicated"). put("Replicas", 0); EditableNode editableContainerSpec = editableServiceSpec. getOrCreateObject("TaskTemplate"). getOrCreateObject("ContainerSpec"); imageName.ifPresent(image -> editableContainerSpec.put("Image", image)); applyEnv(editableContainerSpec, createAgentParameters.getEnv()); applyLabels(editableServiceSpec, createAgentParameters.getLabels()); Node serviceNode = client.createService(editableServiceSpec.saveNode()); String id = serviceNode.getAsString("ID"); String warning = serviceNode.getAsString("Warning", null); Node inspectNode = client.inspectService(id); Node spec = inspectNode.getObject("Spec"); String serviceName = spec.getAsString("Name"); String resolvedImage = spec. getObject("TaskTemplate"). getObject("ContainerSpec"). getAsString("Image"); List<String> warnings = warning != null ? Collections.singletonList(warning) : Collections.emptyList(); return new NewAgentHolderInfo(id, serviceName, resolvedImage, warnings); } SwarmDockerClientFacade(DockerClient client); @Nonnull @Override NewAgentHolderInfo createAgent(@Nonnull CreateAgentParameters createAgentParameters); @Override String startAgent(@Nonnull String serviceId); @Override String restartAgent(@Nonnull String serviceId); @Nonnull @Override List<AgentHolderInfo> listAgentHolders(@Nonnull String labelFilter, @Nonnull String valueFilter); @Override boolean terminateAgentContainer(@Nonnull String containerId, Duration timeout, boolean removeContainer); @Override CharSequence getLogs(@Nonnull String serviceId); @Nonnull @Override StreamHandler streamLogs(@Nonnull String serviceId); @Override boolean supportQueryingLogs();  }
@Test public void createAgentMustReturnServiceId() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); dockerClient.localImage("resolved-image", "latest"); NewAgentHolderInfo agentInfo = facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); assertThat(agentInfo.getId()).isEqualTo(dockerClient.getServices().get(0).getId()); }
@Test public void createAgentMustReturnServiceName() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); dockerClient.localImage("resolved-image", "latest"); NewAgentHolderInfo agentInfo = facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); assertThat(agentInfo.getName()).isEqualTo(dockerClient.getServices().get(0).getName()); }
@Test public void createAgentMustReturnResolvedImageName() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(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@resolved"); }
@Test public void createAgentMustReturnWarnings() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); dockerClient .localImage("resolved-image", "latest") .serviceCreationWarning("foo"); NewAgentHolderInfo agentInfo = facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest")); assertThat(agentInfo.getWarnings()).isEqualTo(listOf("foo")); }
@Test public void pullNotSupported() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); dockerClient.newRegistryImage("resolved-image", "latest"); assertThatExceptionOfType(DockerClientFacadeException.class) .isThrownBy(() -> facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). pullStrategy(PullStrategy.PULL))); assertThatExceptionOfType(DockerClientFacadeException.class) .isThrownBy(() -> facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). pullStrategy(PullStrategy.PULL_IGNORE_FAILURE))); }
@Test public void serviceLabels() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); facade.createAgent(CreateAgentParameters.from(Node.EMPTY_OBJECT). imageName("resolved-image:latest"). label("key1", "value1"). label("key2", "value2")); List<TestDockerClient.Service> services = dockerClient.getServices(); assertThat(services).hasSize(1); assertThat(services.get(0).getLabels()). containsEntry("key1", "value1"). containsEntry("key2", "value2"); }
@Test public void containerEnv() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(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<TestDockerClient.Service> services = dockerClient.getServices(); assertThat(services).hasSize(1); assertThat(services.get(0).getEnv()).isEqualTo(env); }