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