Skip to content

Commit cc22628

Browse files
committed
add e2e tests to check interpolation in environment files
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 8f4f29f commit cc22628

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
FOO=FOO-from-dot-env
1+
FOO=FOO-from-dot-env
2+
IMPLICIT_ENV_FILE_TO_ENV_FILE=IMPLICIT_ENV_FILE_VALUE
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
ZOT=${FOO:-ZOT}
22
QIX=some ${FOO} value
33
BAR_FROM_ENV_FILE=${BAR}
4-
BY_PROVIDER_FROM_ENV_FILE: ${EXAMPLE_URL}
4+
BY_PROVIDER_FROM_ENV_FILE: ${EXAMPLE_URL}
5+
BY_OS_FROM_ENV_FILE: ${OS_TO_ENV_FILE}
6+
BY_CMD_FROM_ENV_FILE: ${CMD_TO_ENV_FILE}
7+
BY_IMPLICIT_ENV_FILE: ${IMPLICIT_ENV_FILE_TO_ENV_FILE}
8+
BY_EXPLICIT_ENV_FILE: ${EXPLICIT_ENV_FILE_TO_ENV_FILE}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EXPLICIT_ENV_FILE_TO_ENV_FILE=EXPLICIT_ENV_FILE_VALUE

pkg/e2e/interpolation_test.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"testing"
2727

2828
"gotest.tools/v3/assert"
29+
"gotest.tools/v3/icmd"
2930
)
3031

3132
func Test_interpolation(t *testing.T) {
@@ -40,7 +41,7 @@ func Test_interpolation(t *testing.T) {
4041
})
4142

4243
res := c.RunDockerComposeCmd(t, "-f", "fixtures/interpolation/compose.yaml", "--project-name", projectName, "up")
43-
env := getEnv(res.Combined())
44+
env := getEnv(res.Combined(), false)
4445

4546
assert.Check(t, slices.Contains(env, "FOO=FOO-from-dot-env"))
4647
assert.Check(t, slices.Contains(env, "BAR=bar_from_environment"))
@@ -64,7 +65,7 @@ func Test_interpolationWithInclude(t *testing.T) {
6465
})
6566

6667
res := c.RunDockerComposeCmd(t, "-f", "fixtures/interpolation/include/compose.yaml", "--project-name", projectName, "up")
67-
env := getEnv(res.Combined())
68+
env := getEnv(res.Combined(), false)
6869

6970
assert.Check(t, slices.Contains(env, "FOO=FOO-from-include"))
7071
assert.Check(t, slices.Contains(env, "BAR=bar_from_environment"))
@@ -87,7 +88,7 @@ func Test_interpolationWithExtends(t *testing.T) {
8788
})
8889

8990
res := c.RunDockerComposeCmd(t, "-f", "fixtures/interpolation/extends/compose.yaml", "--project-name", projectName, "up")
90-
env := getEnv(res.Combined())
91+
env := getEnv(res.Combined(), false)
9192

9293
assert.Check(t, slices.Contains(env, "FOO=FOO-from-extends"))
9394
assert.Check(t, slices.Contains(env, "BAR=BAR-from-extends"))
@@ -98,14 +99,48 @@ func Test_interpolationWithExtends(t *testing.T) {
9899
assert.Check(t, slices.Contains(env, "BY_PROVIDER_FROM_ENV_FILE=https://magic.cloud/example"))
99100
}
100101

101-
func getEnv(out string) []string {
102+
func TestInterpolationInEnvFile(t *testing.T) {
103+
provider, err := findExecutable("example-provider")
104+
assert.NilError(t, err)
105+
path := fmt.Sprintf("%s%s%s", os.Getenv("PATH"), string(os.PathListSeparator), filepath.Dir(provider))
106+
c := NewParallelCLI(t, WithEnv("PATH="+path))
107+
108+
const projectName = "interpolation-env-file"
109+
t.Log("interpolation in env file from os env and implicit env file")
110+
cmd := c.NewDockerComposeCmd(t, "-f", "fixtures/interpolation/compose.yaml", "--project-name", projectName, "up")
111+
cmd.Env = append(cmd.Env, "OS_TO_ENV_FILE=OS_TO_ENV_FILE_VALUE")
112+
t.Cleanup(func() {
113+
c.cleanupWithDown(t, projectName)
114+
})
115+
116+
res := icmd.RunCmd(cmd)
117+
assert.NilError(t, res.Error, res.Combined())
118+
env := getEnv(res.Combined(), false)
119+
assert.Check(t, slices.Contains(env, "BY_OS_FROM_ENV_FILE=OS_TO_ENV_FILE_VALUE"), env)
120+
assert.Check(t, slices.Contains(env, "BY_IMPLICIT_ENV_FILE=IMPLICIT_ENV_FILE_VALUE"), env)
121+
122+
t.Log("interpolation in env file from command env and explicit env file")
123+
cmd = c.NewDockerComposeCmd(t, "-f", "fixtures/interpolation/compose.yaml", "--project-name", projectName,
124+
"--env-file", "fixtures/interpolation/explicit_env_file.env", "run",
125+
"--env", "BY_CMD_FROM_ENV_FILE=CMD_TO_ENV_FILE_VALUE", "--rm", "test")
126+
127+
res = icmd.RunCmd(cmd)
128+
env = getEnv(res.Combined(), true)
129+
assert.Check(t, slices.Contains(env, "BY_EXPLICIT_ENV_FILE=EXPLICIT_ENV_FILE_VALUE"), env)
130+
assert.Check(t, slices.Contains(env, "BY_CMD_FROM_ENV_FILE=CMD_TO_ENV_FILE_VALUE"), env)
131+
}
132+
133+
func getEnv(out string, run bool) []string {
102134
var env []string
103135
scanner := bufio.NewScanner(strings.NewReader(out))
104136
for scanner.Scan() {
105137
line := scanner.Text()
106-
if strings.HasPrefix(line, "test-1 | ") {
138+
if !run && strings.HasPrefix(line, "test-1 | ") {
107139
env = append(env, line[10:])
108140
}
141+
if run && strings.Contains(line, "=") && len(strings.Split(line, "=")) == 2 {
142+
env = append(env, line)
143+
}
109144
}
110145
slices.Sort(env)
111146
return env

0 commit comments

Comments
 (0)