container_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "encoding/json"
  16. "fmt"
  17. "os"
  18. "strings"
  19. "testing"
  20. "time"
  21. "gotest.tools/v3/assert"
  22. "gotest.tools/v3/icmd"
  23. "gotest.tools/v3/poll"
  24. "github.com/docker/compose-cli/cli/cmd"
  25. . "github.com/docker/compose-cli/utils/e2e"
  26. )
  27. var binDir string
  28. func TestMain(m *testing.M) {
  29. p, cleanup, err := SetupExistingCLI()
  30. if err != nil {
  31. fmt.Println(err)
  32. os.Exit(1)
  33. }
  34. binDir = p
  35. exitCode := m.Run()
  36. cleanup()
  37. os.Exit(exitCode)
  38. }
  39. func TestLocalBackendRun(t *testing.T) {
  40. c := NewParallelE2eCLI(t, binDir)
  41. c.RunDockerCmd("context", "create", "local", "test-context").Assert(t, icmd.Success)
  42. c.RunDockerCmd("context", "use", "test-context").Assert(t, icmd.Success)
  43. t.Run("run", func(t *testing.T) {
  44. t.Parallel()
  45. res := c.RunDockerCmd("run", "-d", "nginx")
  46. containerName := strings.TrimSpace(res.Combined())
  47. t.Cleanup(func() {
  48. _ = c.RunDockerOrExitError("rm", "-f", containerName)
  49. })
  50. res = c.RunDockerCmd("inspect", containerName)
  51. res.Assert(t, icmd.Expected{Out: `"Status": "running"`})
  52. })
  53. t.Run("run rm", func(t *testing.T) {
  54. t.Parallel()
  55. res := c.RunDockerCmd("run", "--rm", "-d", "nginx")
  56. containerName := strings.TrimSpace(res.Combined())
  57. t.Cleanup(func() {
  58. _ = c.RunDockerOrExitError("rm", "-f", containerName)
  59. })
  60. _ = c.RunDockerCmd("stop", containerName)
  61. checkRemoved := func(t poll.LogT) poll.Result {
  62. res = c.RunDockerOrExitError("inspect", containerName)
  63. if res.ExitCode == 1 && strings.Contains(res.Stderr(), "No such container") {
  64. return poll.Success()
  65. }
  66. return poll.Continue("waiting for container to be removed")
  67. }
  68. poll.WaitOn(t, checkRemoved, poll.WithDelay(1*time.Second), poll.WithTimeout(10*time.Second))
  69. })
  70. t.Run("run with ports", func(t *testing.T) {
  71. res := c.RunDockerCmd("run", "-d", "-p", "85:80", "nginx")
  72. containerName := strings.TrimSpace(res.Combined())
  73. t.Cleanup(func() {
  74. _ = c.RunDockerOrExitError("rm", "-f", containerName)
  75. })
  76. res = c.RunDockerCmd("inspect", containerName)
  77. inspect := &cmd.ContainerInspectView{}
  78. err := json.Unmarshal([]byte(res.Stdout()), inspect)
  79. assert.NilError(t, err)
  80. assert.Equal(t, inspect.Status, "running")
  81. nginxID := inspect.ID
  82. res = c.RunDockerCmd("ps")
  83. nginxFound := false
  84. lines := Lines(res.Stdout())
  85. for _, line := range lines {
  86. fields := strings.Fields(line)
  87. if fields[0] == nginxID {
  88. nginxFound = true
  89. assert.Equal(t, fields[1], "nginx")
  90. assert.Equal(t, fields[2], "/docker-entrypoint.sh")
  91. assert.Equal(t, fields[len(fields)-1], "0.0.0.0:85->80/tcp")
  92. }
  93. }
  94. assert.Assert(t, nginxFound, res.Stdout())
  95. res = c.RunDockerCmd("ps", "--format", "json")
  96. res.Assert(t, icmd.Expected{Out: `"Image":"nginx","Status":"Up Less than a second","Command":"/docker-entrypoint.sh nginx -g 'daemon off;'","Ports":["0.0.0.0:85->80/tcp"`})
  97. res = c.RunDockerCmd("ps", "--quiet")
  98. res.Assert(t, icmd.Expected{Out: nginxID + "\n"})
  99. })
  100. t.Run("run with volume", func(t *testing.T) {
  101. t.Parallel()
  102. t.Cleanup(func() {
  103. _ = c.RunDockerOrExitError("volume", "rm", "local-test")
  104. })
  105. c.RunDockerCmd("volume", "create", "local-test")
  106. c.RunDockerCmd("run", "--rm", "-d", "--volume", "local-test:/data", "alpine", "sh", "-c", `echo "testdata" > /data/test`)
  107. // FIXME: Remove sleep when race to attach to dead container is fixed
  108. res := c.RunDockerOrExitError("run", "--rm", "--volume", "local-test:/data", "alpine", "sh", "-c", "cat /data/test && sleep 1")
  109. res.Assert(t, icmd.Expected{Out: "testdata"})
  110. })
  111. t.Run("inspect not found", func(t *testing.T) {
  112. t.Parallel()
  113. res := c.RunDockerOrExitError("inspect", "nonexistentcontainer")
  114. res.Assert(t, icmd.Expected{
  115. ExitCode: 1,
  116. Err: "Error: No such container: nonexistentcontainer",
  117. })
  118. })
  119. }
  120. func TestLocalBackendVolumes(t *testing.T) {
  121. c := NewParallelE2eCLI(t, binDir)
  122. c.RunDockerCmd("context", "create", "local", "test-context").Assert(t, icmd.Success)
  123. c.RunDockerCmd("context", "use", "test-context").Assert(t, icmd.Success)
  124. t.Run("volume crud", func(t *testing.T) {
  125. t.Parallel()
  126. name := "crud"
  127. t.Cleanup(func() {
  128. _ = c.RunDockerOrExitError("volume", "rm", name)
  129. })
  130. res := c.RunDockerCmd("volume", "create", name)
  131. res.Assert(t, icmd.Expected{Out: name})
  132. res = c.RunDockerCmd("volume", "ls")
  133. res.Assert(t, icmd.Expected{Out: name})
  134. res = c.RunDockerCmd("volume", "inspect", name)
  135. res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`"ID": "%s"`, name)})
  136. res = c.RunDockerCmd("volume", "rm", name)
  137. res.Assert(t, icmd.Expected{Out: name})
  138. res = c.RunDockerOrExitError("volume", "inspect", name)
  139. res.Assert(t, icmd.Expected{ExitCode: 1})
  140. })
  141. }