compose_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. "fmt"
  16. "net/http"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "testing"
  21. "time"
  22. testify "github.com/stretchr/testify/assert"
  23. "gotest.tools/v3/assert"
  24. "gotest.tools/v3/icmd"
  25. )
  26. func TestLocalComposeUp(t *testing.T) {
  27. // this test shares a fixture with TestCompatibility and can't run at the same time
  28. c := NewCLI(t)
  29. const projectName = "compose-e2e-demo"
  30. t.Run("up", func(t *testing.T) {
  31. c.RunDockerComposeCmd(t, "-f", "./fixtures/sentences/compose.yaml", "--project-name", projectName, "up", "-d")
  32. })
  33. t.Run("check accessing running app", func(t *testing.T) {
  34. res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  35. res.Assert(t, icmd.Expected{Out: `web`})
  36. endpoint := "http://localhost:90"
  37. output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  38. assert.Assert(t, strings.Contains(output, `"word":`))
  39. res = c.RunDockerCmd(t, "network", "ls")
  40. res.Assert(t, icmd.Expected{Out: projectName + "_default"})
  41. })
  42. t.Run("top", func(t *testing.T) {
  43. res := c.RunDockerComposeCmd(t, "-p", projectName, "top")
  44. output := res.Stdout()
  45. head := []string{"UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"}
  46. for _, h := range head {
  47. assert.Assert(t, strings.Contains(output, h), output)
  48. }
  49. assert.Assert(t, strings.Contains(output, `java -Xmx8m -Xms8m -jar /app/words.jar`), output)
  50. assert.Assert(t, strings.Contains(output, `/dispatcher`), output)
  51. })
  52. t.Run("check compose labels", func(t *testing.T) {
  53. res := c.RunDockerCmd(t, "inspect", projectName+"-web-1")
  54. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
  55. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`})
  56. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`})
  57. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.config-hash":`})
  58. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.config_files":`})
  59. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.working_dir":`})
  60. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.service": "web"`})
  61. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version":`})
  62. res = c.RunDockerCmd(t, "network", "inspect", projectName+"_default")
  63. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.network": "default"`})
  64. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": `})
  65. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version": `})
  66. })
  67. t.Run("check user labels", func(t *testing.T) {
  68. res := c.RunDockerCmd(t, "inspect", projectName+"-web-1")
  69. res.Assert(t, icmd.Expected{Out: `"my-label": "test"`})
  70. })
  71. t.Run("check healthcheck output", func(t *testing.T) {
  72. c.WaitForCmdResult(t, c.NewDockerComposeCmd(t, "-p", projectName, "ps", "--format", "json"),
  73. IsHealthy(projectName+"-web-1"),
  74. 5*time.Second, 1*time.Second)
  75. res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  76. assertServiceStatus(t, projectName, "web", "(healthy)", res.Stdout())
  77. })
  78. t.Run("images", func(t *testing.T) {
  79. res := c.RunDockerComposeCmd(t, "-p", projectName, "images")
  80. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 gtardif/sentences-db latest`})
  81. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 gtardif/sentences-web latest`})
  82. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-words-1 gtardif/sentences-api latest`})
  83. })
  84. t.Run("down SERVICE", func(t *testing.T) {
  85. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "web")
  86. res := c.RunDockerComposeCmd(t, "--project-name", projectName, "ps")
  87. assert.Assert(t, !strings.Contains(res.Combined(), "compose-e2e-demo-web-1"), res.Combined())
  88. assert.Assert(t, strings.Contains(res.Combined(), "compose-e2e-demo-db-1"), res.Combined())
  89. })
  90. t.Run("down", func(t *testing.T) {
  91. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  92. })
  93. t.Run("check containers after down", func(t *testing.T) {
  94. res := c.RunDockerCmd(t, "ps", "--all")
  95. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  96. })
  97. t.Run("check networks after down", func(t *testing.T) {
  98. res := c.RunDockerCmd(t, "network", "ls")
  99. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  100. })
  101. }
  102. func TestDownComposefileInParentFolder(t *testing.T) {
  103. c := NewParallelCLI(t)
  104. tmpFolder, err := os.MkdirTemp("fixtures/simple-composefile", "test-tmp")
  105. assert.NilError(t, err)
  106. defer os.Remove(tmpFolder) //nolint:errcheck
  107. projectName := filepath.Base(tmpFolder)
  108. res := c.RunDockerComposeCmd(t, "--project-directory", tmpFolder, "up", "-d")
  109. res.Assert(t, icmd.Expected{Err: "Started", ExitCode: 0})
  110. res = c.RunDockerComposeCmd(t, "-p", projectName, "down")
  111. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  112. }
  113. func TestAttachRestart(t *testing.T) {
  114. t.Skip("Skipping test until we can fix it")
  115. if _, ok := os.LookupEnv("CI"); ok {
  116. t.Skip("Skipping test on CI... flaky")
  117. }
  118. c := NewParallelCLI(t)
  119. cmd := c.NewDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
  120. res := icmd.StartCmd(cmd)
  121. defer c.RunDockerComposeCmd(t, "-p", "attach-restart", "down")
  122. c.WaitForCondition(t, func() (bool, string) {
  123. debug := res.Combined()
  124. return strings.Count(res.Stdout(),
  125. "failing-1 exited with code 1") == 3, fmt.Sprintf("'failing-1 exited with code 1' not found 3 times in : \n%s\n",
  126. debug)
  127. }, 4*time.Minute, 2*time.Second)
  128. assert.Equal(t, strings.Count(res.Stdout(), "failing-1 | world"), 3, res.Combined())
  129. }
  130. func TestInitContainer(t *testing.T) {
  131. c := NewParallelCLI(t)
  132. res := c.RunDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/init-container", "up")
  133. defer c.RunDockerComposeCmd(t, "-p", "init-container", "down")
  134. testify.Regexp(t, "foo-1 | hello(?m:.*)bar-1 | world", res.Stdout())
  135. }
  136. func TestRm(t *testing.T) {
  137. c := NewParallelCLI(t)
  138. const projectName = "compose-e2e-rm"
  139. t.Run("up", func(t *testing.T) {
  140. c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "up", "-d")
  141. })
  142. t.Run("rm --stop --force simple", func(t *testing.T) {
  143. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
  144. "--stop", "--force", "simple")
  145. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  146. })
  147. t.Run("check containers after rm", func(t *testing.T) {
  148. res := c.RunDockerCmd(t, "ps", "--all")
  149. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"-simple"), res.Combined())
  150. assert.Assert(t, strings.Contains(res.Combined(), projectName+"-another"), res.Combined())
  151. })
  152. t.Run("up (again)", func(t *testing.T) {
  153. c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "up", "-d")
  154. })
  155. t.Run("rm ---stop --force <none>", func(t *testing.T) {
  156. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
  157. "--stop", "--force")
  158. res.Assert(t, icmd.Expected{ExitCode: 0})
  159. })
  160. t.Run("check containers after rm", func(t *testing.T) {
  161. res := c.RunDockerCmd(t, "ps", "--all")
  162. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"-simple"), res.Combined())
  163. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"-another"), res.Combined())
  164. })
  165. t.Run("down", func(t *testing.T) {
  166. c.RunDockerComposeCmd(t, "-p", projectName, "down")
  167. })
  168. }
  169. func TestCompatibility(t *testing.T) {
  170. // this test shares a fixture with TestLocalComposeUp and can't run at the same time
  171. c := NewCLI(t)
  172. const projectName = "compose-e2e-compatibility"
  173. t.Run("up", func(t *testing.T) {
  174. c.RunDockerComposeCmd(t, "--compatibility", "-f", "./fixtures/sentences/compose.yaml", "--project-name",
  175. projectName, "up", "-d")
  176. })
  177. t.Run("check container names", func(t *testing.T) {
  178. res := c.RunDockerCmd(t, "ps", "--format", "{{.Names}}")
  179. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_web_1"})
  180. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_words_1"})
  181. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_db_1"})
  182. })
  183. t.Run("down", func(t *testing.T) {
  184. c.RunDockerComposeCmd(t, "-p", projectName, "down")
  185. })
  186. }
  187. func TestConvert(t *testing.T) {
  188. const projectName = "compose-e2e-convert"
  189. c := NewParallelCLI(t)
  190. wd, err := os.Getwd()
  191. assert.NilError(t, err)
  192. t.Run("up", func(t *testing.T) {
  193. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose.yaml", "-p", projectName, "convert")
  194. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services:
  195. nginx:
  196. build:
  197. context: %s
  198. dockerfile: Dockerfile
  199. networks:
  200. default: null
  201. networks:
  202. default:
  203. name: compose-e2e-convert_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
  204. })
  205. }
  206. func TestConvertInterpolate(t *testing.T) {
  207. const projectName = "compose-e2e-convert-interpolate"
  208. c := NewParallelCLI(t)
  209. wd, err := os.Getwd()
  210. assert.NilError(t, err)
  211. t.Run("convert", func(t *testing.T) {
  212. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose-interpolate.yaml", "-p", projectName, "convert", "--no-interpolate")
  213. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services:
  214. nginx:
  215. build:
  216. context: %s
  217. dockerfile: ${MYVAR}
  218. networks:
  219. default: null
  220. networks:
  221. default:
  222. name: compose-e2e-convert-interpolate_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
  223. })
  224. }
  225. func TestStopWithDependenciesAttached(t *testing.T) {
  226. const projectName = "compose-e2e-stop-with-deps"
  227. c := NewParallelCLI(t, WithEnv("COMMAND=echo hello"))
  228. cleanup := func() {
  229. c.RunDockerComposeCmd(t, "-p", projectName, "down", "--remove-orphans", "--timeout=0")
  230. }
  231. cleanup()
  232. t.Cleanup(cleanup)
  233. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dependencies/compose.yaml", "-p", projectName, "up", "--attach-dependencies", "foo")
  234. res.Assert(t, icmd.Expected{Out: "exited with code 0"})
  235. }