Search in sources :

Example 1 with Endpoint

use of com.spotify.docker.client.messages.swarm.Endpoint in project docker-client by spotify.

the class DefaultDockerClientTest method testInspectServiceEndpoint.

@Test
public void testInspectServiceEndpoint() throws Exception {
    requireDockerApiVersionAtLeast("1.24", "swarm support");
    final String name = randomName();
    final String imageName = "demo/test";
    final PortConfig.Builder portConfigBuilder = PortConfig.builder().name("web").protocol("tcp").publishedPort(8080).targetPort(80);
    if (dockerApiVersionAtLeast("1.25")) {
        portConfigBuilder.publishMode(PortConfigPublishMode.INGRESS);
    }
    final PortConfig expectedPort1 = portConfigBuilder.build();
    final ServiceSpec spec = ServiceSpec.builder().name(name).endpointSpec(EndpointSpec.builder().addPort(expectedPort1).addPort(PortConfig.builder().targetPort(22).publishMode(PortConfigPublishMode.HOST).build()).build()).taskTemplate(TaskSpec.builder().containerSpec(ContainerSpec.builder().image(imageName).build()).build()).build();
    sut.createService(spec);
    final Service service = sut.inspectService(name);
    final Endpoint endpoint = service.endpoint();
    final PortConfig.Builder portConfigBuilder2 = PortConfig.builder().targetPort(22).protocol("tcp");
    if (dockerApiVersionAtLeast("1.25")) {
        portConfigBuilder2.publishMode(PortConfigPublishMode.HOST);
    }
    final PortConfig expectedPort2Spec = portConfigBuilder2.build();
    assertThat(endpoint.spec().ports(), containsInAnyOrder(expectedPort1, expectedPort2Spec));
    // API versions less than 1.25 get assigned a random published port and have null publish mode
    final Matcher<Integer> publishedPortMatcher;
    final Matcher<PortConfigPublishMode> publishModeMatcher;
    if (dockerApiVersionLessThan("1.25")) {
        publishedPortMatcher = any(Integer.class);
        publishModeMatcher = nullValue(PortConfigPublishMode.class);
    } else {
        publishedPortMatcher = nullValue(Integer.class);
        publishModeMatcher = equalTo(PortConfigPublishMode.HOST);
    }
    // noinspection unchecked
    assertThat(endpoint.ports(), containsInAnyOrder(equalTo(expectedPort1), portConfigWith(nullValue(String.class), equalTo("tcp"), equalTo(22), publishedPortMatcher, publishModeMatcher)));
    // noinspection ConstantConditions
    assertThat(endpoint.virtualIps().size(), equalTo(1));
}
Also used : Endpoint(com.spotify.docker.client.messages.swarm.Endpoint) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) PortConfigPublishMode(com.spotify.docker.client.messages.swarm.PortConfig.PortConfigPublishMode) ReplicatedService(com.spotify.docker.client.messages.swarm.ReplicatedService) CompletionService(java.util.concurrent.CompletionService) Service(com.spotify.docker.client.messages.swarm.Service) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExecutorService(java.util.concurrent.ExecutorService) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) PortConfig(com.spotify.docker.client.messages.swarm.PortConfig) Test(org.junit.Test)

Aggregations

Endpoint (com.spotify.docker.client.messages.swarm.Endpoint)1 PortConfig (com.spotify.docker.client.messages.swarm.PortConfig)1 PortConfigPublishMode (com.spotify.docker.client.messages.swarm.PortConfig.PortConfigPublishMode)1 ReplicatedService (com.spotify.docker.client.messages.swarm.ReplicatedService)1 Service (com.spotify.docker.client.messages.swarm.Service)1 ServiceSpec (com.spotify.docker.client.messages.swarm.ServiceSpec)1 Long.toHexString (java.lang.Long.toHexString)1 CompletionService (java.util.concurrent.CompletionService)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 ExecutorService (java.util.concurrent.ExecutorService)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1 Test (org.junit.Test)1