compose_environment_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2e
  14. import (
  15. "strings"
  16. "testing"
  17. "gotest.tools/v3/assert"
  18. "gotest.tools/v3/icmd"
  19. )
  20. func TestEnvPriority(t *testing.T) {
  21. c := NewParallelCLI(t)
  22. t.Run("up", func(t *testing.T) {
  23. c.RunDockerOrExitError(t, "rmi", "env-compose-priority")
  24. c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
  25. "up", "-d", "--build")
  26. })
  27. // Full options activated
  28. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From OS Environment)
  29. // 2. Compose File (service::environment section)
  30. // 3. Compose File (service::env_file section file)
  31. // 4. Container Image ENV directive
  32. // 5. Variable is not defined
  33. t.Run("compose file priority", func(t *testing.T) {
  34. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
  35. "--env-file", "./fixtures/environment/env-priority/.env.override",
  36. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  37. cmd.Env = append(cmd.Env, "WHEREAMI=shell")
  38. res := icmd.RunCmd(cmd)
  39. assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
  40. })
  41. // Full options activated
  42. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected
  43. // 2. Compose File (service::environment section)
  44. // 3. Compose File (service::env_file section file)
  45. // 4. Container Image ENV directive
  46. // 5. Variable is not defined
  47. t.Run("compose file priority", func(t *testing.T) {
  48. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
  49. "--env-file", "./fixtures/environment/env-priority/.env.override",
  50. "run", "--rm", "-e", "WHEREAMI=shell", "env-compose-priority")
  51. res := icmd.RunCmd(cmd)
  52. assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
  53. })
  54. // No Compose file, all other options
  55. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From OS Environment)
  56. // 2. Compose File (service::environment section)
  57. // 3. Compose File (service::env_file section file)
  58. // 4. Container Image ENV directive
  59. // 5. Variable is not defined
  60. t.Run("shell priority", func(t *testing.T) {
  61. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  62. "--env-file", "./fixtures/environment/env-priority/.env.override",
  63. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  64. cmd.Env = append(cmd.Env, "WHEREAMI=shell")
  65. res := icmd.RunCmd(cmd)
  66. assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
  67. })
  68. // No Compose file, all other options with env variable from OS environment
  69. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment)
  70. // 2. Compose File (service::environment section)
  71. // 3. Compose File (service::env_file section file)
  72. // 4. Container Image ENV directive
  73. // 5. Variable is not defined
  74. t.Run("shell priority file with default value", func(t *testing.T) {
  75. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  76. "--env-file", "./fixtures/environment/env-priority/.env.override.with.default",
  77. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  78. cmd.Env = append(cmd.Env, "WHEREAMI=shell")
  79. res := icmd.RunCmd(cmd)
  80. assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
  81. })
  82. // No Compose file, all other options with env variable from OS environment
  83. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment default value from file in --env-file)
  84. // 2. Compose File (service::environment section)
  85. // 3. Compose File (service::env_file section file)
  86. // 4. Container Image ENV directive
  87. // 5. Variable is not defined
  88. t.Run("shell priority implicitly set", func(t *testing.T) {
  89. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  90. "--env-file", "./fixtures/environment/env-priority/.env.override.with.default",
  91. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  92. res := icmd.RunCmd(cmd)
  93. assert.Equal(t, strings.TrimSpace(res.Stdout()), "EnvFileDefaultValue")
  94. })
  95. // No Compose file, all other options with env variable from OS environment
  96. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment default value from file in COMPOSE_ENV_FILES)
  97. // 2. Compose File (service::environment section)
  98. // 3. Compose File (service::env_file section file)
  99. // 4. Container Image ENV directive
  100. // 5. Variable is not defined
  101. t.Run("shell priority from COMPOSE_ENV_FILES variable", func(t *testing.T) {
  102. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  103. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  104. cmd.Env = append(cmd.Env, "COMPOSE_ENV_FILES=./fixtures/environment/env-priority/.env.override.with.default")
  105. res := icmd.RunCmd(cmd)
  106. assert.Equal(t, strings.TrimSpace(res.Stdout()), "EnvFileDefaultValue")
  107. })
  108. // No Compose file and env variable pass to the run command
  109. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected
  110. // 2. Compose File (service::environment section)
  111. // 3. Compose File (service::env_file section file)
  112. // 4. Container Image ENV directive
  113. // 5. Variable is not defined
  114. t.Run("shell priority from run command", func(t *testing.T) {
  115. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  116. "--env-file", "./fixtures/environment/env-priority/.env.override",
  117. "run", "--rm", "-e", "WHEREAMI=shell-run", "env-compose-priority")
  118. assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell-run")
  119. })
  120. // No Compose file & no env variable but override env file
  121. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by .env as a default --env-file value)
  122. // 2. Compose File (service::environment section)
  123. // 3. Compose File (service::env_file section file)
  124. // 4. Container Image ENV directive
  125. // 5. Variable is not defined
  126. t.Run("override env file from compose", func(t *testing.T) {
  127. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env-file.yaml",
  128. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  129. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Env File")
  130. })
  131. // No Compose file & no env variable but override by default env file
  132. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env-file value)
  133. // 2. Compose File (service::environment section)
  134. // 3. Compose File (service::env_file section file)
  135. // 4. Container Image ENV directive
  136. // 5. Variable is not defined
  137. t.Run("override env file", func(t *testing.T) {
  138. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  139. "--env-file", "./fixtures/environment/env-priority/.env.override",
  140. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  141. assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
  142. })
  143. // No Compose file & no env variable but override env file
  144. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env-file value)
  145. // 2. Compose File (service::environment section)
  146. // 3. Compose File (service::env_file section file)
  147. // 4. Container Image ENV directive
  148. // 5. Variable is not defined
  149. t.Run("env file", func(t *testing.T) {
  150. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  151. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  152. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Env File")
  153. })
  154. // No Compose file & no env variable, using an empty override env file
  155. // 1. Command Line (docker compose run --env <KEY[=VAL]>)
  156. // 2. Compose File (service::environment section)
  157. // 3. Compose File (service::env_file section file)
  158. // 4. Container Image ENV directive <-- Result expected
  159. // 5. Variable is not defined
  160. t.Run("use Dockerfile", func(t *testing.T) {
  161. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  162. "--env-file", "./fixtures/environment/env-priority/.env.empty",
  163. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  164. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Dockerfile")
  165. })
  166. t.Run("down", func(t *testing.T) {
  167. c.RunDockerComposeCmd(t, "--project-name", "env-priority", "down")
  168. })
  169. }
  170. func TestEnvInterpolation(t *testing.T) {
  171. c := NewParallelCLI(t)
  172. t.Run("shell priority from run command", func(t *testing.T) {
  173. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation/compose.yaml", "config")
  174. cmd.Env = append(cmd.Env, "WHEREAMI=shell")
  175. res := icmd.RunCmd(cmd)
  176. res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`})
  177. })
  178. t.Run("shell priority from run command using default value fallback", func(t *testing.T) {
  179. c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation-default-value/compose.yaml", "config").
  180. Assert(t, icmd.Expected{Out: `IMAGE: default_env:EnvFileDefaultValue`})
  181. })
  182. }
  183. func TestCommentsInEnvFile(t *testing.T) {
  184. c := NewParallelCLI(t)
  185. t.Run("comments in env files", func(t *testing.T) {
  186. c.RunDockerOrExitError(t, "rmi", "env-file-comments")
  187. c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-file-comments/compose.yaml", "up", "-d", "--build")
  188. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-file-comments/compose.yaml",
  189. "run", "--rm", "-e", "COMMENT", "-e", "NO_COMMENT", "env-file-comments")
  190. res.Assert(t, icmd.Expected{Out: `COMMENT=1234`})
  191. res.Assert(t, icmd.Expected{Out: `NO_COMMENT=1234#5`})
  192. c.RunDockerComposeCmd(t, "--project-name", "env-file-comments", "down", "--rmi", "all")
  193. })
  194. }