1- import Dockerode from "dockerode" ;
21import fetch from "node-fetch" ;
32import path from "path" ;
43import { createServer , Server } from "http" ;
@@ -9,19 +8,13 @@ import { Readable } from "stream";
98import { RandomUuid } from "./uuid" ;
109import { TestContainers } from "./test-containers" ;
1110import { RandomPortClient } from "./port-client" ;
12- import { getRunningContainerNames } from "./test-helper" ;
11+ import { getContainerById , getEvents , getRunningContainerNames } from "./test-helper" ;
1312
1413describe ( "GenericContainer" , ( ) => {
1514 jest . setTimeout ( 180_000 ) ;
1615
1716 const fixtures = path . resolve ( __dirname , ".." , "fixtures" , "docker" ) ;
1817
19- let dockerodeClient : Dockerode ;
20-
21- beforeEach ( ( ) => {
22- dockerodeClient = new Dockerode ( ) ;
23- } ) ;
24-
2518 it ( "should wait for port" , async ( ) => {
2619 const container = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.12" ) . withExposedPorts ( 8080 ) . start ( ) ;
2720
@@ -95,7 +88,7 @@ describe("GenericContainer", () => {
9588
9689 it ( "should set network mode" , async ( ) => {
9790 const container = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.12" ) . withNetworkMode ( "host" ) . start ( ) ;
98- const dockerContainer = dockerodeClient . getContainer ( container . getId ( ) ) ;
91+ const dockerContainer = getContainerById ( container . getId ( ) ) ;
9992
10093 const containerInfo = await dockerContainer . inspect ( ) ;
10194
@@ -177,7 +170,7 @@ describe("GenericContainer", () => {
177170
178171 it ( "should set default log driver" , async ( ) => {
179172 const container = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.12" ) . withDefaultLogDriver ( ) . start ( ) ;
180- const dockerContainer = dockerodeClient . getContainer ( container . getId ( ) ) ;
173+ const dockerContainer = getContainerById ( container . getId ( ) ) ;
181174
182175 const containerInfo = await dockerContainer . inspect ( ) ;
183176
@@ -193,7 +186,7 @@ describe("GenericContainer", () => {
193186 . withPrivilegedMode ( )
194187 . withExposedPorts ( 8080 )
195188 . start ( ) ;
196- const dockerContainer = dockerodeClient . getContainer ( container . getId ( ) ) ;
189+ const dockerContainer = getContainerById ( container . getId ( ) ) ;
197190 const containerInfo = await dockerContainer . inspect ( ) ;
198191 expect ( containerInfo . HostConfig . Privileged ) . toBe ( true ) ;
199192
@@ -207,7 +200,7 @@ describe("GenericContainer", () => {
207200 it ( "should use pull policy" , async ( ) => {
208201 const container1 = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.12" ) . withExposedPorts ( 8080 ) . start ( ) ;
209202
210- const events = ( await dockerodeClient . getEvents ( ) ) . setEncoding ( "utf-8" ) as Readable ;
203+ const events = await getEvents ( ) ;
211204
212205 const container2 = await new GenericContainer ( "cristianrgreco/testcontainer:1.1.12" )
213206 . withPullPolicy ( new AlwaysPullPolicy ( ) )
@@ -277,7 +270,7 @@ describe("GenericContainer", () => {
277270 . start ( )
278271 ) . rejects . toThrowError ( "Port 8081 not bound after 0ms" ) ;
279272
280- expect ( await getRunningContainerNames ( dockerodeClient ) ) . not . toContain ( containerName ) ;
273+ expect ( await getRunningContainerNames ( ) ) . not . toContain ( containerName ) ;
281274 } ) ;
282275
283276 it ( "should stop the container when the log message wait strategy times out" , async ( ) => {
@@ -292,7 +285,7 @@ describe("GenericContainer", () => {
292285 . start ( )
293286 ) . rejects . toThrowError ( `Log message "unexpected" not received after 0ms` ) ;
294287
295- expect ( await getRunningContainerNames ( dockerodeClient ) ) . not . toContain ( containerName ) ;
288+ expect ( await getRunningContainerNames ( ) ) . not . toContain ( containerName ) ;
296289 } ) ;
297290
298291 it ( "should stop the container when the health check wait strategy times out" , async ( ) => {
@@ -309,7 +302,7 @@ describe("GenericContainer", () => {
309302 . start ( )
310303 ) . rejects . toThrowError ( "Health check not healthy after 0ms" ) ;
311304
312- expect ( await getRunningContainerNames ( dockerodeClient ) ) . not . toContain ( containerName ) ;
305+ expect ( await getRunningContainerNames ( ) ) . not . toContain ( containerName ) ;
313306 } ) ;
314307
315308 it ( "should stop the container when the health check fails" , async ( ) => {
@@ -326,7 +319,7 @@ describe("GenericContainer", () => {
326319 . start ( )
327320 ) . rejects . toThrowError ( "Health check failed" ) ;
328321
329- expect ( await getRunningContainerNames ( dockerodeClient ) ) . not . toContain ( containerName ) ;
322+ expect ( await getRunningContainerNames ( ) ) . not . toContain ( containerName ) ;
330323 } ) ;
331324
332325 it ( "should honour .dockerignore file" , async ( ) => {
0 commit comments