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));
}
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());
}
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);
}
}
}
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();
}
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));
}
Aggregations