Skip to content

Commit 6017f38

Browse files
committed
add test fopr compose
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent b0921b4 commit 6017f38

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

cmd/nerdctl/compose/compose_up_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,57 @@ services:
8383
assert.NilError(t, err)
8484
assert.Equal(t, "hi\n", string(testB))
8585
}
86+
87+
func TestComposeUpEnvFile(t *testing.T) {
88+
if runtime.GOOS == "windows" {
89+
t.Skip("Skipping test on Windows")
90+
}
91+
92+
base := testutil.NewBase(t)
93+
94+
// Create a temporary directory for the test
95+
tmpDir := t.TempDir()
96+
fmt.Printf("Created temporary directory: %s\n", tmpDir)
97+
98+
// Create an .env file
99+
envFilePath := filepath.Join(tmpDir, ".env")
100+
envFileContent := `
101+
TEST_VAR1=Hello
102+
TEST_VAR2=World
103+
`
104+
err := os.WriteFile(envFilePath, []byte(envFileContent), 0644)
105+
assert.NilError(t, err)
106+
fmt.Printf("Created .env file at: %s\n", envFilePath)
107+
fmt.Printf("Env file content:\n%s\n", envFileContent)
108+
109+
// Create docker-compose.yml
110+
dockerComposeYAML := fmt.Sprintf(`
111+
version: '3'
112+
services:
113+
test:
114+
image: %s
115+
command: sh -c 'echo $TEST_VAR1 $TEST_VAR2 > /tmp/test_output'
116+
`, testutil.CommonImage)
117+
118+
comp := testutil.NewComposeDir(t, dockerComposeYAML)
119+
defer comp.CleanUp()
120+
fmt.Printf("Created docker-compose.yml at: %s\n", comp.YAMLFullPath())
121+
fmt.Printf("Docker Compose YAML content:\n%s\n", dockerComposeYAML)
122+
123+
// Run compose up with env-file
124+
upCmd := base.ComposeCmd("-f", comp.YAMLFullPath(), "--env-file", envFilePath, "up")
125+
upCmd.AssertOK()
126+
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down").AssertOK()
127+
128+
// Check if the environment variables were correctly passed and used
129+
containerID := base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "-q").OutLines()[0]
130+
fmt.Printf("Container ID: %s\n", containerID)
131+
132+
execCmd := base.Cmd("exec", containerID, "cat", "/tmp/test_output")
133+
out := execCmd.Out()
134+
135+
fmt.Printf("Command output: %s\n", out)
136+
137+
assert.Equal(t, "Hello World\n", out)
138+
fmt.Println("Test completed successfully")
139+
}

docs/command-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,7 @@ Flags:
13911391
- :whale: `-p, --project-name`: Specify an alternate project name
13921392
- :nerd_face: `--ipfs-address`: Multiaddr of IPFS API (default uses `$IPFS_PATH` env variable if defined or local directory `~/.ipfs`)
13931393
- :whale: `--profile: Specify a profile to enable
1394+
- :whale: `--env-file` : Specify an alternate environment file
13941395

13951396
### :whale: nerdctl compose up
13961397

0 commit comments

Comments
 (0)