compose_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/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:80"
  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 TestLocalComposeBuild(t *testing.T) {
  84. c := NewParallelE2eCLI(t, binDir)
  85. t.Run("build named and unnamed images", func(t *testing.T) {
  86. //ensure local test run does not reuse previously build image
  87. c.RunDockerOrExitError("rmi", "build-test_nginx")
  88. c.RunDockerOrExitError("rmi", "custom-nginx")
  89. res := c.RunDockerCmd("compose", "build", "--workdir", "fixtures/build-test")
  90. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  91. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  92. c.RunDockerCmd("image", "inspect", "custom-nginx")
  93. })
  94. t.Run("build as part of up", func(t *testing.T) {
  95. c.RunDockerOrExitError("rmi", "build-test_nginx")
  96. c.RunDockerOrExitError("rmi", "custom-nginx")
  97. res := c.RunDockerCmd("compose", "up", "-d", "--workdir", "fixtures/build-test")
  98. t.Cleanup(func() {
  99. c.RunDockerCmd("compose", "down", "--workdir", "fixtures/build-test")
  100. })
  101. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  102. output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
  103. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  104. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  105. c.RunDockerCmd("image", "inspect", "custom-nginx")
  106. })
  107. t.Run("no rebuild when up again", func(t *testing.T) {
  108. res := c.RunDockerCmd("compose", "up", "-d", "--workdir", "fixtures/build-test")
  109. assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static /usr/share/nginx/html"), res.Stdout())
  110. })
  111. t.Run("cleanup build project", func(t *testing.T) {
  112. c.RunDockerCmd("compose", "down", "--workdir", "fixtures/build-test")
  113. c.RunDockerCmd("rmi", "build-test_nginx")
  114. c.RunDockerCmd("rmi", "custom-nginx")
  115. })
  116. }
  117. func TestLocalComposeVolume(t *testing.T) {
  118. c := NewParallelE2eCLI(t, binDir)
  119. const projectName = "compose-e2e-volume"
  120. t.Run("up with build and no image name, volume", func(t *testing.T) {
  121. //ensure local test run does not reuse previously build image
  122. c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx")
  123. c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol")
  124. c.RunDockerCmd("compose", "up", "-d", "--workdir", "fixtures/volume-test", "--project-name", projectName)
  125. })
  126. t.Run("access bind mount data", func(t *testing.T) {
  127. output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)
  128. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  129. })
  130. t.Run("check container volume specs", func(t *testing.T) {
  131. res := c.RunDockerCmd("inspect", "compose-e2e-volume_nginx2_1", "--format", "{{ json .HostConfig.Mounts }}")
  132. //nolint
  133. res.Assert(t, icmd.Expected{Out: `[{"Type":"volume","Source":"compose-e2e-volume_staticVol","Target":"/usr/share/nginx/html","ReadOnly":true},{"Type":"volume","Target":"/usr/src/app/node_modules"}]`})
  134. })
  135. t.Run("cleanup volume project", func(t *testing.T) {
  136. c.RunDockerCmd("compose", "down", "--project-name", projectName)
  137. c.RunDockerCmd("volume", "rm", projectName+"_staticVol")
  138. })
  139. }