@@ -21,7 +21,9 @@ import (
2121 "os"
2222 "path/filepath"
2323 "runtime"
24+ "strings"
2425 "testing"
26+ "time"
2527
2628 "gotest.tools/v3/assert"
2729 "gotest.tools/v3/icmd"
@@ -91,50 +93,44 @@ func TestComposeUpEnvFile(t *testing.T) {
9193
9294 base := testutil .NewBase (t )
9395
94- // Create a temporary directory for the test
9596 tmpDir := t .TempDir ()
9697 fmt .Printf ("Created temporary directory: %s\n " , tmpDir )
9798
98- // Create an .env file
9999 envFilePath := filepath .Join (tmpDir , ".env" )
100- envFileContent := `
101- TEST_VAR1=Hello
102- TEST_VAR2=World
103- `
100+ envFileContent := `TEST_VAR1=Hello TEST_VAR2=World`
104101 err := os .WriteFile (envFilePath , []byte (envFileContent ), 0644 )
105102 assert .NilError (t , err )
106103 fmt .Printf ("Created .env file at: %s\n " , envFilePath )
107104 fmt .Printf ("Env file content:\n %s\n " , envFileContent )
108105
109- // Create docker-compose.yml
110106 dockerComposeYAML := fmt .Sprintf (`
111107version: '3'
112108services:
113109 test:
114110 image: %s
115- command: sh -c 'echo $TEST_VAR1 $TEST_VAR2 > /tmp/test_output '
111+ command: sh -c 'echo $TEST_VAR1 $TEST_VAR2'
116112` , testutil .CommonImage )
117113
118114 comp := testutil .NewComposeDir (t , dockerComposeYAML )
119115 defer comp .CleanUp ()
120116 fmt .Printf ("Created docker-compose.yml at: %s\n " , comp .YAMLFullPath ())
121117 fmt .Printf ("Docker Compose YAML content:\n %s\n " , dockerComposeYAML )
122118
123- // Run compose up with env-file
124- upCmd := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "--env-file" , envFilePath , "up" , "-d" )
119+ upCmd := base .ComposeCmd ("--env-file" , envFilePath , "-f" , comp .YAMLFullPath (), "up" , "-d" )
125120 upCmd .AssertOK ()
121+ time .Sleep (5 * time .Second )
126122 defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" ).AssertOK ()
127123
128- // Print compose logs
129- logsCmd := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "logs" )
130- fmt .Printf ("Compose logs:\n %s\n " , logsCmd .Run ().Combined ())
124+ psCmd := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "-a" )
125+ fmt .Printf ("Compose ps output:\n %s\n " , psCmd .Run ().Combined ())
131126
132- // Get container ID
133- containerID := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "-q" ).OutLines ()[0 ]
127+ containerID := strings .TrimSpace (base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "-q" ).Out ())
134128 fmt .Printf ("Container ID: %s\n " , containerID )
129+ if containerID == "" {
130+ t .Fatalf ("Failed to get container ID" )
131+ }
135132
136- // Execute command in the container
137- execCmd := base .Cmd ("exec" , containerID , "cat" , "/tmp/test_output" )
133+ execCmd := base .Cmd ("logs" , containerID )
138134 out := execCmd .Out ()
139135
140136 fmt .Printf ("Command output: %s\n " , out )
0 commit comments