Search in sources :

Example 46 with ContainerInfo

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

the class DefaultDockerClientTest method testAutoRemoveWhenSetToTrue.

@Test
public void testAutoRemoveWhenSetToTrue() throws Exception {
    requireDockerApiVersionAtLeast("1.25", "AutoRemove");
    // Container should be removed after it is stopped (new since API v.1.25)
    // Pull image
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(HostConfig.builder().autoRemove(// Default is false
    true).build()).build();
    final ContainerCreation container = sut.createContainer(config, randomName());
    final ContainerInfo info = sut.inspectContainer(container.id());
    assertThat(info.hostConfig().autoRemove(), is(true));
    sut.startContainer(container.id());
    sut.stopContainer(container.id(), 5);
    await().until(containerIsRunning(sut, container.id()), is(false));
    // A ContainerNotFoundException should be thrown since the container is removed when it stops
    exception.expect(ContainerNotFoundException.class);
    sut.inspectContainer(container.id());
}
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 47 with ContainerInfo

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

the class DefaultDockerClientTest method testContainerVolumes.

@Test
public void testContainerVolumes() throws Exception {
    requireDockerApiVersionAtLeast("1.20", "Creating a container with volumes and inspecting volumes");
    sut.pull(BUSYBOX_LATEST);
    final String namedVolumeName = "aVolume";
    final String namedVolumeFrom = "/a/host/path";
    final String namedVolumeTo = "/a/destination/path";
    final String bindObjectFrom = "/some/path";
    final String bindObjectTo = "/some/other/path";
    final String bindStringFrom = "/local/path";
    final String bindStringTo = "/remote/path";
    final String anonVolumeTo = "/foo";
    final Volume volume = Volume.builder().name(namedVolumeName).mountpoint(namedVolumeFrom).build();
    sut.createVolume(volume);
    final Bind bindUsingVolume = Bind.from(volume).to(namedVolumeTo).build();
    final Bind bind = Bind.from(bindObjectFrom).to(bindObjectTo).readOnly(true).build();
    final HostConfig hostConfig = HostConfig.builder().appendBinds(bind).appendBinds(bindStringFrom + ":" + bindStringTo).appendBinds(bindUsingVolume).build();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).addVolume(anonVolumeTo).hostConfig(hostConfig).build();
    final String id = sut.createContainer(volumeConfig, randomName()).id();
    final ContainerInfo containerInfo = sut.inspectContainer(id);
    final List<ContainerMount> mounts = containerInfo.mounts();
    assertThat(mounts.size(), equalTo(4));
    {
        final ContainerMount bindObjectMount = Iterables.find(mounts, new Predicate<ContainerMount>() {

            @Override
            public boolean apply(ContainerMount mount) {
                return bindObjectFrom.equals(mount.source());
            }
        }, null);
        assertThat("Did not find mount from bind object", bindObjectMount, notNullValue());
        assertThat(bindObjectMount.source(), is(bindObjectFrom));
        assertThat(bindObjectMount.destination(), is(bindObjectTo));
        assertThat(bindObjectMount.driver(), isEmptyOrNullString());
        assertThat(bindObjectMount.rw(), is(false));
        assertThat(bindObjectMount.mode(), is(equalTo("ro")));
        if (dockerApiVersionAtLeast("1.25") && dockerApiVersionLessThan("1.30")) {
            // From version 1.25 to 1.29, the API behaved like this
            assertThat(bindObjectMount.name(), isEmptyOrNullString());
            assertThat(bindObjectMount.propagation(), isEmptyOrNullString());
        } else if (dockerApiVersionAtLeast("1.22") || dockerApiVersionAtLeast("1.30")) {
            // From version 1.22 to 1.24, and from 1.30 up, the API behaves like this
            assertThat(bindObjectMount.name(), isEmptyOrNullString());
            assertThat(bindObjectMount.propagation(), is(equalTo("rprivate")));
        } else {
            // Below version 1.22
            assertThat(bindObjectMount.name(), is(nullValue()));
            assertThat(bindObjectMount.propagation(), is(nullValue()));
        }
        if (dockerApiVersionAtLeast("1.26")) {
            assertThat(bindObjectMount.type(), is(equalTo("bind")));
            assertThat(bindObjectMount.driver(), isEmptyOrNullString());
        } else {
            assertThat(bindObjectMount.type(), is(nullValue()));
            assertThat(bindObjectMount.driver(), is(nullValue()));
        }
    }
    {
        final ContainerMount bindStringMount = Iterables.find(mounts, new Predicate<ContainerMount>() {

            @Override
            public boolean apply(ContainerMount mount) {
                return bindStringFrom.equals(mount.source());
            }
        }, null);
        assertThat("Did not find mount from bind string", bindStringMount, notNullValue());
        assertThat(bindStringMount.source(), is(equalTo(bindStringFrom)));
        assertThat(bindStringMount.destination(), is(equalTo(bindStringTo)));
        assertThat(bindStringMount.driver(), isEmptyOrNullString());
        assertThat(bindStringMount.rw(), is(true));
        assertThat(bindStringMount.mode(), is(equalTo("")));
        if (dockerApiVersionAtLeast("1.25") && dockerApiVersionLessThan("1.30")) {
            // From version 1.25 to 1.29, the API behaved like this
            assertThat(bindStringMount.name(), isEmptyOrNullString());
            assertThat(bindStringMount.propagation(), isEmptyOrNullString());
        } else if (dockerApiVersionAtLeast("1.22") || dockerApiVersionAtLeast("1.30")) {
            // From version 1.22 to 1.24, and from 1.30 up, the API behaves like this
            assertThat(bindStringMount.name(), isEmptyOrNullString());
            assertThat(bindStringMount.propagation(), is(equalTo("rprivate")));
        } else {
            // Below version 1.22
            assertThat(bindStringMount.name(), is(nullValue()));
            assertThat(bindStringMount.propagation(), is(nullValue()));
        }
        if (dockerApiVersionAtLeast("1.26")) {
            assertThat(bindStringMount.type(), is(equalTo("bind")));
            assertThat(bindStringMount.driver(), isEmptyOrNullString());
        } else {
            assertThat(bindStringMount.type(), is(nullValue()));
            assertThat(bindStringMount.driver(), is(nullValue()));
        }
    }
    {
        final ContainerMount namedVolumeMount = Iterables.find(mounts, new Predicate<ContainerMount>() {

            @Override
            public boolean apply(ContainerMount mount) {
                return namedVolumeTo.equals(mount.destination());
            }
        }, null);
        assertThat("Did not find mount from named volume", namedVolumeMount, notNullValue());
        assertThat(namedVolumeMount.name(), is(equalTo(namedVolumeName)));
        assertThat(namedVolumeMount.source(), containsString("/" + namedVolumeName + "/"));
        assertThat(namedVolumeMount.destination(), is(equalTo(namedVolumeTo)));
        assertThat(namedVolumeMount.rw(), is(true));
        assertThat(namedVolumeMount.mode(), is(equalTo("z")));
        assertThat(namedVolumeMount.name(), is(namedVolumeName));
        assertThat(namedVolumeMount.driver(), is(equalTo("local")));
        if (dockerApiVersionAtLeast("1.25")) {
            assertThat(namedVolumeMount.propagation(), isEmptyOrNullString());
        } else if (dockerApiVersionAtLeast("1.22")) {
            assertThat(namedVolumeMount.propagation(), is(equalTo("rprivate")));
        } else {
            assertThat(namedVolumeMount.propagation(), is(nullValue()));
        }
        if (dockerApiVersionAtLeast("1.26")) {
            assertThat(namedVolumeMount.type(), is(equalTo("volume")));
        } else {
            assertThat(namedVolumeMount.type(), is(nullValue()));
        }
    }
    {
        final ContainerMount anonVolumeMount = Iterables.find(mounts, new Predicate<ContainerMount>() {

            @Override
            public boolean apply(ContainerMount mount) {
                return anonVolumeTo.equals(mount.destination());
            }
        }, null);
        assertThat("Did not find mount from anonymous volume", anonVolumeMount, notNullValue());
        assertThat(anonVolumeMount.source(), containsString("/" + anonVolumeMount.name() + "/"));
        assertThat(anonVolumeMount.destination(), is(equalTo(anonVolumeTo)));
        assertThat(anonVolumeMount.mode(), isEmptyOrNullString());
        assertThat(anonVolumeMount.rw(), is(true));
        assertThat(anonVolumeMount.mode(), is(equalTo("")));
        assertThat(anonVolumeMount.name(), not(isEmptyOrNullString()));
        assertThat(anonVolumeMount.driver(), is(equalTo("local")));
        if (dockerApiVersionAtLeast("1.22")) {
            assertThat(anonVolumeMount.propagation(), isEmptyOrNullString());
        } else {
            assertThat(anonVolumeMount.propagation(), is(nullValue()));
        }
        if (dockerApiVersionAtLeast("1.26")) {
            assertThat(anonVolumeMount.type(), is(equalTo("volume")));
        } else {
            assertThat(anonVolumeMount.type(), is(nullValue()));
        }
    }
    assertThat(containerInfo.config().volumes(), hasItem(anonVolumeTo));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) Bind(com.spotify.docker.client.messages.HostConfig.Bind) SecretBind(com.spotify.docker.client.messages.swarm.SecretBind) Volume(com.spotify.docker.client.messages.Volume) HostConfig(com.spotify.docker.client.messages.HostConfig) 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) ContainerMount(com.spotify.docker.client.messages.ContainerMount) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Example 48 with ContainerInfo

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

the class DefaultDockerClientTest method testExecInspect.

@Test
public void testExecInspect() throws Exception {
    requireDockerApiVersionAtLeast("1.16", "Exec Inspect");
    // CircleCI uses lxc, doesn't support exec - https://circleci.com/docs/docker/#docker-exec
    assumeFalse(CIRCLECI);
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    sut.startContainer(containerId);
    final List<ExecCreateParam> createParams = newArrayList(ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr(), ExecCreateParam.attachStdin(), ExecCreateParam.tty());
    // some functionality in this test depends on API 1.19 (exec user)
    final boolean execUserSupported = dockerApiVersionAtLeast("1.19");
    if (execUserSupported) {
        createParams.add(ExecCreateParam.user("1000"));
    }
    final ExecCreation execCreation = sut.execCreate(containerId, new String[] { "sh", "-c", "exit 2" }, createParams.toArray(new ExecCreateParam[createParams.size()]));
    final String execId = execCreation.id();
    log.info("execId = {}", execId);
    final ExecState notStarted = sut.execInspect(execId);
    assertThat(notStarted.id(), is(execId));
    assertThat(notStarted.running(), is(false));
    if (dockerApiVersionLessThan("1.22")) {
        assertThat(notStarted.exitCode(), is(0));
    } else {
        assertThat(notStarted.exitCode(), nullValue());
    }
    assertThat(notStarted.openStdin(), is(true));
    assertThat(notStarted.openStderr(), is(true));
    assertThat(notStarted.openStdout(), is(true));
    try (final LogStream stream = sut.execStart(execId)) {
        stream.readFully();
    }
    final ExecState started = sut.execInspect(execId);
    assertThat(started.id(), is(execId));
    assertThat(started.running(), is(false));
    assertThat(started.exitCode(), is(2));
    assertThat(started.openStdin(), is(true));
    assertThat(started.openStderr(), is(true));
    assertThat(started.openStdout(), is(true));
    final ProcessConfig processConfig = started.processConfig();
    assertThat(processConfig.privileged(), is(false));
    if (execUserSupported) {
        assertThat(processConfig.user(), is("1000"));
    }
    assertThat(processConfig.tty(), is(true));
    assertThat(processConfig.entrypoint(), is("sh"));
    assertThat(processConfig.arguments(), Matchers.<List<String>>is(ImmutableList.of("-c", "exit 2")));
    if (dockerApiVersionLessThan("1.22")) {
        final ContainerInfo containerInfo = started.container();
        assertThat(containerInfo.path(), is("sh"));
        assertThat(containerInfo.args(), Matchers.<List<String>>is(ImmutableList.of("-c", "while :; do sleep 1; done")));
        assertThat(containerInfo.config().image(), is(BUSYBOX_LATEST));
    } else {
        assertNotNull(started.containerId(), "containerId");
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ExecCreateParam(com.spotify.docker.client.DockerClient.ExecCreateParam) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ProcessConfig(com.spotify.docker.client.messages.ProcessConfig) 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) ExecState(com.spotify.docker.client.messages.ExecState) Test(org.junit.Test)

Example 49 with ContainerInfo

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

the class DefaultDockerClientTest method testStopContainer.

@Test
public void testStopContainer() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    sut.startContainer(containerId);
    // Must be running
    {
        final ContainerInfo containerInfo = sut.inspectContainer(containerId);
        assertThat(containerInfo.state().running(), equalTo(true));
    }
    sut.stopContainer(containerId, 5);
    // Must no longer be running
    {
        final ContainerInfo containerInfo = sut.inspectContainer(containerId);
        assertThat(containerInfo.state().running(), equalTo(false));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) 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 50 with ContainerInfo

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

the class DefaultDockerClientTest method testKillContainer.

@Test
public void testKillContainer() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    sut.startContainer(containerId);
    // Must be running
    final ContainerInfo containerInfo = sut.inspectContainer(containerId);
    assertThat(containerInfo.state().running(), equalTo(true));
    sut.killContainer(containerId);
    // Should not be running
    final ContainerInfo containerInfoLatest = sut.inspectContainer(containerId);
    assertFalse(containerInfoLatest.state().running());
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) 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