@@ -6,6 +6,7 @@ import { GenericContainer } from "./generic-container";
66import { AlwaysPullPolicy } from "./pull-policy" ;
77import { Wait } from "./wait" ;
88import { Readable } from "stream" ;
9+ import { RandomUuid } from "./uuid" ;
910
1011describe ( "GenericContainer" , ( ) => {
1112 jest . setTimeout ( 180_000 ) ;
@@ -245,6 +246,69 @@ describe("GenericContainer", () => {
245246 await container . stop ( ) ;
246247 } ) ;
247248
249+ it ( "should stop the container when the host port check wait strategy times out" , async ( ) => {
250+ const containerName = `container-${ new RandomUuid ( ) . nextUuid ( ) } ` ;
251+
252+ await expect (
253+ new GenericContainer ( "cristianrgreco/testcontainer" , "1.1.12" )
254+ . withName ( containerName )
255+ . withExposedPorts ( 8081 )
256+ . withStartupTimeout ( new Duration ( 0 , TemporalUnit . MILLISECONDS ) )
257+ . start ( )
258+ ) . rejects . toThrowError ( "Port 8081 not bound after 0ms" ) ;
259+
260+ expect ( await getRunningContainerNames ( ) ) . not . toContain ( containerName ) ;
261+ } ) ;
262+
263+ it ( "should stop the container when the log message wait strategy times out" , async ( ) => {
264+ const containerName = `container-${ new RandomUuid ( ) . nextUuid ( ) } ` ;
265+
266+ await expect (
267+ new GenericContainer ( "cristianrgreco/testcontainer" , "1.1.12" )
268+ . withName ( containerName )
269+ . withExposedPorts ( 8080 )
270+ . withWaitStrategy ( Wait . forLogMessage ( "unexpected" ) )
271+ . withStartupTimeout ( new Duration ( 0 , TemporalUnit . MILLISECONDS ) )
272+ . start ( )
273+ ) . rejects . toThrowError ( `Log message "unexpected" not received after 0ms` ) ;
274+
275+ expect ( await getRunningContainerNames ( ) ) . not . toContain ( containerName ) ;
276+ } ) ;
277+
278+ it ( "should stop the container when the health check wait strategy times out" , async ( ) => {
279+ const containerName = `container-${ new RandomUuid ( ) . nextUuid ( ) } ` ;
280+
281+ const context = path . resolve ( fixtures , "docker-with-health-check" ) ;
282+ const customGenericContainer = await GenericContainer . fromDockerfile ( context ) . build ( ) ;
283+ await expect (
284+ customGenericContainer
285+ . withName ( containerName )
286+ . withExposedPorts ( 8080 )
287+ . withWaitStrategy ( Wait . forHealthCheck ( ) )
288+ . withStartupTimeout ( new Duration ( 0 , TemporalUnit . MILLISECONDS ) )
289+ . start ( )
290+ ) . rejects . toThrowError ( "Health check not healthy after 0ms" ) ;
291+
292+ expect ( await getRunningContainerNames ( ) ) . not . toContain ( containerName ) ;
293+ } ) ;
294+
295+ it ( "should stop the container when the health check fails" , async ( ) => {
296+ const containerName = `container-${ new RandomUuid ( ) . nextUuid ( ) } ` ;
297+
298+ const context = path . resolve ( fixtures , "docker-with-health-check" ) ;
299+ const customGenericContainer = await GenericContainer . fromDockerfile ( context ) . build ( ) ;
300+ await expect (
301+ customGenericContainer
302+ . withName ( containerName )
303+ . withExposedPorts ( 8080 )
304+ . withHealthCheck ( { test : "exit 1" } )
305+ . withWaitStrategy ( Wait . forHealthCheck ( ) )
306+ . start ( )
307+ ) . rejects . toThrowError ( "Health check failed" ) ;
308+
309+ expect ( await getRunningContainerNames ( ) ) . not . toContain ( containerName ) ;
310+ } ) ;
311+
248312 it ( "should honour .dockerignore file" , async ( ) => {
249313 const context = path . resolve ( fixtures , "docker-with-dockerignore" ) ;
250314 const container = await GenericContainer . fromDockerfile ( context ) . build ( ) ;
@@ -312,4 +376,12 @@ describe("GenericContainer", () => {
312376 await startedContainer . stop ( ) ;
313377 } ) ;
314378 } ) ;
379+
380+ const getRunningContainerNames = async ( ) : Promise < string [ ] > => {
381+ const containers = await dockerodeClient . listContainers ( ) ;
382+ return containers
383+ . map ( ( container ) => container . Names )
384+ . reduce ( ( result , containerNames ) => [ ...result , ...containerNames ] , [ ] )
385+ . map ( ( containerName ) => containerName . replace ( "/" , "" ) ) ;
386+ } ;
315387} ) ;
0 commit comments