@@ -21,6 +21,7 @@ import (
2121 "os"
2222 "path/filepath"
2323 "runtime"
24+ "strings"
2425 "testing"
2526
2627 "gotest.tools/v3/assert"
@@ -84,6 +85,62 @@ services:
8485 assert .Equal (t , "hi\n " , string (testB ))
8586}
8687
88+ // func TestComposeUpEnvFile(t *testing.T) {
89+ // if runtime.GOOS == "windows" {
90+ // t.Skip("Skipping test on Windows")
91+ // }
92+
93+ // base := testutil.NewBase(t)
94+
95+ // tmpDir := t.TempDir()
96+ // fmt.Printf("Created temporary directory: %s\n", tmpDir)
97+
98+ // envFilePath := filepath.Join(tmpDir, ".env")
99+ // envFileContent := `
100+ // TEST_VAR1=Hello
101+ // TEST_VAR2=World
102+ // `
103+ // err := os.WriteFile(envFilePath, []byte(envFileContent), 0644)
104+ // assert.NilError(t, err)
105+ // fmt.Printf("Created .env file at: %s\n", envFilePath)
106+ // fmt.Printf("Env file content:\n%s\n", envFileContent)
107+
108+ // dockerComposeYAML := fmt.Sprintf(`
109+ // version: '3'
110+ // services:
111+ // test:
112+ // image: %s
113+ // command: sh -c 'echo $TEST_VAR1 $TEST_VAR2 > /tmp/test_output'
114+ // `, testutil.CommonImage)
115+
116+ // comp := testutil.NewComposeDir(t, dockerComposeYAML)
117+ // defer comp.CleanUp()
118+ // fmt.Printf("Created docker-compose.yml at: %s\n", comp.YAMLFullPath())
119+ // fmt.Printf("Docker Compose YAML content:\n%s\n", dockerComposeYAML)
120+
121+ // upCmd := base.ComposeCmd("--env-file", envFilePath, "-f", comp.YAMLFullPath(), "up", "-d")
122+ // upCmd.AssertOK()
123+ // time.Sleep(5 * time.Second)
124+ // defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down").AssertOK()
125+
126+ // psCmd := base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "-a")
127+ // fmt.Printf("Compose ps output:\n%s\n", psCmd.Run().Combined())
128+
129+ // containerID := strings.TrimSpace(base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "-q").Out())
130+ // fmt.Printf("Container ID: %s\n", containerID)
131+ // if containerID == "" {
132+ // t.Fatalf("Failed to get container ID")
133+ // }
134+
135+ // execCmd := base.Cmd("exec", containerID, "cat", "/tmp/test_output")
136+ // out := execCmd.Out()
137+
138+ // fmt.Printf("Command output: %s\n", out)
139+
140+ // assert.Equal(t, "Hello World\n", out)
141+ // fmt.Println("Test completed successfully")
142+ // }
143+
87144func TestComposeUpEnvFile (t * testing.T ) {
88145 if runtime .GOOS == "windows" {
89146 t .Skip ("Skipping test on Windows" )
@@ -112,7 +169,7 @@ version: '3'
112169services:
113170 test:
114171 image: %s
115- command: sh -c 'echo $TEST_VAR1 $TEST_VAR2 > /tmp/test_output'
172+ command: sh -c 'echo $TEST_VAR1 $TEST_VAR2 > /tmp/test_output && tail -f /dev/null '
116173` , testutil .CommonImage )
117174
118175 comp := testutil .NewComposeDir (t , dockerComposeYAML )
@@ -121,24 +178,39 @@ services:
121178 fmt .Printf ("Docker Compose YAML content:\n %s\n " , dockerComposeYAML )
122179
123180 // Run compose up with env-file
124- upCmd := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "--env-file" , envFilePath , "up" , "-d" )
125- upCmd .AssertOK ()
126- defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" ).AssertOK ()
181+ upCmd := base .ComposeCmd ("--env-file" , envFilePath , "-f" , comp .YAMLFullPath (), "up" , "-d" )
182+ upResult := upCmd .Run ()
183+ fmt .Printf ("Compose up result:\n Exit Code: %d\n Stdout:\n %s\n Stderr:\n %s\n " ,
184+ upResult .ExitCode , upResult .Stdout (), upResult .Stderr ())
185+
186+ defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" ).Run ()
187+
188+ // Print compose ps output
189+ psCmd := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" )
190+ fmt .Printf ("Compose ps output:\n %s\n " , psCmd .Run ().Combined ())
127191
128192 // Print compose logs
129193 logsCmd := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "logs" )
130194 fmt .Printf ("Compose logs:\n %s\n " , logsCmd .Run ().Combined ())
131195
132- // Get container ID
133- containerID := base .ComposeCmd ( "-f " , comp . YAMLFullPath (), "ps" , "-q" ). OutLines ()[ 0 ]
134- fmt .Printf ("Container ID: %s\n " , containerID )
196+ // Check if the image exists
197+ imageCmd := base .Cmd ( "images " , testutil . CommonImage )
198+ fmt .Printf ("Image check result: \n %s\n " , imageCmd . Run (). Combined () )
135199
136- // Execute command in the container
137- execCmd := base .Cmd ("exec" , containerID , "cat" , "/tmp/test_output" )
138- out := execCmd .Out ()
200+ // Get container name
201+ containerName := strings .TrimSpace (base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "-q" ).Out ())
202+ fmt .Printf ("Container Name/ID: %s\n " , containerName )
203+
204+ if containerName == "" {
205+ t .Fatalf ("Failed to get container name/ID" )
206+ }
139207
140- fmt .Printf ("Command output: %s\n " , out )
208+ // Execute command in the container
209+ execCmd := base .Cmd ("exec" , containerName , "cat" , "/tmp/test_output" )
210+ execResult := execCmd .Run ()
211+ fmt .Printf ("Exec result:\n Exit Code: %d\n Stdout:\n %s\n Stderr:\n %s\n " ,
212+ execResult .ExitCode , execResult .Stdout (), execResult .Stderr ())
141213
142- assert .Equal (t , "Hello World\n " , out )
214+ assert .Equal (t , "Hello World\n " , execResult . Stdout () )
143215 fmt .Println ("Test completed successfully" )
144216}
0 commit comments