|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestPs(t *testing.T) { |
| 28 | + c := NewParallelE2eCLI(t, binDir) |
| 29 | + const projectName = "e2e-ps" |
| 30 | + |
| 31 | + res := c.RunDockerComposeCmd("-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "up", "-d") |
| 32 | + if assert.NoError(t, res.Error) { |
| 33 | + t.Cleanup(func() { |
| 34 | + _ = c.RunDockerComposeCmd("--project-name", projectName, "down") |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + assert.Contains(t, res.Combined(), "Container e2e-ps-busybox-1 Started", res.Combined()) |
| 39 | + |
| 40 | + t.Run("pretty", func(t *testing.T) { |
| 41 | + res = c.RunDockerComposeCmd("-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps") |
| 42 | + lines := strings.Split(res.Combined(), "\n") |
| 43 | + assert.Equal(t, 4, len(lines)) |
| 44 | + count := 0 |
| 45 | + for _, line := range lines[1:3] { |
| 46 | + if strings.Contains(line, "e2e-ps-busybox-1") { |
| 47 | + assert.True(t, strings.Contains(line, "127.0.0.1:8001->8000/tcp")) |
| 48 | + count++ |
| 49 | + } |
| 50 | + if strings.Contains(line, "e2e-ps-nginx-1") { |
| 51 | + assert.True(t, strings.Contains(line, "80/tcp, 443/tcp, 8080/tcp")) |
| 52 | + count++ |
| 53 | + } |
| 54 | + } |
| 55 | + assert.Equal(t, 2, count, "Did not match both services:\n"+res.Combined()) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("json", func(t *testing.T) { |
| 59 | + res = c.RunDockerComposeCmd("-f", "./fixtures/ps-test/compose.yaml", "--project-name", projectName, "ps", "--format", "json") |
| 60 | + var output []map[string]interface{} |
| 61 | + err := json.Unmarshal([]byte(res.Combined()), &output) |
| 62 | + assert.NoError(t, err) |
| 63 | + |
| 64 | + count := 0 |
| 65 | + assert.Equal(t, 2, len(output)) |
| 66 | + for _, service := range output { |
| 67 | + publishers := service["Publishers"].([]interface{}) |
| 68 | + if service["Name"] == "e2e-ps-busybox-1" { |
| 69 | + assert.Equal(t, 1, len(publishers)) |
| 70 | + publisher := publishers[0].(map[string]interface{}) |
| 71 | + assert.Equal(t, "127.0.0.1", publisher["URL"]) |
| 72 | + assert.Equal(t, 8000.0, publisher["TargetPort"]) |
| 73 | + assert.Equal(t, 8001.0, publisher["PublishedPort"]) |
| 74 | + assert.Equal(t, "tcp", publisher["Protocol"]) |
| 75 | + count++ |
| 76 | + } |
| 77 | + if service["Name"] == "e2e-ps-nginx-1" { |
| 78 | + assert.Equal(t, 3, len(publishers)) |
| 79 | + publisher := publishers[0].(map[string]interface{}) |
| 80 | + assert.Equal(t, 80.0, publisher["TargetPort"]) |
| 81 | + assert.Equal(t, 0.0, publisher["PublishedPort"]) |
| 82 | + assert.Equal(t, "tcp", publisher["Protocol"]) |
| 83 | + publisher = publishers[1].(map[string]interface{}) |
| 84 | + assert.Equal(t, 443.0, publisher["TargetPort"]) |
| 85 | + assert.Equal(t, 0.0, publisher["PublishedPort"]) |
| 86 | + assert.Equal(t, "tcp", publisher["Protocol"]) |
| 87 | + publisher = publishers[2].(map[string]interface{}) |
| 88 | + assert.Equal(t, 8080.0, publisher["TargetPort"]) |
| 89 | + assert.Equal(t, 0.0, publisher["PublishedPort"]) |
| 90 | + assert.Equal(t, "tcp", publisher["Protocol"]) |
| 91 | + count++ |
| 92 | + } |
| 93 | + } |
| 94 | + assert.Equal(t, 2, count, "Did not match both services:\n"+res.Combined()) |
| 95 | + }) |
| 96 | +} |
0 commit comments