@@ -21,6 +21,7 @@ import (
21
21
"os"
22
22
"path/filepath"
23
23
"runtime"
24
+ "strings"
24
25
"testing"
25
26
26
27
"gotest.tools/v3/assert"
@@ -84,6 +85,62 @@ services:
84
85
assert .Equal (t , "hi\n " , string (testB ))
85
86
}
86
87
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
+
87
144
func TestComposeUpEnvFile (t * testing.T ) {
88
145
if runtime .GOOS == "windows" {
89
146
t .Skip ("Skipping test on Windows" )
@@ -112,7 +169,7 @@ version: '3'
112
169
services:
113
170
test:
114
171
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 '
116
173
` , testutil .CommonImage )
117
174
118
175
comp := testutil .NewComposeDir (t , dockerComposeYAML )
@@ -121,24 +178,39 @@ services:
121
178
fmt .Printf ("Docker Compose YAML content:\n %s\n " , dockerComposeYAML )
122
179
123
180
// 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 ())
127
191
128
192
// Print compose logs
129
193
logsCmd := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "logs" )
130
194
fmt .Printf ("Compose logs:\n %s\n " , logsCmd .Run ().Combined ())
131
195
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 () )
135
199
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
+ }
139
207
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 ())
141
213
142
- assert .Equal (t , "Hello World\n " , out )
214
+ assert .Equal (t , "Hello World\n " , execResult . Stdout () )
143
215
fmt .Println ("Test completed successfully" )
144
216
}
0 commit comments