compose_environment_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. stdout := res.Stdout()
  107. assert.Equal(t, strings.TrimSpace(stdout), "EnvFileDefaultValue")
  108. })
  109. // No Compose file and env variable pass to the run command
  110. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected
  111. // 2. Compose File (service::environment section)
  112. // 3. Compose File (service::env_file section file)
  113. // 4. Container Image ENV directive
  114. // 5. Variable is not defined
  115. t.Run("shell priority from run command", func(t *testing.T) {
  116. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  117. "--env-file", "./fixtures/environment/env-priority/.env.override",
  118. "run", "--rm", "-e", "WHEREAMI=shell-run", "env-compose-priority")
  119. assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell-run")
  120. })
  121. // No Compose file & no env variable but override env file
  122. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by .env as a default --env-file value)
  123. // 2. Compose File (service::environment section)
  124. // 3. Compose File (service::env_file section file)
  125. // 4. Container Image ENV directive
  126. // 5. Variable is not defined
  127. t.Run("override env file from compose", func(t *testing.T) {
  128. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env-file.yaml",
  129. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  130. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Env File")
  131. })
  132. // No Compose file & no env variable but override by default env file
  133. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env-file value)
  134. // 2. Compose File (service::environment section)
  135. // 3. Compose File (service::env_file section file)
  136. // 4. Container Image ENV directive
  137. // 5. Variable is not defined
  138. t.Run("override env file", func(t *testing.T) {
  139. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  140. "--env-file", "./fixtures/environment/env-priority/.env.override",
  141. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  142. assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
  143. })
  144. // No Compose file & no env variable but override env file
  145. // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env-file value)
  146. // 2. Compose File (service::environment section)
  147. // 3. Compose File (service::env_file section file)
  148. // 4. Container Image ENV directive
  149. // 5. Variable is not defined
  150. t.Run("env file", func(t *testing.T) {
  151. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  152. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  153. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Env File")
  154. })
  155. // No Compose file & no env variable, using an empty override env file
  156. // 1. Command Line (docker compose run --env <KEY[=VAL]>)
  157. // 2. Compose File (service::environment section)
  158. // 3. Compose File (service::env_file section file)
  159. // 4. Container Image ENV directive <-- Result expected
  160. // 5. Variable is not defined
  161. t.Run("use Dockerfile", func(t *testing.T) {
  162. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
  163. "--env-file", "./fixtures/environment/env-priority/.env.empty",
  164. "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  165. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Dockerfile")
  166. })
  167. t.Run("down", func(t *testing.T) {
  168. c.RunDockerComposeCmd(t, "--project-name", "env-priority", "down")
  169. })
  170. }
  171. func TestEnvInterpolation(t *testing.T) {
  172. c := NewParallelCLI(t)
  173. t.Run("shell priority from run command", func(t *testing.T) {
  174. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation/compose.yaml", "config")
  175. cmd.Env = append(cmd.Env, "WHEREAMI=shell")
  176. res := icmd.RunCmd(cmd)
  177. res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`})
  178. })
  179. t.Run("shell priority from run command using default value fallback", func(t *testing.T) {
  180. c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation-default-value/compose.yaml", "config").
  181. Assert(t, icmd.Expected{Out: `IMAGE: default_env:EnvFileDefaultValue`})
  182. })
  183. }
  184. func TestCommentsInEnvFile(t *testing.T) {
  185. c := NewParallelCLI(t)
  186. t.Run("comments in env files", func(t *testing.T) {
  187. c.RunDockerOrExitError(t, "rmi", "env-file-comments")
  188. c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-file-comments/compose.yaml", "up", "-d", "--build")
  189. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-file-comments/compose.yaml",
  190. "run", "--rm", "-e", "COMMENT", "-e", "NO_COMMENT", "env-file-comments")
  191. res.Assert(t, icmd.Expected{Out: `COMMENT=1234`})
  192. res.Assert(t, icmd.Expected{Out: `NO_COMMENT=1234#5`})
  193. c.RunDockerComposeCmd(t, "--project-name", "env-file-comments", "down", "--rmi", "all")
  194. })
  195. }