|
| 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 | +} |
0 commit comments