Search in sources :

Example 16 with HostConfig

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

the class DefaultDockerClientTest method testIpcMode.

@Test
public void testIpcMode() throws Exception {
    requireDockerApiVersionAtLeast("1.18", "IpcMode");
    final HostConfig hostConfig = HostConfig.builder().ipcMode("host").build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").hostConfig(hostConfig).build();
    final ContainerCreation container = sut.createContainer(config, randomName());
    final String containerId = container.id();
    sut.startContainer(containerId);
    final ContainerInfo info = sut.inspectContainer(containerId);
    assertThat(info.hostConfig().ipcMode(), is("host"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) 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 17 with HostConfig

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

the class DefaultDockerClientUnitTest method testCapAddAndDrop.

@Test
@SuppressWarnings("unchecked")
public void testCapAddAndDrop() throws Exception {
    final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
    final HostConfig hostConfig = HostConfig.builder().capAdd(ImmutableList.of("foo", "bar")).capAdd(ImmutableList.of("baz", "qux")).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).build();
    server.enqueue(new MockResponse());
    dockerClient.createContainer(containerConfig);
    final RecordedRequest recordedRequest = takeRequestImmediately();
    assertThat(recordedRequest.getMethod(), is("POST"));
    assertThat(recordedRequest.getPath(), is("/containers/create"));
    assertThat(recordedRequest.getHeader("Content-Type"), is("application/json"));
    // TODO (mbrown): use hamcrest-jackson for this, once we upgrade to Java 8
    final JsonNode requestJson = toJson(recordedRequest.getBody());
    final JsonNode capAddNode = requestJson.get("HostConfig").get("CapAdd");
    assertThat(capAddNode.isArray(), is(true));
    assertThat(childrenTextNodes((ArrayNode) capAddNode), containsInAnyOrder("baz", "qux"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HostConfig(com.spotify.docker.client.messages.HostConfig) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Test(org.junit.Test)

Example 18 with HostConfig

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

the class DefaultDockerClientUnitTest method testBindBuilderSelinuxLabeling.

@Test
public void testBindBuilderSelinuxLabeling() throws Exception {
    final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
    final Bind bindNoSelinuxLabel = HostConfig.Bind.builder().from("noselinux").to("noselinux").build();
    final Bind bindSharedSelinuxContent = HostConfig.Bind.builder().from("shared").to("shared").selinuxLabeling(true).build();
    final Bind bindPrivateSelinuxContent = HostConfig.Bind.builder().from("private").to("private").selinuxLabeling(false).build();
    final HostConfig hostConfig = HostConfig.builder().binds(bindNoSelinuxLabel, bindSharedSelinuxContent, bindPrivateSelinuxContent).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).build();
    server.enqueue(new MockResponse());
    dockerClient.createContainer(containerConfig);
    final RecordedRequest recordedRequest = takeRequestImmediately();
    final JsonNode requestJson = toJson(recordedRequest.getBody());
    final JsonNode binds = requestJson.get("HostConfig").get("Binds");
    assertThat(binds.isArray(), is(true));
    Set<String> bindSet = childrenTextNodes((ArrayNode) binds);
    assertThat(bindSet, hasSize(3));
    assertThat(bindSet, hasItem(allOf(containsString("noselinux"), not(containsString("z")), not(containsString("Z")))));
    assertThat(bindSet, hasItem(allOf(containsString("shared"), containsString("z"))));
    assertThat(bindSet, hasItem(allOf(containsString("private"), containsString("Z"))));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Bind(com.spotify.docker.client.messages.HostConfig.Bind) ConfigBind(com.spotify.docker.client.messages.swarm.ConfigBind) HostConfig(com.spotify.docker.client.messages.HostConfig) JsonNode(com.fasterxml.jackson.databind.JsonNode) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 19 with HostConfig

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

the class PushPullIT method startAuthedRegistry.

private static String startAuthedRegistry(final DockerClient client) throws Exception {
    final Map<String, List<PortBinding>> ports = singletonMap("5000/tcp", Collections.singletonList(PortBinding.of("0.0.0.0", 5000)));
    final HostConfig hostConfig = HostConfig.builder().portBindings(ports).binds(ImmutableList.of(Resources.getResource("dockerRegistry/auth").getPath() + ":/auth", Resources.getResource("dockerRegistry/certs").getPath() + ":/certs")).privileged(true).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().image(REGISTRY_IMAGE).hostConfig(hostConfig).env(ImmutableList.of("REGISTRY_AUTH=htpasswd", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd", "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt", "REGISTRY_HTTP_TLS_KEY=/certs/domain.key", "REGISTRY_HTTP_SECRET=super-secret")).build();
    return startAndAwaitContainer(client, containerConfig, REGISTRY_NAME);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) HostConfig(com.spotify.docker.client.messages.HostConfig) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 20 with HostConfig

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

the class DefaultDockerClientTest method testContainerWithCpuOptions.

@Test
public void testContainerWithCpuOptions() throws Exception {
    requireDockerApiVersionAtLeast("1.18", "Container creation with cpu options");
    sut.pull(BUSYBOX_LATEST);
    final HostConfig expected = HostConfig.builder().cpuShares(4096L).cpusetCpus("0,1").build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(expected).build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    sut.startContainer(id);
    final HostConfig actual = sut.inspectContainer(id).hostConfig();
    assertThat(actual.cpuShares(), equalTo(expected.cpuShares()));
    assertThat(actual.cpusetCpus(), equalTo(expected.cpusetCpus()));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

HostConfig (com.spotify.docker.client.messages.HostConfig)53 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)45 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)30 Test (org.junit.Test)29 Matchers.containsString (org.hamcrest.Matchers.containsString)22 Long.toHexString (java.lang.Long.toHexString)19 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)19 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)16 DockerClient (com.spotify.docker.client.DockerClient)12 List (java.util.List)11 DockerException (com.spotify.docker.client.exceptions.DockerException)8 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)6 LogStream (com.spotify.docker.client.LogStream)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)4 PortBinding (com.spotify.docker.client.messages.PortBinding)4 IOException (java.io.IOException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ImmutableList (com.google.common.collect.ImmutableList)3