|
| 1 | +/* |
| 2 | + Copyright The containerd 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 compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/containerd/nerdctl/mod/tigron/expect" |
| 24 | + "github.com/containerd/nerdctl/mod/tigron/require" |
| 25 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 26 | + |
| 27 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 28 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 29 | +) |
| 30 | + |
| 31 | +func TestComposeStats(t *testing.T) { |
| 32 | + var dockerComposeYAML = fmt.Sprintf(` |
| 33 | +services: |
| 34 | + svc0: |
| 35 | + image: %s |
| 36 | + command: "sleep infinity" |
| 37 | + svc1: |
| 38 | + image: %s |
| 39 | + command: "sleep infinity" |
| 40 | +`, testutil.CommonImage, testutil.CommonImage) |
| 41 | + |
| 42 | + testCase := nerdtest.Setup() |
| 43 | + |
| 44 | + testCase.Require = require.All(nerdtest.CgroupsAccessible) |
| 45 | + |
| 46 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 47 | + data.Temp().Save(dockerComposeYAML, "compose.yaml") |
| 48 | + data.Labels().Set("yamlPath", data.Temp().Path("compose.yaml")) |
| 49 | + data.Labels().Set("project", data.Identifier()) |
| 50 | + helpers.Ensure("compose", "-f", data.Temp().Path("compose.yaml"), "-p", data.Identifier(), "up", "-d") |
| 51 | + } |
| 52 | + |
| 53 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 54 | + helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "-p", data.Identifier(), "down") |
| 55 | + } |
| 56 | + |
| 57 | + compose := func(data test.Data, helpers test.Helpers, args ...string) test.TestableCommand { |
| 58 | + return helpers.Command(append([]string{"compose", "-f", data.Labels().Get("yamlPath"), "-p", data.Labels().Get("project")}, args...)...) |
| 59 | + } |
| 60 | + |
| 61 | + testCase.SubTests = []*test.Case{ |
| 62 | + { |
| 63 | + Description: "all services", |
| 64 | + NoParallel: true, |
| 65 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 66 | + return compose(data, helpers, "stats", "--no-stream", "--no-trunc", "--format", "{{.Name}}") |
| 67 | + }, |
| 68 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 69 | + return &test.Expected{ |
| 70 | + Output: expect.All( |
| 71 | + expect.Contains(data.Labels().Get("project")+"-svc0-1"), |
| 72 | + expect.Contains(data.Labels().Get("project")+"-svc1-1"), |
| 73 | + ), |
| 74 | + } |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + Description: "one service", |
| 79 | + NoParallel: true, |
| 80 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 81 | + return compose(data, helpers, "stats", "--no-stream", "--no-trunc", "--format", "{{.Name}}", "svc0") |
| 82 | + }, |
| 83 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 84 | + return &test.Expected{ |
| 85 | + Output: expect.All( |
| 86 | + expect.Contains(data.Labels().Get("project")+"-svc0-1"), |
| 87 | + expect.DoesNotContain(data.Labels().Get("project")+"-svc1-1"), |
| 88 | + ), |
| 89 | + } |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + Description: "stopped service is skipped", |
| 94 | + NoParallel: true, |
| 95 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 96 | + helpers.Ensure("compose", "-f", data.Labels().Get("yamlPath"), "-p", data.Labels().Get("project"), "stop", "svc1") |
| 97 | + }, |
| 98 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 99 | + return compose(data, helpers, "stats", "--no-stream", "--no-trunc", "--format", "{{.Name}}", "svc1") |
| 100 | + }, |
| 101 | + Expected: test.Expects(0, nil, expect.Equals("")), |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + testCase.Run(t) |
| 106 | +} |
0 commit comments