crush_rule_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright 2020 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. "encoding/json"
  16. "testing"
  17. "github.com/pkg/errors"
  18. cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
  19. "github.com/rook/rook/pkg/clusterd"
  20. exectest "github.com/rook/rook/pkg/util/exec/test"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. func TestBuildStretchClusterCrushRule(t *testing.T) {
  24. var crushMap CrushMap
  25. err := json.Unmarshal([]byte(testCrushMap), &crushMap)
  26. assert.NoError(t, err)
  27. pool := cephv1.PoolSpec{
  28. FailureDomain: "datacenter",
  29. CrushRoot: cephv1.DefaultCRUSHRoot,
  30. Replicated: cephv1.ReplicatedSpec{
  31. ReplicasPerFailureDomain: 2,
  32. },
  33. }
  34. rule := buildTwoStepCrushRule(crushMap, "stretched", pool)
  35. assert.Equal(t, 2, rule.ID)
  36. }
  37. func TestBuildCrushSteps(t *testing.T) {
  38. pool := cephv1.PoolSpec{
  39. FailureDomain: "datacenter",
  40. CrushRoot: cephv1.DefaultCRUSHRoot,
  41. Replicated: cephv1.ReplicatedSpec{
  42. ReplicasPerFailureDomain: 2,
  43. },
  44. }
  45. steps := buildTwoStepCrushSteps(pool)
  46. assert.Equal(t, 4, len(steps))
  47. assert.Equal(t, cephv1.DefaultCRUSHRoot, steps[0].ItemName)
  48. assert.Equal(t, "datacenter", steps[1].Type)
  49. assert.Equal(t, uint(2), steps[2].Number)
  50. }
  51. func TestCompileCRUSHMap(t *testing.T) {
  52. executor := &exectest.MockExecutor{}
  53. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  54. logger.Infof("Command: %s %v", command, args)
  55. if command == "crushtool" && args[0] == "--compile" && args[1] == "/tmp/063990228" && args[2] == "--outfn" && args[3] == "/tmp/063990228.compiled" {
  56. return "3", nil
  57. }
  58. return "", errors.Errorf("unexpected ceph command '%v'", args)
  59. }
  60. err := compileCRUSHMap(&clusterd.Context{Executor: executor}, "/tmp/063990228")
  61. assert.Nil(t, err)
  62. }
  63. func TestDecompileCRUSHMap(t *testing.T) {
  64. executor := &exectest.MockExecutor{}
  65. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  66. logger.Infof("Command: %s %v", command, args)
  67. if command == "crushtool" && args[0] == "--decompile" && args[1] == "/tmp/063990228" && args[2] == "--outfn" && args[3] == "/tmp/063990228.decompiled" {
  68. return "3", nil
  69. }
  70. return "", errors.Errorf("unexpected ceph command '%v'", args)
  71. }
  72. err := decompileCRUSHMap(&clusterd.Context{Executor: executor}, "/tmp/063990228")
  73. assert.Nil(t, err)
  74. }
  75. func TestInjectCRUSHMapMap(t *testing.T) {
  76. executor := &exectest.MockExecutor{}
  77. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  78. logger.Infof("Command: %s %v", command, args)
  79. if args[0] == "osd" && args[1] == "setcrushmap" && args[2] == "--in-file" && args[3] == "/tmp/063990228.compiled" {
  80. return "3", nil
  81. }
  82. return "", errors.Errorf("unexpected ceph command '%v'", args)
  83. }
  84. err := injectCRUSHMap(&clusterd.Context{Executor: executor}, AdminTestClusterInfo("mycluster"), "/tmp/063990228.compiled")
  85. assert.Nil(t, err)
  86. }
  87. func TestSetCRUSHMapMap(t *testing.T) {
  88. executor := &exectest.MockExecutor{}
  89. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  90. logger.Infof("Command: %s %v", command, args)
  91. if args[0] == "osd" && args[1] == "crush" && args[2] == "set" && args[3] == "/tmp/063990228.compiled" {
  92. return "3", nil
  93. }
  94. return "", errors.Errorf("unexpected ceph command '%v'", args)
  95. }
  96. err := setCRUSHMap(&clusterd.Context{Executor: executor}, AdminTestClusterInfo("mycluster"), "/tmp/063990228.compiled")
  97. assert.Nil(t, err)
  98. }
  99. func Test_generateRuleID(t *testing.T) {
  100. tests := []struct {
  101. name string
  102. args []ruleSpec
  103. want int
  104. }{
  105. {"ordered rules", []ruleSpec{{ID: 1}, {ID: 2}, {ID: 3}}, 4},
  106. {"unordered rules", []ruleSpec{{ID: 1}, {ID: 3}, {ID: 2}, {ID: 5}}, 6},
  107. {"unordered rules", []ruleSpec{{ID: 1}, {ID: 3}, {ID: 2}}, 4},
  108. {"ordered rules", []ruleSpec{{ID: 1}, {ID: 3}}, 4},
  109. {"ordered rules", []ruleSpec{{ID: 1}}, 2},
  110. }
  111. for _, tt := range tests {
  112. t.Run(tt.name, func(t *testing.T) {
  113. if got := generateRuleID(tt.args); got != tt.want {
  114. t.Errorf("generateRuleID() = %v, want %v", got, tt.want)
  115. }
  116. })
  117. }
  118. }