ps_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. Project string
  56. Publishers api.PortPublishers
  57. }
  58. var output []element
  59. out := res.Stdout()
  60. dec := json.NewDecoder(strings.NewReader(out))
  61. for dec.More() {
  62. var s element
  63. require.NoError(t, dec.Decode(&s), "Failed to unmarshal ps JSON output")
  64. output = append(output, s)
  65. }
  66. count := 0
  67. assert.Equal(t, 2, len(output))
  68. for _, service := range output {
  69. assert.Equal(t, projectName, service.Project)
  70. publishers := service.Publishers
  71. if service.Name == "e2e-ps-busybox-1" {
  72. assert.Equal(t, 1, len(publishers))
  73. assert.Equal(t, api.PortPublishers{
  74. {
  75. URL: "127.0.0.1",
  76. TargetPort: 8000,
  77. PublishedPort: 8001,
  78. Protocol: "tcp",
  79. },
  80. }, publishers)
  81. count++
  82. }
  83. if service.Name == "e2e-ps-nginx-1" {
  84. assert.Equal(t, 3, len(publishers))
  85. assert.Equal(t, api.PortPublishers{
  86. {TargetPort: 80, Protocol: "tcp"},
  87. {TargetPort: 443, Protocol: "tcp"},
  88. {TargetPort: 8080, Protocol: "tcp"},
  89. }, publishers)
  90. count++
  91. }
  92. }
  93. assert.Equal(t, 2, count, "Did not match both services:\n"+res.Combined())
  94. })
  95. t.Run("ps --all", func(t *testing.T) {
  96. res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
  97. assert.NoError(t, res.Error)
  98. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps")
  99. lines := strings.Split(res.Stdout(), "\n")
  100. assert.Equal(t, 2, len(lines))
  101. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "--all")
  102. lines = strings.Split(res.Stdout(), "\n")
  103. assert.Equal(t, 4, len(lines))
  104. })
  105. t.Run("ps unknown", func(t *testing.T) {
  106. res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
  107. assert.NoError(t, res.Error)
  108. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "nginx")
  109. res.Assert(t, icmd.Success)
  110. res = c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "unknown")
  111. res.Assert(t, icmd.Expected{ExitCode: 1, Err: "no such service: unknown"})
  112. })
  113. }