Search in sources :

Example 1 with Network

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

use of com.spotify.docker.client.messages.Network in project linuxtools by eclipse.

the class DockerConnection method listNetworks.

@Override
public List<IDockerNetwork> listNetworks() throws DockerException, InterruptedException {
    try {
        List<Network> networkList = client.listNetworks();
        ArrayList<IDockerNetwork> networks = new ArrayList<>();
        for (Network n : networkList) {
            networks.add(new DockerNetwork(n));
        }
        return networks;
    } catch (com.spotify.docker.client.exceptions.DockerException e) {
        throw new DockerException(e.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) IDockerNetwork(org.eclipse.linuxtools.docker.core.IDockerNetwork) Network(com.spotify.docker.client.messages.Network) IDockerNetwork(org.eclipse.linuxtools.docker.core.IDockerNetwork) ArrayList(java.util.ArrayList) IDockerNetwork(org.eclipse.linuxtools.docker.core.IDockerNetwork)

Example 3 with Network

use of com.spotify.docker.client.messages.Network 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 4 with Network

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

the class DefaultDockerClientTest method testFilterNetworks.

@Test
public void testFilterNetworks() throws Exception {
    requireDockerApiVersionAtLeast("1.22", "networks");
    final NetworkConfig network1Config = NetworkConfig.builder().checkDuplicate(true).name(randomName()).labels(ImmutableMap.of("is-test", "true")).build();
    final NetworkConfig network2Config = NetworkConfig.builder().checkDuplicate(true).name(randomName()).labels(ImmutableMap.of("is-test", "")).build();
    final Network network1 = createNetwork(network1Config);
    final Network network2 = createNetwork(network2Config);
    final Network hostNetwork = getHostNetwork();
    List<Network> networks;
    // filter by id
    networks = sut.listNetworks(ListNetworksParam.byNetworkId(network1.id()));
    assertThat(networks, hasItem(network1));
    assertThat(networks, not(hasItem(network2)));
    // filter by name
    networks = sut.listNetworks(ListNetworksParam.byNetworkName(network1.name()));
    assertThat(networks, hasItem(network1));
    assertThat(networks, not(hasItem(network2)));
    // filter by type
    networks = sut.listNetworks(ListNetworksParam.withType(BUILTIN));
    assertThat(networks, hasItem(hostNetwork));
    assertThat(networks, not(hasItems(network1, network2)));
    networks = sut.listNetworks(ListNetworksParam.builtInNetworks());
    assertThat(networks, hasItem(hostNetwork));
    assertThat(networks, not(hasItems(network1, network2)));
    networks = sut.listNetworks(ListNetworksParam.customNetworks());
    assertThat(networks, not(hasItem(hostNetwork)));
    assertThat(networks, hasItems(network1, network2));
    // filter by driver
    if (dockerApiVersionAtLeast("1.24")) {
        networks = sut.listNetworks(ListNetworksParam.withDriver("bridge"));
        assertThat(networks, not(hasItem(hostNetwork)));
        assertThat(networks, hasItems(network1, network2));
        networks = sut.listNetworks(ListNetworksParam.withDriver("host"));
        assertThat(networks, hasItem(hostNetwork));
        assertThat(networks, not(hasItems(network1, network2)));
    }
    // filter by label
    if (dockerApiVersionAtLeast("1.24")) {
        networks = sut.listNetworks(ListNetworksParam.withLabel("is-test"));
        assertThat(networks, not(hasItem(hostNetwork)));
        assertThat(networks, hasItems(network1, network2));
        networks = sut.listNetworks(ListNetworksParam.withLabel("is-test", "true"));
        assertThat(networks, hasItem(network1));
        assertThat(networks, not(hasItem(network2)));
        networks = sut.listNetworks(ListNetworksParam.withLabel("is-test", "false"));
        assertThat(networks, not(hasItems(network1, network2)));
    }
    sut.removeNetwork(network1.id());
    sut.removeNetwork(network2.id());
}
Also used : AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) Network(com.spotify.docker.client.messages.Network) NetworkConfig(com.spotify.docker.client.messages.NetworkConfig) Test(org.junit.Test)

Example 5 with Network

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

the class DefaultDockerClientTest method testNetworks.

@Test
public void testNetworks() throws Exception {
    requireDockerApiVersionAtLeast("1.21", "createNetwork and listNetworks");
    assumeFalse(CIRCLECI);
    final String networkName = randomName();
    final IpamConfig ipamConfig = IpamConfig.create("192.168.0.0/24", "192.168.0.0/24", "192.168.0.1");
    final Ipam ipam = Ipam.builder().driver("default").config(singletonList(ipamConfig)).build();
    final Map<String, String> labels = ImmutableMap.of("name", "starship", "foo", "bar");
    final NetworkConfig networkConfig = NetworkConfig.builder().name(networkName).driver("bridge").checkDuplicate(true).ipam(ipam).internal(false).enableIPv6(false).labels(labels).build();
    final NetworkCreation networkCreation = sut.createNetwork(networkConfig);
    assertThat(networkCreation.id(), is(notNullValue()));
    assertThat(networkCreation.warnings(), is(nullValue()));
    final List<Network> networks = sut.listNetworks();
    assertTrue(networks.size() > 0);
    Network network = null;
    for (final Network n : networks) {
        if (n.name().equals(networkName)) {
            network = n;
        }
    }
    assertThat(network, is(notNullValue()));
    // noinspection ConstantConditions
    assertThat(network.id(), is(notNullValue()));
    assertThat(sut.inspectNetwork(network.id()).name(), is(networkName));
    assertThat(network.ipam(), equalTo(ipam));
    if (dockerApiVersionAtLeast("1.23")) {
        assertThat(network.internal(), is(false));
        assertThat(network.enableIPv6(), is(false));
        assertThat(network.labels(), is(labels));
    }
    sut.removeNetwork(network.id());
    exception.expect(NetworkNotFoundException.class);
    sut.inspectNetwork(network.id());
}
Also used : Ipam(com.spotify.docker.client.messages.Ipam) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) Network(com.spotify.docker.client.messages.Network) 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) Test(org.junit.Test)

Aggregations

Network (com.spotify.docker.client.messages.Network)5 AttachedNetwork (com.spotify.docker.client.messages.AttachedNetwork)4 Test (org.junit.Test)4 NetworkConfig (com.spotify.docker.client.messages.NetworkConfig)3 NetworkCreation (com.spotify.docker.client.messages.NetworkCreation)3 Long.toHexString (java.lang.Long.toHexString)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)3 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)2 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 EndpointIpamConfig (com.spotify.docker.client.messages.EndpointConfig.EndpointIpamConfig)2 Ipam (com.spotify.docker.client.messages.Ipam)2 IpamConfig (com.spotify.docker.client.messages.IpamConfig)2 EndpointConfig (com.spotify.docker.client.messages.EndpointConfig)1 NetworkConnection (com.spotify.docker.client.messages.NetworkConnection)1 ArrayList (java.util.ArrayList)1 DockerException (org.eclipse.linuxtools.docker.core.DockerException)1 IDockerNetwork (org.eclipse.linuxtools.docker.core.IDockerNetwork)1