Search in sources :

Example 1 with DockerRequestException

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);
        }
    }
}
Also used : Path(java.nio.file.Path) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) ImagePushFailedException(com.spotify.docker.client.exceptions.ImagePushFailedException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) BadParamException(com.spotify.docker.client.exceptions.BadParamException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) ConflictException(com.spotify.docker.client.exceptions.ConflictException) UnsupportedApiVersionException(com.spotify.docker.client.exceptions.UnsupportedApiVersionException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) ExpectedException(org.junit.rules.ExpectedException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ImmutableSet(com.google.common.collect.ImmutableSet) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecretFile(com.spotify.docker.client.messages.swarm.SecretFile) File(java.io.File) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) Test(org.junit.Test)

Example 2 with DockerRequestException

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();
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) Container(com.spotify.docker.client.messages.Container) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) 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) After(org.junit.After)

Example 3 with DockerRequestException

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.
    }
}
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 4 with DockerRequestException

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;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) TaskNotFoundException(com.spotify.docker.client.exceptions.TaskNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) NodeNotFoundException(com.spotify.docker.client.exceptions.NodeNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) ServiceNotFoundException(com.spotify.docker.client.exceptions.ServiceNotFoundException) ExecNotFoundException(com.spotify.docker.client.exceptions.ExecNotFoundException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) WebTarget(javax.ws.rs.client.WebTarget) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Example 5 with DockerRequestException

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;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) ConflictException(com.spotify.docker.client.exceptions.ConflictException) ExecStartConflictException(com.spotify.docker.client.exceptions.ExecStartConflictException) ExecCreateConflictException(com.spotify.docker.client.exceptions.ExecCreateConflictException) WebTarget(javax.ws.rs.client.WebTarget) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Aggregations

DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)45 WebTarget (javax.ws.rs.client.WebTarget)37 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)18 DockerException (com.spotify.docker.client.exceptions.DockerException)14 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)12 NonSwarmNodeException (com.spotify.docker.client.exceptions.NonSwarmNodeException)11 NetworkNotFoundException (com.spotify.docker.client.exceptions.NetworkNotFoundException)9 NodeNotFoundException (com.spotify.docker.client.exceptions.NodeNotFoundException)9 ExecNotFoundException (com.spotify.docker.client.exceptions.ExecNotFoundException)8 NotFoundException (com.spotify.docker.client.exceptions.NotFoundException)8 ServiceNotFoundException (com.spotify.docker.client.exceptions.ServiceNotFoundException)8 VolumeNotFoundException (com.spotify.docker.client.exceptions.VolumeNotFoundException)8 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)7 TaskNotFoundException (com.spotify.docker.client.exceptions.TaskNotFoundException)6 Test (org.junit.Test)6 BadParamException (com.spotify.docker.client.exceptions.BadParamException)5 ConflictException (com.spotify.docker.client.exceptions.ConflictException)5 IOException (java.io.IOException)5 ExecCreateConflictException (com.spotify.docker.client.exceptions.ExecCreateConflictException)4 ExecStartConflictException (com.spotify.docker.client.exceptions.ExecStartConflictException)4