@@ -21,7 +21,9 @@ import (
21
21
"os"
22
22
"path/filepath"
23
23
"runtime"
24
+ "strings"
24
25
"testing"
26
+ "time"
25
27
26
28
"gotest.tools/v3/assert"
27
29
"gotest.tools/v3/icmd"
@@ -91,50 +93,44 @@ func TestComposeUpEnvFile(t *testing.T) {
91
93
92
94
base := testutil .NewBase (t )
93
95
94
- // Create a temporary directory for the test
95
96
tmpDir := t .TempDir ()
96
97
fmt .Printf ("Created temporary directory: %s\n " , tmpDir )
97
98
98
- // Create an .env file
99
99
envFilePath := filepath .Join (tmpDir , ".env" )
100
- envFileContent := `
101
- TEST_VAR1=Hello
102
- TEST_VAR2=World
103
- `
100
+ envFileContent := `TEST_VAR1=Hello TEST_VAR2=World`
104
101
err := os .WriteFile (envFilePath , []byte (envFileContent ), 0644 )
105
102
assert .NilError (t , err )
106
103
fmt .Printf ("Created .env file at: %s\n " , envFilePath )
107
104
fmt .Printf ("Env file content:\n %s\n " , envFileContent )
108
105
109
- // Create docker-compose.yml
110
106
dockerComposeYAML := fmt .Sprintf (`
111
107
version: '3'
112
108
services:
113
109
test:
114
110
image: %s
115
- command: sh -c 'echo $TEST_VAR1 $TEST_VAR2 > /tmp/test_output '
111
+ command: sh -c 'echo $TEST_VAR1 $TEST_VAR2'
116
112
` , testutil .CommonImage )
117
113
118
114
comp := testutil .NewComposeDir (t , dockerComposeYAML )
119
115
defer comp .CleanUp ()
120
116
fmt .Printf ("Created docker-compose.yml at: %s\n " , comp .YAMLFullPath ())
121
117
fmt .Printf ("Docker Compose YAML content:\n %s\n " , dockerComposeYAML )
122
118
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" )
125
120
upCmd .AssertOK ()
121
+ time .Sleep (5 * time .Second )
126
122
defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" ).AssertOK ()
127
123
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 ())
131
126
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 ())
134
128
fmt .Printf ("Container ID: %s\n " , containerID )
129
+ if containerID == "" {
130
+ t .Fatalf ("Failed to get container ID" )
131
+ }
135
132
136
- // Execute command in the container
137
- execCmd := base .Cmd ("exec" , containerID , "cat" , "/tmp/test_output" )
133
+ execCmd := base .Cmd ("logs" , containerID )
138
134
out := execCmd .Out ()
139
135
140
136
fmt .Printf ("Command output: %s\n " , out )
0 commit comments