compose_environment_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. projectDir := "./fixtures/environment/env-priority"
  23. t.Run("up", func(t *testing.T) {
  24. c.RunDockerOrExitError(t, "rmi", "env-compose-priority")
  25. c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
  26. "--project-directory", projectDir, "up", "-d", "--build")
  27. })
  28. // Full options activated
  29. // 1. Compose file <-- Result expected
  30. // 2. Shell environment variables
  31. // 3. Environment file
  32. // 4. Dockerfile
  33. // 5. Variable is not defined
  34. t.Run("compose file priority", func(t *testing.T) {
  35. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
  36. "--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", "run",
  37. "--rm", "-e", "WHEREAMI", "env-compose-priority")
  38. cmd.Env = append(cmd.Env, "WHEREAMI=shell")
  39. res := icmd.RunCmd(cmd)
  40. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Compose File")
  41. })
  42. // No Compose file, all other options
  43. // 1. Compose file
  44. // 2. Shell environment variables <-- Result expected
  45. // 3. Environment file
  46. // 4. Dockerfile
  47. // 5. Variable is not defined
  48. t.Run("shell priority", func(t *testing.T) {
  49. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml", "--project-directory",
  50. projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", "run", "--rm", "-e",
  51. "WHEREAMI", "env-compose-priority")
  52. cmd.Env = append(cmd.Env, "WHEREAMI=shell")
  53. res := icmd.RunCmd(cmd)
  54. assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
  55. })
  56. // No Compose file and env variable pass to the run command
  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 from run command", func(t *testing.T) {
  63. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml", "--project-directory",
  64. projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", "run", "--rm", "-e",
  65. "WHEREAMI=shell-run", "env-compose-priority")
  66. assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell-run")
  67. })
  68. // No Compose file & no env variable but override env file
  69. // 1. Compose file
  70. // 2. Shell environment variables
  71. // 3. Environment file <-- Result expected
  72. // 4. Dockerfile
  73. // 5. Variable is not defined
  74. t.Run("override env file", func(t *testing.T) {
  75. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml", "--project-directory",
  76. projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", "run", "--rm", "-e",
  77. "WHEREAMI", "env-compose-priority")
  78. assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
  79. })
  80. // No Compose file & no env variable but override env file
  81. // 1. Compose file
  82. // 2. Shell environment variables
  83. // 3. Environment file <-- Result expected
  84. // 4. Dockerfile
  85. // 5. Variable is not defined
  86. t.Run("env file", func(t *testing.T) {
  87. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml", "--project-directory",
  88. projectDir, "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
  89. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Env File")
  90. })
  91. // No Compose file & no env variable, using an empty override env file
  92. // 1. Compose file
  93. // 2. Shell environment variables
  94. // 3. Environment file
  95. // 4. Dockerfile <-- Result expected
  96. // 5. Variable is not defined
  97. t.Run("use Dockerfile", func(t *testing.T) {
  98. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml", "--project-directory",
  99. projectDir, "--env-file", "./fixtures/environment/env-priority/.env.empty", "run", "--rm", "-e", "WHEREAMI",
  100. "env-compose-priority")
  101. assert.Equal(t, strings.TrimSpace(res.Stdout()), "Dockerfile")
  102. })
  103. t.Run("down", func(t *testing.T) {
  104. c.RunDockerComposeCmd(t, "--project-directory", projectDir, "down")
  105. })
  106. }
  107. func TestEnvInterpolation(t *testing.T) {
  108. c := NewParallelCLI(t)
  109. projectDir := "./fixtures/environment/env-interpolation"
  110. // No variable defined in the Compose file and env variable pass to the run command
  111. // 1. Compose file
  112. // 2. Shell environment variables <-- Result expected
  113. // 3. Environment file
  114. // 4. Dockerfile
  115. // 5. Variable is not defined
  116. t.Run("shell priority from run command", func(t *testing.T) {
  117. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation/compose.yaml",
  118. "--project-directory", projectDir, "config")
  119. cmd.Env = append(cmd.Env, "WHEREAMI=shell")
  120. res := icmd.RunCmd(cmd)
  121. res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`})
  122. })
  123. }
  124. func TestCommentsInEnvFile(t *testing.T) {
  125. c := NewParallelCLI(t)
  126. projectDir := "./fixtures/environment/env-file-comments"
  127. t.Run("comments in env files", func(t *testing.T) {
  128. c.RunDockerOrExitError(t, "rmi", "env-file-comments")
  129. c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-file-comments/compose.yaml", "--project-directory",
  130. projectDir, "up", "-d", "--build")
  131. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-file-comments/compose.yaml",
  132. "--project-directory", projectDir, "run", "--rm", "-e", "COMMENT", "-e", "NO_COMMENT", "env-file-comments")
  133. res.Assert(t, icmd.Expected{Out: `COMMENT=1234`})
  134. res.Assert(t, icmd.Expected{Out: `NO_COMMENT=1234#5`})
  135. c.RunDockerComposeCmd(t, "--project-directory", projectDir, "down", "--rmi", "all")
  136. })
  137. }