Search in sources :

Example 6 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project docker-client by spotify.

the class DefaultDockerClientTest method testOomKillDisable.

@Test
public void testOomKillDisable() throws Exception {
    requireDockerApiVersionAtLeast("1.20", "OomKillDisable");
    // Pull image
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(HostConfig.builder().oomKillDisable(// Defaults to false
    true).build()).build();
    final ContainerCreation container = sut.createContainer(config, randomName());
    final ContainerInfo info = sut.inspectContainer(container.id());
    assertThat(info.hostConfig().oomKillDisable(), is(true));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Test(org.junit.Test)

Example 7 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project docker-client by spotify.

the class DefaultDockerClientTest method testContainerLabels.

@Test
public void testContainerLabels() throws Exception {
    requireDockerApiVersionAtLeast("1.18", "labels");
    sut.pull(BUSYBOX_LATEST);
    final Map<String, String> labels = ImmutableMap.of("name", "starship", "foo", "bar");
    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).labels(labels).cmd("sleep", "1000").build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    // Start the container
    sut.startContainer(id);
    final ContainerInfo containerInfo = sut.inspectContainer(id);
    assertThat(containerInfo.config().labels(), is(labels));
    final Map<String, String> labels2 = ImmutableMap.of("name", "starship", "foo", "baz");
    // Create second container with different labels
    final ContainerConfig config2 = ContainerConfig.builder().image(BUSYBOX_LATEST).labels(labels2).cmd("sleep", "1000").build();
    final String name2 = randomName();
    final ContainerCreation creation2 = sut.createContainer(config2, name2);
    final String id2 = creation2.id();
    // Start the second container
    sut.startContainer(id2);
    final ContainerInfo containerInfo2 = sut.inspectContainer(id2);
    assertThat(containerInfo2.config().labels(), is(labels2));
    // Check that both containers are listed when we filter with a "name" label
    final List<Container> containers = sut.listContainers(withLabel("name"));
    final List<String> ids = containersToIds(containers);
    assertThat(ids.size(), equalTo(2));
    assertThat(ids, containsInAnyOrder(id, id2));
    // Check that the first container is listed when we filter with a "foo=bar" label
    final List<Container> barContainers = sut.listContainers(withLabel("foo", "bar"));
    final List<String> barIds = containersToIds(barContainers);
    assertThat(barIds.size(), equalTo(1));
    assertThat(barIds, contains(id));
    // Check that the second container is listed when we filter with a "foo=baz" label
    final List<Container> bazContainers = sut.listContainers(withLabel("foo", "baz"));
    final List<String> bazIds = containersToIds(bazContainers);
    assertThat(bazIds.size(), equalTo(1));
    assertThat(bazIds, contains(id2));
    // Check that no containers are listed when we filter with a "foo=qux" label
    final List<Container> quxContainers = sut.listContainers(withLabel("foo", "qux"));
    assertThat(quxContainers.size(), equalTo(0));
    // Clean up
    sut.removeContainer(id, forceKill());
    sut.removeContainer(id2, forceKill());
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) Container(com.spotify.docker.client.messages.Container) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo 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 9 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo 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 10 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project docker-client by spotify.

the class DefaultDockerClientTest method testAttachContainer.

@Test
public void testAttachContainer() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final String volumeContainer = randomName();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).addVolume("/foo").cmd("sh", "-c", "ls -la; sleep 3").build();
    sut.createContainer(volumeConfig, volumeContainer);
    sut.startContainer(volumeContainer);
    Thread.sleep(1000L);
    final String logs;
    try (LogStream stream = sut.attachContainer(volumeContainer, AttachParameter.LOGS, AttachParameter.STDOUT, AttachParameter.STDERR, AttachParameter.STREAM)) {
        logs = stream.readFully();
    }
    assertThat(logs, containsString("total"));
    sut.waitContainer(volumeContainer);
    final ContainerInfo info = sut.inspectContainer(volumeContainer);
    assertThat(info.state().running(), is(false));
    assertThat(info.state().exitCode(), is(0));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)68 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)45 Test (org.junit.Test)41 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)35 Matchers.containsString (org.hamcrest.Matchers.containsString)33 Long.toHexString (java.lang.Long.toHexString)31 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)31 DockerException (com.spotify.docker.client.exceptions.DockerException)15 HostConfig (com.spotify.docker.client.messages.HostConfig)15 IOException (java.io.IOException)9 DockerClient (com.spotify.docker.client.DockerClient)8 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)7 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)5 Container (com.spotify.docker.client.messages.Container)5 ImageInfo (com.spotify.docker.client.messages.ImageInfo)5 LogStream (com.spotify.docker.client.LogStream)4 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)4 AttachedNetwork (com.spotify.docker.client.messages.AttachedNetwork)4 Path (java.nio.file.Path)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)3