SwarmDockerClientFacade extends BaseDockerClientFacade { @Nonnull @Override public List<AgentHolderInfo> listAgentHolders(@Nonnull String labelFilter, @Nonnull String valueFilter) { try { List<Node> services = client.listServicesWithLabel(Collections.singletonMap(labelFilter, valueFilter)). getArrayValues(); List<AgentHolderInfo> agentHolderInfos = new ArrayList<>(services.size()); for (Node service : services) { String id = service.getAsString("ID"); Node spec = service.getObject("Spec"); Map<String, String> labels = spec.getObject("Labels", Node.EMPTY_OBJECT).getObjectValues().entrySet() .stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getAsString())); Node replicated = spec.getObject("Mode", Node.EMPTY_OBJECT). getObject("Replicated", null); String name = spec.getAsString("Name"); String createdAt = service.getAsString("CreatedAt"); Instant creationTimestamp = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(createdAt)); if (replicated == null) { LOG.warn("Service " + id + " is not replicated and will be ignored."); continue; } List<Node> tasks = client.listTasks(id).getArrayValues(); for (Node task : tasks) { String taskId = task.getAsString("ID"); Node status = task.getObject("Status"); String state = status.getAsString("State"); String msg = status.getAsString("Message", ""); String err = status.getAsString("Err", ""); String stateMsg = state; String sep = " - "; if (!msg.isEmpty()) { stateMsg += sep + msg; sep = ": "; } if (!err.isEmpty()) { stateMsg += sep + err; } agentHolderInfos.add(new AgentHolderInfo(id, taskId, labels, stateMsg, name, creationTimestamp, TaskRunningState.isRunning(state))); } } return agentHolderInfos; } catch (NodeProcessingException e) { throw new DockerClientFacadeException("Failed to list agent services tasks.", e); } } 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 listAgentServices() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); Instant ts = Instant.now(); TestDockerClient.Service service = new TestDockerClient.Service(). name("the_service_name"). label("foo", "bar"). creationTimestamp(ts); TestDockerClient.Task task = service.pushTask().state(TaskRunningState.RUNNING.toString().toLowerCase()); dockerClient.service(service); List<AgentHolderInfo> servicesInfo = facade.listAgentHolders("foo", "bar"); assertThat(servicesInfo).hasSize(1); AgentHolderInfo serviceInfo = servicesInfo.get(0); assertThat(serviceInfo.getId()).isEqualTo(serviceInfo.getId()); assertThat(serviceInfo.getTaskId()).isEqualTo(task.getId()); assertThat(serviceInfo.isRunning()).isTrue(); assertThat(serviceInfo.getName()).isEqualTo(serviceInfo.getName()); assertThat(serviceInfo.getLabels()).isEqualTo(mapOf(pair("foo", "bar"))); assertThat(serviceInfo.getCreationTimestamp()).isEqualTo(ts); }
@Test public void listAgentServicesMustIgnoreNonMatchingService() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); TestDockerClient.Service service = new TestDockerClient.Service(). label("foo", "baz"); service.pushTask().state(TaskRunningState.RUNNING.toString().toLowerCase()); dockerClient.service(service); List<AgentHolderInfo> servicesInfo = facade.listAgentHolders("foo", "bar"); assertThat(servicesInfo).isEmpty(); }
@Test public void listAgentServicesMustIgnoreServiceWithNoTask() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); Instant ts = Instant.now(); TestDockerClient.Service service = new TestDockerClient.Service(). name("the_service_name"). label("foo", "bar"). creationTimestamp(ts); dockerClient.service(service); List<AgentHolderInfo> servicesInfo = facade.listAgentHolders("foo", "bar"); assertThat(servicesInfo).isEmpty(); }
@Test public void listAgentServicesMustReportNonRunningAgent() { SwarmDockerClientFacade facade = new SwarmDockerClientFacade(dockerClient); TestDockerClient.Service service = new TestDockerClient.Service(). label("foo", "bar"); service.pushTask().state("stopped"); dockerClient.service(service); List<AgentHolderInfo> servicesInfo = facade.listAgentHolders("foo", "bar"); assertThat(servicesInfo).hasSize(1); AgentHolderInfo serviceInfo = servicesInfo.get(0); assertThat(serviceInfo.getId()).isEqualTo(service.getId()); assertThat(serviceInfo.isRunning()).isFalse(); }