compose_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "io/ioutil"
  17. "net/http"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "testing"
  22. "time"
  23. testify "github.com/stretchr/testify/assert"
  24. "gotest.tools/v3/assert"
  25. "gotest.tools/v3/icmd"
  26. )
  27. var binDir string
  28. func TestMain(m *testing.M) {
  29. exitCode := m.Run()
  30. os.Exit(exitCode)
  31. }
  32. func TestLocalComposeUp(t *testing.T) {
  33. c := NewParallelE2eCLI(t, binDir)
  34. const projectName = "compose-e2e-demo"
  35. t.Run("up", func(t *testing.T) {
  36. c.RunDockerCmd("compose", "-f", "./fixtures/sentences/compose.yaml", "--project-name", projectName, "up", "-d")
  37. })
  38. t.Run("check accessing running app", func(t *testing.T) {
  39. res := c.RunDockerCmd("compose", "-p", projectName, "ps")
  40. res.Assert(t, icmd.Expected{Out: `web`})
  41. endpoint := "http://localhost:90"
  42. output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
  43. assert.Assert(t, strings.Contains(output, `"word":`))
  44. res = c.RunDockerCmd("network", "ls")
  45. res.Assert(t, icmd.Expected{Out: projectName + "_default"})
  46. })
  47. t.Run("top", func(t *testing.T) {
  48. res := c.RunDockerCmd("compose", "-p", projectName, "top")
  49. output := res.Stdout()
  50. head := []string{"UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"}
  51. for _, h := range head {
  52. assert.Assert(t, strings.Contains(output, h), output)
  53. }
  54. assert.Assert(t, strings.Contains(output, `java -Xmx8m -Xms8m -jar /app/words.jar`), output)
  55. assert.Assert(t, strings.Contains(output, `/dispatcher`), output)
  56. })
  57. t.Run("check compose labels", func(t *testing.T) {
  58. res := c.RunDockerCmd("inspect", projectName+"-web-1")
  59. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
  60. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`})
  61. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`})
  62. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.config-hash":`})
  63. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.config_files":`})
  64. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.working_dir":`})
  65. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.service": "web"`})
  66. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version":`})
  67. res = c.RunDockerCmd("network", "inspect", projectName+"_default")
  68. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.network": "default"`})
  69. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": `})
  70. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version": `})
  71. })
  72. t.Run("check user labels", func(t *testing.T) {
  73. res := c.RunDockerCmd("inspect", projectName+"-web-1")
  74. res.Assert(t, icmd.Expected{Out: `"my-label": "test"`})
  75. })
  76. t.Run("check healthcheck output", func(t *testing.T) {
  77. c.WaitForCmdResult(c.NewDockerCmd("compose", "-p", projectName, "ps", "--format", "json"),
  78. StdoutContains(`"Name":"compose-e2e-demo-web-1","Command":"/dispatcher","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`),
  79. 5*time.Second, 1*time.Second)
  80. res := c.RunDockerCmd("compose", "-p", projectName, "ps")
  81. res.Assert(t, icmd.Expected{Out: `NAME COMMAND SERVICE STATUS PORTS`})
  82. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 "/dispatcher" web running (healthy) 0.0.0.0:90->80/tcp, :::90->80/tcp`})
  83. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 "docker-entrypoint.s…" db running 5432/tcp`})
  84. })
  85. t.Run("images", func(t *testing.T) {
  86. res := c.RunDockerCmd("compose", "-p", projectName, "images")
  87. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 gtardif/sentences-db latest`})
  88. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 gtardif/sentences-web latest`})
  89. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-words-1 gtardif/sentences-api latest`})
  90. })
  91. t.Run("down", func(t *testing.T) {
  92. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  93. })
  94. t.Run("check containers after down", func(t *testing.T) {
  95. res := c.RunDockerCmd("ps", "--all")
  96. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  97. })
  98. t.Run("check networks after down", func(t *testing.T) {
  99. res := c.RunDockerCmd("network", "ls")
  100. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  101. })
  102. }
  103. func TestComposePull(t *testing.T) {
  104. c := NewParallelE2eCLI(t, binDir)
  105. res := c.RunDockerOrExitError("compose", "--project-directory", "fixtures/simple-composefile", "pull")
  106. output := res.Combined()
  107. assert.Assert(t, strings.Contains(output, "simple Pulled"))
  108. assert.Assert(t, strings.Contains(output, "another Pulled"))
  109. }
  110. func TestDownComposefileInParentFolder(t *testing.T) {
  111. c := NewParallelE2eCLI(t, binDir)
  112. tmpFolder, err := ioutil.TempDir("fixtures/simple-composefile", "test-tmp")
  113. assert.NilError(t, err)
  114. defer os.Remove(tmpFolder) // nolint: errcheck
  115. projectName := filepath.Base(tmpFolder)
  116. res := c.RunDockerCmd("compose", "--project-directory", tmpFolder, "up", "-d")
  117. res.Assert(t, icmd.Expected{Err: "Started", ExitCode: 0})
  118. res = c.RunDockerCmd("compose", "-p", projectName, "down")
  119. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  120. }
  121. func TestAttachRestart(t *testing.T) {
  122. c := NewParallelE2eCLI(t, binDir)
  123. cmd := c.NewDockerCmd("compose", "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
  124. res := icmd.StartCmd(cmd)
  125. defer c.RunDockerOrExitError("compose", "-p", "attach-restart", "down")
  126. c.WaitForCondition(func() (bool, string) {
  127. debug := res.Combined()
  128. return strings.Count(res.Stdout(), "failing-1 exited with code 1") == 3, fmt.Sprintf("'failing-1 exited with code 1' not found 3 times in : \n%s\n", debug)
  129. }, 2*time.Minute, 2*time.Second)
  130. assert.Equal(t, strings.Count(res.Stdout(), "failing-1 | world"), 3, res.Combined())
  131. }
  132. func TestInitContainer(t *testing.T) {
  133. c := NewParallelE2eCLI(t, binDir)
  134. res := c.RunDockerOrExitError("compose", "--ansi=never", "--project-directory", "./fixtures/init-container", "up")
  135. defer c.RunDockerOrExitError("compose", "-p", "init-container", "down")
  136. testify.Regexp(t, "foo-1 | hello(?m:.*)bar-1 | world", res.Stdout())
  137. }
  138. func TestRm(t *testing.T) {
  139. c := NewParallelE2eCLI(t, binDir)
  140. const projectName = "compose-e2e-rm"
  141. t.Run("up", func(t *testing.T) {
  142. c.RunDockerCmd("compose", "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "up", "-d")
  143. })
  144. t.Run("rm -sf", func(t *testing.T) {
  145. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm", "-sf", "simple")
  146. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  147. })
  148. t.Run("check containers after rm -sf", func(t *testing.T) {
  149. res := c.RunDockerCmd("ps", "--all")
  150. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"_simple"), res.Combined())
  151. })
  152. t.Run("down", func(t *testing.T) {
  153. c.RunDockerCmd("compose", "-p", projectName, "down")
  154. })
  155. }
  156. func TestCompatibility(t *testing.T) {
  157. c := NewParallelE2eCLI(t, binDir)
  158. const projectName = "compose-e2e-compatibility"
  159. t.Run("up", func(t *testing.T) {
  160. c.RunDockerCmd("compose", "--compatibility", "-f", "./fixtures/sentences/compose.yaml", "--project-name", projectName, "up", "-d")
  161. })
  162. t.Run("check container names", func(t *testing.T) {
  163. res := c.RunDockerCmd("ps", "--format", "{{.Names}}")
  164. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_web_1"})
  165. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_words_1"})
  166. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_db_1"})
  167. })
  168. t.Run("down", func(t *testing.T) {
  169. c.RunDockerCmd("compose", "-p", projectName, "down")
  170. })
  171. }
  172. func TestConvert(t *testing.T) {
  173. const projectName = "compose-e2e-convert"
  174. c := NewParallelE2eCLI(t, binDir)
  175. wd, err := os.Getwd()
  176. assert.NilError(t, err)
  177. t.Run("up", func(t *testing.T) {
  178. res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yaml", "-p", projectName, "convert")
  179. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services:
  180. nginx:
  181. build:
  182. context: %s
  183. dockerfile: Dockerfile
  184. networks:
  185. default: null
  186. networks:
  187. default:
  188. name: compose-e2e-convert_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
  189. })
  190. }