compose_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. "strings"
  19. "testing"
  20. "time"
  21. "gotest.tools/v3/assert"
  22. "gotest.tools/v3/icmd"
  23. . "github.com/docker/compose-cli/utils/e2e"
  24. )
  25. var binDir string
  26. func TestMain(m *testing.M) {
  27. p, cleanup, err := SetupExistingCLI()
  28. if err != nil {
  29. fmt.Println(err)
  30. os.Exit(1)
  31. }
  32. binDir = p
  33. exitCode := m.Run()
  34. cleanup()
  35. os.Exit(exitCode)
  36. }
  37. func TestLocalComposeUp(t *testing.T) {
  38. c := NewParallelE2eCLI(t, binDir)
  39. const projectName = "compose-e2e-demo"
  40. t.Run("up", func(t *testing.T) {
  41. c.RunDockerCmd("compose", "-f", "./fixtures/sentences/compose.yaml", "--project-name", projectName, "up", "-d")
  42. })
  43. t.Run("check accessing running app", func(t *testing.T) {
  44. res := c.RunDockerCmd("compose", "-p", projectName, "ps")
  45. res.Assert(t, icmd.Expected{Out: `web`})
  46. endpoint := "http://localhost:90"
  47. output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  48. assert.Assert(t, strings.Contains(output, `"word":`))
  49. res = c.RunDockerCmd("network", "ls")
  50. res.Assert(t, icmd.Expected{Out: projectName + "_default"})
  51. })
  52. t.Run("check compose labels", func(t *testing.T) {
  53. wd, _ := os.Getwd()
  54. res := c.RunDockerCmd("inspect", projectName+"_web_1")
  55. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
  56. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`})
  57. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`})
  58. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.config-hash":`})
  59. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`"com.docker.compose.project.config_files": "%s/fixtures/sentences/compose.yaml"`, wd)})
  60. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.working_dir":`})
  61. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.service": "web"`})
  62. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version":`})
  63. res = c.RunDockerCmd("network", "inspect", projectName+"_default")
  64. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.network": "default"`})
  65. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": `})
  66. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version": `})
  67. })
  68. t.Run("check user labels", func(t *testing.T) {
  69. res := c.RunDockerCmd("inspect", projectName+"_web_1")
  70. res.Assert(t, icmd.Expected{Out: `"my-label": "test"`})
  71. })
  72. t.Run("check healthcheck output", func(t *testing.T) {
  73. c.WaitForCmdResult(c.NewDockerCmd("compose", "-p", projectName, "ps", "--format", "json"),
  74. StdoutContains(`"Name":"compose-e2e-demo_web_1","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`),
  75. 5*time.Second, 1*time.Second)
  76. res := c.RunDockerCmd("compose", "-p", projectName, "ps")
  77. res.Assert(t, icmd.Expected{Out: `NAME SERVICE STATUS PORTS`})
  78. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_web_1 web running (healthy) 0.0.0.0:90->80/tcp`})
  79. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo_db_1 db running 5432/tcp`})
  80. })
  81. t.Run("down", func(t *testing.T) {
  82. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  83. })
  84. t.Run("check containers after down", func(t *testing.T) {
  85. res := c.RunDockerCmd("ps", "--all")
  86. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  87. })
  88. t.Run("check networks after down", func(t *testing.T) {
  89. res := c.RunDockerCmd("network", "ls")
  90. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  91. })
  92. }
  93. func TestLocalComposeRun(t *testing.T) {
  94. c := NewParallelE2eCLI(t, binDir)
  95. t.Run("compose run", func(t *testing.T) {
  96. res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "back")
  97. lines := Lines(res.Stdout())
  98. assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout())
  99. })
  100. t.Run("check run container exited", func(t *testing.T) {
  101. res := c.RunDockerCmd("ps", "--all")
  102. lines := Lines(res.Stdout())
  103. var runContainerID string
  104. var truncatedSlug string
  105. for _, line := range lines {
  106. fields := strings.Fields(line)
  107. containerID := fields[len(fields)-1]
  108. assert.Assert(t, !strings.HasPrefix(containerID, "run-test_front"))
  109. if strings.HasPrefix(containerID, "run-test_back") {
  110. // only the one-off container for back service
  111. assert.Assert(t, strings.HasPrefix(containerID, "run-test_back_run_"), containerID)
  112. truncatedSlug = strings.Replace(containerID, "run-test_back_run_", "", 1)
  113. runContainerID = containerID
  114. }
  115. if strings.HasPrefix(containerID, "run-test_db_1") {
  116. assert.Assert(t, strings.Contains(line, "Up"), line)
  117. }
  118. }
  119. assert.Assert(t, runContainerID != "")
  120. res = c.RunDockerCmd("inspect", runContainerID)
  121. res.Assert(t, icmd.Expected{Out: ` "Status": "exited"`})
  122. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
  123. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "run-test"`})
  124. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "True",`})
  125. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.slug": "` + truncatedSlug})
  126. })
  127. t.Run("compose run --rm", func(t *testing.T) {
  128. res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "--rm", "back", "/bin/sh", "-c", "echo Hello again")
  129. lines := Lines(res.Stdout())
  130. assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout())
  131. })
  132. t.Run("check run container removed", func(t *testing.T) {
  133. res := c.RunDockerCmd("ps", "--all")
  134. assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout())
  135. })
  136. t.Run("down", func(t *testing.T) {
  137. c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "down")
  138. res := c.RunDockerCmd("ps", "--all")
  139. assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
  140. })
  141. }
  142. func TestNetworks(t *testing.T) {
  143. c := NewParallelE2eCLI(t, binDir)
  144. const projectName = "network_e2e"
  145. t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
  146. c.RunDockerOrExitError("network", "rm", projectName+"_dbnet")
  147. c.RunDockerOrExitError("network", "rm", "microservices")
  148. })
  149. t.Run("up", func(t *testing.T) {
  150. c.RunDockerCmd("compose", "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "up", "-d")
  151. })
  152. t.Run("check running project", func(t *testing.T) {
  153. res := c.RunDockerCmd("compose", "-p", projectName, "ps")
  154. res.Assert(t, icmd.Expected{Out: `web`})
  155. endpoint := "http://localhost:80"
  156. output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  157. assert.Assert(t, strings.Contains(output, `"word":`))
  158. res = c.RunDockerCmd("network", "ls")
  159. res.Assert(t, icmd.Expected{Out: projectName + "_dbnet"})
  160. res.Assert(t, icmd.Expected{Out: "microservices"})
  161. })
  162. t.Run("down", func(t *testing.T) {
  163. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  164. })
  165. t.Run("check networks after down", func(t *testing.T) {
  166. res := c.RunDockerCmd("network", "ls")
  167. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  168. assert.Assert(t, !strings.Contains(res.Combined(), "microservices"), res.Combined())
  169. })
  170. }
  171. func TestLocalComposeBuild(t *testing.T) {
  172. c := NewParallelE2eCLI(t, binDir)
  173. t.Run("build named and unnamed images", func(t *testing.T) {
  174. // ensure local test run does not reuse previously build image
  175. c.RunDockerOrExitError("rmi", "build-test_nginx")
  176. c.RunDockerOrExitError("rmi", "custom-nginx")
  177. res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "build")
  178. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  179. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  180. c.RunDockerCmd("image", "inspect", "custom-nginx")
  181. })
  182. t.Run("build as part of up", func(t *testing.T) {
  183. c.RunDockerOrExitError("rmi", "build-test_nginx")
  184. c.RunDockerOrExitError("rmi", "custom-nginx")
  185. res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "up", "-d")
  186. t.Cleanup(func() {
  187. c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "down")
  188. })
  189. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  190. output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
  191. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  192. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  193. c.RunDockerCmd("image", "inspect", "custom-nginx")
  194. })
  195. t.Run("no rebuild when up again", func(t *testing.T) {
  196. res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "up", "-d")
  197. assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static /usr/share/nginx/html"), res.Stdout())
  198. })
  199. t.Run("cleanup build project", func(t *testing.T) {
  200. c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "down")
  201. c.RunDockerCmd("rmi", "build-test_nginx")
  202. c.RunDockerCmd("rmi", "custom-nginx")
  203. })
  204. }
  205. func TestLocalComposeVolume(t *testing.T) {
  206. c := NewParallelE2eCLI(t, binDir)
  207. const projectName = "compose-e2e-volume"
  208. t.Run("up with build and no image name, volume", func(t *testing.T) {
  209. // ensure local test run does not reuse previously build image
  210. c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx")
  211. c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol")
  212. c.RunDockerOrExitError("volume", "rm", "myvolume")
  213. c.RunDockerCmd("compose", "--workdir", "fixtures/volume-test", "--project-name", projectName, "up", "-d")
  214. })
  215. t.Run("access bind mount data", func(t *testing.T) {
  216. output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)
  217. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  218. })
  219. t.Run("check container volume specs", func(t *testing.T) {
  220. res := c.RunDockerCmd("inspect", "compose-e2e-volume_nginx2_1", "--format", "{{ json .Mounts }}")
  221. output := res.Stdout()
  222. // nolint
  223. assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"","RW":true,"Propagation":""`), output)
  224. assert.Assert(t, strings.Contains(output, `"Destination":"/myconfig","Mode":"","RW":false,"Propagation":"rprivate"`), output)
  225. })
  226. t.Run("check config content", func(t *testing.T) {
  227. output := c.RunDockerCmd("exec", "compose-e2e-volume_nginx2_1", "cat", "/myconfig").Stdout()
  228. assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
  229. })
  230. t.Run("check secrets content", func(t *testing.T) {
  231. output := c.RunDockerCmd("exec", "compose-e2e-volume_nginx2_1", "cat", "/run/secrets/mysecret").Stdout()
  232. assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
  233. })
  234. t.Run("check container bind-mounts specs", func(t *testing.T) {
  235. res := c.RunDockerCmd("inspect", "compose-e2e-volume_nginx_1", "--format", "{{ json .HostConfig.Mounts }}")
  236. output := res.Stdout()
  237. // nolint
  238. assert.Assert(t, strings.Contains(output, `"Type":"bind"`))
  239. assert.Assert(t, strings.Contains(output, `"Target":"/usr/share/nginx/html"`))
  240. })
  241. t.Run("cleanup volume project", func(t *testing.T) {
  242. c.RunDockerCmd("compose", "--project-name", projectName, "down")
  243. c.RunDockerCmd("volume", "rm", projectName+"_staticVol")
  244. })
  245. }
  246. func TestComposePull(t *testing.T) {
  247. c := NewParallelE2eCLI(t, binDir)
  248. res := c.RunDockerOrExitError("compose", "--workdir", "fixtures/simple-composefile", "pull")
  249. output := res.Combined()
  250. assert.Assert(t, strings.Contains(output, "simple Pulled"))
  251. assert.Assert(t, strings.Contains(output, "another Pulled"))
  252. }