ps_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "strings"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/stretchr/testify/require"
  20. "gotest.tools/v3/icmd"
  21. "github.com/docker/compose/v2/pkg/api"
  22. )
  23. func TestPs(t *testing.T) {
  24. c := NewParallelCLI(t)
  25. const projectName = "e2e-ps"
  26. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "up", "-d")
  27. if assert.NoError(t, res.Error) {
  28. t.Cleanup(func() {
  29. _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  30. })
  31. }
  32. assert.Contains(t, res.Combined(), "Container e2e-ps-busybox-1 Started", res.Combined())
  33. t.Run("table", func(t *testing.T) {
  34. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps")
  35. lines := strings.Split(res.Stdout(), "\n")
  36. assert.Equal(t, 4, len(lines))
  37. count := 0
  38. for _, line := range lines[1:3] {
  39. if strings.Contains(line, "e2e-ps-busybox-1") {
  40. assert.True(t, strings.Contains(line, "127.0.0.1:8001->8000/tcp"))
  41. count++
  42. }
  43. if strings.Contains(line, "e2e-ps-nginx-1") {
  44. assert.True(t, strings.Contains(line, "80/tcp, 443/tcp, 8080/tcp"))
  45. count++
  46. }
  47. }
  48. assert.Equal(t, 2, count, "Did not match both services:\n"+res.Combined())
  49. })
  50. t.Run("json", func(t *testing.T) {
  51. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps",
  52. "--format", "json")
  53. type element struct {
  54. Name string
  55. Publishers api.PortPublishers
  56. }
  57. var output []element
  58. out := res.Stdout()
  59. dec := json.NewDecoder(strings.NewReader(out))
  60. for dec.More() {
  61. var s element
  62. require.NoError(t, dec.Decode(&s), "Failed to unmarshal ps JSON output")
  63. output = append(output, s)
  64. }
  65. count := 0
  66. assert.Equal(t, 2, len(output))
  67. for _, service := range output {
  68. publishers := service.Publishers
  69. if service.Name == "e2e-ps-busybox-1" {
  70. assert.Equal(t, 1, len(publishers))
  71. assert.Equal(t, api.PortPublishers{
  72. {
  73. URL: "127.0.0.1",
  74. TargetPort: 8000,
  75. PublishedPort: 8001,
  76. Protocol: "tcp",
  77. },
  78. }, publishers)
  79. count++
  80. }
  81. if service.Name == "e2e-ps-nginx-1" {
  82. assert.Equal(t, 3, len(publishers))
  83. assert.Equal(t, api.PortPublishers{
  84. {TargetPort: 80, Protocol: "tcp"},
  85. {TargetPort: 443, Protocol: "tcp"},
  86. {TargetPort: 8080, Protocol: "tcp"},
  87. }, publishers)
  88. count++
  89. }
  90. }
  91. assert.Equal(t, 2, count, "Did not match both services:\n"+res.Combined())
  92. })
  93. t.Run("ps --all", func(t *testing.T) {
  94. res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
  95. assert.NoError(t, res.Error)
  96. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps")
  97. lines := strings.Split(res.Stdout(), "\n")
  98. assert.Equal(t, 2, len(lines))
  99. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "--all")
  100. lines = strings.Split(res.Stdout(), "\n")
  101. assert.Equal(t, 4, len(lines))
  102. })
  103. t.Run("ps unknown", func(t *testing.T) {
  104. res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
  105. assert.NoError(t, res.Error)
  106. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "nginx")
  107. res.Assert(t, icmd.Success)
  108. res = c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "unknown")
  109. res.Assert(t, icmd.Expected{ExitCode: 1, Err: "no such service: unknown"})
  110. })
  111. }