compose_run_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. defer c.cleanupWithDown(t, "run-test")
  24. t.Run("compose run", func(t *testing.T) {
  25. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back")
  26. lines := Lines(res.Stdout())
  27. assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout())
  28. assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
  29. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo",
  30. "Hello one more time")
  31. lines = Lines(res.Stdout())
  32. assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout())
  33. assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
  34. })
  35. t.Run("check run container exited", func(t *testing.T) {
  36. res := c.RunDockerCmd(t, "ps", "--all")
  37. lines := Lines(res.Stdout())
  38. var runContainerID string
  39. var truncatedSlug string
  40. for _, line := range lines {
  41. fields := strings.Fields(line)
  42. containerID := fields[len(fields)-1]
  43. assert.Assert(t, !strings.HasPrefix(containerID, "run-test-front"))
  44. if strings.HasPrefix(containerID, "run-test-back") {
  45. // only the one-off container for back service
  46. assert.Assert(t, strings.HasPrefix(containerID, "run-test-back-run-"), containerID)
  47. truncatedSlug = strings.Replace(containerID, "run-test-back-run-", "", 1)
  48. runContainerID = containerID
  49. }
  50. if strings.HasPrefix(containerID, "run-test-db-1") {
  51. assert.Assert(t, strings.Contains(line, "Up"), line)
  52. }
  53. }
  54. assert.Assert(t, runContainerID != "")
  55. res = c.RunDockerCmd(t, "inspect", runContainerID)
  56. res.Assert(t, icmd.Expected{Out: ` "Status": "exited"`})
  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", "--remove-orphans", "--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. t.Run("COMPOSE_PROGRESS quiet", func(t *testing.T) {
  144. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/quiet-pull.yaml", "down", "--remove-orphans", "--rmi", "all")
  145. res.Assert(t, icmd.Success)
  146. cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/run-test/quiet-pull.yaml", "run", "backend")
  147. res = icmd.RunCmd(cmd, func(c *icmd.Cmd) {
  148. c.Env = append(c.Env, "COMPOSE_PROGRESS=quiet")
  149. })
  150. assert.Assert(t, !strings.Contains(res.Combined(), "Pull complete"), res.Combined())
  151. assert.Assert(t, !strings.Contains(res.Combined(), "Pulled"), res.Combined())
  152. })
  153. t.Run("--pull", func(t *testing.T) {
  154. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/pull.yaml", "down", "--remove-orphans", "--rmi", "all")
  155. res.Assert(t, icmd.Success)
  156. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/pull.yaml", "run", "--pull", "always", "backend")
  157. assert.Assert(t, strings.Contains(res.Combined(), "Image nginx Pulling"), res.Combined())
  158. assert.Assert(t, strings.Contains(res.Combined(), "Image nginx Pulled"), res.Combined())
  159. })
  160. t.Run("compose run --env-from-file", func(t *testing.T) {
  161. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--env-from-file", "./fixtures/run-test/run.env",
  162. "front", "env")
  163. res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
  164. })
  165. t.Run("compose run -rm with stop signal", func(t *testing.T) {
  166. projectName := "run-test"
  167. res := c.RunDockerComposeCmd(t, "--project-name", projectName, "-f", "./fixtures/ps-test/compose.yaml", "run", "--rm", "-d", "nginx")
  168. res.Assert(t, icmd.Success)
  169. res = c.RunDockerCmd(t, "ps", "--quiet", "--filter", "name=run-test-nginx")
  170. containerID := strings.TrimSpace(res.Stdout())
  171. res = c.RunDockerCmd(t, "stop", containerID)
  172. res.Assert(t, icmd.Success)
  173. res = c.RunDockerCmd(t, "ps", "--all", "--filter", "name=run-test-nginx", "--format", "'{{.Names}}'")
  174. assert.Assert(t, !strings.Contains(res.Stdout(), "run-test-nginx"), res.Stdout())
  175. })
  176. t.Run("compose run --env", func(t *testing.T) {
  177. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--env", "FOO=BAR",
  178. "front", "env")
  179. res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
  180. })
  181. t.Run("compose run --build", func(t *testing.T) {
  182. c.cleanupWithDown(t, "run-test", "--rmi=local")
  183. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "build", "echo", "hello world")
  184. res.Assert(t, icmd.Expected{Out: "hello world"})
  185. })
  186. t.Run("compose run with piped input detection", func(t *testing.T) {
  187. if composeStandaloneMode {
  188. t.Skip("Skipping test compose with piped input detection in standalone mode")
  189. }
  190. // Test that piped input is properly detected and TTY is automatically disabled
  191. // This tests the logic added in run.go that checks dockerCli.In().IsTerminal()
  192. cmd := c.NewCmd("sh", "-c", "echo 'piped-content' | docker compose -f ./fixtures/run-test/piped-test.yaml run --rm piped-test")
  193. res := icmd.RunCmd(cmd)
  194. res.Assert(t, icmd.Expected{Out: "piped-content"})
  195. res.Assert(t, icmd.Success)
  196. })
  197. t.Run("compose run piped input should not allocate TTY", func(t *testing.T) {
  198. if composeStandaloneMode {
  199. t.Skip("Skipping test compose with piped input detection in standalone mode")
  200. }
  201. // Test that when stdin is piped, the container correctly detects no TTY
  202. // This verifies that the automatic noTty=true setting works correctly
  203. cmd := c.NewCmd("sh", "-c", "echo '' | docker compose -f ./fixtures/run-test/piped-test.yaml run --rm tty-test")
  204. res := icmd.RunCmd(cmd)
  205. res.Assert(t, icmd.Expected{Out: "No TTY detected"})
  206. res.Assert(t, icmd.Success)
  207. })
  208. t.Run("compose run piped input with explicit --tty should fail", func(t *testing.T) {
  209. if composeStandaloneMode {
  210. t.Skip("Skipping test compose with piped input detection in standalone mode")
  211. }
  212. // Test that explicitly requesting TTY with piped input fails with proper error message
  213. // This should trigger the "input device is not a TTY" error
  214. cmd := c.NewCmd("sh", "-c", "echo 'test' | docker compose -f ./fixtures/run-test/piped-test.yaml run --rm --tty piped-test")
  215. res := icmd.RunCmd(cmd)
  216. res.Assert(t, icmd.Expected{
  217. ExitCode: 1,
  218. Err: "the input device is not a TTY",
  219. })
  220. })
  221. t.Run("compose run piped input with --no-TTY=false should fail", func(t *testing.T) {
  222. if composeStandaloneMode {
  223. t.Skip("Skipping test compose with piped input detection in standalone mode")
  224. }
  225. // Test that explicitly disabling --no-TTY (i.e., requesting TTY) with piped input fails
  226. // This should also trigger the "input device is not a TTY" error
  227. cmd := c.NewCmd("sh", "-c", "echo 'test' | docker compose -f ./fixtures/run-test/piped-test.yaml run --rm --no-TTY=false piped-test")
  228. res := icmd.RunCmd(cmd)
  229. res.Assert(t, icmd.Expected{
  230. ExitCode: 1,
  231. Err: "the input device is not a TTY",
  232. })
  233. })
  234. }