Search in sources :

Example 1 with EndpointConfig

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

Aggregations

AttachedNetwork (com.spotify.docker.client.messages.AttachedNetwork)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1 EndpointConfig (com.spotify.docker.client.messages.EndpointConfig)1 EndpointIpamConfig (com.spotify.docker.client.messages.EndpointConfig.EndpointIpamConfig)1 Ipam (com.spotify.docker.client.messages.Ipam)1 IpamConfig (com.spotify.docker.client.messages.IpamConfig)1 Network (com.spotify.docker.client.messages.Network)1 NetworkConfig (com.spotify.docker.client.messages.NetworkConfig)1 NetworkConnection (com.spotify.docker.client.messages.NetworkConnection)1 NetworkCreation (com.spotify.docker.client.messages.NetworkCreation)1 Long.toHexString (java.lang.Long.toHexString)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1 Test (org.junit.Test)1