Search in sources :

Example 1 with ServiceMode

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

the class DefaultDockerClientTest method testCreateServiceWithDefaults.

@Test
public void testCreateServiceWithDefaults() throws Exception {
    requireDockerApiVersionAtLeast("1.24", "swarm support");
    final String serviceName = randomName();
    final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").command(new String[] { "ping", "-c1000", "localhost" }).mounts(Mount.builder().volumeOptions(VolumeOptions.builder().driverConfig(com.spotify.docker.client.messages.mount.Driver.builder().build()).build()).bindOptions(BindOptions.builder().build()).tmpfsOptions(TmpfsOptions.builder().build()).build()).build()).resources(ResourceRequirements.builder().build()).restartPolicy(RestartPolicy.builder().build()).placement(Placement.create(null)).networks(NetworkAttachmentConfig.builder().build()).logDriver(Driver.builder().build()).build();
    final ServiceMode serviceMode = ServiceMode.builder().replicated(ReplicatedService.builder().build()).build();
    final ServiceSpec serviceSpec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(serviceMode).updateConfig(UpdateConfig.create(null, null, null)).networks(Collections.<NetworkAttachmentConfig>emptyList()).endpointSpec(EndpointSpec.builder().addPort(PortConfig.builder().build()).build()).build();
    final ServiceCreateResponse response = sut.createService(serviceSpec);
    assertThat(response.id(), is(notNullValue()));
    sut.listTasks();
    final Service service = sut.inspectService(serviceName);
    final ServiceSpec actualServiceSpec = service.spec();
    assertThat(actualServiceSpec.mode().replicated().replicas(), equalTo(1L));
    assertThat(actualServiceSpec.taskTemplate().logDriver().options(), equalTo(Collections.<String, String>emptyMap()));
    assertThat(actualServiceSpec.endpointSpec().mode(), equalTo(EndpointSpec.Mode.RESOLUTION_MODE_VIP));
    assertThat(actualServiceSpec.updateConfig().failureAction(), equalTo("pause"));
    final PortConfig.Builder portConfigBuilder = PortConfig.builder().protocol(PROTOCOL_TCP);
    if (dockerApiVersionAtLeast("1.25")) {
        // Ingress publish mode is the default for ports in API versions >= 1.25
        portConfigBuilder.publishMode(PortConfigPublishMode.INGRESS);
    }
    final PortConfig expectedPortConfig = portConfigBuilder.build();
    assertThat(actualServiceSpec.endpointSpec().ports(), contains(expectedPortConfig));
    assertThat(service.endpoint().spec().ports(), contains(expectedPortConfig));
    final ContainerSpec containerSpec = actualServiceSpec.taskTemplate().containerSpec();
    assertThat(containerSpec.labels(), equalTo(Collections.<String, String>emptyMap()));
    assertThat(containerSpec.mounts().size(), equalTo(1));
    final Mount mount = containerSpec.mounts().get(0);
    assertThat(mount.type(), equalTo("bind"));
    final VolumeOptions volumeOptions = mount.volumeOptions();
    assertThat(volumeOptions.noCopy(), nullValue());
    assertThat(volumeOptions.labels(), nullValue());
    final com.spotify.docker.client.messages.mount.Driver driver = volumeOptions.driverConfig();
    assertThat(driver.name(), nullValue());
    assertThat(driver.options(), nullValue());
    final RestartPolicy restartPolicy = actualServiceSpec.taskTemplate().restartPolicy();
    assertThat(restartPolicy.condition(), equalTo(RESTART_POLICY_ANY));
    assertThat(restartPolicy.maxAttempts(), equalTo(0));
}
Also used : TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) ContainerSpec(com.spotify.docker.client.messages.swarm.ContainerSpec) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) ContainerMount(com.spotify.docker.client.messages.ContainerMount) Mount(com.spotify.docker.client.messages.mount.Mount) 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) RestartPolicy(com.spotify.docker.client.messages.swarm.RestartPolicy) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ServiceMode(com.spotify.docker.client.messages.swarm.ServiceMode) VolumeOptions(com.spotify.docker.client.messages.mount.VolumeOptions) ServiceCreateResponse(com.spotify.docker.client.messages.ServiceCreateResponse) PortConfig(com.spotify.docker.client.messages.swarm.PortConfig) Test(org.junit.Test)

Example 2 with ServiceMode

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

the class DefaultDockerClientTest method createServiceSpec.

private ServiceSpec createServiceSpec(final String serviceName, final Map<String, String> labels) {
    final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").command(new String[] { "ping", "-c1000", "localhost" }).build()).build();
    final ServiceMode serviceMode = ServiceMode.withReplicas(4);
    return ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(serviceMode).labels(labels).build();
}
Also used : TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ServiceMode(com.spotify.docker.client.messages.swarm.ServiceMode)

Example 3 with ServiceMode

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

the class DefaultDockerClientTest method testInspectService.

@Test
public void testInspectService() throws Exception {
    requireDockerApiVersionAtLeast("1.24", "swarm support");
    final String[] commandLine = { "ping", "-c4", "localhost" };
    final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").command(commandLine).build()).logDriver(Driver.builder().name("json-file").addOption("max-file", "3").addOption("max-size", "10M").build()).resources(ResourceRequirements.builder().limits(com.spotify.docker.client.messages.swarm.Resources.builder().memoryBytes(10 * 1024 * 1024L).build()).build()).restartPolicy(RestartPolicy.builder().condition("on-failure").delay(10000000L).maxAttempts(10).build()).build();
    final EndpointSpec endpointSpec = EndpointSpec.builder().addPort(PortConfig.builder().name("web").protocol("tcp").publishedPort(8080).targetPort(80).build()).build();
    final ServiceMode serviceMode = ServiceMode.withReplicas(4);
    final String serviceName = randomName();
    final ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(serviceMode).endpointSpec(endpointSpec).build();
    final ServiceCreateResponse response = sut.createService(spec);
    final Service service = sut.inspectService(response.id());
    assertThat(service.spec().name(), is(serviceName));
    assertThat(service.spec().taskTemplate().containerSpec().image(), latestImageNameMatcher("alpine"));
    assertThat(service.spec().taskTemplate().containerSpec().command(), equalTo(Arrays.asList(commandLine)));
}
Also used : TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) EndpointSpec(com.spotify.docker.client.messages.swarm.EndpointSpec) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) 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) ServiceMode(com.spotify.docker.client.messages.swarm.ServiceMode) ServiceCreateResponse(com.spotify.docker.client.messages.ServiceCreateResponse) Test(org.junit.Test)

Aggregations

ServiceMode (com.spotify.docker.client.messages.swarm.ServiceMode)3 TaskSpec (com.spotify.docker.client.messages.swarm.TaskSpec)3 Long.toHexString (java.lang.Long.toHexString)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)3 ServiceCreateResponse (com.spotify.docker.client.messages.ServiceCreateResponse)2 ReplicatedService (com.spotify.docker.client.messages.swarm.ReplicatedService)2 Service (com.spotify.docker.client.messages.swarm.Service)2 ServiceSpec (com.spotify.docker.client.messages.swarm.ServiceSpec)2 CompletionService (java.util.concurrent.CompletionService)2 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)2 ExecutorService (java.util.concurrent.ExecutorService)2 Test (org.junit.Test)2 ContainerMount (com.spotify.docker.client.messages.ContainerMount)1 Mount (com.spotify.docker.client.messages.mount.Mount)1 VolumeOptions (com.spotify.docker.client.messages.mount.VolumeOptions)1 ContainerSpec (com.spotify.docker.client.messages.swarm.ContainerSpec)1 EndpointSpec (com.spotify.docker.client.messages.swarm.EndpointSpec)1 PortConfig (com.spotify.docker.client.messages.swarm.PortConfig)1 RestartPolicy (com.spotify.docker.client.messages.swarm.RestartPolicy)1