Skip to content

Commit a1b3f95

Browse files
committed
add e2e tests to verify env variables priority
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 5b6b674 commit a1b3f95

File tree

15 files changed

+243
-7
lines changed

15 files changed

+243
-7
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.2
77
github.com/buger/goterm v1.0.4
88
github.com/cnabio/cnab-to-oci v0.3.1-beta1
9-
github.com/compose-spec/compose-go v1.2.6
9+
github.com/compose-spec/compose-go v1.2.7
1010
github.com/containerd/console v1.0.3
1111
github.com/containerd/containerd v1.6.2
1212
github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e
@@ -69,7 +69,7 @@ require (
6969
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
7070
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
7171
github.com/hashicorp/errwrap v1.1.0 // indirect
72-
github.com/imdario/mergo v0.3.12 // indirect
72+
github.com/imdario/mergo v0.3.13 // indirect
7373
github.com/inconshreveable/mousetrap v1.0.0 // indirect
7474
github.com/json-iterator/go v1.1.12 // indirect
7575
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
@@ -123,7 +123,7 @@ require (
123123
google.golang.org/protobuf v1.27.1 // indirect
124124
gopkg.in/inf.v0 v0.9.1 // indirect
125125
gopkg.in/yaml.v2 v2.4.0 // indirect
126-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
126+
gopkg.in/yaml.v3 v3.0.0 // indirect
127127
k8s.io/apimachinery v0.23.4 // indirect; see replace for the actual version used
128128
k8s.io/client-go v0.23.4 // indirect; see replace for the actual version used
129129
k8s.io/klog/v2 v2.30.0 // indirect

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
301301
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
302302
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
303303
github.com/compose-spec/compose-go v1.0.8/go.mod h1:REnCbBugoIdHB7S1sfkN/aJ7AJpNApGNjNiVjA9L8x4=
304-
github.com/compose-spec/compose-go v1.2.6 h1:9l2Y/yNn/OSngnkUBP8h8CchTmMcf1MW4BeUEaZXy8k=
305-
github.com/compose-spec/compose-go v1.2.6/go.mod h1:cg8yTeI+7rfQsEW9XHOLx0sNVjGKEXr6XwVB4fxmG3A=
304+
github.com/compose-spec/compose-go v1.2.7 h1:eqKGZhdOQEGKW/FmFDt4xyEPhCpbA5dr0PkcWD895aI=
305+
github.com/compose-spec/compose-go v1.2.7/go.mod h1:Jl9L8zJrt4aGY1XAz03DvHAu8V3/f00TK+uJL4BayDU=
306306
github.com/compose-spec/godotenv v1.1.1/go.mod h1:zF/3BOa18Z24tts5qnO/E9YURQanJTBUf7nlcCTNsyc=
307307
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
308308
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=
@@ -870,8 +870,9 @@ github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
870870
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
871871
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
872872
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
873-
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
874873
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
874+
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
875+
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
875876
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
876877
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
877878
github.com/intel/goresctrl v0.2.0/go.mod h1:+CZdzouYFn5EsxgqAQTEzMfwKwuc0fVdMrT9FCCAVRQ=
@@ -2139,8 +2140,9 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
21392140
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
21402141
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21412142
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2142-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
21432143
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2144+
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
2145+
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21442146
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
21452147
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
21462148
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=

pkg/compose/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts
116116
if len(opts.Environment) > 0 {
117117
env := types.NewMappingWithEquals(opts.Environment)
118118
projectEnv := env.Resolve(func(s string) (string, bool) {
119+
if _, ok := service.Environment[s]; ok {
120+
return "", false
121+
}
119122
v, ok := project.Environment[s]
120123
return v, ok
121124
}).RemoveEmpty()

pkg/e2e/compose_environment_test.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package e2e
18+
19+
import (
20+
"os"
21+
"strings"
22+
"testing"
23+
24+
"gotest.tools/v3/assert"
25+
"gotest.tools/v3/icmd"
26+
)
27+
28+
func TestEnvPriority(t *testing.T) {
29+
c := NewParallelE2eCLI(t, binDir)
30+
31+
projectDir := "./fixtures/environment/env-priority"
32+
33+
t.Run("up", func(t *testing.T) {
34+
c.RunDockerOrExitError("rmi", "env-compose-priority")
35+
c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
36+
"--project-directory", projectDir, "up", "-d", "--build")
37+
})
38+
39+
// Full options activated
40+
// 1. Compose file <-- Result expected
41+
// 2. Shell environment variables
42+
// 3. Environment file
43+
// 4. Dockerfile
44+
// 5. Variable is not defined
45+
t.Run("compose file priority", func(t *testing.T) {
46+
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
47+
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
48+
49+
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
50+
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
51+
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
52+
53+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Compose File")
54+
})
55+
56+
// No Compose file, all other options
57+
// 1. Compose file
58+
// 2. Shell environment variables <-- Result expected
59+
// 3. Environment file
60+
// 4. Dockerfile
61+
// 5. Variable is not defined
62+
t.Run("shell priority", func(t *testing.T) {
63+
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
64+
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
65+
66+
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
67+
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
68+
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
69+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
70+
})
71+
72+
// No Compose file and env variable pass to the run command
73+
// 1. Compose file
74+
// 2. Shell environment variables <-- Result expected
75+
// 3. Environment file
76+
// 4. Dockerfile
77+
// 5. Variable is not defined
78+
t.Run("shell priority from run command", func(t *testing.T) {
79+
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
80+
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
81+
"run", "--rm", "-e", "WHEREAMI=shell-run", "env-compose-priority")
82+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell-run")
83+
})
84+
85+
// No Compose file & no env variable but override env file
86+
// 1. Compose file
87+
// 2. Shell environment variables
88+
// 3. Environment file <-- Result expected
89+
// 4. Dockerfile
90+
// 5. Variable is not defined
91+
t.Run("override env file", func(t *testing.T) {
92+
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
93+
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
94+
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
95+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
96+
})
97+
98+
// No Compose file & no env variable but override env file
99+
// 1. Compose file
100+
// 2. Shell environment variables
101+
// 3. Environment file <-- Result expected
102+
// 4. Dockerfile
103+
// 5. Variable is not defined
104+
t.Run("env file", func(t *testing.T) {
105+
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
106+
"--project-directory", projectDir, "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
107+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Env File")
108+
})
109+
110+
// No Compose file & no env variable, using an empty override env file
111+
// 1. Compose file
112+
// 2. Shell environment variables
113+
// 3. Environment file
114+
// 4. Dockerfile <-- Result expected
115+
// 5. Variable is not defined
116+
t.Run("use Dockerfile", func(t *testing.T) {
117+
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
118+
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.empty",
119+
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
120+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Dockerfile")
121+
})
122+
123+
t.Run("down", func(t *testing.T) {
124+
c.RunDockerComposeCmd("--project-directory", projectDir, "down")
125+
})
126+
}
127+
128+
func TestEnvInterpolation(t *testing.T) {
129+
c := NewParallelE2eCLI(t, binDir)
130+
131+
projectDir := "./fixtures/environment/env-interpolation"
132+
133+
// No variable defined in the Compose file and env variable pass to the run command
134+
// 1. Compose file
135+
// 2. Shell environment variables <-- Result expected
136+
// 3. Environment file
137+
// 4. Dockerfile
138+
// 5. Variable is not defined
139+
t.Run("shell priority from run command", func(t *testing.T) {
140+
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
141+
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
142+
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-interpolation/compose.yaml",
143+
"--project-directory", projectDir, "config")
144+
145+
res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`})
146+
})
147+
}
148+
149+
func TestCommentsInEnvFile(t *testing.T) {
150+
c := NewParallelE2eCLI(t, binDir)
151+
152+
projectDir := "./fixtures/environment/env-file-comments"
153+
154+
t.Run("comments in env files", func(t *testing.T) {
155+
c.RunDockerOrExitError("rmi", "env-file-comments")
156+
157+
c.RunDockerComposeCmd("-f", "./fixtures/environment/env-file-comments/compose.yaml",
158+
"--project-directory", projectDir, "up", "-d", "--build")
159+
160+
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-file-comments/compose.yaml",
161+
"--project-directory", projectDir, "run", "--rm",
162+
"-e", "COMMENT", "-e", "NO_COMMENT", "env-file-comments")
163+
164+
res.Assert(t, icmd.Expected{Out: `COMMENT=1234`})
165+
res.Assert(t, icmd.Expected{Out: `NO_COMMENT=1234#5`})
166+
167+
c.RunDockerComposeCmd("--project-directory", projectDir, "down", "--rmi", "all")
168+
})
169+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
COMMENT=1234#5
2+
NO_COMMENT="1234#5"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2020 Docker Compose CLI authors
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM alpine
16+
ENV COMMENT=Dockerfile
17+
ENV NO_COMMENT=Dockerfile
18+
CMD ["sh", "-c", "printenv", "|", "grep", "COMMENT"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
env-file-comments:
3+
build:
4+
context: .
5+
image: env-file-comments
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
WHEREAMI=Env File
2+
IMAGE=default_env:${WHEREAMI}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
env-interpolation:
3+
image: bash
4+
environment:
5+
IMAGE: ${IMAGE}
6+
command: echo "$IMAGE"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WHEREAMI=Env File

0 commit comments

Comments
 (0)