Search in sources :

Example 1 with Bind

use of com.spotify.docker.client.messages.HostConfig.Bind 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 2 with Bind

use of com.spotify.docker.client.messages.HostConfig.Bind 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)

Aggregations

ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)2 HostConfig (com.spotify.docker.client.messages.HostConfig)2 Bind (com.spotify.docker.client.messages.HostConfig.Bind)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Predicate (com.google.common.base.Predicate)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1 ContainerMount (com.spotify.docker.client.messages.ContainerMount)1 Volume (com.spotify.docker.client.messages.Volume)1 ConfigBind (com.spotify.docker.client.messages.swarm.ConfigBind)1 SecretBind (com.spotify.docker.client.messages.swarm.SecretBind)1 Long.toHexString (java.lang.Long.toHexString)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1