Skip to content

Commit

Permalink
add e2e tests to verify env variables priority
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
  • Loading branch information
glours committed May 30, 2022
1 parent 5b6b674 commit e87c121
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 2 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ require (
)

replace (
// temporary use of a custom version to check e2e tests before merging Compose-go
github.com/compose-spec/compose-go v1.2.6 => github.com/glours/compose-go v0.0.0-20220527195524-da32e3a02ca5

github.com/docker/cli => github.com/docker/cli v20.10.3-0.20220309205733-2b52f62e9627+incompatible
github.com/docker/docker => github.com/docker/docker v20.10.3-0.20220309172631-83b51522df43+incompatible

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,6 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/compose-spec/compose-go v1.0.8/go.mod h1:REnCbBugoIdHB7S1sfkN/aJ7AJpNApGNjNiVjA9L8x4=
github.com/compose-spec/compose-go v1.2.6 h1:9l2Y/yNn/OSngnkUBP8h8CchTmMcf1MW4BeUEaZXy8k=
github.com/compose-spec/compose-go v1.2.6/go.mod h1:cg8yTeI+7rfQsEW9XHOLx0sNVjGKEXr6XwVB4fxmG3A=
github.com/compose-spec/godotenv v1.1.1/go.mod h1:zF/3BOa18Z24tts5qnO/E9YURQanJTBUf7nlcCTNsyc=
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=
Expand Down Expand Up @@ -555,6 +553,8 @@ github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2H
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/glours/compose-go v0.0.0-20220527195524-da32e3a02ca5 h1:XvDauzPCuP0LzlLoCh2PjCQo/pGGWUfd0DlApMG5530=
github.com/glours/compose-go v0.0.0-20220527195524-da32e3a02ca5/go.mod h1:cg8yTeI+7rfQsEW9XHOLx0sNVjGKEXr6XwVB4fxmG3A=
github.com/go-critic/go-critic v0.4.1/go.mod h1:7/14rZGnZbY6E38VEGk2kVhoq6itzc1E68facVDK23g=
github.com/go-critic/go-critic v0.4.3/go.mod h1:j4O3D4RoIwRqlZw5jJpx0BNfXWWbpcJoKu5cYSe4YmQ=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down
3 changes: 3 additions & 0 deletions pkg/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts
if len(opts.Environment) > 0 {
env := types.NewMappingWithEquals(opts.Environment)
projectEnv := env.Resolve(func(s string) (string, bool) {
if _, ok := service.Environment[s]; ok {
return "", false
}
v, ok := project.Environment[s]
return v, ok
}).RemoveEmpty()
Expand Down
171 changes: 171 additions & 0 deletions pkg/e2e/compose_environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"os"
"strings"
"testing"

"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)

func TestEnvPriority(t *testing.T) {
c := NewParallelE2eCLI(t, binDir)

projectDir := "./fixtures/environment/env-priority"

t.Run("up", func(t *testing.T) {
c.RunDockerOrExitError("rmi", "env-compose-priority")
c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
"--project-directory", projectDir, "up", "-d", "--build")
})

// Full options activated
// 1. Compose file <-- Result expected
// 2. Shell environment variables
// 3. Environment file
// 4. Dockerfile
// 5. Variable is not defined
t.Run("compose file priority", func(t *testing.T) {
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
defer os.Unsetenv("WHEREAMI") //nolint:errcheck

res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")

assert.Equal(t, strings.TrimSpace(res.Stdout()), "Compose File")
})

// No Compose file, all other options
// 1. Compose file
// 2. Shell environment variables <-- Result expected
// 3. Environment file
// 4. Dockerfile
// 5. Variable is not defined
t.Run("shell priority", func(t *testing.T) {
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
defer os.Unsetenv("WHEREAMI") //nolint:errcheck

res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
})

// No Compose file and env variable pass to the run command
// 1. Compose file
// 2. Shell environment variables <-- Result expected
// 3. Environment file
// 4. Dockerfile
// 5. Variable is not defined
t.Run("shell priority from run command", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
"run", "--rm", "-e", "WHEREAMI=shell-run", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell-run")
})

// No Compose file & no env variable but override env file
// 1. Compose file
// 2. Shell environment variables
// 3. Environment file <-- Result expected
// 4. Dockerfile
// 5. Variable is not defined
t.Run("override env file", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
})

// No Compose file & no env variable but override env file
// 1. Compose file
// 2. Shell environment variables
// 3. Environment file <-- Result expected
// 4. Dockerfile
// 5. Variable is not defined
t.Run("env file", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Env File")
})

// No Compose file & no env variable, using an empty override env file
// 1. Compose file
// 2. Shell environment variables
// 3. Environment file
// 4. Dockerfile <-- Result expected
// 5. Variable is not defined
t.Run("use Dockerfile", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.empty",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Dockerfile")
})

t.Run("down", func(t *testing.T) {
c.RunDockerComposeCmd("--project-directory", projectDir, "down")
})
}

func TestEnvInterpolation(t *testing.T) {
c := NewParallelE2eCLI(t, binDir)

projectDir := "./fixtures/environment/env-interpolation"

// No variable defined in the Compose file and env variable pass to the run command
// 1. Compose file
// 2. Shell environment variables <-- Result expected
// 3. Environment file
// 4. Dockerfile
// 5. Variable is not defined
t.Run("shell priority from run command", func(t *testing.T) {
os.Clearenv()
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-interpolation/compose.yaml",
"--project-directory", projectDir, "config")

res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`})
})
}

func TestCommentsInEnvFile(t *testing.T) {
c := NewParallelE2eCLI(t, binDir)

projectDir := "./fixtures/environment/env-file-comments"

t.Run("comments in env files", func(t *testing.T) {
c.RunDockerOrExitError("rmi", "env-file-comments")
os.Clearenv()

c.RunDockerComposeCmd("-f", "./fixtures/environment/env-file-comments/compose.yaml",
"--project-directory", projectDir, "up", "-d", "--build")

res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-file-comments/compose.yaml",
"--project-directory", projectDir, "run", "--rm",
"-e", "COMMENT", "-e", "NO_COMMENT", "env-file-comments")

res.Assert(t, icmd.Expected{Out: `COMMENT=1234`})
res.Assert(t, icmd.Expected{Out: `NO_COMMENT=1234#5`})

c.RunDockerComposeCmd("--project-directory", projectDir, "down", "--rmi", "all")
})
}
2 changes: 2 additions & 0 deletions pkg/e2e/fixtures/environment/env-file-comments/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMMENT=1234#5
NO_COMMENT="1234#5"
18 changes: 18 additions & 0 deletions pkg/e2e/fixtures/environment/env-file-comments/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2020 Docker Compose CLI authors

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM alpine
ENV COMMENT=Dockerfile
ENV NO_COMMENT=Dockerfile
CMD ["sh", "-c", "printenv", "|", "grep", "COMMENT"]
5 changes: 5 additions & 0 deletions pkg/e2e/fixtures/environment/env-file-comments/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
env-file-comments:
build:
context: .
image: env-file-comments
2 changes: 2 additions & 0 deletions pkg/e2e/fixtures/environment/env-interpolation/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WHEREAMI=Env File
IMAGE=default_env:${WHEREAMI}
6 changes: 6 additions & 0 deletions pkg/e2e/fixtures/environment/env-interpolation/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
env-interpolation:
image: bash
environment:
IMAGE: ${IMAGE}
command: echo "$IMAGE"
1 change: 1 addition & 0 deletions pkg/e2e/fixtures/environment/env-priority/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WHEREAMI=Env File
Empty file.
1 change: 1 addition & 0 deletions pkg/e2e/fixtures/environment/env-priority/.env.override
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WHEREAMI=override
17 changes: 17 additions & 0 deletions pkg/e2e/fixtures/environment/env-priority/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2020 Docker Compose CLI authors

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM alpine
ENV WHEREAMI=Dockerfile
CMD ["printenv", "WHEREAMI"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
env-compose-priority:
image: env-compose-priority
build:
context: .
environment:
WHEREAMI: "Compose File"
3 changes: 3 additions & 0 deletions pkg/e2e/fixtures/environment/env-priority/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
env-compose-priority:
image: env-compose-priority

0 comments on commit e87c121

Please sign in to comment.