1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

validate service parameter in client side to avoid api call

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2016-09-19 16:40:44 +08:00
parent c8a19aee09
commit 5ba203e73a
2 changed files with 71 additions and 10 deletions

View file

@ -46,9 +46,17 @@ func runScale(dockerCli *command.DockerCli, args []string) error {
var errors []string
for _, arg := range args {
parts := strings.SplitN(arg, "=", 2)
serviceID, scale := parts[0], parts[1]
serviceID, scaleStr := parts[0], parts[1]
// validate input arg scale number
scale, err := strconv.ParseUint(scaleStr, 10, 64)
if err != nil {
errors = append(errors, fmt.Sprintf("%s: invalid replicas value %s: %v", serviceID, scaleStr, err))
continue
}
if err := runServiceScale(dockerCli, serviceID, scale); err != nil {
errors = append(errors, fmt.Sprintf("%s: %s", serviceID, err.Error()))
errors = append(errors, fmt.Sprintf("%s: %v", serviceID, err))
}
}
@ -58,12 +66,11 @@ func runScale(dockerCli *command.DockerCli, args []string) error {
return fmt.Errorf(strings.Join(errors, "\n"))
}
func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale string) error {
func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale uint64) error {
client := dockerCli.Client()
ctx := context.Background()
service, _, err := client.ServiceInspectWithRaw(ctx, serviceID)
if err != nil {
return err
}
@ -72,17 +79,14 @@ func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale strin
if serviceMode.Replicated == nil {
return fmt.Errorf("scale can only be used with replicated mode")
}
uintScale, err := strconv.ParseUint(scale, 10, 64)
if err != nil {
return fmt.Errorf("invalid replicas value %s: %s", scale, err.Error())
}
serviceMode.Replicated.Replicas = &uintScale
serviceMode.Replicated.Replicas = &scale
err = client.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
if err != nil {
return err
}
fmt.Fprintf(dockerCli.Out(), "%s scaled to %s\n", serviceID, scale)
fmt.Fprintf(dockerCli.Out(), "%s scaled to %d\n", serviceID, scale)
return nil
}

View file

@ -0,0 +1,57 @@
// +build !windows
package main
import (
"fmt"
"strings"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
func (s *DockerSwarmSuite) TestServiceScale(c *check.C) {
d := s.AddDaemon(c, true, true)
service1Name := "TestService1"
service1Args := append([]string{"service", "create", "--name", service1Name, defaultSleepImage}, sleepCommandForDaemonPlatform()...)
// global mode
service2Name := "TestService2"
service2Args := append([]string{"service", "create", "--name", service2Name, "--mode=global", defaultSleepImage}, sleepCommandForDaemonPlatform()...)
// Create services
out, err := d.Cmd(service1Args...)
c.Assert(err, checker.IsNil)
out, err = d.Cmd(service2Args...)
c.Assert(err, checker.IsNil)
out, err = d.Cmd("service", "scale", "TestService1=2")
c.Assert(err, checker.IsNil)
out, err = d.Cmd("service", "scale", "TestService1=foobar")
c.Assert(err, checker.NotNil)
str := fmt.Sprintf("%s: invalid replicas value %s", service1Name, "foobar")
if !strings.Contains(out, str) {
c.Errorf("got: %s, expected has sub string: %s", out, str)
}
out, err = d.Cmd("service", "scale", "TestService1=-1")
c.Assert(err, checker.NotNil)
str = fmt.Sprintf("%s: invalid replicas value %s", service1Name, "-1")
if !strings.Contains(out, str) {
c.Errorf("got: %s, expected has sub string: %s", out, str)
}
// TestService2 is a global mode
out, err = d.Cmd("service", "scale", "TestService2=2")
c.Assert(err, checker.NotNil)
str = fmt.Sprintf("%s: scale can only be used with replicated mode\n", service2Name)
if out != str {
c.Errorf("got: %s, expected: %s", out, str)
}
}