docker_cli_service_scale_test.go 1.6 KB

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