scale_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2e
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. testify "github.com/stretchr/testify/assert"
  19. "gotest.tools/v3/assert"
  20. "gotest.tools/v3/icmd"
  21. )
  22. const NO_STATE_TO_CHECK = ""
  23. func TestScaleBasicCases(t *testing.T) {
  24. c := NewCLI(t, WithEnv(
  25. "COMPOSE_PROJECT_NAME=scale-basic-tests"))
  26. reset := func() {
  27. c.RunDockerComposeCmd(t, "down", "--rmi", "all")
  28. }
  29. t.Cleanup(reset)
  30. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "up", "-d")
  31. res.Assert(t, icmd.Success)
  32. t.Log("scale up one service")
  33. res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "dbadmin=2")
  34. checkServiceContainer(t, res.Combined(), "scale-basic-tests-dbadmin", "Started", 2)
  35. t.Log("scale up 2 services")
  36. res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "front=3", "back=2")
  37. checkServiceContainer(t, res.Combined(), "scale-basic-tests-front", "Running", 2)
  38. checkServiceContainer(t, res.Combined(), "scale-basic-tests-front", "Started", 1)
  39. checkServiceContainer(t, res.Combined(), "scale-basic-tests-back", "Running", 1)
  40. checkServiceContainer(t, res.Combined(), "scale-basic-tests-back", "Started", 1)
  41. t.Log("scale down one service")
  42. res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "dbadmin=1")
  43. checkServiceContainer(t, res.Combined(), "scale-basic-tests-dbadmin", "Running", 1)
  44. t.Log("scale to 0 a service")
  45. res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "dbadmin=0")
  46. assert.Check(t, res.Stdout() == "", res.Stdout())
  47. t.Log("scale down 2 services")
  48. res = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "front=2", "back=1")
  49. checkServiceContainer(t, res.Combined(), "scale-basic-tests-front", "Running", 2)
  50. assert.Check(t, !strings.Contains(res.Combined(), "Container scale-basic-tests-front-3 Running"), res.Combined())
  51. checkServiceContainer(t, res.Combined(), "scale-basic-tests-back", "Running", 1)
  52. }
  53. func TestScaleWithDepsCases(t *testing.T) {
  54. c := NewCLI(t, WithEnv(
  55. "COMPOSE_PROJECT_NAME=scale-deps-tests"))
  56. reset := func() {
  57. c.RunDockerComposeCmd(t, "down", "--rmi", "all")
  58. }
  59. t.Cleanup(reset)
  60. res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "up", "-d", "--scale", "db=2")
  61. res.Assert(t, icmd.Success)
  62. res = c.RunDockerComposeCmd(t, "ps")
  63. checkServiceContainer(t, res.Combined(), "scale-deps-tests-db", NO_STATE_TO_CHECK, 2)
  64. t.Log("scale up 1 service with --no-deps")
  65. _ = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "--no-deps", "back=2")
  66. res = c.RunDockerComposeCmd(t, "ps")
  67. checkServiceContainer(t, res.Combined(), "scale-deps-tests-back", NO_STATE_TO_CHECK, 2)
  68. checkServiceContainer(t, res.Combined(), "scale-deps-tests-db", NO_STATE_TO_CHECK, 2)
  69. t.Log("scale up 1 service without --no-deps")
  70. _ = c.RunDockerComposeCmd(t, "--project-directory", "fixtures/scale", "scale", "back=2")
  71. res = c.RunDockerComposeCmd(t, "ps")
  72. checkServiceContainer(t, res.Combined(), "scale-deps-tests-back", NO_STATE_TO_CHECK, 2)
  73. checkServiceContainer(t, res.Combined(), "scale-deps-tests-db", NO_STATE_TO_CHECK, 1)
  74. }
  75. func checkServiceContainer(t *testing.T, stdout, containerName, containerState string, count int) {
  76. found := 0
  77. lines := strings.Split(stdout, "\n")
  78. for _, line := range lines {
  79. if strings.Contains(line, containerName) && strings.Contains(line, containerState) {
  80. found++
  81. }
  82. }
  83. if found == count {
  84. return
  85. }
  86. errMessage := fmt.Sprintf("expected %d but found %d instance(s) of container %s in stoud", count, found, containerName)
  87. if containerState != "" {
  88. errMessage += fmt.Sprintf(" with expected state %s", containerState)
  89. }
  90. testify.Fail(t, errMessage, stdout)
  91. }