Search in sources :

Example 1 with SecretFile

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

the class DefaultDockerClientTest method testCreateServiceWithSecretHostnameHostsAndHealthcheck.

@Test
public void testCreateServiceWithSecretHostnameHostsAndHealthcheck() throws Exception {
    requireDockerApiVersionAtLeast("1.26", "swarm support");
    final String hostname = "tshost-{{.Task.Slot}}";
    final String[] hosts = { "127.0.0.1 test.local", "127.0.0.1 test" };
    final String[] healthcheckCmd = { "ping", "-c", "1", "127.0.0.1" };
    for (final Secret secret : sut.listSecrets()) {
        sut.deleteSecret(secret.id());
    }
    assertThat(sut.listSecrets().size(), equalTo(0));
    final String secretData = Base64.encodeAsString("testdata".getBytes());
    final Map<String, String> labels = ImmutableMap.of("foo", "bar", "1", "a");
    final SecretSpec secretSpec = SecretSpec.builder().name("asecret").data(secretData).labels(labels).build();
    final SecretCreateResponse secretResponse = sut.createSecret(secretSpec);
    final String secretId = secretResponse.id();
    assertThat(secretId, is(notNullValue()));
    final SecretFile secretFile = SecretFile.builder().name("bsecret").uid("1001").gid("1002").mode(0640L).build();
    final SecretBind secretBind = SecretBind.builder().file(secretFile).secretId(secretId).secretName("asecret").build();
    final String[] commandLine = { "ping", "-c4", "localhost" };
    final long interval = dockerApiVersionLessThan("1.30") ? 30L : 30000000L;
    final long timeout = dockerApiVersionLessThan("1.30") ? 3L : 3000000L;
    final int retries = 3;
    final long startPeriod = dockerApiVersionLessThan("1.30") ? 15L : 15000000L;
    final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").secrets(Arrays.asList(secretBind)).hostname(hostname).hosts(Arrays.asList(hosts)).healthcheck(Healthcheck.create(Arrays.asList(healthcheckCmd), interval, timeout, retries, startPeriod)).command(commandLine).build()).build();
    final String serviceName = randomName();
    final ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).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().hostname(), is(hostname));
    assertThat(service.spec().taskTemplate().containerSpec().hosts(), containsInAnyOrder(hosts));
    assertThat(service.spec().taskTemplate().containerSpec().secrets().size(), equalTo(1));
    SecretBind secret = service.spec().taskTemplate().containerSpec().secrets().get(0);
    assertThat(secret.secretId(), equalTo(secretId));
    assertThat(secret.secretName(), equalTo("asecret"));
    assertThat(secret.file().name(), equalTo("bsecret"));
    assertThat(secret.file().uid(), equalTo("1001"));
    assertThat(secret.file().gid(), equalTo("1002"));
    assertThat(secret.file().mode(), equalTo(0640L));
    assertThat(service.spec().taskTemplate().containerSpec().healthcheck().test(), equalTo(Arrays.asList(healthcheckCmd)));
    assertThat(service.spec().taskTemplate().containerSpec().healthcheck().interval(), equalTo(interval));
    assertThat(service.spec().taskTemplate().containerSpec().healthcheck().timeout(), equalTo(timeout));
    assertThat(service.spec().taskTemplate().containerSpec().healthcheck().retries(), equalTo(retries));
    final Matcher<Long> startPeriodMatcher = dockerApiVersionLessThan("1.29") ? nullValue(Long.class) : equalTo(startPeriod);
    assertThat(service.spec().taskTemplate().containerSpec().healthcheck().startPeriod(), startPeriodMatcher);
}
Also used : SecretSpec(com.spotify.docker.client.messages.swarm.SecretSpec) TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) SecretBind(com.spotify.docker.client.messages.swarm.SecretBind) 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) SecretFile(com.spotify.docker.client.messages.swarm.SecretFile) Endpoint(com.spotify.docker.client.messages.swarm.Endpoint) Secret(com.spotify.docker.client.messages.swarm.Secret) SecretCreateResponse(com.spotify.docker.client.messages.swarm.SecretCreateResponse) ServiceCreateResponse(com.spotify.docker.client.messages.ServiceCreateResponse) Test(org.junit.Test)

Aggregations

ServiceCreateResponse (com.spotify.docker.client.messages.ServiceCreateResponse)1 Endpoint (com.spotify.docker.client.messages.swarm.Endpoint)1 ReplicatedService (com.spotify.docker.client.messages.swarm.ReplicatedService)1 Secret (com.spotify.docker.client.messages.swarm.Secret)1 SecretBind (com.spotify.docker.client.messages.swarm.SecretBind)1 SecretCreateResponse (com.spotify.docker.client.messages.swarm.SecretCreateResponse)1 SecretFile (com.spotify.docker.client.messages.swarm.SecretFile)1 SecretSpec (com.spotify.docker.client.messages.swarm.SecretSpec)1 Service (com.spotify.docker.client.messages.swarm.Service)1 ServiceSpec (com.spotify.docker.client.messages.swarm.ServiceSpec)1 TaskSpec (com.spotify.docker.client.messages.swarm.TaskSpec)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