|
| 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 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "gotest.tools/v3/assert" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/mod/tigron/expect" |
| 27 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 28 | + "github.com/containerd/nerdctl/mod/tigron/tig" |
| 29 | + |
| 30 | + "github.com/containerd/nerdctl/v2/pkg/composer/serviceparser" |
| 31 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 32 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 33 | +) |
| 34 | + |
| 35 | +func TestComposeScale(t *testing.T) { |
| 36 | + testCase := nerdtest.Setup() |
| 37 | + |
| 38 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 39 | + dockerComposeYAML := fmt.Sprintf(` |
| 40 | +services: |
| 41 | + svc0: |
| 42 | + image: %s |
| 43 | + command: "sleep infinity" |
| 44 | +`, testutil.CommonImage) |
| 45 | + |
| 46 | + composePath := data.Temp().Save(dockerComposeYAML, "compose.yaml") |
| 47 | + projectName := filepath.Base(filepath.Dir(composePath)) |
| 48 | + data.Labels().Set("composeYAML", composePath) |
| 49 | + for _, n := range []string{"1", "2", "3"} { |
| 50 | + data.Labels().Set("container"+n, serviceparser.DefaultContainerName(projectName, "svc0", n)) |
| 51 | + } |
| 52 | + |
| 53 | + helpers.Ensure("compose", "-f", composePath, "up", "-d") |
| 54 | + nerdtest.EnsureContainerStarted(helpers, data.Labels().Get("container1")) |
| 55 | + } |
| 56 | + |
| 57 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 58 | + if data.Labels().Get("composeYAML") != "" { |
| 59 | + helpers.Anyhow("compose", "-f", data.Labels().Get("composeYAML"), "down", "-v") |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + testCase.SubTests = []*test.Case{ |
| 64 | + { |
| 65 | + Description: "rejects an argument without replicas", |
| 66 | + NoParallel: true, |
| 67 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 68 | + return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "scale", "svc0") |
| 69 | + }, |
| 70 | + Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil), |
| 71 | + }, |
| 72 | + { |
| 73 | + Description: "rejects an unknown service", |
| 74 | + NoParallel: true, |
| 75 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 76 | + return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "scale", "unknown=2") |
| 77 | + }, |
| 78 | + Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil), |
| 79 | + }, |
| 80 | + { |
| 81 | + Description: "scales up", |
| 82 | + NoParallel: true, |
| 83 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 84 | + return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "scale", "svc0=3") |
| 85 | + }, |
| 86 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 87 | + return &test.Expected{ |
| 88 | + ExitCode: expect.ExitCodeSuccess, |
| 89 | + Output: func(stdout string, t tig.T) { |
| 90 | + for _, n := range []string{"1", "2", "3"} { |
| 91 | + nerdtest.EnsureContainerStarted(helpers, data.Labels().Get("container"+n)) |
| 92 | + } |
| 93 | + }, |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + Description: "scales down", |
| 99 | + NoParallel: true, |
| 100 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 101 | + // Scaling down must keep the remaining replica as it is, not recreate it. |
| 102 | + data.Labels().Set("container1ID", helpers.Capture("container", "inspect", "--format", "{{.ID}}", data.Labels().Get("container1"))) |
| 103 | + return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "scale", "svc0=1") |
| 104 | + }, |
| 105 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 106 | + return &test.Expected{ |
| 107 | + ExitCode: expect.ExitCodeSuccess, |
| 108 | + Output: func(stdout string, t tig.T) { |
| 109 | + id := helpers.Capture("container", "inspect", "--format", "{{.ID}}", data.Labels().Get("container1")) |
| 110 | + assert.Equal(t, data.Labels().Get("container1ID"), id) |
| 111 | + helpers.Fail("container", "inspect", data.Labels().Get("container2")) |
| 112 | + helpers.Fail("container", "inspect", data.Labels().Get("container3")) |
| 113 | + }, |
| 114 | + } |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + testCase.Run(t) |
| 120 | +} |
0 commit comments