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);
}
Aggregations