compose_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 TestDownComposefileInParentFolder(t *testing.T) {
  99. c := NewParallelCLI(t)
  100. tmpFolder, err := os.MkdirTemp("fixtures/simple-composefile", "test-tmp")
  101. assert.NilError(t, err)
  102. defer os.Remove(tmpFolder) //nolint:errcheck
  103. projectName := filepath.Base(tmpFolder)
  104. res := c.RunDockerComposeCmd(t, "--project-directory", tmpFolder, "up", "-d")
  105. res.Assert(t, icmd.Expected{Err: "Started", ExitCode: 0})
  106. res = c.RunDockerComposeCmd(t, "-p", projectName, "down")
  107. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  108. }
  109. func TestAttachRestart(t *testing.T) {
  110. c := NewParallelCLI(t)
  111. cmd := c.NewDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
  112. res := icmd.StartCmd(cmd)
  113. defer c.RunDockerComposeCmd(t, "-p", "attach-restart", "down")
  114. c.WaitForCondition(t, func() (bool, string) {
  115. debug := res.Combined()
  116. return strings.Count(res.Stdout(),
  117. "failing-1 exited with code 1") == 3, fmt.Sprintf("'failing-1 exited with code 1' not found 3 times in : \n%s\n",
  118. debug)
  119. }, 2*time.Minute, 2*time.Second)
  120. assert.Equal(t, strings.Count(res.Stdout(), "failing-1 | world"), 3, res.Combined())
  121. }
  122. func TestInitContainer(t *testing.T) {
  123. c := NewParallelCLI(t)
  124. res := c.RunDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/init-container", "up")
  125. defer c.RunDockerComposeCmd(t, "-p", "init-container", "down")
  126. testify.Regexp(t, "foo-1 | hello(?m:.*)bar-1 | world", res.Stdout())
  127. }
  128. func TestRm(t *testing.T) {
  129. c := NewParallelCLI(t)
  130. const projectName = "compose-e2e-rm"
  131. t.Run("up", func(t *testing.T) {
  132. c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "up", "-d")
  133. })
  134. t.Run("rm -sf", func(t *testing.T) {
  135. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
  136. "-sf", "simple")
  137. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  138. })
  139. t.Run("check containers after rm -sf", func(t *testing.T) {
  140. res := c.RunDockerCmd(t, "ps", "--all")
  141. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"_simple"), res.Combined())
  142. })
  143. t.Run("rm -sf <none>", func(t *testing.T) {
  144. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
  145. "-sf", "simple")
  146. res.Assert(t, icmd.Expected{ExitCode: 0})
  147. })
  148. t.Run("down", func(t *testing.T) {
  149. c.RunDockerComposeCmd(t, "-p", projectName, "down")
  150. })
  151. }
  152. func TestCompatibility(t *testing.T) {
  153. // this test shares a fixture with TestLocalComposeUp and can't run at the same time
  154. c := NewCLI(t)
  155. const projectName = "compose-e2e-compatibility"
  156. t.Run("up", func(t *testing.T) {
  157. c.RunDockerComposeCmd(t, "--compatibility", "-f", "./fixtures/sentences/compose.yaml", "--project-name",
  158. projectName, "up", "-d")
  159. })
  160. t.Run("check container names", func(t *testing.T) {
  161. res := c.RunDockerCmd(t, "ps", "--format", "{{.Names}}")
  162. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_web_1"})
  163. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_words_1"})
  164. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_db_1"})
  165. })
  166. t.Run("down", func(t *testing.T) {
  167. c.RunDockerComposeCmd(t, "-p", projectName, "down")
  168. })
  169. }
  170. func TestConvert(t *testing.T) {
  171. const projectName = "compose-e2e-convert"
  172. c := NewParallelCLI(t)
  173. wd, err := os.Getwd()
  174. assert.NilError(t, err)
  175. t.Run("up", func(t *testing.T) {
  176. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose.yaml", "-p", projectName, "convert")
  177. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services:
  178. nginx:
  179. build:
  180. context: %s
  181. dockerfile: Dockerfile
  182. networks:
  183. default: null
  184. networks:
  185. default:
  186. name: compose-e2e-convert_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
  187. })
  188. }
  189. func TestConvertInterpolate(t *testing.T) {
  190. const projectName = "compose-e2e-convert-interpolate"
  191. c := NewParallelCLI(t)
  192. wd, err := os.Getwd()
  193. assert.NilError(t, err)
  194. t.Run("convert", func(t *testing.T) {
  195. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose-interpolate.yaml", "-p", projectName, "convert", "--no-interpolate")
  196. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services:
  197. nginx:
  198. build:
  199. context: %s
  200. dockerfile: ${MYVAR}
  201. networks:
  202. default: null
  203. networks:
  204. default:
  205. name: compose-e2e-convert-interpolate_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
  206. })
  207. }