use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.
the class DefaultDockerClientTest method integrationTest.
@Test
@SuppressWarnings("deprecation")
public void integrationTest() throws Exception {
// Pull image
sut.pull(BUSYBOX_LATEST);
// Create container
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
final String name = randomName();
final ContainerCreation creation = sut.createContainer(config, name);
final String id = creation.id();
assertThat(creation.warnings(), anyOf(is(empty()), is(nullValue())));
assertThat(id, is(any(String.class)));
// Inspect using container ID
{
final ContainerInfo info = sut.inspectContainer(id);
assertThat(info.id(), equalTo(id));
assertThat(info.config().image(), equalTo(config.image()));
assertThat(info.config().cmd(), equalTo(config.cmd()));
}
// Inspect using container name
{
final ContainerInfo info = sut.inspectContainer(name);
assertThat(info.config().image(), equalTo(config.image()));
assertThat(info.config().cmd(), equalTo(config.cmd()));
}
// Start container
sut.startContainer(id);
final Path dockerDirectory = getResource("dockerSslDirectory");
// to a directory in a container
if (dockerApiVersionAtLeast("1.20")) {
try {
sut.copyToContainer(dockerDirectory, id, "/tmp");
} catch (Exception e) {
fail("error copying files to container");
}
// Copy the same files from container
final ImmutableSet.Builder<String> filesDownloaded = ImmutableSet.builder();
try (TarArchiveInputStream tarStream = new TarArchiveInputStream(dockerApiVersionLessThan("1.24") ? sut.copyContainer(id, "/tmp") : sut.archiveContainer(id, "/tmp"))) {
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
filesDownloaded.add(entry.getName());
}
}
// Check that we got back what we put in
final File folder = new File(dockerDirectory.toString());
final File[] files = folder.listFiles();
if (files != null) {
for (final File file : files) {
if (!file.isDirectory()) {
Boolean found = false;
for (final String fileDownloaded : filesDownloaded.build()) {
if (fileDownloaded.contains(file.getName())) {
found = true;
}
}
assertTrue(found);
}
}
}
}
// Kill container
sut.killContainer(id);
try {
// Remove the container
sut.removeContainer(id);
} catch (DockerRequestException e) {
// CircleCI doesn't let you remove a container :(
if (!CIRCLECI) {
// Verify that the container is gone
exception.expect(ContainerNotFoundException.class);
sut.inspectContainer(id);
}
}
}
use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.
the class DefaultDockerClientTest method tearDown.
@After
public void tearDown() throws Exception {
if (dockerApiVersionAtLeast("1.24")) {
try {
final List<Service> services = sut.listServices();
for (final Service service : services) {
if (service.spec().name().startsWith(nameTag)) {
sut.removeService(service.id());
}
}
} catch (DockerException e) {
log.warn("Ignoring DockerException in teardown", e);
}
}
// Remove containers
final List<Container> containers = sut.listContainers();
for (final Container container : containers) {
final ContainerInfo info = sut.inspectContainer(container.id());
if (info != null && info.name().startsWith(nameTag)) {
try {
sut.killContainer(info.id());
} catch (DockerRequestException e) {
// Docker 1.6 sometimes fails to kill a container because it disappears.
// https://github.com/docker/docker/issues/12738
log.warn("Failed to kill container {}", info.id(), e);
}
}
}
// Close the client
sut.close();
}
use of com.spotify.docker.client.exceptions.DockerRequestException 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.exceptions.DockerRequestException in project docker-client by spotify.
the class DefaultDockerClient method inspectConfig.
@Override
public Config inspectConfig(final String configId) throws DockerException, InterruptedException {
assertApiVersionIsAbove("1.30");
final WebTarget resource = resource().path("configs").path(configId);
try {
return request(GET, Config.class, resource, resource.request(APPLICATION_JSON_TYPE));
} catch (final DockerRequestException ex) {
switch(ex.status()) {
case 404:
throw new NotFoundException("Config " + configId + " not found.", ex);
case 503:
throw new NonSwarmNodeException("Config not part of swarm.", ex);
default:
throw ex;
}
}
}
use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.
the class DefaultDockerClient method createSecret.
@Override
public SecretCreateResponse createSecret(final SecretSpec secret) throws DockerException, InterruptedException {
assertApiVersionIsAbove("1.25");
final WebTarget resource = resource().path("secrets").path("create");
try {
return request(POST, SecretCreateResponse.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(secret));
} catch (final DockerRequestException ex) {
switch(ex.status()) {
case 406:
throw new NonSwarmNodeException("Server not part of swarm.", ex);
case 409:
throw new ConflictException("Name conflicts with an existing object.", ex);
default:
throw ex;
}
}
}
Aggregations