compose_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. IsHealthy(projectName+"-web-1"),
  74. 5*time.Second, 1*time.Second)
  75. res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
  76. assertServiceStatus(t, projectName, "web", "(healthy)", res.Stdout())
  77. })
  78. t.Run("images", func(t *testing.T) {
  79. res := c.RunDockerComposeCmd(t, "-p", projectName, "images")
  80. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 gtardif/sentences-db latest`})
  81. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 gtardif/sentences-web latest`})
  82. res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-words-1 gtardif/sentences-api latest`})
  83. })
  84. t.Run("down SERVICE", func(t *testing.T) {
  85. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "web")
  86. res := c.RunDockerComposeCmd(t, "--project-name", projectName, "ps")
  87. assert.Assert(t, !strings.Contains(res.Combined(), "compose-e2e-demo-web-1"), res.Combined())
  88. assert.Assert(t, strings.Contains(res.Combined(), "compose-e2e-demo-db-1"), res.Combined())
  89. })
  90. t.Run("down", func(t *testing.T) {
  91. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  92. })
  93. t.Run("check containers after down", func(t *testing.T) {
  94. res := c.RunDockerCmd(t, "ps", "--all")
  95. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  96. })
  97. t.Run("check networks after down", func(t *testing.T) {
  98. res := c.RunDockerCmd(t, "network", "ls")
  99. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  100. })
  101. }
  102. func TestDownComposefileInParentFolder(t *testing.T) {
  103. c := NewParallelCLI(t)
  104. tmpFolder, err := os.MkdirTemp("fixtures/simple-composefile", "test-tmp")
  105. assert.NilError(t, err)
  106. defer os.Remove(tmpFolder) //nolint:errcheck
  107. projectName := filepath.Base(tmpFolder)
  108. res := c.RunDockerComposeCmd(t, "--project-directory", tmpFolder, "up", "-d")
  109. res.Assert(t, icmd.Expected{Err: "Started", ExitCode: 0})
  110. res = c.RunDockerComposeCmd(t, "-p", projectName, "down")
  111. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  112. }
  113. func TestAttachRestart(t *testing.T) {
  114. t.Skip("Skipping test until we can fix it")
  115. if _, ok := os.LookupEnv("CI"); ok {
  116. t.Skip("Skipping test on CI... flaky")
  117. }
  118. c := NewParallelCLI(t)
  119. cmd := c.NewDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
  120. res := icmd.StartCmd(cmd)
  121. defer c.RunDockerComposeCmd(t, "-p", "attach-restart", "down")
  122. c.WaitForCondition(t, func() (bool, string) {
  123. debug := res.Combined()
  124. return strings.Count(res.Stdout(),
  125. "failing-1 exited with code 1") == 3, fmt.Sprintf("'failing-1 exited with code 1' not found 3 times in : \n%s\n",
  126. debug)
  127. }, 4*time.Minute, 2*time.Second)
  128. assert.Equal(t, strings.Count(res.Stdout(), "failing-1 | world"), 3, res.Combined())
  129. }
  130. func TestInitContainer(t *testing.T) {
  131. c := NewParallelCLI(t)
  132. res := c.RunDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/init-container", "up", "--menu=false")
  133. defer c.RunDockerComposeCmd(t, "-p", "init-container", "down")
  134. testify.Regexp(t, "foo-1 | hello(?m:.*)bar-1 | world", res.Stdout())
  135. }
  136. func TestRm(t *testing.T) {
  137. c := NewParallelCLI(t)
  138. const projectName = "compose-e2e-rm"
  139. t.Run("up", func(t *testing.T) {
  140. c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "up", "-d")
  141. })
  142. t.Run("rm --stop --force simple", func(t *testing.T) {
  143. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
  144. "--stop", "--force", "simple")
  145. res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
  146. })
  147. t.Run("check containers after rm", func(t *testing.T) {
  148. res := c.RunDockerCmd(t, "ps", "--all")
  149. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"-simple"), res.Combined())
  150. assert.Assert(t, strings.Contains(res.Combined(), projectName+"-another"), res.Combined())
  151. })
  152. t.Run("up (again)", func(t *testing.T) {
  153. c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "up", "-d")
  154. })
  155. t.Run("rm ---stop --force <none>", func(t *testing.T) {
  156. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
  157. "--stop", "--force")
  158. res.Assert(t, icmd.Expected{ExitCode: 0})
  159. })
  160. t.Run("check containers after rm", func(t *testing.T) {
  161. res := c.RunDockerCmd(t, "ps", "--all")
  162. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"-simple"), res.Combined())
  163. assert.Assert(t, !strings.Contains(res.Combined(), projectName+"-another"), res.Combined())
  164. })
  165. t.Run("down", func(t *testing.T) {
  166. c.RunDockerComposeCmd(t, "-p", projectName, "down")
  167. })
  168. }
  169. func TestCompatibility(t *testing.T) {
  170. // this test shares a fixture with TestLocalComposeUp and can't run at the same time
  171. c := NewCLI(t)
  172. const projectName = "compose-e2e-compatibility"
  173. t.Run("up", func(t *testing.T) {
  174. c.RunDockerComposeCmd(t, "--compatibility", "-f", "./fixtures/sentences/compose.yaml", "--project-name",
  175. projectName, "up", "-d")
  176. })
  177. t.Run("check container names", func(t *testing.T) {
  178. res := c.RunDockerCmd(t, "ps", "--format", "{{.Names}}")
  179. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_web_1"})
  180. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_words_1"})
  181. res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_db_1"})
  182. })
  183. t.Run("down", func(t *testing.T) {
  184. c.RunDockerComposeCmd(t, "-p", projectName, "down")
  185. })
  186. }
  187. func TestConfig(t *testing.T) {
  188. const projectName = "compose-e2e-convert"
  189. c := NewParallelCLI(t)
  190. wd, err := os.Getwd()
  191. assert.NilError(t, err)
  192. t.Run("up", func(t *testing.T) {
  193. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose.yaml", "-p", projectName, "convert")
  194. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`name: %s
  195. services:
  196. nginx:
  197. build:
  198. context: %s
  199. dockerfile: Dockerfile
  200. networks:
  201. default: null
  202. networks:
  203. default:
  204. name: compose-e2e-convert_default
  205. `, projectName, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
  206. })
  207. }
  208. func TestConfigInterpolate(t *testing.T) {
  209. const projectName = "compose-e2e-convert-interpolate"
  210. c := NewParallelCLI(t)
  211. wd, err := os.Getwd()
  212. assert.NilError(t, err)
  213. t.Run("convert", func(t *testing.T) {
  214. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose-interpolate.yaml", "-p", projectName, "convert", "--no-interpolate")
  215. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`name: %s
  216. networks:
  217. default:
  218. name: compose-e2e-convert-interpolate_default
  219. services:
  220. nginx:
  221. build:
  222. context: %s
  223. dockerfile: ${MYVAR}
  224. networks:
  225. default: null
  226. `, projectName, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
  227. })
  228. }
  229. func TestStopWithDependenciesAttached(t *testing.T) {
  230. const projectName = "compose-e2e-stop-with-deps"
  231. c := NewParallelCLI(t, WithEnv("COMMAND=echo hello"))
  232. cleanup := func() {
  233. c.RunDockerComposeCmd(t, "-p", projectName, "down", "--remove-orphans", "--timeout=0")
  234. }
  235. cleanup()
  236. t.Cleanup(cleanup)
  237. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dependencies/compose.yaml", "-p", projectName, "up", "--attach-dependencies", "foo", "--menu=false")
  238. res.Assert(t, icmd.Expected{Out: "exited with code 0"})
  239. }
  240. func TestRemoveOrphaned(t *testing.T) {
  241. const projectName = "compose-e2e-remove-orphaned"
  242. c := NewParallelCLI(t)
  243. cleanup := func() {
  244. c.RunDockerComposeCmd(t, "-p", projectName, "down", "--remove-orphans", "--timeout=0")
  245. }
  246. cleanup()
  247. t.Cleanup(cleanup)
  248. // run stack
  249. c.RunDockerComposeCmd(t, "-f", "./fixtures/sentences/compose.yaml", "-p", projectName, "up", "-d")
  250. // down "web" service with orphaned removed
  251. c.RunDockerComposeCmd(t, "-f", "./fixtures/sentences/compose.yaml", "-p", projectName, "down", "--remove-orphans", "web")
  252. // check "words" service has not been considered orphaned
  253. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/sentences/compose.yaml", "-p", projectName, "ps", "--format", "{{.Name}}")
  254. res.Assert(t, icmd.Expected{Out: fmt.Sprintf("%s-words-1", projectName)})
  255. }
  256. func TestComposeFileSetByDotEnv(t *testing.T) {
  257. c := NewCLI(t)
  258. cmd := c.NewDockerComposeCmd(t, "config")
  259. cmd.Dir = filepath.Join(".", "fixtures", "dotenv")
  260. res := icmd.RunCmd(cmd)
  261. res.Assert(t, icmd.Expected{
  262. ExitCode: 0,
  263. Out: "image: test:latest",
  264. })
  265. res.Assert(t, icmd.Expected{
  266. Out: "image: enabled:profile",
  267. })
  268. }
  269. func TestComposeFileSetByProjectDirectory(t *testing.T) {
  270. c := NewCLI(t)
  271. dir := filepath.Join(".", "fixtures", "dotenv", "development")
  272. cmd := c.NewDockerComposeCmd(t, "--project-directory", dir, "config")
  273. res := icmd.RunCmd(cmd)
  274. res.Assert(t, icmd.Expected{
  275. ExitCode: 0,
  276. Out: "image: backend:latest",
  277. })
  278. }
  279. func TestComposeFileSetByEnvFile(t *testing.T) {
  280. c := NewCLI(t)
  281. dotEnv, err := os.CreateTemp(t.TempDir(), ".env")
  282. assert.NilError(t, err)
  283. err = os.WriteFile(dotEnv.Name(), []byte(`
  284. COMPOSE_FILE=fixtures/dotenv/development/compose.yaml
  285. IMAGE_NAME=test
  286. IMAGE_TAG=latest
  287. COMPOSE_PROFILES=test
  288. `), 0o700)
  289. assert.NilError(t, err)
  290. cmd := c.NewDockerComposeCmd(t, "--env-file", dotEnv.Name(), "config")
  291. res := icmd.RunCmd(cmd)
  292. res.Assert(t, icmd.Expected{
  293. Out: "image: test:latest",
  294. })
  295. res.Assert(t, icmd.Expected{
  296. Out: "image: enabled:profile",
  297. })
  298. }
  299. func TestNestedDotEnv(t *testing.T) {
  300. c := NewCLI(t)
  301. cmd := c.NewDockerComposeCmd(t, "run", "echo")
  302. cmd.Dir = filepath.Join(".", "fixtures", "nested")
  303. res := icmd.RunCmd(cmd)
  304. res.Assert(t, icmd.Expected{
  305. ExitCode: 0,
  306. Out: "root win=root",
  307. })
  308. cmd = c.NewDockerComposeCmd(t, "run", "echo")
  309. cmd.Dir = filepath.Join(".", "fixtures", "nested", "sub")
  310. res = icmd.RunCmd(cmd)
  311. res.Assert(t, icmd.Expected{
  312. ExitCode: 0,
  313. Out: "root sub win=sub",
  314. })
  315. }
  316. func TestUnnecesaryResources(t *testing.T) {
  317. const projectName = "compose-e2e-unnecessary-resources"
  318. c := NewParallelCLI(t)
  319. t.Cleanup(func() {
  320. c.RunDockerComposeCmd(t, "-p", projectName, "down", "-t=0")
  321. })
  322. res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/external/compose.yaml", "-p", projectName, "up", "-d")
  323. res.Assert(t, icmd.Expected{
  324. ExitCode: 1,
  325. Err: "network foo_bar declared as external, but could not be found",
  326. })
  327. c.RunDockerComposeCmd(t, "-f", "./fixtures/external/compose.yaml", "-p", projectName, "up", "-d", "test")
  328. // Should not fail as missing external network is not used
  329. }