compose_run_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "os"
  16. "strings"
  17. "testing"
  18. "gotest.tools/v3/assert"
  19. "gotest.tools/v3/icmd"
  20. )
  21. func TestLocalComposeRun(t *testing.T) {
  22. c := NewParallelCLI(t)
  23. t.Run("compose run", func(t *testing.T) {
  24. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back")
  25. lines := Lines(res.Stdout())
  26. assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout())
  27. assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
  28. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo",
  29. "Hello one more time")
  30. lines = Lines(res.Stdout())
  31. assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout())
  32. assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
  33. })
  34. t.Run("check run container exited", func(t *testing.T) {
  35. res := c.RunDockerCmd(t, "ps", "--all")
  36. lines := Lines(res.Stdout())
  37. var runContainerID string
  38. var truncatedSlug string
  39. for _, line := range lines {
  40. fields := strings.Fields(line)
  41. containerID := fields[len(fields)-1]
  42. assert.Assert(t, !strings.HasPrefix(containerID, "run-test-front"))
  43. if strings.HasPrefix(containerID, "run-test-back") {
  44. // only the one-off container for back service
  45. assert.Assert(t, strings.HasPrefix(containerID, "run-test-back-run-"), containerID)
  46. truncatedSlug = strings.Replace(containerID, "run-test-back-run-", "", 1)
  47. runContainerID = containerID
  48. }
  49. if strings.HasPrefix(containerID, "run-test-db-1") {
  50. assert.Assert(t, strings.Contains(line, "Up"), line)
  51. }
  52. }
  53. assert.Assert(t, runContainerID != "")
  54. res = c.RunDockerCmd(t, "inspect", runContainerID)
  55. res.Assert(t, icmd.Expected{Out: ` "Status": "exited"`})
  56. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
  57. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "run-test"`})
  58. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "True",`})
  59. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.slug": "` + truncatedSlug})
  60. })
  61. t.Run("compose run --rm", func(t *testing.T) {
  62. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--rm", "back", "echo",
  63. "Hello again")
  64. lines := Lines(res.Stdout())
  65. assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout())
  66. res = c.RunDockerCmd(t, "ps", "--all")
  67. assert.Assert(t, strings.Contains(res.Stdout(), "run-test-back"), res.Stdout())
  68. })
  69. t.Run("down", func(t *testing.T) {
  70. c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "down", "--remove-orphans")
  71. res := c.RunDockerCmd(t, "ps", "--all")
  72. assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
  73. })
  74. t.Run("compose run --volumes", func(t *testing.T) {
  75. wd, err := os.Getwd()
  76. assert.NilError(t, err)
  77. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--volumes", wd+":/foo",
  78. "back", "/bin/sh", "-c", "ls /foo")
  79. res.Assert(t, icmd.Expected{Out: "compose_run_test.go"})
  80. res = c.RunDockerCmd(t, "ps", "--all")
  81. assert.Assert(t, strings.Contains(res.Stdout(), "run-test-back"), res.Stdout())
  82. })
  83. t.Run("compose run --publish", func(t *testing.T) {
  84. c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/ports.yaml", "run", "--publish", "8081:80", "-d", "back",
  85. "/bin/sh", "-c", "sleep 1")
  86. res := c.RunDockerCmd(t, "ps")
  87. assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
  88. assert.Assert(t, !strings.Contains(res.Stdout(), "8082->80/tcp"), res.Stdout())
  89. })
  90. t.Run("compose run --service-ports", func(t *testing.T) {
  91. c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/ports.yaml", "run", "--service-ports", "-d", "back",
  92. "/bin/sh", "-c", "sleep 1")
  93. res := c.RunDockerCmd(t, "ps")
  94. assert.Assert(t, strings.Contains(res.Stdout(), "8082->80/tcp"), res.Stdout())
  95. })
  96. t.Run("compose run orphan", func(t *testing.T) {
  97. // Use different compose files to get an orphan container
  98. c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/orphan.yaml", "run", "simple")
  99. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
  100. assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
  101. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
  102. res = icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
  103. cmd.Env = append(cmd.Env, "COMPOSE_IGNORE_ORPHANS=True")
  104. })
  105. assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
  106. })
  107. t.Run("down", func(t *testing.T) {
  108. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "down")
  109. icmd.RunCmd(cmd, func(c *icmd.Cmd) {
  110. c.Env = append(c.Env, "COMPOSE_REMOVE_ORPHANS=True")
  111. })
  112. res := c.RunDockerCmd(t, "ps", "--all")
  113. assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
  114. })
  115. t.Run("run starts only container and dependencies", func(t *testing.T) {
  116. // ensure that even if another service is up run does not start it: https://github.com/docker/compose/issues/9459
  117. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/deps.yaml", "up", "service_b", "--menu=false")
  118. res.Assert(t, icmd.Success)
  119. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/deps.yaml", "run", "service_a")
  120. assert.Assert(t, strings.Contains(res.Combined(), "shared_dep"), res.Combined())
  121. assert.Assert(t, !strings.Contains(res.Combined(), "service_b"), res.Combined())
  122. c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/deps.yaml", "down", "--remove-orphans")
  123. })
  124. t.Run("run without dependencies", func(t *testing.T) {
  125. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/deps.yaml", "run", "--no-deps", "service_a")
  126. assert.Assert(t, !strings.Contains(res.Combined(), "shared_dep"), res.Combined())
  127. assert.Assert(t, !strings.Contains(res.Combined(), "service_b"), res.Combined())
  128. c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/deps.yaml", "down", "--remove-orphans")
  129. })
  130. t.Run("run with not required dependency", func(t *testing.T) {
  131. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dependencies/deps-not-required.yaml", "run", "foo")
  132. assert.Assert(t, strings.Contains(res.Combined(), "foo"), res.Combined())
  133. assert.Assert(t, !strings.Contains(res.Combined(), "bar"), res.Combined())
  134. c.RunDockerComposeCmd(t, "-f", "./fixtures/dependencies/deps-not-required.yaml", "down", "--remove-orphans")
  135. })
  136. t.Run("--quiet-pull", func(t *testing.T) {
  137. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/quiet-pull.yaml", "down", "--rmi", "all")
  138. res.Assert(t, icmd.Success)
  139. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/quiet-pull.yaml", "run", "--quiet-pull", "backend")
  140. assert.Assert(t, !strings.Contains(res.Combined(), "Pull complete"), res.Combined())
  141. assert.Assert(t, strings.Contains(res.Combined(), "Pulled"), res.Combined())
  142. })
  143. }