profiles_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. const (
  21. profiledService = "profiled-service"
  22. regularService = "regular-service"
  23. )
  24. func TestExplicitProfileUsage(t *testing.T) {
  25. c := NewParallelCLI(t)
  26. const projectName = "compose-e2e-explicit-profiles"
  27. const profileName = "test-profile"
  28. t.Run("compose up with profile", func(t *testing.T) {
  29. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  30. "-p", projectName, "--profile", profileName, "up", "-d")
  31. res.Assert(t, icmd.Expected{ExitCode: 0})
  32. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  33. res.Assert(t, icmd.Expected{Out: regularService})
  34. res.Assert(t, icmd.Expected{Out: profiledService})
  35. })
  36. t.Run("compose stop with profile", func(t *testing.T) {
  37. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  38. "-p", projectName, "--profile", profileName, "stop")
  39. res.Assert(t, icmd.Expected{ExitCode: 0})
  40. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  41. assert.Assert(t, !strings.Contains(res.Combined(), regularService))
  42. assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
  43. })
  44. t.Run("compose start with profile", func(t *testing.T) {
  45. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  46. "-p", projectName, "--profile", profileName, "start")
  47. res.Assert(t, icmd.Expected{ExitCode: 0})
  48. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  49. res.Assert(t, icmd.Expected{Out: regularService})
  50. res.Assert(t, icmd.Expected{Out: profiledService})
  51. })
  52. t.Run("compose restart with profile", func(t *testing.T) {
  53. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  54. "-p", projectName, "--profile", profileName, "restart")
  55. res.Assert(t, icmd.Expected{ExitCode: 0})
  56. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  57. res.Assert(t, icmd.Expected{Out: regularService})
  58. res.Assert(t, icmd.Expected{Out: profiledService})
  59. })
  60. t.Run("down", func(t *testing.T) {
  61. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  62. })
  63. t.Run("check containers after down", func(t *testing.T) {
  64. res := c.RunDockerCmd(t, "ps")
  65. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  66. })
  67. }
  68. func TestNoProfileUsage(t *testing.T) {
  69. c := NewParallelCLI(t)
  70. const projectName = "compose-e2e-no-profiles"
  71. t.Run("compose up without profile", func(t *testing.T) {
  72. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  73. "-p", projectName, "up", "-d")
  74. res.Assert(t, icmd.Expected{ExitCode: 0})
  75. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  76. res.Assert(t, icmd.Expected{Out: regularService})
  77. assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
  78. })
  79. t.Run("compose stop without profile", func(t *testing.T) {
  80. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  81. "-p", projectName, "stop")
  82. res.Assert(t, icmd.Expected{ExitCode: 0})
  83. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  84. assert.Assert(t, !strings.Contains(res.Combined(), regularService))
  85. assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
  86. })
  87. t.Run("compose start without profile", func(t *testing.T) {
  88. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  89. "-p", projectName, "start")
  90. res.Assert(t, icmd.Expected{ExitCode: 0})
  91. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  92. res.Assert(t, icmd.Expected{Out: regularService})
  93. assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
  94. })
  95. t.Run("compose restart without profile", func(t *testing.T) {
  96. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  97. "-p", projectName, "restart")
  98. res.Assert(t, icmd.Expected{ExitCode: 0})
  99. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  100. res.Assert(t, icmd.Expected{Out: regularService})
  101. assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
  102. })
  103. t.Run("down", func(t *testing.T) {
  104. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  105. })
  106. t.Run("check containers after down", func(t *testing.T) {
  107. res := c.RunDockerCmd(t, "ps")
  108. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  109. })
  110. }
  111. func TestActiveProfileViaTargetedService(t *testing.T) {
  112. c := NewParallelCLI(t)
  113. const projectName = "compose-e2e-via-target-service-profiles"
  114. const profileName = "test-profile"
  115. t.Run("compose up with service name", func(t *testing.T) {
  116. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  117. "-p", projectName, "up", profiledService, "-d")
  118. res.Assert(t, icmd.Expected{ExitCode: 0})
  119. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  120. assert.Assert(t, !strings.Contains(res.Combined(), regularService))
  121. res.Assert(t, icmd.Expected{Out: profiledService})
  122. res = c.RunDockerComposeCmd(t, "-p", projectName, "--profile", profileName, "ps")
  123. assert.Assert(t, !strings.Contains(res.Combined(), regularService))
  124. res.Assert(t, icmd.Expected{Out: profiledService})
  125. })
  126. t.Run("compose stop with service name", func(t *testing.T) {
  127. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  128. "-p", projectName, "stop", profiledService)
  129. res.Assert(t, icmd.Expected{ExitCode: 0})
  130. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  131. assert.Assert(t, !strings.Contains(res.Combined(), regularService))
  132. assert.Assert(t, !strings.Contains(res.Combined(), profiledService))
  133. })
  134. t.Run("compose start with service name", func(t *testing.T) {
  135. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  136. "-p", projectName, "start", profiledService)
  137. res.Assert(t, icmd.Expected{ExitCode: 0})
  138. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  139. assert.Assert(t, !strings.Contains(res.Combined(), regularService))
  140. res.Assert(t, icmd.Expected{Out: profiledService})
  141. })
  142. t.Run("compose restart with service name", func(t *testing.T) {
  143. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  144. "-p", projectName, "restart")
  145. res.Assert(t, icmd.Expected{ExitCode: 0})
  146. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--status", "running")
  147. assert.Assert(t, !strings.Contains(res.Combined(), regularService))
  148. res.Assert(t, icmd.Expected{Out: profiledService})
  149. })
  150. t.Run("down", func(t *testing.T) {
  151. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  152. })
  153. t.Run("check containers after down", func(t *testing.T) {
  154. res := c.RunDockerCmd(t, "ps")
  155. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  156. })
  157. }
  158. func TestDotEnvProfileUsage(t *testing.T) {
  159. c := NewParallelCLI(t)
  160. const projectName = "compose-e2e-dotenv-profiles"
  161. const profileName = "test-profile"
  162. t.Cleanup(func() {
  163. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  164. })
  165. t.Run("compose up with profile", func(t *testing.T) {
  166. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/profiles/compose.yaml",
  167. "--env-file", "./fixtures/profiles/test-profile.env",
  168. "-p", projectName, "--profile", profileName, "up", "-d")
  169. res.Assert(t, icmd.Expected{ExitCode: 0})
  170. res = c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  171. res.Assert(t, icmd.Expected{Out: regularService})
  172. res.Assert(t, icmd.Expected{Out: profiledService})
  173. })
  174. }