ps_test.go 3.9 KB

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