Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit bbdced6

Browse files
committed
test case
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 9601aa5 commit bbdced6

File tree

2 files changed

+67
-19
lines changed

2 files changed

+67
-19
lines changed

cli/cmd/compose/up.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func runUp(ctx context.Context, opts upOptions, services []string) error {
123123
return err
124124
}
125125

126-
err = applyScale(opts.scale, project)
126+
err = applyScaleOpt(opts.scale, project)
127127
if err != nil {
128128
return err
129129
}
@@ -142,7 +142,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
142142
return err
143143
}
144144

145-
err = applyScale(opts.scale, project)
145+
err = applyScaleOpt(opts.scale, project)
146146
if err != nil {
147147
return err
148148
}
@@ -215,8 +215,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
215215
return err
216216
}
217217

218-
func applyScale(opts []string, project *types.Project) error {
219-
SCALE:
218+
func applyScaleOpt(opts []string, project *types.Project) error {
220219
for _, scale := range opts {
221220
split := strings.Split(scale, "=")
222221
if len(split) != 2 {
@@ -227,26 +226,33 @@ SCALE:
227226
if err != nil {
228227
return err
229228
}
230-
for i, s := range project.Services {
231-
if s.Name == name {
232-
service, err := project.GetService(name)
233-
if err != nil {
234-
return err
235-
}
236-
if service.Deploy == nil {
237-
service.Deploy = &types.DeployConfig{}
238-
}
239-
count := uint64(replicas)
240-
service.Deploy.Replicas = &count
241-
project.Services[i] = service
242-
continue SCALE
243-
}
229+
err = setServiceScale(project, name, replicas)
230+
if err != nil {
231+
return err
244232
}
245-
return fmt.Errorf("unknown service %q", name)
246233
}
247234
return nil
248235
}
249236

237+
func setServiceScale(project *types.Project, name string, replicas int) error {
238+
for i, s := range project.Services {
239+
if s.Name == name {
240+
service, err := project.GetService(name)
241+
if err != nil {
242+
return err
243+
}
244+
if service.Deploy == nil {
245+
service.Deploy = &types.DeployConfig{}
246+
}
247+
count := uint64(replicas)
248+
service.Deploy.Replicas = &count
249+
project.Services[i] = service
250+
return nil
251+
}
252+
}
253+
return fmt.Errorf("unknown service %q", name)
254+
}
255+
250256
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
251257
c, err := client.NewWithDefaultLocalBackend(ctx)
252258
if err != nil {

cli/cmd/compose/up_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 compose
18+
19+
import (
20+
"testing"
21+
22+
"github.com/compose-spec/compose-go/types"
23+
"gotest.tools/v3/assert"
24+
)
25+
26+
func TestApplyScaleOpt(t *testing.T) {
27+
p := types.Project{
28+
Services: []types.ServiceConfig{
29+
{
30+
Name: "foo",
31+
},
32+
{
33+
Name: "bar",
34+
},
35+
},
36+
}
37+
err := applyScaleOpt([]string{"foo=2"}, &p)
38+
assert.NilError(t, err)
39+
foo, err := p.GetService("foo")
40+
assert.NilError(t, err)
41+
assert.Check(t, *foo.Deploy.Replicas == 2)
42+
}

0 commit comments

Comments
 (0)