mirror of https://github.com/docker/compose.git
test case
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
9601aa5af3
commit
bbdced6f0b
|
@ -123,7 +123,7 @@ func runUp(ctx context.Context, opts upOptions, services []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = applyScale(opts.scale, project)
|
err = applyScaleOpt(opts.scale, project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -142,7 +142,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = applyScale(opts.scale, project)
|
err = applyScaleOpt(opts.scale, project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -215,8 +215,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyScale(opts []string, project *types.Project) error {
|
func applyScaleOpt(opts []string, project *types.Project) error {
|
||||||
SCALE:
|
|
||||||
for _, scale := range opts {
|
for _, scale := range opts {
|
||||||
split := strings.Split(scale, "=")
|
split := strings.Split(scale, "=")
|
||||||
if len(split) != 2 {
|
if len(split) != 2 {
|
||||||
|
@ -227,26 +226,33 @@ SCALE:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for i, s := range project.Services {
|
err = setServiceScale(project, name, replicas)
|
||||||
if s.Name == name {
|
if err != nil {
|
||||||
service, err := project.GetService(name)
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if service.Deploy == nil {
|
|
||||||
service.Deploy = &types.DeployConfig{}
|
|
||||||
}
|
|
||||||
count := uint64(replicas)
|
|
||||||
service.Deploy.Replicas = &count
|
|
||||||
project.Services[i] = service
|
|
||||||
continue SCALE
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return fmt.Errorf("unknown service %q", name)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setServiceScale(project *types.Project, name string, replicas int) error {
|
||||||
|
for i, s := range project.Services {
|
||||||
|
if s.Name == name {
|
||||||
|
service, err := project.GetService(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if service.Deploy == nil {
|
||||||
|
service.Deploy = &types.DeployConfig{}
|
||||||
|
}
|
||||||
|
count := uint64(replicas)
|
||||||
|
service.Deploy.Replicas = &count
|
||||||
|
project.Services[i] = service
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("unknown service %q", name)
|
||||||
|
}
|
||||||
|
|
||||||
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
|
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
|
||||||
c, err := client.NewWithDefaultLocalBackend(ctx)
|
c, err := client.NewWithDefaultLocalBackend(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
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 compose
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/compose-spec/compose-go/types"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestApplyScaleOpt(t *testing.T) {
|
||||||
|
p := types.Project{
|
||||||
|
Services: []types.ServiceConfig{
|
||||||
|
{
|
||||||
|
Name: "foo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := applyScaleOpt([]string{"foo=2"}, &p)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
foo, err := p.GetService("foo")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Check(t, *foo.Deploy.Replicas == 2)
|
||||||
|
}
|
Loading…
Reference in New Issue