compose_environment_test.go 9.2 KB

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