forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectToNetworkCmdIT.java
More file actions
96 lines (74 loc) · 4.13 KB
/
Copy pathConnectToNetworkCmdIT.java
File metadata and controls
96 lines (74 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.github.dockerjava.cmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.CreateNetworkResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Network;
import net.jcip.annotations.ThreadSafe;
import org.junit.Test;
import static com.github.dockerjava.junit.DockerAssume.assumeNotSwarm;
import static com.github.dockerjava.core.DockerRule.DEFAULT_IMAGE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* TODO fix parallel.
*/
@ThreadSafe
public class ConnectToNetworkCmdIT extends CmdIT {
@Test
public void connectToNetwork() throws InterruptedException {
assumeNotSwarm("no network in swarm", dockerRule);
String networkName = "connectToNetwork";
CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999").exec();
dockerRule.getClient().startContainerCmd(container.getId()).exec();
CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName(networkName).exec();
dockerRule.getClient().connectToNetworkCmd().withNetworkId(network.getId()).withContainerId(container.getId()).exec();
Network updatedNetwork = dockerRule.getClient().inspectNetworkCmd().withNetworkId(network.getId()).exec();
assertTrue(updatedNetwork.getContainers().containsKey(container.getId()));
InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
assertNotNull(inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName));
}
@Test
public void connectToNetworkWithContainerNetwork() throws InterruptedException {
assumeNotSwarm("no network in swarm", dockerRule);
final String subnetPrefix = "10.100.100";
final String networkName = "ContainerWithNetwork";
final String containerIp = subnetPrefix + ".100";
CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
.withCmd("sleep", "9999")
.exec();
dockerRule.getClient().startContainerCmd(container.getId()).exec();
try {
dockerRule.getClient().removeNetworkCmd(networkName).exec();
} catch (DockerException ignore) {
}
CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd()
.withName(networkName)
.withIpam(new Network.Ipam()
.withConfig(new Network.Ipam.Config()
.withSubnet(subnetPrefix + ".0/24")))
.exec();
dockerRule.getClient().connectToNetworkCmd()
.withNetworkId(network.getId())
.withContainerId(container.getId())
.withContainerNetwork(new ContainerNetwork()
.withAliases("aliasName")
.withIpamConfig(new ContainerNetwork.Ipam()
.withIpv4Address(containerIp)))
.exec();
Network updatedNetwork = dockerRule.getClient().inspectNetworkCmd().withNetworkId(network.getId()).exec();
Network.ContainerNetworkConfig containerNetworkConfig = updatedNetwork.getContainers().get(container.getId());
assertNotNull(containerNetworkConfig);
assertThat(containerNetworkConfig.getIpv4Address(), is(containerIp + "/24"));
InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
ContainerNetwork testNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName);
assertNotNull(testNetwork);
assertThat(testNetwork.getAliases(), hasItem("aliasName"));
assertThat(testNetwork.getGateway(), is(subnetPrefix + ".1"));
assertThat(testNetwork.getIpAddress(), is(containerIp));
}
}