mgr_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright 2019 The Rook Authors. All rights reserved.
  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 client
  14. import (
  15. "testing"
  16. "github.com/pkg/errors"
  17. "github.com/rook/rook/pkg/clusterd"
  18. exectest "github.com/rook/rook/pkg/util/exec/test"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. func TestEnableModuleRetries(t *testing.T) {
  22. moduleEnableRetries := 0
  23. moduleEnableWaitTime = 0
  24. executor := &exectest.MockExecutor{}
  25. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  26. logger.Infof("Command: %s %v", command, args)
  27. switch {
  28. case args[0] == "balancer" && args[1] == "on":
  29. return "", nil
  30. case args[0] == "mgr" && args[1] == "module" && args[2] == "enable":
  31. if args[3] == "prometheus" || args[3] == "pg_autoscaler" || args[3] == "crash" {
  32. return "", nil
  33. }
  34. case args[0] == "mgr" && args[1] == "module" && args[2] == "disable":
  35. if args[3] == "prometheus" || args[3] == "pg_autoscaler" || args[3] == "crash" {
  36. return "", nil
  37. }
  38. }
  39. moduleEnableRetries = moduleEnableRetries + 1
  40. return "", errors.Errorf("unexpected ceph command %q", args)
  41. }
  42. clusterInfo := AdminTestClusterInfo("mycluster")
  43. _ = MgrEnableModule(&clusterd.Context{Executor: executor}, clusterInfo, "invalidModuleName", false)
  44. assert.Equal(t, 5, moduleEnableRetries)
  45. moduleEnableRetries = 0
  46. _ = MgrEnableModule(&clusterd.Context{Executor: executor}, clusterInfo, "pg_autoscaler", false)
  47. assert.Equal(t, 0, moduleEnableRetries)
  48. // Balancer skipped
  49. _ = MgrEnableModule(&clusterd.Context{Executor: executor}, clusterInfo, "balancer", false)
  50. assert.Equal(t, 0, moduleEnableRetries)
  51. }
  52. func TestEnableModule(t *testing.T) {
  53. executor := &exectest.MockExecutor{}
  54. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  55. logger.Infof("Command: %s %v", command, args)
  56. switch {
  57. case args[0] == "mgr" && args[1] == "module" && args[2] == "enable":
  58. if args[3] == "prometheus" || args[3] == "pg_autoscaler" || args[3] == "crash" {
  59. return "", nil
  60. }
  61. case args[0] == "mgr" && args[1] == "module" && args[2] == "disable":
  62. if args[3] == "prometheus" || args[3] == "pg_autoscaler" || args[3] == "crash" {
  63. return "", nil
  64. }
  65. }
  66. return "", errors.Errorf("unexpected ceph command %q", args)
  67. }
  68. clusterInfo := AdminTestClusterInfo("mycluster")
  69. err := enableModule(&clusterd.Context{Executor: executor}, clusterInfo, "pg_autoscaler", true, "enable")
  70. assert.NoError(t, err)
  71. err = enableModule(&clusterd.Context{Executor: executor}, clusterInfo, "prometheus", true, "disable")
  72. assert.NoError(t, err)
  73. err = enableModule(&clusterd.Context{Executor: executor}, clusterInfo, "invalidModuleName", false, "enable")
  74. assert.Error(t, err)
  75. err = enableModule(&clusterd.Context{Executor: executor}, clusterInfo, "pg_autoscaler", false, "invalidCommandArgs")
  76. assert.Error(t, err)
  77. }
  78. func TestEnableDisableBalancerModule(t *testing.T) {
  79. executor := &exectest.MockExecutor{}
  80. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  81. logger.Infof("Command: %s %v", command, args)
  82. switch {
  83. case args[0] == "balancer" && args[1] == "on":
  84. return "", nil
  85. case args[0] == "balancer" && args[1] == "off":
  86. return "", nil
  87. }
  88. return "", errors.Errorf("unexpected ceph command %q", args)
  89. }
  90. clusterInfo := AdminTestClusterInfo("mycluster")
  91. err := enableDisableBalancerModule(&clusterd.Context{Executor: executor}, clusterInfo, "on")
  92. assert.NoError(t, err)
  93. err = enableDisableBalancerModule(&clusterd.Context{Executor: executor}, clusterInfo, "off")
  94. assert.NoError(t, err)
  95. }
  96. func TestSetBalancerMode(t *testing.T) {
  97. executor := &exectest.MockExecutor{}
  98. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  99. logger.Infof("Command: %s %v", command, args)
  100. if args[0] == "balancer" && args[1] == "mode" && args[2] == "upmap" {
  101. return "", nil
  102. }
  103. return "", errors.Errorf("unexpected ceph command %q", args)
  104. }
  105. err := setBalancerMode(&clusterd.Context{Executor: executor}, AdminTestClusterInfo("mycluster"), "upmap")
  106. assert.NoError(t, err)
  107. }