use of com.spotify.docker.client.messages.swarm.SecretSpec 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.
}
}
use of com.spotify.docker.client.messages.swarm.SecretSpec 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);
}
Aggregations