backend_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "os"
  17. "strings"
  18. "testing"
  19. "time"
  20. "gotest.tools/v3/assert"
  21. "gotest.tools/v3/assert/cmp"
  22. "gotest.tools/v3/icmd"
  23. "gotest.tools/v3/poll"
  24. . "github.com/docker/compose-cli/tests/framework"
  25. )
  26. var binDir string
  27. func TestMain(m *testing.M) {
  28. p, cleanup, err := SetupExistingCLI()
  29. if err != nil {
  30. fmt.Println(err)
  31. os.Exit(1)
  32. }
  33. binDir = p
  34. exitCode := m.Run()
  35. cleanup()
  36. os.Exit(exitCode)
  37. }
  38. func TestLocalBackendRun(t *testing.T) {
  39. c := NewParallelE2eCLI(t, binDir)
  40. c.RunDockerCmd("context", "create", "local", "test-context").Assert(t, icmd.Success)
  41. c.RunDockerCmd("context", "use", "test-context").Assert(t, icmd.Success)
  42. t.Run("run", func(t *testing.T) {
  43. t.Parallel()
  44. res := c.RunDockerCmd("run", "-d", "nginx")
  45. containerName := strings.TrimSpace(res.Combined())
  46. t.Cleanup(func() {
  47. _ = c.RunDockerOrExitError("rm", "-f", containerName)
  48. })
  49. res = c.RunDockerCmd("inspect", containerName)
  50. res.Assert(t, icmd.Expected{Out: `"Status": "running"`})
  51. })
  52. t.Run("run rm", func(t *testing.T) {
  53. t.Parallel()
  54. res := c.RunDockerCmd("run", "--rm", "-d", "nginx")
  55. containerName := strings.TrimSpace(res.Combined())
  56. t.Cleanup(func() {
  57. _ = c.RunDockerOrExitError("rm", "-f", containerName)
  58. })
  59. _ = c.RunDockerCmd("stop", containerName)
  60. checkRemoved := func(t poll.LogT) poll.Result {
  61. res = c.RunDockerOrExitError("inspect", containerName)
  62. if res.ExitCode == 1 && strings.Contains(res.Stderr(), "No such container") {
  63. return poll.Success()
  64. }
  65. return poll.Continue("waiting for container to be removed")
  66. }
  67. poll.WaitOn(t, checkRemoved, poll.WithDelay(1*time.Second), poll.WithTimeout(10*time.Second))
  68. })
  69. t.Run("run with ports", func(t *testing.T) {
  70. res := c.RunDockerCmd("run", "-d", "-p", "80", "nginx")
  71. containerName := strings.TrimSpace(res.Combined())
  72. t.Cleanup(func() {
  73. _ = c.RunDockerOrExitError("rm", "-f", containerName)
  74. })
  75. res = c.RunDockerCmd("inspect", containerName)
  76. res.Assert(t, icmd.Expected{Out: `"Status": "running"`})
  77. res = c.RunDockerCmd("ps")
  78. assert.Assert(t, cmp.Regexp(`0\.0\.0\.0:\d*->80/tcp`, res.Stdout()))
  79. })
  80. t.Run("run with volume", func(t *testing.T) {
  81. t.Parallel()
  82. t.Cleanup(func() {
  83. _ = c.RunDockerOrExitError("volume", "rm", "local-test")
  84. })
  85. c.RunDockerCmd("volume", "create", "local-test")
  86. c.RunDockerCmd("run", "--rm", "-d", "--volume", "local-test:/data", "alpine", "sh", "-c", `echo "testdata" > /data/test`)
  87. // FIXME: Remove sleep when race to attach to dead container is fixed
  88. res := c.RunDockerOrExitError("run", "--rm", "--volume", "local-test:/data", "alpine", "sh", "-c", "cat /data/test && sleep 1")
  89. res.Assert(t, icmd.Expected{Out: "testdata"})
  90. })
  91. t.Run("inspect not found", func(t *testing.T) {
  92. t.Parallel()
  93. res := c.RunDockerOrExitError("inspect", "nonexistentcontainer")
  94. res.Assert(t, icmd.Expected{
  95. ExitCode: 1,
  96. Err: "Error: No such container: nonexistentcontainer",
  97. })
  98. })
  99. }
  100. func TestLocalBackendVolumes(t *testing.T) {
  101. c := NewParallelE2eCLI(t, binDir)
  102. c.RunDockerCmd("context", "create", "local", "test-context").Assert(t, icmd.Success)
  103. c.RunDockerCmd("context", "use", "test-context").Assert(t, icmd.Success)
  104. t.Run("volume crud", func(t *testing.T) {
  105. t.Parallel()
  106. name := "crud"
  107. t.Cleanup(func() {
  108. _ = c.RunDockerOrExitError("volume", "rm", name)
  109. })
  110. res := c.RunDockerCmd("volume", "create", name)
  111. res.Assert(t, icmd.Expected{Out: name})
  112. res = c.RunDockerCmd("volume", "ls")
  113. res.Assert(t, icmd.Expected{Out: name})
  114. res = c.RunDockerCmd("volume", "inspect", name)
  115. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`"ID": "%s"`, name)})
  116. res = c.RunDockerCmd("volume", "rm", name)
  117. res.Assert(t, icmd.Expected{Out: name})
  118. res = c.RunDockerOrExitError("volume", "inspect", name)
  119. res.Assert(t, icmd.Expected{ExitCode: 1})
  120. })
  121. }