11import { ContainerInspectInfo } from "dockerode" ;
2- import { InspectResult } from "../types" ;
32import { mapInspectResult } from "../utils/map-inspect-result" ;
43import { inspectContainerUntilPortsExposed } from "./inspect-container-util-ports-exposed" ;
54
6- function mockExposed ( ) : { inspectResult : ContainerInspectInfo ; mappedInspectResult : InspectResult } {
5+ function mockInspectResult ( ports : ContainerInspectInfo [ "NetworkSettings" ] [ "Ports" ] ) {
76 const date = new Date ( ) ;
87
98 const inspectResult : ContainerInspectInfo = {
@@ -22,34 +21,7 @@ function mockExposed(): { inspectResult: ContainerInspectInfo; mappedInspectResu
2221 FinishedAt : date . toISOString ( ) ,
2322 } ,
2423 NetworkSettings : {
25- Ports : { "8080/tcp" : [ { HostIp : "0.0.0.0" , HostPort : "45000" } ] } ,
26- Networks : { } ,
27- } ,
28- } as unknown as ContainerInspectInfo ;
29-
30- return { inspectResult, mappedInspectResult : mapInspectResult ( inspectResult ) } ;
31- }
32-
33- function mockNotExposed ( ) : { inspectResult : ContainerInspectInfo ; mappedInspectResult : InspectResult } {
34- const date = new Date ( ) ;
35-
36- const inspectResult : ContainerInspectInfo = {
37- Name : "container-id" ,
38- Config : {
39- Hostname : "hostname" ,
40- Labels : { } ,
41- } ,
42- State : {
43- Health : {
44- Status : "healthy" ,
45- } ,
46- Status : "running" ,
47- Running : true ,
48- StartedAt : date . toISOString ( ) ,
49- FinishedAt : date . toISOString ( ) ,
50- } ,
51- NetworkSettings : {
52- Ports : { "8080/tcp" : [ ] } ,
24+ Ports : ports ,
5325 Networks : { } ,
5426 } ,
5527 } as unknown as ContainerInspectInfo ;
@@ -58,7 +30,7 @@ function mockNotExposed(): { inspectResult: ContainerInspectInfo; mappedInspectR
5830}
5931
6032test ( "returns the inspect results when all ports are exposed" , async ( ) => {
61- const data = mockExposed ( ) ;
33+ const data = mockInspectResult ( { "8080/tcp" : [ { HostIp : "0.0.0.0" , HostPort : "45000" } ] } ) ;
6234 const inspectFn = vi . fn ( ) . mockResolvedValueOnce ( data . inspectResult ) ;
6335
6436 const result = await inspectContainerUntilPortsExposed ( inspectFn , [ 8080 ] , "container-id" ) ;
@@ -67,8 +39,8 @@ test("returns the inspect results when all ports are exposed", async () => {
6739} ) ;
6840
6941test ( "retries the inspect if ports are not yet exposed" , async ( ) => {
70- const data1 = mockNotExposed ( ) ;
71- const data2 = mockExposed ( ) ;
42+ const data1 = mockInspectResult ( { "8080/tcp" : [ ] } ) ;
43+ const data2 = mockInspectResult ( { "8080/tcp" : [ { HostIp : "0.0.0.0" , HostPort : "45000" } ] } ) ;
7244 const inspectFn = vi
7345 . fn ( )
7446 . mockResolvedValueOnce ( data1 . inspectResult )
@@ -81,8 +53,17 @@ test("retries the inspect if ports are not yet exposed", async () => {
8153 expect ( inspectFn ) . toHaveBeenCalledTimes ( 3 ) ;
8254} ) ;
8355
84- test ( "throws an error when ports are not exposed within timeout" , async ( ) => {
85- const data = mockNotExposed ( ) ;
56+ test ( "throws an error when host ports are not exposed within timeout" , async ( ) => {
57+ const data = mockInspectResult ( { "8080/tcp" : [ ] } ) ;
58+ const inspectFn = vi . fn ( ) . mockResolvedValue ( data . inspectResult ) ;
59+
60+ await expect ( inspectContainerUntilPortsExposed ( inspectFn , [ 8080 ] , "container-id" , 0 ) ) . rejects . toThrow (
61+ "Container did not expose all ports after starting"
62+ ) ;
63+ } ) ;
64+
65+ test ( "throws an error when container ports not exposed within timeout" , async ( ) => {
66+ const data = mockInspectResult ( { } ) ;
8667 const inspectFn = vi . fn ( ) . mockResolvedValue ( data . inspectResult ) ;
8768
8869 await expect ( inspectContainerUntilPortsExposed ( inspectFn , [ 8080 ] , "container-id" , 0 ) ) . rejects . toThrow (
0 commit comments