|
6 | 6 | import static org.hamcrest.Matchers.containsInAnyOrder; |
7 | 7 | import static org.hamcrest.Matchers.containsString; |
8 | 8 | import static org.hamcrest.Matchers.equalTo; |
| 9 | +import static org.hamcrest.Matchers.hasItemInArray; |
9 | 10 | import static org.hamcrest.Matchers.is; |
10 | 11 | import static org.hamcrest.Matchers.isEmptyString; |
11 | 12 | import static org.hamcrest.Matchers.not; |
12 | 13 |
|
13 | 14 | import java.lang.reflect.Method; |
14 | 15 | import java.security.SecureRandom; |
15 | 16 | import java.util.Arrays; |
| 17 | +import java.util.UUID; |
16 | 18 |
|
17 | 19 | import org.testng.ITestResult; |
18 | 20 | import org.testng.annotations.AfterMethod; |
|
25 | 27 | import com.github.dockerjava.api.DockerException; |
26 | 28 | import com.github.dockerjava.api.command.CreateContainerResponse; |
27 | 29 | import com.github.dockerjava.api.command.InspectContainerResponse; |
| 30 | +import com.github.dockerjava.api.model.Bind; |
28 | 31 | import com.github.dockerjava.api.model.HostConfig; |
29 | 32 | import com.github.dockerjava.api.model.Link; |
30 | 33 | import com.github.dockerjava.api.model.Links; |
@@ -96,6 +99,55 @@ public void createContainerWithVolume() throws DockerException { |
96 | 99 | contains("/var/log")); |
97 | 100 | } |
98 | 101 |
|
| 102 | + @Test |
| 103 | + public void createContainerWithVolumesFrom() throws DockerException { |
| 104 | + |
| 105 | + Volume volume1 = new Volume("/opt/webapp1"); |
| 106 | + Volume volume2 = new Volume("/opt/webapp2"); |
| 107 | + |
| 108 | + String container1Name = UUID.randomUUID().toString(); |
| 109 | + |
| 110 | + // create a running container with bind mounts |
| 111 | + CreateContainerResponse container1 = dockerClient |
| 112 | + .createContainerCmd("busybox").withCmd("sleep", "9999") |
| 113 | + .withName(container1Name).exec(); |
| 114 | + LOG.info("Created container1 {}", container1.toString()); |
| 115 | + |
| 116 | + dockerClient.startContainerCmd(container1.getId()).withBinds( |
| 117 | + new Bind("/src/webapp1", volume1), new Bind("/src/webapp2", volume2)).exec(); |
| 118 | + LOG.info("Started container1 {}", container1.toString()); |
| 119 | + |
| 120 | + InspectContainerResponse inspectContainerResponse1 = dockerClient.inspectContainerCmd( |
| 121 | + container1.getId()).exec(); |
| 122 | + |
| 123 | + assertContainerHasVolumes(inspectContainerResponse1, volume1, volume2); |
| 124 | + |
| 125 | + // create a second container with volumes from first container |
| 126 | + CreateContainerResponse container2 = dockerClient |
| 127 | + .createContainerCmd("busybox").withCmd("sleep", "9999") |
| 128 | + .withVolumesFrom(container1Name).exec(); |
| 129 | + LOG.info("Created container2 {}", container2.toString()); |
| 130 | + |
| 131 | + InspectContainerResponse inspectContainerResponse2 = dockerClient |
| 132 | + .inspectContainerCmd(container2.getId()).exec(); |
| 133 | + |
| 134 | + // No volumes are created, the information is just stored in .HostConfig.VolumesFrom |
| 135 | + assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(container1Name)); |
| 136 | + |
| 137 | + // To ensure that the information stored in VolumesFrom really is considered |
| 138 | + // when starting the container, we start it and verify that it has the same |
| 139 | + // bind mounts as the first container. |
| 140 | + // This is somehow out of scope here, but it helped me to understand how the |
| 141 | + // VolumesFrom feature really works. |
| 142 | + dockerClient.startContainerCmd(container2.getId()).exec(); |
| 143 | + LOG.info("Started container2 {}", container2.toString()); |
| 144 | + |
| 145 | + inspectContainerResponse2 = dockerClient.inspectContainerCmd(container2.getId()).exec(); |
| 146 | + |
| 147 | + assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(container1Name)); |
| 148 | + assertContainerHasVolumes(inspectContainerResponse2, volume1, volume2); |
| 149 | + } |
| 150 | + |
99 | 151 | @Test |
100 | 152 | public void createContainerWithEnv() throws DockerException { |
101 | 153 |
|
|
0 commit comments