compose_run_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 := NewParallelE2eCLI(t, binDir)
  23. t.Run("compose run", func(t *testing.T) {
  24. res := c.RunDockerCmd("compose", "-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.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello one more time")
  29. lines = Lines(res.Stdout())
  30. assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout())
  31. assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
  32. })
  33. t.Run("check run container exited", func(t *testing.T) {
  34. res := c.RunDockerCmd("ps", "--all")
  35. lines := Lines(res.Stdout())
  36. var runContainerID string
  37. var truncatedSlug string
  38. for _, line := range lines {
  39. fields := strings.Fields(line)
  40. containerID := fields[len(fields)-1]
  41. assert.Assert(t, !strings.HasPrefix(containerID, "run-test_front"))
  42. if strings.HasPrefix(containerID, "run-test_back") {
  43. // only the one-off container for back service
  44. assert.Assert(t, strings.HasPrefix(containerID, "run-test_back_run_"), containerID)
  45. truncatedSlug = strings.Replace(containerID, "run-test_back_run_", "", 1)
  46. runContainerID = containerID
  47. }
  48. if strings.HasPrefix(containerID, "run-test-db-1") {
  49. assert.Assert(t, strings.Contains(line, "Up"), line)
  50. }
  51. }
  52. assert.Assert(t, runContainerID != "")
  53. res = c.RunDockerCmd("inspect", runContainerID)
  54. res.Assert(t, icmd.Expected{Out: ` "Status": "exited"`})
  55. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
  56. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "run-test"`})
  57. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "True",`})
  58. res.Assert(t, icmd.Expected{Out: `"com.docker.compose.slug": "` + truncatedSlug})
  59. })
  60. t.Run("compose run --rm", func(t *testing.T) {
  61. res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "--rm", "back", "echo", "Hello again")
  62. lines := Lines(res.Stdout())
  63. assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout())
  64. res = c.RunDockerCmd("ps", "--all")
  65. assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout())
  66. })
  67. t.Run("down", func(t *testing.T) {
  68. c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "down")
  69. res := c.RunDockerCmd("ps", "--all")
  70. assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
  71. })
  72. t.Run("compose run --volumes", func(t *testing.T) {
  73. wd, err := os.Getwd()
  74. assert.NilError(t, err)
  75. res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "--volumes", wd+":/foo", "back", "/bin/sh", "-c", "ls /foo")
  76. res.Assert(t, icmd.Expected{Out: "compose_run_test.go"})
  77. res = c.RunDockerCmd("ps", "--all")
  78. assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout())
  79. })
  80. t.Run("compose run --publish", func(t *testing.T) {
  81. c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "--publish", "8081:80", "-d", "back", "/bin/sh", "-c", "sleep 1")
  82. res := c.RunDockerCmd("ps")
  83. assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
  84. })
  85. t.Run("down", func(t *testing.T) {
  86. c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "down")
  87. res := c.RunDockerCmd("ps", "--all")
  88. assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
  89. })
  90. }