Search in sources :

Example 1 with ContainerRenameConflictException

use of com.spotify.docker.client.exceptions.ContainerRenameConflictException in project docker-client by spotify.

the class DefaultDockerClient method renameContainer.

@Override
public void renameContainer(final String containerId, final String name) throws DockerException, InterruptedException {
    WebTarget resource = resource().path("containers").path(containerId).path("rename");
    if (name == null) {
        throw new IllegalArgumentException("Cannot rename container to null");
    }
    checkArgument(CONTAINER_NAME_PATTERN.matcher(name).matches(), "Invalid container name: \"%s\"", name);
    resource = resource.queryParam("name", name);
    log.info("Renaming container with id {}. New name {}.", containerId, name);
    try {
        request(POST, resource, resource.request());
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new ContainerNotFoundException(containerId, e);
            case 409:
                throw new ContainerRenameConflictException(containerId, name, e);
            default:
                throw e;
        }
    }
}
Also used : ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException)

Example 2 with ContainerRenameConflictException

use of com.spotify.docker.client.exceptions.ContainerRenameConflictException in project docker-client by spotify.

the class DefaultDockerClientTest method testRenameContainer.

@Test
public void testRenameContainer() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final String originalName = randomName();
    final String newName = randomName();
    // Create a container with originalName
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final ContainerCreation creation = sut.createContainer(config, originalName);
    final String id = creation.id();
    assertThat(sut.inspectContainer(id).name(), equalToIgnoreLeadingSlash(originalName));
    // Rename to newName
    sut.renameContainer(id, newName);
    assertThat(sut.inspectContainer(id).name(), equalToIgnoreLeadingSlash(newName));
    // We should no longer find a container with originalName
    try {
        sut.inspectContainer(originalName);
        fail("There should be no container with name " + originalName);
    } catch (ContainerNotFoundException e) {
        // Note, even though property in ContainerNotFoundException is named containerId,
        // in this case it holds the name, since that is what we passed to inspectContainer.
        assertThat(e.getContainerId(), equalToIgnoreLeadingSlash(originalName));
    }
    // Try to rename to a disallowed name (not matching /?[a-zA-Z0-9_-]+).
    // Should get IllegalArgumentException.
    final String badName = "abc123.!*";
    try {
        sut.renameContainer(id, badName);
        fail("We should not be able to rename a container " + badName);
    } catch (IllegalArgumentException ignored) {
    // Pass
    }
    // Try to rename to null
    try {
        sut.renameContainer(id, null);
        fail("We should not be able to rename a container null");
    } catch (IllegalArgumentException ignored) {
    // Pass
    }
    // Create another container with originalName
    final ContainerConfig config2 = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final ContainerCreation creation2 = sut.createContainer(config2, originalName);
    final String id2 = creation2.id();
    assertThat(sut.inspectContainer(id2).name(), equalToIgnoreLeadingSlash(originalName));
    // Try to rename another container to newName. Should get a ContainerRenameConflictException.
    try {
        sut.renameContainer(id2, newName);
        fail("We should not be able to rename container " + id2 + " to " + newName);
    } catch (ContainerRenameConflictException e) {
        assertThat(e.getContainerId(), equalTo(id2));
        assertThat(e.getNewName(), equalToIgnoreLeadingSlash(newName));
    } catch (DockerRequestException ignored) {
        // This is a docker bug. Docker responds with HTTP 500 when it should be HTTP 409.
        // See https://github.com/docker/docker/issues/21016.
        // Fixed in version 1.11.0 API version 1.23. So we should see a lower API version here.
        // Seems to be a regression in API version 1.3[23] https://github.com/moby/moby/issues/35472
        assertThat(dockerApiVersionLessThan("1.23") || dockerApiVersionEquals("1.32") || dockerApiVersionEquals("1.33"), is(true));
    }
    // Rename a non-existent id. Should get ContainerNotFoundException.
    final String badId = "no_container_with_this_id_should_exist_otherwise_things_are_weird";
    try {
        sut.renameContainer(badId, randomName());
        fail("There should be no container with id " + badId);
    } catch (ContainerNotFoundException e) {
        assertThat(e.getContainerId(), equalTo(badId));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) 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) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) Test(org.junit.Test)

Aggregations

ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)2 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)2 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)2 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 Long.toHexString (java.lang.Long.toHexString)1 WebTarget (javax.ws.rs.client.WebTarget)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1 Test (org.junit.Test)1