2022-06-08 20:56:13 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-06-16 16:04:23 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-12-21 09:23:44 +01:00
|
|
|
"gotest.tools/v3/icmd"
|
2022-06-08 22:13:31 +02:00
|
|
|
|
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
2022-06-08 20:56:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPs(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2022-06-08 20:56:13 +02:00
|
|
|
const projectName = "e2e-ps"
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "up", "-d")
|
2024-04-24 10:25:39 +02:00
|
|
|
require.NoError(t, res.Error)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
|
|
|
})
|
2022-06-08 20:56:13 +02:00
|
|
|
|
|
|
|
assert.Contains(t, res.Combined(), "Container e2e-ps-busybox-1 Started", res.Combined())
|
|
|
|
|
2022-12-11 10:59:01 +01:00
|
|
|
t.Run("table", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps")
|
2022-06-16 16:04:23 +02:00
|
|
|
lines := strings.Split(res.Stdout(), "\n")
|
2024-04-24 10:25:39 +02:00
|
|
|
assert.Len(t, lines, 4)
|
2022-06-08 20:56:13 +02:00
|
|
|
count := 0
|
|
|
|
for _, line := range lines[1:3] {
|
|
|
|
if strings.Contains(line, "e2e-ps-busybox-1") {
|
|
|
|
assert.True(t, strings.Contains(line, "127.0.0.1:8001->8000/tcp"))
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
if strings.Contains(line, "e2e-ps-nginx-1") {
|
|
|
|
assert.True(t, strings.Contains(line, "80/tcp, 443/tcp, 8080/tcp"))
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, 2, count, "Did not match both services:\n"+res.Combined())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps",
|
|
|
|
"--format", "json")
|
2023-08-24 09:08:32 +02:00
|
|
|
type element struct {
|
|
|
|
Name string
|
2023-11-24 10:19:34 +01:00
|
|
|
Project string
|
2023-08-24 09:08:32 +02:00
|
|
|
Publishers api.PortPublishers
|
|
|
|
}
|
|
|
|
var output []element
|
|
|
|
out := res.Stdout()
|
|
|
|
dec := json.NewDecoder(strings.NewReader(out))
|
2023-08-22 09:26:09 +02:00
|
|
|
for dec.More() {
|
2023-08-24 09:08:32 +02:00
|
|
|
var s element
|
2023-08-22 09:26:09 +02:00
|
|
|
require.NoError(t, dec.Decode(&s), "Failed to unmarshal ps JSON output")
|
|
|
|
output = append(output, s)
|
|
|
|
}
|
2022-06-08 20:56:13 +02:00
|
|
|
|
|
|
|
count := 0
|
2024-04-24 10:25:39 +02:00
|
|
|
assert.Len(t, output, 2)
|
2022-06-08 20:56:13 +02:00
|
|
|
for _, service := range output {
|
2023-11-24 10:19:34 +01:00
|
|
|
assert.Equal(t, projectName, service.Project)
|
2022-06-08 22:13:31 +02:00
|
|
|
publishers := service.Publishers
|
|
|
|
if service.Name == "e2e-ps-busybox-1" {
|
2024-04-24 10:25:39 +02:00
|
|
|
assert.Len(t, publishers, 1)
|
2022-06-08 22:13:31 +02:00
|
|
|
assert.Equal(t, api.PortPublishers{
|
|
|
|
{
|
|
|
|
URL: "127.0.0.1",
|
|
|
|
TargetPort: 8000,
|
|
|
|
PublishedPort: 8001,
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
}, publishers)
|
2022-06-08 20:56:13 +02:00
|
|
|
count++
|
|
|
|
}
|
2022-06-08 22:13:31 +02:00
|
|
|
if service.Name == "e2e-ps-nginx-1" {
|
2024-04-24 10:25:39 +02:00
|
|
|
assert.Len(t, publishers, 3)
|
2022-06-08 22:13:31 +02:00
|
|
|
assert.Equal(t, api.PortPublishers{
|
|
|
|
{TargetPort: 80, Protocol: "tcp"},
|
|
|
|
{TargetPort: 443, Protocol: "tcp"},
|
|
|
|
{TargetPort: 8080, Protocol: "tcp"},
|
|
|
|
}, publishers)
|
|
|
|
|
2022-06-08 20:56:13 +02:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, 2, count, "Did not match both services:\n"+res.Combined())
|
|
|
|
})
|
2022-12-16 09:06:07 +01:00
|
|
|
|
|
|
|
t.Run("ps --all", func(t *testing.T) {
|
|
|
|
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
|
2024-04-24 10:25:39 +02:00
|
|
|
require.NoError(t, res.Error)
|
2022-12-16 09:06:07 +01:00
|
|
|
|
|
|
|
res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps")
|
|
|
|
lines := strings.Split(res.Stdout(), "\n")
|
2024-04-24 10:25:39 +02:00
|
|
|
assert.Len(t, lines, 2)
|
2022-12-16 09:06:07 +01:00
|
|
|
|
|
|
|
res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "--all")
|
|
|
|
lines = strings.Split(res.Stdout(), "\n")
|
2024-04-24 10:25:39 +02:00
|
|
|
assert.Len(t, lines, 4)
|
2022-12-16 09:06:07 +01:00
|
|
|
})
|
|
|
|
|
2022-12-21 09:23:44 +01:00
|
|
|
t.Run("ps unknown", func(t *testing.T) {
|
|
|
|
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
|
2024-04-24 10:25:39 +02:00
|
|
|
require.NoError(t, res.Error)
|
2022-12-21 09:23:44 +01:00
|
|
|
|
|
|
|
res = c.RunDockerComposeCmd(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "nginx")
|
|
|
|
res.Assert(t, icmd.Success)
|
|
|
|
|
|
|
|
res = c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "unknown")
|
|
|
|
res.Assert(t, icmd.Expected{ExitCode: 1, Err: "no such service: unknown"})
|
|
|
|
})
|
2022-06-08 20:56:13 +02:00
|
|
|
}
|