github.com/wozhu6104/docker@v20.10.10+incompatible/integration-cli/docker_cli_service_scale_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	"gotest.tools/v3/assert"
    11  )
    12  
    13  func (s *DockerSwarmSuite) TestServiceScale(c *testing.T) {
    14  	d := s.AddDaemon(c, true, true)
    15  
    16  	service1Name := "TestService1"
    17  	service1Args := append([]string{"service", "create", "--detach", "--no-resolve-image", "--name", service1Name, "busybox"}, sleepCommandForDaemonPlatform()...)
    18  
    19  	// global mode
    20  	service2Name := "TestService2"
    21  	service2Args := append([]string{"service", "create", "--detach", "--no-resolve-image", "--name", service2Name, "--mode=global", "busybox"}, sleepCommandForDaemonPlatform()...)
    22  
    23  	// Create services
    24  	_, err := d.Cmd(service1Args...)
    25  	assert.NilError(c, err)
    26  
    27  	_, err = d.Cmd(service2Args...)
    28  	assert.NilError(c, err)
    29  
    30  	_, err = d.Cmd("service", "scale", "TestService1=2")
    31  	assert.NilError(c, err)
    32  
    33  	out, err := d.Cmd("service", "scale", "TestService1=foobar")
    34  	assert.ErrorContains(c, err, "")
    35  
    36  	str := fmt.Sprintf("%s: invalid replicas value %s", service1Name, "foobar")
    37  	if !strings.Contains(out, str) {
    38  		c.Errorf("got: %s, expected has sub string: %s", out, str)
    39  	}
    40  
    41  	out, err = d.Cmd("service", "scale", "TestService1=-1")
    42  	assert.ErrorContains(c, err, "")
    43  
    44  	str = fmt.Sprintf("%s: invalid replicas value %s", service1Name, "-1")
    45  	if !strings.Contains(out, str) {
    46  		c.Errorf("got: %s, expected has sub string: %s", out, str)
    47  	}
    48  
    49  	// TestService2 is a global mode
    50  	out, err = d.Cmd("service", "scale", "TestService2=2")
    51  	assert.ErrorContains(c, err, "")
    52  
    53  	str = fmt.Sprintf("%s: scale can only be used with replicated mode\n", service2Name)
    54  	if out != str {
    55  		c.Errorf("got: %s, expected: %s", out, str)
    56  	}
    57  }