use of com.spotify.docker.client.messages.NetworkCreation 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());
}
use of com.spotify.docker.client.messages.NetworkCreation in project docker-client by spotify.
the class DefaultDockerClientTest method testNetworkDrivers.
@Test
public void testNetworkDrivers() throws Exception {
requireDockerApiVersionAtLeast("1.21", "networks");
NetworkConfig.Builder networkConfigBuilder = NetworkConfig.builder();
if (dockerApiVersionEquals("1.24")) {
// workaround for https://github.com/docker/docker/issues/25735
networkConfigBuilder = networkConfigBuilder.ipam(Ipam.create("default", Collections.<IpamConfig>emptyList()));
}
final NetworkConfig bridgeDriverConfig = networkConfigBuilder.name(randomName()).driver("bridge").build();
final NetworkCreation bridgeDriverCreation = sut.createNetwork(bridgeDriverConfig);
assertThat(bridgeDriverCreation, notNullValue());
assertThat(bridgeDriverCreation.id(), notNullValue());
assertThat(bridgeDriverCreation.warnings(), anyOf(nullValue(String.class), equalTo("")));
sut.removeNetwork(bridgeDriverCreation.id());
if (dockerApiVersionAtLeast("1.24")) {
// These network drivers only exist in later versions
final NetworkConfig macvlanDriverConfig = networkConfigBuilder.name(randomName()).driver("macvlan").build();
final NetworkCreation macvlanDriverCreation = sut.createNetwork(macvlanDriverConfig);
assertThat(macvlanDriverCreation, notNullValue());
assertThat(macvlanDriverCreation.id(), notNullValue());
assertThat(macvlanDriverCreation.warnings(), anyOf(nullValue(String.class), equalTo("")));
sut.removeNetwork(macvlanDriverCreation.id());
final NetworkConfig overlayDriverConfig = networkConfigBuilder.name(randomName()).driver("overlay").build();
final NetworkCreation overlayDriverCreation = sut.createNetwork(overlayDriverConfig);
assertThat(overlayDriverCreation, notNullValue());
assertThat(overlayDriverCreation.id(), notNullValue());
assertThat(overlayDriverCreation.warnings(), anyOf(nullValue(String.class), equalTo("")));
sut.removeNetwork(overlayDriverCreation.id());
}
}
use of com.spotify.docker.client.messages.NetworkCreation in project docker-client by spotify.
the class DefaultDockerClientTest method createNetwork.
private Network createNetwork(final NetworkConfig networkConfig) throws DockerException, InterruptedException {
final NetworkCreation networkCreation = sut.createNetwork(networkConfig);
assertThat(networkCreation.id(), is(notNullValue()));
assertThat(networkCreation.warnings(), is(nullValue()));
return sut.inspectNetwork(networkCreation.id());
}
use of com.spotify.docker.client.messages.NetworkCreation 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());
}
use of com.spotify.docker.client.messages.NetworkCreation in project docker-client by spotify.
the class DefaultDockerClientTest method testCreateServiceWithNetwork.
@Test
public void testCreateServiceWithNetwork() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final String networkName = randomName();
final String serviceName = randomName();
NetworkConfig.Builder networkConfigBuilder = NetworkConfig.builder().driver("overlay").name(networkName);
if (dockerApiVersionEquals("1.24")) {
// workaround for https://github.com/docker/docker/issues/25735
networkConfigBuilder = networkConfigBuilder.ipam(Ipam.create("default", Collections.<IpamConfig>emptyList()));
}
final NetworkCreation networkCreation = sut.createNetwork(networkConfigBuilder.build());
final String networkId = networkCreation.id();
assertThat(networkId, is(notNullValue()));
final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").command(new String[] { "ping", "-c1000", "localhost" }).build()).build();
final ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(ServiceMode.withReplicas(1L)).networks(NetworkAttachmentConfig.builder().target(networkName).build()).build();
final ServiceCreateResponse response = sut.createService(spec);
assertThat(response.id(), is(notNullValue()));
final Service inspectService = sut.inspectService(serviceName);
assertThat(inspectService.spec().networks().size(), is(1));
assertThat(Iterables.find(inspectService.spec().networks(), new Predicate<NetworkAttachmentConfig>() {
@Override
public boolean apply(NetworkAttachmentConfig config) {
return networkId.equals(config.target());
}
}, null), is(notNullValue()));
sut.removeService(serviceName);
sut.removeNetwork(networkName);
}
Aggregations