Search in sources :

Example 1 with AttachedNetwork

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

the class DefaultDockerClientTest method testNetworksConnectContainer.

@Test
public void testNetworksConnectContainer() throws Exception {
    requireDockerApiVersionAtLeast("1.21", "createNetwork and listNetworks");
    assumeFalse(CIRCLECI);
    sut.pull(BUSYBOX_LATEST);
    final String networkName = randomName();
    final String containerName = randomName();
    final NetworkCreation networkCreation = sut.createNetwork(NetworkConfig.builder().name(networkName).build());
    assertThat(networkCreation.id(), is(notNullValue()));
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    assertThat(containerCreation.id(), is(notNullValue()));
    sut.startContainer(containerCreation.id());
    sut.connectToNetwork(containerCreation.id(), networkCreation.id());
    Network network = sut.inspectNetwork(networkCreation.id());
    assertThat(network.containers().size(), equalTo(1));
    final Network.Container container = network.containers().get(containerCreation.id());
    assertThat(container, notNullValue());
    if (dockerApiVersionAtLeast("1.22")) {
        assertThat(container.name(), notNullValue());
    }
    assertThat(container.macAddress(), notNullValue());
    assertThat(container.ipv4Address(), notNullValue());
    assertThat(container.ipv6Address(), notNullValue());
    final ContainerInfo containerInfo = sut.inspectContainer(containerCreation.id());
    assertThat(containerInfo.networkSettings().networks().size(), is(2));
    final AttachedNetwork attachedNetwork = containerInfo.networkSettings().networks().get(networkName);
    assertThat(attachedNetwork, is(notNullValue()));
    if (dockerApiVersionAtLeast("1.22")) {
        assertThat(attachedNetwork.networkId(), is(notNullValue()));
    }
    assertThat(attachedNetwork.endpointId(), is(notNullValue()));
    assertThat(attachedNetwork.gateway(), is(notNullValue()));
    assertThat(attachedNetwork.ipAddress(), is(notNullValue()));
    assertThat(attachedNetwork.ipPrefixLen(), is(notNullValue()));
    assertThat(attachedNetwork.macAddress(), is(notNullValue()));
    assertThat(attachedNetwork.ipv6Gateway(), is(notNullValue()));
    assertThat(attachedNetwork.globalIPv6Address(), is(notNullValue()));
    assertThat(attachedNetwork.globalIPv6PrefixLen(), greaterThanOrEqualTo(0));
    sut.disconnectFromNetwork(containerCreation.id(), networkCreation.id());
    network = sut.inspectNetwork(networkCreation.id());
    assertThat(network.containers().size(), equalTo(0));
    sut.stopContainer(containerCreation.id(), 1);
    sut.removeContainer(containerCreation.id());
    sut.removeNetwork(networkCreation.id());
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) Network(com.spotify.docker.client.messages.Network) 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) NetworkCreation(com.spotify.docker.client.messages.NetworkCreation) Test(org.junit.Test)

Example 2 with AttachedNetwork

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

the class DefaultDockerClientTest method testNetworksConnectContainerWithEndpointConfig.

@Test
public void testNetworksConnectContainerWithEndpointConfig() throws Exception {
    requireDockerApiVersionAtLeast("1.22", "createNetwork and listNetworks");
    assumeFalse(CIRCLECI);
    final String networkName = randomName();
    final String containerName = randomName();
    final String subnet = "172.20.0.0/16";
    final String ipRange = "172.20.10.0/24";
    final String gateway = "172.20.10.11";
    final IpamConfig ipamConfigToCreate = IpamConfig.create(subnet, ipRange, gateway);
    final Ipam ipamToCreate = Ipam.builder().driver("default").config(Lists.newArrayList(ipamConfigToCreate)).build();
    final NetworkConfig networkingConfig = NetworkConfig.builder().name(networkName).ipam(ipamToCreate).build();
    final NetworkCreation networkCreation = sut.createNetwork(networkingConfig);
    assertThat(networkCreation.id(), is(notNullValue()));
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    assertThat(containerCreation.id(), is(notNullValue()));
    sut.startContainer(containerCreation.id());
    // Those are some of the extra parameters that can be set along with the network connection
    final String ip = "172.20.10.1";
    final String dummyAlias = "value-does-not-matter";
    final EndpointConfig endpointConfig = EndpointConfig.builder().ipamConfig(EndpointIpamConfig.builder().ipv4Address(ip).build()).aliases(ImmutableList.<String>of(dummyAlias)).build();
    final NetworkConnection networkConnection = NetworkConnection.builder().containerId(containerCreation.id()).endpointConfig(endpointConfig).build();
    sut.connectToNetwork(networkCreation.id(), networkConnection);
    Network network = sut.inspectNetwork(networkCreation.id());
    Network.Container networkContainer = network.containers().get(containerCreation.id());
    assertThat(network.containers().size(), equalTo(1));
    assertThat(networkContainer, notNullValue());
    final ContainerInfo containerInfo = sut.inspectContainer(containerCreation.id());
    assertThat(containerInfo.networkSettings().networks(), is(notNullValue()));
    assertThat(containerInfo.networkSettings().networks().size(), is(2));
    assertThat(containerInfo.networkSettings().networks(), hasKey(networkName));
    final AttachedNetwork attachedNetwork = containerInfo.networkSettings().networks().get(networkName);
    assertThat(attachedNetwork, is(notNullValue()));
    assertThat(attachedNetwork.networkId(), is(equalTo(networkCreation.id())));
    assertThat(attachedNetwork.endpointId(), is(notNullValue()));
    assertThat(attachedNetwork.gateway(), is(equalTo(gateway)));
    assertThat(attachedNetwork.ipAddress(), is(equalTo(ip)));
    assertThat(attachedNetwork.ipPrefixLen(), is(notNullValue()));
    assertThat(attachedNetwork.macAddress(), is(notNullValue()));
    assertThat(attachedNetwork.ipv6Gateway(), is(notNullValue()));
    assertThat(attachedNetwork.globalIPv6Address(), is(notNullValue()));
    assertThat(attachedNetwork.globalIPv6PrefixLen(), greaterThanOrEqualTo(0));
    assertThat(attachedNetwork.aliases(), is(notNullValue()));
    assertThat(dummyAlias, isIn(attachedNetwork.aliases()));
    sut.disconnectFromNetwork(containerCreation.id(), networkCreation.id());
    network = sut.inspectNetwork(networkCreation.id());
    assertThat(network.containers().size(), equalTo(0));
    sut.stopContainer(containerCreation.id(), 1);
    sut.removeContainer(containerCreation.id());
    sut.removeNetwork(networkCreation.id());
}
Also used : NetworkConnection(com.spotify.docker.client.messages.NetworkConnection) NetworkConfig(com.spotify.docker.client.messages.NetworkConfig) IpamConfig(com.spotify.docker.client.messages.IpamConfig) EndpointIpamConfig(com.spotify.docker.client.messages.EndpointConfig.EndpointIpamConfig) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) NetworkCreation(com.spotify.docker.client.messages.NetworkCreation) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) Ipam(com.spotify.docker.client.messages.Ipam) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) Network(com.spotify.docker.client.messages.Network) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) EndpointConfig(com.spotify.docker.client.messages.EndpointConfig) Test(org.junit.Test)

Example 3 with AttachedNetwork

use of com.spotify.docker.client.messages.AttachedNetwork in project zalenium by zalando.

the class DockerContainerMock method getMockedDockerContainerClient.

@SuppressWarnings("ConstantConditions")
public static DockerContainerClient getMockedDockerContainerClient(String networkName) {
    DockerClient dockerClient = mock(DockerClient.class);
    ExecCreation execCreation = mock(ExecCreation.class);
    LogStream logStream = mock(LogStream.class);
    when(logStream.readFully()).thenReturn("ANY_STRING");
    when(execCreation.id()).thenReturn("ANY_ID");
    ContainerCreation containerCreation = mock(ContainerCreation.class);
    when(containerCreation.id()).thenReturn("ANY_CONTAINER_ID");
    AttachedNetwork attachedNetwork = mock(AttachedNetwork.class);
    NetworkSettings networkSettings = mock(NetworkSettings.class);
    HostConfig hostConfig = mock(HostConfig.class);
    ImageInfo imageInfo = mock(ImageInfo.class);
    ContainerConfig containerConfig = mock(ContainerConfig.class);
    ContainerInfo containerInfo = mock(ContainerInfo.class);
    ContainerMount tmpMountedMount = mock(ContainerMount.class);
    when(tmpMountedMount.destination()).thenReturn("/tmp/node/tmp/mounted");
    when(tmpMountedMount.source()).thenReturn("/tmp/mounted");
    ContainerMount homeFolderMount = mock(ContainerMount.class);
    when(homeFolderMount.destination()).thenReturn("/tmp/node/home/seluser/folder");
    when(homeFolderMount.source()).thenReturn("/tmp/folder");
    when(containerInfo.mounts()).thenReturn(ImmutableList.of(tmpMountedMount, homeFolderMount));
    when(attachedNetwork.ipAddress()).thenReturn("127.0.0.1");
    when(networkSettings.networks()).thenReturn(ImmutableMap.of(networkName, attachedNetwork));
    when(networkSettings.ipAddress()).thenReturn("");
    when(containerInfo.networkSettings()).thenReturn(networkSettings);
    when(hostConfig.extraHosts()).thenReturn(null);
    when(containerInfo.hostConfig()).thenReturn(hostConfig);
    String[] httpEnvVars = { "zalenium_http_proxy=http://34.211.100.239:8080", "zalenium_https_proxy=http://34.211.100.239:8080" };
    when(containerConfig.env()).thenReturn(ImmutableList.copyOf(Arrays.asList(httpEnvVars)));
    when(containerInfo.config()).thenReturn(containerConfig);
    String containerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
    Container container_40000 = mock(Container.class);
    when(container_40000.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40000")));
    when(container_40000.id()).thenReturn(containerId);
    when(container_40000.status()).thenReturn("running");
    when(container_40000.image()).thenReturn("elgalu/selenium");
    Container container_40001 = mock(Container.class);
    when(container_40001.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40001")));
    when(container_40001.id()).thenReturn(containerId);
    when(container_40001.status()).thenReturn("running");
    when(container_40001.image()).thenReturn("elgalu/selenium");
    String zaleniumContainerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
    Container zalenium = mock(Container.class);
    when(zalenium.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium")));
    when(zalenium.id()).thenReturn(zaleniumContainerId);
    when(zalenium.status()).thenReturn("running");
    when(zalenium.image()).thenReturn("dosel/zalenium");
    Info dockerInfo = mock(Info.class);
    when(dockerInfo.name()).thenReturn("ubuntu_vm");
    try {
        URL logsLocation = TestUtils.class.getClassLoader().getResource("logs.tar");
        URL videosLocation = TestUtils.class.getClassLoader().getResource("videos.tar");
        File logsFile = new File(logsLocation.getPath());
        File videosFile = new File(videosLocation.getPath());
        when(dockerClient.archiveContainer(containerId, "/var/log/cont/")).thenReturn(new FileInputStream(logsFile));
        when(dockerClient.archiveContainer(containerId, "/videos/")).thenReturn(new FileInputStream(videosFile));
        String[] startVideo = { "bash", "-c", START_RECORDING.getContainerAction() };
        String[] stopVideo = { "bash", "-c", STOP_RECORDING.getContainerAction() };
        String[] transferLogs = { "bash", "-c", TRANSFER_LOGS.getContainerAction() };
        String[] cleanupContainer = { "bash", "-c", CLEANUP_CONTAINER.getContainerAction() };
        String[] sendNotificationCompleted = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(COMPLETED.getTestNotificationMessage()) };
        String[] sendNotificationTimeout = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(TIMEOUT.getTestNotificationMessage()) };
        when(dockerClient.execCreate(containerId, startVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, stopVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, transferLogs, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, cleanupContainer, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, sendNotificationCompleted, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, sendNotificationTimeout, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execStart(anyString())).thenReturn(logStream);
        doNothing().when(dockerClient).stopContainer(anyString(), anyInt());
        when(dockerClient.info()).thenReturn(dockerInfo);
        when(dockerClient.createContainer(any(ContainerConfig.class), anyString())).thenReturn(containerCreation);
        when(dockerClient.listContainers(DockerClient.ListContainersParam.allContainers())).thenReturn(Arrays.asList(container_40000, container_40001, zalenium));
        when(containerConfig.labels()).thenReturn(ImmutableMap.of("selenium_firefox_version", "52", "selenium_chrome_version", "58"));
        when(imageInfo.config()).thenReturn(containerConfig);
        when(dockerClient.inspectContainer(null)).thenReturn(containerInfo);
        when(dockerClient.inspectContainer(zaleniumContainerId)).thenReturn(containerInfo);
        when(dockerClient.inspectContainer(containerId)).thenReturn(containerInfo);
        when(dockerClient.inspectImage(anyString())).thenReturn(imageInfo);
        when(dockerClient.listImages(DockerClient.ListImagesParam.byName("elgalu/selenium"))).thenReturn(Collections.emptyList());
    } catch (DockerException | InterruptedException | IOException e) {
        e.printStackTrace();
    }
    DockerContainerClient dockerContainerClient = new DockerContainerClient();
    dockerContainerClient.setContainerClient(dockerClient);
    return dockerContainerClient;
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) DockerContainerClient(de.zalando.ep.zalenium.container.DockerContainerClient) LogStream(com.spotify.docker.client.LogStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) Info(com.spotify.docker.client.messages.Info) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) URL(java.net.URL) FileInputStream(java.io.FileInputStream) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) NetworkSettings(com.spotify.docker.client.messages.NetworkSettings) Container(com.spotify.docker.client.messages.Container) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) File(java.io.File) ContainerMount(com.spotify.docker.client.messages.ContainerMount)

Example 4 with AttachedNetwork

use of com.spotify.docker.client.messages.AttachedNetwork in project zalenium by zalando.

the class DockerContainerClient method getZaleniumNetwork.

private synchronized String getZaleniumNetwork(String zaleniumContainerName) {
    if (zaleniumNetwork != null) {
        return zaleniumNetwork;
    }
    String zaleniumContainerId = getContainerId(zaleniumContainerName);
    try {
        ContainerInfo containerInfo = dockerClient.inspectContainer(zaleniumContainerId);
        ImmutableMap<String, AttachedNetwork> networks = containerInfo.networkSettings().networks();
        for (Map.Entry<String, AttachedNetwork> networkEntry : networks.entrySet()) {
            if (!DEFAULT_DOCKER_NETWORK_NAME.equalsIgnoreCase(networkEntry.getKey())) {
                zaleniumNetwork = networkEntry.getKey();
                return zaleniumNetwork;
            }
        }
    } catch (DockerException | InterruptedException e) {
        logger.debug(nodeId + " Error while getting Zalenium network.", e);
        ga.trackException(e);
    }
    zaleniumNetwork = DEFAULT_DOCKER_NETWORK_MODE;
    return zaleniumNetwork;
}
Also used : AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

AttachedNetwork (com.spotify.docker.client.messages.AttachedNetwork)4 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)4 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)3 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)3 DockerException (com.spotify.docker.client.exceptions.DockerException)2 Network (com.spotify.docker.client.messages.Network)2 NetworkCreation (com.spotify.docker.client.messages.NetworkCreation)2 Long.toHexString (java.lang.Long.toHexString)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 DockerClient (com.spotify.docker.client.DockerClient)1 LogStream (com.spotify.docker.client.LogStream)1 Container (com.spotify.docker.client.messages.Container)1 ContainerMount (com.spotify.docker.client.messages.ContainerMount)1 EndpointConfig (com.spotify.docker.client.messages.EndpointConfig)1 EndpointIpamConfig (com.spotify.docker.client.messages.EndpointConfig.EndpointIpamConfig)1 ExecCreation (com.spotify.docker.client.messages.ExecCreation)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1