Search in sources :

Example 41 with ContainerInfo

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

the class DefaultDockerClientTest method testContainerVolumeNoCopy.

@Test
public void testContainerVolumeNoCopy() throws Exception {
    requireDockerApiVersionAtLeast("1.23", "Creating a container with volumes with nocopy mode");
    sut.pull(BUSYBOX_LATEST);
    final String aVolumeName = "avolume";
    final String aVolumeTo = "/some/path";
    final String nocopyVolumeName = "avolume2";
    final String nocopyVolumeTo = "/some/other/path";
    sut.createVolume(Volume.builder().name(aVolumeName).build());
    sut.createVolume(Volume.builder().name(nocopyVolumeName).build());
    final HostConfig hostConfig = HostConfig.builder().appendBinds(Bind.from(aVolumeName).to(aVolumeTo).readOnly(true).build()).appendBinds(Bind.from(nocopyVolumeName).to(nocopyVolumeTo).noCopy(true).build()).build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(hostConfig).build();
    final String id = sut.createContainer(config, randomName()).id();
    final ContainerInfo info = sut.inspectContainer(id);
    final List<ContainerMount> mounts = info.mounts();
    assertThat(mounts.size(), equalTo(2));
    {
        final ContainerMount aMount = Iterables.find(mounts, new Predicate<ContainerMount>() {

            @Override
            public boolean apply(ContainerMount mount) {
                return !("nocopy".equals(mount.mode()));
            }
        }, null);
        assertThat("Could not find a mount (without nocopy)", aMount, notNullValue());
        assertThat(aMount.mode(), is(equalTo("ro")));
        assertThat(aMount.rw(), is(false));
        assertThat(aMount.source(), containsString("/" + aVolumeName + "/"));
        assertThat(aMount.destination(), is(equalTo(aVolumeTo)));
    }
    {
        final ContainerMount nocopyMount = Iterables.find(mounts, new Predicate<ContainerMount>() {

            @Override
            public boolean apply(ContainerMount mount) {
                return "nocopy".equals(mount.mode());
            }
        }, null);
        assertThat("Could not find mount (with nocopy)", nocopyMount, notNullValue());
        assertThat(nocopyMount.mode(), is(equalTo("nocopy")));
        assertThat(nocopyMount.rw(), is(true));
        assertThat(nocopyMount.source(), containsString("/" + nocopyVolumeName + "/"));
        assertThat(nocopyMount.destination(), is(equalTo(nocopyVolumeTo)));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) 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 42 with ContainerInfo

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

the class DefaultDockerClientTest method testVolumesFrom.

@Test
public void testVolumesFrom() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final String volumeContainer = randomName();
    final String mountContainer = randomName();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).addVolume("/foo").cmd("touch", "/foo/bar").build();
    sut.createContainer(volumeConfig, volumeContainer);
    sut.startContainer(volumeContainer);
    sut.waitContainer(volumeContainer);
    final HostConfig mountHostConfig = HostConfig.builder().volumesFrom(volumeContainer).build();
    final ContainerConfig mountConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(mountHostConfig).cmd("ls", "/foo").build();
    sut.createContainer(mountConfig, mountContainer);
    sut.startContainer(mountContainer);
    sut.waitContainer(mountContainer);
    final ContainerInfo info = sut.inspectContainer(mountContainer);
    assertThat(info.state().running(), is(false));
    assertThat(info.state().exitCode(), is(0));
    final String logs;
    try (LogStream stream = sut.logs(info.id(), stdout(), stderr())) {
        logs = stream.readFully();
    }
    assertThat(logs, containsString("bar"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) 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) Test(org.junit.Test)

Example 43 with ContainerInfo

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

the class DefaultDockerClientTest method testRestartContainer.

@Test
public void testRestartContainer() 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));
    }
    final ContainerInfo tempContainerInfo = sut.inspectContainer(containerId);
    final Integer originalPid = tempContainerInfo.state().pid();
    sut.restartContainer(containerId);
    // Should be running with short run time
    {
        final ContainerInfo containerInfoLatest = sut.inspectContainer(containerId);
        assertTrue(containerInfoLatest.state().running());
        assertThat(containerInfoLatest.state().pid(), not(equalTo(originalPid)));
    }
}
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 44 with ContainerInfo

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

the class DefaultDockerClientTest method testTmpfs.

@Test
public void testTmpfs() throws Exception {
    requireDockerApiVersionAtLeast("1.22", "TmpFs");
    // Pull image
    sut.pull(BUSYBOX_LATEST);
    final ImmutableMap<String, String> tmpfs = ImmutableMap.of("/tmp", "rw,noexec,nosuid,size=50m");
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(HostConfig.builder().tmpfs(tmpfs).build()).build();
    final ContainerCreation container = sut.createContainer(config, randomName());
    final ContainerInfo info = sut.inspectContainer(container.id());
    assertThat(info.hostConfig().tmpfs(), is(tmpfs));
}
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 45 with ContainerInfo

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

the class DefaultDockerClientTest method testMacAddress.

@Test
public void testMacAddress() throws Exception {
    requireDockerApiVersionAtLeast("1.18", "Mac Address");
    sut.pull(MEMCACHED_LATEST);
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sleep", "1000").macAddress("12:34:56:78:9a:bc").build();
    final ContainerCreation container = sut.createContainer(config, randomName());
    sut.startContainer(container.id());
    final ContainerInfo containerInfo = sut.inspectContainer(container.id());
    assertThat(containerInfo, notNullValue());
    assertThat(containerInfo.config().macAddress(), equalTo("12:34:56:78:9a:bc"));
}
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)

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