Search in sources :

Example 1 with Secret

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

the class DefaultDockerClientTest method testSecretOperations.

@Test
public void testSecretOperations() throws Exception {
    requireDockerApiVersionAtLeast("1.25", "secret support");
    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 response = sut.createSecret(secretSpec);
    final String secretId = response.id();
    assertThat(secretId, is(notNullValue()));
    final SecretSpec secretSpecConflict = SecretSpec.builder().name("asecret").data(secretData).labels(labels).build();
    try {
        sut.createSecret(secretSpecConflict);
        fail("Should fail due to secret name conflict");
    } catch (ConflictException | DockerRequestException ex) {
    // Ignored; Docker should return status code 409 but doesn't in some earlier versions.
    // Recent versions return 409 which translates into ConflictException.
    }
    final SecretSpec secretSpecInvalidData = SecretSpec.builder().name("asecret2").data("plainData").labels(labels).build();
    try {
        sut.createSecret(secretSpecInvalidData);
        fail("Should fail due to non base64 data");
    } catch (DockerException ex) {
    // Ignored
    }
    final Secret secret = sut.inspectSecret(secretId);
    final List<Secret> secrets = sut.listSecrets();
    assertThat(secrets.size(), equalTo(1));
    assertThat(secrets, hasItem(secret));
    sut.deleteSecret(secretId);
    assertThat(sut.listSecrets().size(), equalTo(0));
    try {
        sut.inspectSecret(secretId);
        fail("Should fail because of non-existant secret ID");
    } catch (NotFoundException ex) {
    // Ignored
    }
    try {
        sut.deleteSecret(secretId);
        fail("Should fail because of non-existant secret ID");
    } catch (DockerRequestException | NotFoundException ex) {
    // Ignored; Docker should return status code 404,
    // but it doesn't for some reason in versions < 17.04.
    // So we catch 2 different exceptions here.
    }
}
Also used : Secret(com.spotify.docker.client.messages.swarm.Secret) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) ConflictException(com.spotify.docker.client.exceptions.ConflictException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) SecretSpec(com.spotify.docker.client.messages.swarm.SecretSpec) SecretCreateResponse(com.spotify.docker.client.messages.swarm.SecretCreateResponse) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with Secret

use of com.spotify.docker.client.messages.swarm.Secret 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

Secret (com.spotify.docker.client.messages.swarm.Secret)2 SecretCreateResponse (com.spotify.docker.client.messages.swarm.SecretCreateResponse)2 SecretSpec (com.spotify.docker.client.messages.swarm.SecretSpec)2 Long.toHexString (java.lang.Long.toHexString)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)2 Test (org.junit.Test)2 ConflictException (com.spotify.docker.client.exceptions.ConflictException)1 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)1 DockerException (com.spotify.docker.client.exceptions.DockerException)1 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)1 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)1 NetworkNotFoundException (com.spotify.docker.client.exceptions.NetworkNotFoundException)1 NotFoundException (com.spotify.docker.client.exceptions.NotFoundException)1 VolumeNotFoundException (com.spotify.docker.client.exceptions.VolumeNotFoundException)1 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 SecretBind (com.spotify.docker.client.messages.swarm.SecretBind)1