compose_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/tests/framework"
  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", "up", "-d", "-f", "./fixtures/sentences/docker-compose.yaml", "--project-name", projectName, "-d")
  42. })
  43. t.Run("check running project", func(t *testing.T) {
  44. res := c.RunDockerCmd("compose", "ps", "-p", projectName)
  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. res := c.RunDockerCmd("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": "./fixtures/sentences/docker-compose.yaml"`})
  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("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("inspect", projectName+"_web_1")
  69. res.Assert(t, icmd.Expected{Out: `"my-label": "test"`})
  70. })
  71. t.Run("down", func(t *testing.T) {
  72. _ = c.RunDockerCmd("compose", "down", "--project-name", projectName)
  73. })
  74. t.Run("check containers after down", func(t *testing.T) {
  75. res := c.RunDockerCmd("ps", "--all")
  76. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  77. })
  78. t.Run("check networks after down", func(t *testing.T) {
  79. res := c.RunDockerCmd("network", "ls")
  80. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  81. })
  82. }
  83. func TestLocalComposeRun(t *testing.T) {
  84. c := NewParallelE2eCLI(t, binDir)
  85. t.Run("compose run", func(t *testing.T) {
  86. res := c.RunDockerCmd("compose", "run", "-f", "./fixtures/run-test/docker-compose.yml", "back")
  87. lines := Lines(res.Stdout())
  88. assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout())
  89. })
  90. t.Run("check run container exited", func(t *testing.T) {
  91. res := c.RunDockerCmd("ps", "--all")
  92. lines := Lines(res.Stdout())
  93. var runContainerID string
  94. var truncatedSlug string
  95. for _, line := range lines {
  96. fields := strings.Fields(line)
  97. containerID := fields[len(fields)-1]
  98. assert.Assert(t, !strings.HasPrefix(containerID, "run-test_front"))
  99. if strings.HasPrefix(containerID, "run-test_back") {
  100. //only the one-off container for back service
  101. assert.Assert(t, strings.HasPrefix(containerID, "run-test_back_run_"), containerID)
  102. truncatedSlug = strings.Replace(containerID, "run-test_back_run_", "", 1)
  103. runContainerID = containerID
  104. }
  105. if strings.HasPrefix(containerID, "run-test_db_1") {
  106. assert.Assert(t, strings.Contains(line, "Up"), line)
  107. }
  108. }
  109. assert.Assert(t, runContainerID != "")
  110. res = c.RunDockerCmd("inspect", runContainerID)
  111. res.Assert(t, icmd.Expected{Out: ` "Status": "exited"`})
  112. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
  113. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "run-test"`})
  114. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "True",`})
  115. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.slug": "` + truncatedSlug})
  116. })
  117. t.Run("compose run --rm", func(t *testing.T) {
  118. res := c.RunDockerCmd("compose", "run", "-f", "./fixtures/run-test/docker-compose.yml", "--rm", "back", "/bin/sh", "-c", "echo Hello again")
  119. lines := Lines(res.Stdout())
  120. assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout())
  121. })
  122. t.Run("check run container removed", func(t *testing.T) {
  123. res := c.RunDockerCmd("ps", "--all")
  124. assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout())
  125. })
  126. t.Run("down", func(t *testing.T) {
  127. c.RunDockerCmd("compose", "down", "-f", "./fixtures/run-test/docker-compose.yml")
  128. res := c.RunDockerCmd("ps", "--all")
  129. assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
  130. })
  131. }
  132. func TestNetworks(t *testing.T) {
  133. c := NewParallelE2eCLI(t, binDir)
  134. const projectName = "network_e2e"
  135. t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
  136. c.RunDockerOrExitError("network", "rm", projectName+"_dbnet")
  137. c.RunDockerOrExitError("network", "rm", "microservices")
  138. })
  139. t.Run("up", func(t *testing.T) {
  140. c.RunDockerCmd("compose", "up", "-d", "-f", "./fixtures/network-test/docker-compose.yaml", "--project-name", projectName, "-d")
  141. })
  142. t.Run("check running project", func(t *testing.T) {
  143. res := c.RunDockerCmd("compose", "ps", "-p", projectName)
  144. res.Assert(t, icmd.Expected{Out: `web`})
  145. endpoint := "http://localhost:80"
  146. output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  147. assert.Assert(t, strings.Contains(output, `"word":`))
  148. res = c.RunDockerCmd("network", "ls")
  149. res.Assert(t, icmd.Expected{Out: projectName + "_dbnet"})
  150. res.Assert(t, icmd.Expected{Out: "microservices"})
  151. })
  152. t.Run("down", func(t *testing.T) {
  153. _ = c.RunDockerCmd("compose", "down", "--project-name", projectName)
  154. })
  155. t.Run("check networks after down", func(t *testing.T) {
  156. res := c.RunDockerCmd("network", "ls")
  157. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  158. assert.Assert(t, !strings.Contains(res.Combined(), "microservices"), res.Combined())
  159. })
  160. }
  161. func TestLocalComposeBuild(t *testing.T) {
  162. c := NewParallelE2eCLI(t, binDir)
  163. t.Run("build named and unnamed images", func(t *testing.T) {
  164. //ensure local test run does not reuse previously build image
  165. c.RunDockerOrExitError("rmi", "build-test_nginx")
  166. c.RunDockerOrExitError("rmi", "custom-nginx")
  167. res := c.RunDockerCmd("compose", "build", "--workdir", "fixtures/build-test")
  168. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  169. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  170. c.RunDockerCmd("image", "inspect", "custom-nginx")
  171. })
  172. t.Run("build as part of up", func(t *testing.T) {
  173. c.RunDockerOrExitError("rmi", "build-test_nginx")
  174. c.RunDockerOrExitError("rmi", "custom-nginx")
  175. res := c.RunDockerCmd("compose", "up", "-d", "--workdir", "fixtures/build-test")
  176. t.Cleanup(func() {
  177. c.RunDockerCmd("compose", "down", "--workdir", "fixtures/build-test")
  178. })
  179. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  180. output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
  181. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  182. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  183. c.RunDockerCmd("image", "inspect", "custom-nginx")
  184. })
  185. t.Run("no rebuild when up again", func(t *testing.T) {
  186. res := c.RunDockerCmd("compose", "up", "-d", "--workdir", "fixtures/build-test")
  187. assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static /usr/share/nginx/html"), res.Stdout())
  188. })
  189. t.Run("cleanup build project", func(t *testing.T) {
  190. c.RunDockerCmd("compose", "down", "--workdir", "fixtures/build-test")
  191. c.RunDockerCmd("rmi", "build-test_nginx")
  192. c.RunDockerCmd("rmi", "custom-nginx")
  193. })
  194. }
  195. func TestLocalComposeVolume(t *testing.T) {
  196. c := NewParallelE2eCLI(t, binDir)
  197. const projectName = "compose-e2e-volume"
  198. t.Run("up with build and no image name, volume", func(t *testing.T) {
  199. //ensure local test run does not reuse previously build image
  200. c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx")
  201. c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol")
  202. c.RunDockerOrExitError("volume", "rm", "myvolume")
  203. c.RunDockerCmd("compose", "up", "-d", "--workdir", "fixtures/volume-test", "--project-name", projectName)
  204. })
  205. t.Run("access bind mount data", func(t *testing.T) {
  206. output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)
  207. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  208. })
  209. t.Run("check container volume specs", func(t *testing.T) {
  210. res := c.RunDockerCmd("inspect", "compose-e2e-volume_nginx2_1", "--format", "{{ json .Mounts }}")
  211. output := res.Stdout()
  212. //nolint
  213. assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"","RW":true,"Propagation":""`))
  214. })
  215. t.Run("check container bind-mounts specs", func(t *testing.T) {
  216. res := c.RunDockerCmd("inspect", "compose-e2e-volume_nginx_1", "--format", "{{ json .HostConfig.Mounts }}")
  217. output := res.Stdout()
  218. //nolint
  219. assert.Assert(t, strings.Contains(output, `"Type":"bind"`))
  220. assert.Assert(t, strings.Contains(output, `"Target":"/usr/share/nginx/html"`))
  221. })
  222. t.Run("cleanup volume project", func(t *testing.T) {
  223. c.RunDockerCmd("compose", "down", "--project-name", projectName)
  224. c.RunDockerCmd("volume", "rm", projectName+"_staticVol")
  225. })
  226. }
  227. func TestComposePull(t *testing.T) {
  228. c := NewParallelE2eCLI(t, binDir)
  229. res := c.RunDockerOrExitError("compose", "pull", "--workdir", "fixtures/simple-composefile")
  230. output := res.Combined()
  231. assert.Assert(t, strings.Contains(output, "simple Pulled"))
  232. assert.Assert(t, strings.Contains(output, "another Pulled"))
  233. }