compose_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. StdoutContains(`"Name":"compose-e2e-demo-web-1","Command":"/dispatcher","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`),
  74. 5*time.Second, 1*time.Second)
  75. res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  76. res.Assert(t, icmd.Expected{Out: `NAME COMMAND SERVICE STATUS PORTS`})
  77. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 "/dispatcher" web running (healthy) 0.0.0.0:90->80/tcp`})
  78. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 "docker-entrypoint.s…" db running 5432/tcp`})
  79. })
  80. t.Run("images", func(t *testing.T) {
  81. res := c.RunDockerComposeCmd(t, "-p", projectName, "images")
  82. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 gtardif/sentences-db latest`})
  83. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 gtardif/sentences-web latest`})
  84. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-words-1 gtardif/sentences-api latest`})
  85. })
  86. t.Run("down", func(t *testing.T) {
  87. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  88. })
  89. t.Run("check containers after down", func(t *testing.T) {
  90. res := c.RunDockerCmd(t, "ps", "--all")
  91. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  92. })
  93. t.Run("check networks after down", func(t *testing.T) {
  94. res := c.RunDockerCmd(t, "network", "ls")
  95. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  96. })
  97. }
  98. func TestComposePull(t *testing.T) {
  99. c := NewParallelCLI(t)
  100. t.Run("Verify image pulled", func(t *testing.T) {
  101. // cleanup existing images
  102. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/simple", "down", "--rmi", "all")
  103. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/simple", "pull")
  104. output := res.Combined()
  105. assert.Assert(t, strings.Contains(output, "simple Pulled"))
  106. assert.Assert(t, strings.Contains(output, "another Pulled"))
  107. // verify default policy is 'always' for pull command
  108. res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/simple", "pull")
  109. output = res.Combined()
  110. assert.Assert(t, strings.Contains(output, "simple Pulled"))
  111. assert.Assert(t, strings.Contains(output, "another Pulled"))
  112. })
  113. t.Run("Verify a image is pulled once", func(t *testing.T) {
  114. // cleanup existing images
  115. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/duplicate-images", "down", "--rmi", "all")
  116. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/duplicate-images", "pull")
  117. output := res.Combined()
  118. if strings.Contains(output, "another Pulled") {
  119. assert.Assert(t, strings.Contains(output, "another Pulled"))
  120. assert.Assert(t, strings.Contains(output, "Skipped - Image is already being pulled by another"))
  121. } else {
  122. assert.Assert(t, strings.Contains(output, "simple Pulled"))
  123. assert.Assert(t, strings.Contains(output, "Skipped - Image is already being pulled by simple"))
  124. }
  125. })
  126. t.Run("Verify skipped pull if image is already present locally", func(t *testing.T) {
  127. // make sure the required image is present
  128. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/image-present-locally", "pull")
  129. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/image-present-locally", "pull")
  130. output := res.Combined()
  131. assert.Assert(t, strings.Contains(output, "Skipped - Image is already present locally"))
  132. })
  133. t.Run("Verify skipped no image to be pulled", func(t *testing.T) {
  134. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/no-image-name-given", "pull")
  135. output := res.Combined()
  136. assert.Assert(t, strings.Contains(output, "Skipped - No image to be pulled"))
  137. })
  138. }
  139. func TestDownComposefileInParentFolder(t *testing.T) {
  140. c := NewParallelCLI(t)
  141. tmpFolder, err := os.MkdirTemp("fixtures/simple-composefile", "test-tmp")
  142. assert.NilError(t, err)
  143. defer os.Remove(tmpFolder) //nolint:errcheck
  144. projectName := filepath.Base(tmpFolder)
  145. res := c.RunDockerComposeCmd(t, "--project-directory", tmpFolder, "up", "-d")
  146. res.Assert(t, icmd.Expected{Err: "Started", ExitCode: 0})
  147. res = c.RunDockerComposeCmd(t, "-p", projectName, "down")
  148. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  149. }
  150. func TestAttachRestart(t *testing.T) {
  151. c := NewParallelCLI(t)
  152. cmd := c.NewDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
  153. res := icmd.StartCmd(cmd)
  154. defer c.RunDockerComposeCmd(t, "-p", "attach-restart", "down")
  155. c.WaitForCondition(t, func() (bool, string) {
  156. debug := res.Combined()
  157. return strings.Count(res.Stdout(),
  158. "failing-1 exited with code 1") == 3, fmt.Sprintf("'failing-1 exited with code 1' not found 3 times in : \n%s\n",
  159. debug)
  160. }, 2*time.Minute, 2*time.Second)
  161. assert.Equal(t, strings.Count(res.Stdout(), "failing-1 | world"), 3, res.Combined())
  162. }
  163. func TestInitContainer(t *testing.T) {
  164. c := NewParallelCLI(t)
  165. res := c.RunDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/init-container", "up")
  166. defer c.RunDockerComposeCmd(t, "-p", "init-container", "down")
  167. testify.Regexp(t, "foo-1 | hello(?m:.*)bar-1 | world", res.Stdout())
  168. }
  169. func TestRm(t *testing.T) {
  170. c := NewParallelCLI(t)
  171. const projectName = "compose-e2e-rm"
  172. t.Run("up", func(t *testing.T) {
  173. c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "up", "-d")
  174. })
  175. t.Run("rm -sf", func(t *testing.T) {
  176. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
  177. "-sf", "simple")
  178. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  179. })
  180. t.Run("check containers after rm -sf", func(t *testing.T) {
  181. res := c.RunDockerCmd(t, "ps", "--all")
  182. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"_simple"), res.Combined())
  183. })
  184. t.Run("rm -sf <none>", func(t *testing.T) {
  185. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
  186. "-sf", "simple")
  187. res.Assert(t, icmd.Expected{ExitCode: 0})
  188. })
  189. t.Run("down", func(t *testing.T) {
  190. c.RunDockerComposeCmd(t, "-p", projectName, "down")
  191. })
  192. }
  193. func TestCompatibility(t *testing.T) {
  194. // this test shares a fixture with TestLocalComposeUp and can't run at the same time
  195. c := NewCLI(t)
  196. const projectName = "compose-e2e-compatibility"
  197. t.Run("up", func(t *testing.T) {
  198. c.RunDockerComposeCmd(t, "--compatibility", "-f", "./fixtures/sentences/compose.yaml", "--project-name",
  199. projectName, "up", "-d")
  200. })
  201. t.Run("check container names", func(t *testing.T) {
  202. res := c.RunDockerCmd(t, "ps", "--format", "{{.Names}}")
  203. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_web_1"})
  204. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_words_1"})
  205. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_db_1"})
  206. })
  207. t.Run("down", func(t *testing.T) {
  208. c.RunDockerComposeCmd(t, "-p", projectName, "down")
  209. })
  210. }
  211. func TestConvert(t *testing.T) {
  212. const projectName = "compose-e2e-convert"
  213. c := NewParallelCLI(t)
  214. wd, err := os.Getwd()
  215. assert.NilError(t, err)
  216. t.Run("up", func(t *testing.T) {
  217. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose.yaml", "-p", projectName, "convert")
  218. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services:
  219. nginx:
  220. build:
  221. context: %s
  222. dockerfile: Dockerfile
  223. networks:
  224. default: null
  225. networks:
  226. default:
  227. name: compose-e2e-convert_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
  228. })
  229. }