@@ -65,8 +65,7 @@ public void startContainerWithVolumes() throws DockerException {
6565 Volume volume2 = new Volume ("/opt/webapp2" );
6666
6767 CreateContainerResponse container = dockerClient
68- .createContainerCmd ("busybox" )
69- .withVolumes (volume1 , volume2 )
68+ .createContainerCmd ("busybox" ).withVolumes (volume1 , volume2 )
7069 .withCmd ("true" ).exec ();
7170
7271 LOG .info ("Created container {}" , container .toString ());
@@ -91,8 +90,10 @@ public void startContainerWithVolumes() throws DockerException {
9190
9291 assertContainerHasVolumes (inspectContainerResponse , volume1 , volume2 );
9392
94- assertThat (Arrays .asList (inspectContainerResponse .getVolumesRW ()),
95- contains (new VolumeRW (volume1 , AccessMode .ro ), new VolumeRW (volume2 )));
93+ assertThat (
94+ Arrays .asList (inspectContainerResponse .getVolumesRW ()),
95+ contains (new VolumeRW (volume1 , AccessMode .ro ), new VolumeRW (
96+ volume2 )));
9697
9798 }
9899
@@ -224,6 +225,41 @@ public void startContainerWithPortBindings() throws DockerException {
224225
225226 }
226227
228+ @ Test
229+ public void startContainerWithRandomPortBindings () throws DockerException {
230+
231+ ExposedPort tcp22 = ExposedPort .tcp (22 );
232+ ExposedPort tcp23 = ExposedPort .tcp (23 );
233+
234+ Ports portBindings = new Ports ();
235+ portBindings .bind (tcp22 , Ports .Binding (null ));
236+ portBindings .bind (tcp23 , Ports .Binding (null ));
237+
238+ CreateContainerResponse container = dockerClient
239+ .createContainerCmd ("busybox" ).withCmd ("sleep" , "9999" )
240+ .withExposedPorts (tcp22 , tcp23 ).withPortBindings (portBindings )
241+ .withPublishAllPorts (true ).exec ();
242+
243+ LOG .info ("Created container {}" , container .toString ());
244+
245+ assertThat (container .getId (), not (isEmptyString ()));
246+
247+ dockerClient .startContainerCmd (container .getId ()).exec ();
248+
249+ InspectContainerResponse inspectContainerResponse = dockerClient
250+ .inspectContainerCmd (container .getId ()).exec ();
251+
252+ assertThat (Arrays .asList (inspectContainerResponse .getConfig ()
253+ .getExposedPorts ()), contains (tcp22 , tcp23 ));
254+
255+ assertThat (inspectContainerResponse .getNetworkSettings ().getPorts ()
256+ .getBindings ().get (tcp22 )[0 ].getHostPort (), is (notNullValue ()));
257+
258+ assertThat (inspectContainerResponse .getNetworkSettings ().getPorts ()
259+ .getBindings ().get (tcp23 )[0 ], is (notNullValue ()));
260+
261+ }
262+
227263 @ Test
228264 public void startContainerWithConflictingPortBindings ()
229265 throws DockerException {
@@ -303,8 +339,9 @@ public void startContainerWithLinkingDeprecated() throws DockerException {
303339 is (notNullValue ()));
304340 assertThat (inspectContainerResponse2 .getHostConfig ().getLinks (),
305341 is (notNullValue ()));
306- assertThat (inspectContainerResponse2 .getHostConfig ().getLinks (), equalTo (new Link [] { new Link ("container1" ,
307- "container1Link" ) }));
342+ assertThat (
343+ inspectContainerResponse2 .getHostConfig ().getLinks (),
344+ equalTo (new Link [] { new Link ("container1" , "container1Link" ) }));
308345 assertThat (inspectContainerResponse2 .getId (),
309346 startsWith (container2 .getId ()));
310347 assertThat (inspectContainerResponse2 .getName (), equalTo ("/container2" ));
@@ -313,8 +350,7 @@ public void startContainerWithLinkingDeprecated() throws DockerException {
313350 assertThat (inspectContainerResponse2 .getState ().isRunning (), is (true ));
314351
315352 }
316-
317-
353+
318354 @ Test
319355 public void startContainerWithLinking () throws DockerException {
320356
@@ -348,14 +384,12 @@ public void startContainerWithLinking() throws DockerException {
348384 CreateContainerResponse container2 = dockerClient
349385 .createContainerCmd ("busybox" ).withCmd ("sleep" , "9999" )
350386 .withName ("container2" )
351- .withLinks (new Link ("container1" , "container1Link" ))
352- .exec ();
387+ .withLinks (new Link ("container1" , "container1Link" )).exec ();
353388
354389 LOG .info ("Created container2 {}" , container2 .toString ());
355390 assertThat (container2 .getId (), not (isEmptyString ()));
356391
357- dockerClient .startContainerCmd (container2 .getId ())
358- .exec ();
392+ dockerClient .startContainerCmd (container2 .getId ()).exec ();
359393
360394 InspectContainerResponse inspectContainerResponse2 = dockerClient
361395 .inspectContainerCmd (container2 .getId ()).exec ();
@@ -367,8 +401,9 @@ public void startContainerWithLinking() throws DockerException {
367401 is (notNullValue ()));
368402 assertThat (inspectContainerResponse2 .getHostConfig ().getLinks (),
369403 is (notNullValue ()));
370- assertThat (inspectContainerResponse2 .getHostConfig ().getLinks (), equalTo (new Link [] { new Link ("container1" ,
371- "container1Link" ) }));
404+ assertThat (
405+ inspectContainerResponse2 .getHostConfig ().getLinks (),
406+ equalTo (new Link [] { new Link ("container1" , "container1Link" ) }));
372407 assertThat (inspectContainerResponse2 .getId (),
373408 startsWith (container2 .getId ()));
374409 assertThat (inspectContainerResponse2 .getName (), equalTo ("/container2" ));
@@ -512,8 +547,7 @@ public void startContainerWithExtraHosts() throws DockerException {
512547 assertThat (container .getId (), not (isEmptyString ()));
513548
514549 dockerClient .startContainerCmd (container .getId ())
515- .withExtraHosts ("dockerhost:127.0.0.1" )
516- .exec ();
550+ .withExtraHosts ("dockerhost:127.0.0.1" ).exec ();
517551
518552 InspectContainerResponse inspectContainerResponse = dockerClient
519553 .inspectContainerCmd (container .getId ()).exec ();
0 commit comments