2020import org .testng .annotations .BeforeClass ;
2121import org .testng .annotations .Test ;
2222
23+ import com .github .dockerjava .api .DockerClient ;
24+ import com .github .dockerjava .api .command .CreateContainerResponse ;
25+ import com .github .dockerjava .api .command .InspectContainerResponse ;
26+ import com .github .dockerjava .api .model .ExposedPort ;
27+ import com .github .dockerjava .api .model .Ports ;
28+ import com .github .dockerjava .core .DockerClientImpl ;
2329import com .google .common .collect .ImmutableList ;
2430import com .google .common .collect .Lists ;
25- import com .kpelykh .docker .client .DockerClient ;
26- import com .kpelykh .docker .client .DockerException ;
27- import com .kpelykh .docker .client .model .ContainerConfig ;
28- import com .kpelykh .docker .client .model .ContainerCreateResponse ;
29- import com .kpelykh .docker .client .model .HostConfig ;
30- import com .kpelykh .docker .client .model .Ports ;
31- import com .kpelykh .docker .client .model .Ports .Port ;
3231
3332/**
3433 * Test the InfluxDB API.
@@ -41,30 +40,32 @@ public class InfluxDBTest {
4140
4241 private InfluxDB influxDB ;
4342 private DockerClient dockerClient ;
44- private ContainerCreateResponse container ;
43+ private CreateContainerResponse container ;
4544
4645 /**
4746 * Create a influxDB connection before all tests start.
4847 *
49- * @throws DockerException
5048 * @throws InterruptedException
5149 */
5250 @ BeforeClass
53- public void setUp () throws DockerException , InterruptedException {
51+ public void setUp () throws InterruptedException {
5452 // Disable logging for the DockerClient.
5553 Logger .getLogger ("com.sun.jersey" ).setLevel (Level .OFF );
56- this .dockerClient = new DockerClient ("http://localhost:4243" );
57- this .dockerClient .pull ("majst01/influxdb-java" );
58-
59- ContainerConfig containerConfig = new ContainerConfig ();
60- containerConfig .setImage ("majst01/influxdb-java" );
61- this .container = this .dockerClient .createContainer (containerConfig );
62- HostConfig hostconfig = new HostConfig ();
63- hostconfig .setPortBindings (new Ports ());
64- hostconfig .getPortBindings ().addPort (new Port ("tcp" , "8086" , "0.0.0.0" , "8086" ));
65- this .dockerClient .startContainer (this .container .getId (), hostconfig );
66-
67- this .influxDB = InfluxDBFactory .connect ("http://localhost:8086" , "root" , "root" );
54+ this .dockerClient = new DockerClientImpl ("http://localhost:4243" );
55+ this .dockerClient .pullImageCmd ("majst01/influxdb-java" );
56+
57+ ExposedPort tcp8086 = ExposedPort .tcp (8086 );
58+
59+ Ports portBindings = new Ports ();
60+ portBindings .bind (tcp8086 , Ports .Binding (8086 ));
61+ this .container = this .dockerClient .createContainerCmd ("majst01/influxdb-java" ).exec ();
62+ this .dockerClient .startContainerCmd (this .container .getId ()).withPortBindings (portBindings ).exec ();
63+
64+ InspectContainerResponse inspectContainerResponse = this .dockerClient .inspectContainerCmd (
65+ this .container .getId ()).exec ();
66+
67+ String ip = inspectContainerResponse .getNetworkSettings ().getIpAddress ();
68+ this .influxDB = InfluxDBFactory .connect ("http://" + ip + ":8086" , "root" , "root" );
6869 boolean influxDBstarted = false ;
6970 do {
7071 Pong response ;
@@ -86,17 +87,15 @@ public void setUp() throws DockerException, InterruptedException {
8687
8788 /**
8889 * Ensure all Databases created get dropped afterwards.
89- *
90- * @throws DockerException
9190 */
9291 @ AfterClass
93- public void tearDown () throws DockerException {
92+ public void tearDown () {
9493 List <Database > result = this .influxDB .describeDatabases ();
9594 for (Database database : result ) {
9695 this .influxDB .deleteDatabase (database .getName ());
9796 }
9897
99- this .dockerClient .kill (this .container .getId ());
98+ this .dockerClient .killContainerCmd (this .container .getId ()). exec ( );
10099 }
101100
102101 /**
0 commit comments