|
3 | 3 | import com.github.dockerjava.api.command.CreateContainerResponse;
|
4 | 4 | import com.github.dockerjava.api.command.CreateNetworkResponse;
|
5 | 5 | import com.github.dockerjava.api.command.InspectContainerResponse;
|
| 6 | +import com.github.dockerjava.api.exception.DockerException; |
| 7 | +import com.github.dockerjava.api.exception.NotFoundException; |
6 | 8 | import com.github.dockerjava.api.model.ContainerNetwork;
|
7 | 9 | import com.github.dockerjava.api.model.Network;
|
8 | 10 | import com.github.dockerjava.netty.AbstractNettyDockerClientTest;
|
|
17 | 19 | import java.lang.reflect.Method;
|
18 | 20 | import java.util.Collections;
|
19 | 21 |
|
| 22 | +import static org.hamcrest.Matchers.hasItem; |
| 23 | +import static org.hamcrest.core.Is.is; |
| 24 | +import static org.junit.Assert.assertThat; |
| 25 | + |
20 | 26 | @Test(groups = "integration")
|
21 | 27 | public class ConnectToNetworkCmdExecTest extends AbstractNettyDockerClientTest {
|
22 | 28 |
|
@@ -61,38 +67,49 @@ public void connectToNetwork() throws InterruptedException {
|
61 | 67 |
|
62 | 68 | @Test
|
63 | 69 | public void connectToNetworkWithContainerNetwork() throws InterruptedException {
|
| 70 | + final String NETWORK_SUBNET = "10.100.101.0/24"; |
| 71 | + final String NETWORK_NAME = "nettyTestNetwork"; |
| 72 | + final String CONTAINER_IP = "10.100.101.100"; |
| 73 | + |
| 74 | + CreateContainerResponse container = dockerClient.createContainerCmd("busybox") |
| 75 | + .withCmd("sleep", "9999") |
| 76 | + .exec(); |
64 | 77 |
|
65 |
| - CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999").exec(); |
66 | 78 | dockerClient.startContainerCmd(container.getId()).exec();
|
67 | 79 |
|
| 80 | + try { |
| 81 | + dockerClient.removeNetworkCmd(NETWORK_NAME).exec(); |
| 82 | + } catch (DockerException ignore) { |
| 83 | + } |
| 84 | + |
68 | 85 | CreateNetworkResponse network = dockerClient.createNetworkCmd()
|
69 |
| - .withName("testNetwork") |
| 86 | + .withName(NETWORK_NAME) |
70 | 87 | .withIpam(new Network.Ipam()
|
71 |
| - .withConfig(new Network.Ipam.Config() |
72 |
| - .withSubnet("10.100.100.0/24"))) |
| 88 | + .withConfig(new Network.Ipam.Config() |
| 89 | + .withSubnet(NETWORK_SUBNET))) |
73 | 90 | .exec();
|
74 | 91 |
|
75 | 92 | dockerClient.connectToNetworkCmd()
|
76 | 93 | .withNetworkId(network.getId())
|
77 | 94 | .withContainerId(container.getId())
|
78 | 95 | .withContainerNetwork(new ContainerNetwork()
|
79 |
| - .withAliases("testing") |
80 |
| - .withIpamConfig(new ContainerNetwork.Ipam() |
81 |
| - .withIpv4Address("10.100.100.100"))) |
| 96 | + .withAliases("aliasName") |
| 97 | + .withIpamConfig(new ContainerNetwork.Ipam() |
| 98 | + .withIpv4Address(CONTAINER_IP))) |
82 | 99 | .exec();
|
83 | 100 |
|
84 | 101 | Network updatedNetwork = dockerClient.inspectNetworkCmd().withNetworkId(network.getId()).exec();
|
85 | 102 |
|
86 | 103 | Network.ContainerNetworkConfig containerNetworkConfig = updatedNetwork.getContainers().get(container.getId());
|
87 | 104 | assertNotNull(containerNetworkConfig);
|
88 |
| - assertEquals(containerNetworkConfig.getIpv4Address(), "10.100.100.100/24"); |
| 105 | + assertThat(containerNetworkConfig.getIpv4Address(), is(CONTAINER_IP + "/24")); |
89 | 106 |
|
90 | 107 | InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec();
|
91 | 108 |
|
92 |
| - ContainerNetwork testNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get("testNetwork"); |
| 109 | + ContainerNetwork testNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(NETWORK_NAME); |
93 | 110 | assertNotNull(testNetwork);
|
94 |
| - assertEquals(testNetwork.getAliases(), Collections.singletonList("testing")); |
95 |
| - assertEquals(testNetwork.getGateway(), "10.100.100.1"); |
96 |
| - assertEquals(testNetwork.getIpAddress(), "10.100.100.100"); |
| 111 | + assertThat(testNetwork.getAliases(), hasItem("aliasName")); |
| 112 | + assertEquals(testNetwork.getGateway(), "10.100.101.1"); |
| 113 | + assertEquals(testNetwork.getIpAddress(), CONTAINER_IP); |
97 | 114 | }
|
98 | 115 | }
|
0 commit comments