-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add e2e tests to verify env variables priority
Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
- Loading branch information
Showing
8 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
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" | ||
) | ||
|
||
func TestEnvPriority(t *testing.T) { | ||
c := NewParallelE2eCLI(t, binDir) | ||
|
||
projectDir := "./fixtures/environment/env-file-override" | ||
|
||
t.Run("up", func(t *testing.T) { | ||
c.RunDockerOrExitError("rmi", "env-compose-priority") | ||
c.RunDockerComposeCmd("-f", "./fixtures/environment/env-file-override/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-file-override/compose-with-env.yaml", | ||
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-file-override/.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-file-override/compose.yaml", | ||
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-file-override/.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-file-override/compose.yaml", | ||
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-file-override/.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-file-override/compose.yaml", | ||
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-file-override/.env.override", | ||
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority") | ||
assert.Equal(t, strings.TrimSpace(res.Stdout()), "override") | ||
}) | ||
|
||
// 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-file-override/compose.yaml", | ||
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-file-override/.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") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
WHEREAMI=Env File |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
WHEREAMI=override |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
7 changes: 7 additions & 0 deletions
7
pkg/e2e/fixtures/environment/env-file-override/compose-with-env.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
services: | ||
env-compose-priority: | ||
image: env-compose-priority |