@@ -18,9 +18,11 @@ import (
1818/* -------------------- Dockertest Ephemeral PATH Container Setup -------------------- */
1919
2020const (
21+ imageName = "path-image"
2122 containerName = "path"
2223 internalPathPort = "3000"
23- dockerfilePath = "../Dockerfile"
24+ buildContextDir = ".."
25+ dockerfileName = "Dockerfile"
2426 configMountPoint = ":/app/.config.yaml"
2527 containerEnvImageTag = "IMAGE_TAG=test"
2628 containerExtraHost = "host.docker.internal:host-gateway" // allows the container to access the host machine's Docker daemon
@@ -77,29 +79,68 @@ func setupPathInstance(t *testing.T, configFilePath string) (containerPort strin
7779func setupPathDocker (t * testing.T , configFilePath string ) (* dockertest.Pool , * dockertest.Resource , string ) {
7880 t .Helper ()
7981
80- // eg. {file_path}/path/e2e/.config.test.yaml:/app/ .config.yaml
81- containerConfigMount : = filepath .Join (os .Getenv ("PWD" ), configFilePath ) + configMountPoint
82+ // eg. {file_path}/path/e2e/.shannon .config.yaml
83+ configFilePath = filepath .Join (os .Getenv ("PWD" ), configFilePath )
8284
83- opts := & dockertest.RunOptions {
84- Name : containerName ,
85- Mounts : []string {containerConfigMount },
86- Env : []string {containerEnvImageTag },
87- ExposedPorts : []string {containerPortAndProtocol },
88- ExtraHosts : []string {containerExtraHost },
85+ // Check if config file exists and exit if it does not
86+ if _ , err := os .Stat (configFilePath ); os .IsNotExist (err ) {
87+ t .Fatalf ("config file does not exist: %s" , configFilePath )
8988 }
9089
90+ // eg. {file_path}/path/e2e/.shannon.config.yaml:/app/.config.yaml
91+ containerConfigMount := configFilePath + configMountPoint
92+
93+ // Initialize the dockertest pool
9194 pool , err := dockertest .NewPool ("" )
9295 if err != nil {
9396 t .Fatalf ("Could not construct pool: %s" , err )
9497 }
95- resource , err := pool .BuildAndRunWithOptions (dockerfilePath , opts , func (config * docker.HostConfig ) {
98+
99+ // Build the image and log build output
100+ buildOptions := docker.BuildImageOptions {
101+ Name : imageName ,
102+ ContextDir : buildContextDir ,
103+ Dockerfile : dockerfileName ,
104+ OutputStream : os .Stdout ,
105+ SuppressOutput : false ,
106+ NoCache : false ,
107+ }
108+ if err := pool .Client .BuildImage (buildOptions ); err != nil {
109+ t .Fatalf ("could not build path image: %s" , err )
110+ }
111+
112+ // Run the built image
113+ runOpts := & dockertest.RunOptions {
114+ Name : containerName ,
115+ Repository : imageName ,
116+ Mounts : []string {containerConfigMount },
117+ Env : []string {containerEnvImageTag },
118+ ExposedPorts : []string {containerPortAndProtocol },
119+ ExtraHosts : []string {containerExtraHost },
120+ }
121+ resource , err := pool .RunWithOptions (runOpts , func (config * docker.HostConfig ) {
96122 config .AutoRemove = true
97123 config .RestartPolicy = docker.RestartPolicy {Name : "no" }
98124 })
99125 if err != nil {
100126 t .Fatalf ("Could not start resource: %s" , err )
101127 }
102128
129+ // Print container logs in a goroutine to prevent blocking
130+ go func () {
131+ if err := pool .Client .Logs (docker.LogsOptions {
132+ Container : resource .Container .ID ,
133+ OutputStream : os .Stdout ,
134+ ErrorStream : os .Stderr ,
135+ Stdout : true ,
136+ Stderr : true ,
137+ Follow : true ,
138+ }); err != nil {
139+ fmt .Printf ("could not fetch logs for PATH container: %s" , err )
140+ }
141+ }()
142+
143+ // Handle termination signals
103144 c := make (chan os.Signal , 1 )
104145 signal .Notify (c , os .Interrupt , syscall .SIGTERM )
105146 go func () {
0 commit comments