osd_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. "fmt"
  16. "testing"
  17. "github.com/pkg/errors"
  18. "github.com/rook/rook/pkg/clusterd"
  19. "github.com/rook/rook/pkg/daemon/ceph/client/fake"
  20. "github.com/rook/rook/pkg/operator/ceph/version"
  21. exectest "github.com/rook/rook/pkg/util/exec/test"
  22. "github.com/stretchr/testify/assert"
  23. )
  24. var (
  25. fakeOsdTree = `{
  26. "nodes": [
  27. {
  28. "id": -3,
  29. "name": "minikube",
  30. "type": "host",
  31. "type_id": 1,
  32. "pool_weights": {},
  33. "children": [
  34. 2,
  35. 1,
  36. 0
  37. ]
  38. },
  39. {
  40. "id": -2,
  41. "name": "minikube-2",
  42. "type": "host",
  43. "type_id": 1,
  44. "pool_weights": {},
  45. "children": [
  46. 3,
  47. 4,
  48. 5
  49. ]
  50. }
  51. ]
  52. }`
  53. fakeOSdList = `[0,1,2]`
  54. )
  55. func TestHostTree(t *testing.T) {
  56. executor := &exectest.MockExecutor{}
  57. emptyTreeResult := false
  58. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  59. logger.Infof("Command: %s %v", command, args)
  60. switch {
  61. case args[0] == "osd" && args[1] == "tree":
  62. if emptyTreeResult {
  63. return `not a json`, nil
  64. }
  65. return fakeOsdTree, nil
  66. }
  67. return "", errors.Errorf("unexpected ceph command %q", args)
  68. }
  69. tree, err := HostTree(&clusterd.Context{Executor: executor}, AdminTestClusterInfo("mycluster"))
  70. assert.NoError(t, err)
  71. assert.Equal(t, 2, len(tree.Nodes))
  72. assert.Equal(t, "minikube", tree.Nodes[0].Name)
  73. assert.Equal(t, 3, len(tree.Nodes[0].Children))
  74. emptyTreeResult = true
  75. tree, err = HostTree(&clusterd.Context{Executor: executor}, AdminTestClusterInfo("mycluster"))
  76. assert.Error(t, err)
  77. assert.Equal(t, 0, len(tree.Nodes))
  78. }
  79. func TestOsdListNum(t *testing.T) {
  80. executor := &exectest.MockExecutor{}
  81. emptyOsdListNumResult := false
  82. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  83. logger.Infof("Command: %s %v", command, args)
  84. switch {
  85. case args[0] == "osd" && args[1] == "ls":
  86. if emptyOsdListNumResult {
  87. return `not a json`, nil
  88. }
  89. return fakeOSdList, nil
  90. }
  91. return "", errors.Errorf("unexpected ceph command %q", args)
  92. }
  93. list, err := OsdListNum(&clusterd.Context{Executor: executor}, AdminTestClusterInfo("mycluster"))
  94. assert.NoError(t, err)
  95. assert.Equal(t, 3, len(list))
  96. emptyOsdListNumResult = true
  97. list, err = OsdListNum(&clusterd.Context{Executor: executor}, AdminTestClusterInfo("mycluster"))
  98. assert.Error(t, err)
  99. assert.Equal(t, 0, len(list))
  100. }
  101. func TestOSDDeviceClasses(t *testing.T) {
  102. executor := &exectest.MockExecutor{}
  103. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  104. logger.Infof("Command: %s %v", command, args)
  105. switch {
  106. case args[0] == "osd" && args[1] == "crush" && args[2] == "get-device-class" && len(args) > 3:
  107. return fake.OSDDeviceClassOutput(args[3]), nil
  108. default:
  109. return fake.OSDDeviceClassOutput(""), nil
  110. }
  111. }
  112. context := &clusterd.Context{Executor: executor}
  113. clusterInfo := AdminTestClusterInfo("mycluster")
  114. t.Run("device classes returned", func(t *testing.T) {
  115. deviceClasses, err := OSDDeviceClasses(context, clusterInfo, []string{"0"})
  116. assert.NoError(t, err)
  117. assert.Equal(t, deviceClasses[0].DeviceClass, "hdd")
  118. })
  119. t.Run("error happened when no id provided", func(t *testing.T) {
  120. _, err := OSDDeviceClasses(context, clusterInfo, []string{})
  121. assert.Error(t, err)
  122. })
  123. }
  124. func TestOSDOkToStop(t *testing.T) {
  125. returnString := ""
  126. returnOkResult := true
  127. seenArgs := []string{}
  128. executor := &exectest.MockExecutor{}
  129. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  130. logger.Infof("Command: %s %v", command, args)
  131. switch {
  132. case args[0] == "osd" && args[1] == "ok-to-stop":
  133. seenArgs = args
  134. if returnOkResult {
  135. return returnString, nil
  136. }
  137. return returnString, errors.Errorf("Error EBUSY: unsafe to stop osd(s) at this time (50 PGs are or would become offline)")
  138. }
  139. panic(fmt.Sprintf("unexpected ceph command %q", args))
  140. }
  141. context := &clusterd.Context{Executor: executor}
  142. clusterInfo := AdminTestClusterInfo("mycluster")
  143. doSetup := func() {
  144. seenArgs = []string{}
  145. }
  146. t.Run("output ok to stop", func(t *testing.T) {
  147. doSetup()
  148. clusterInfo.CephVersion = version.Reef
  149. returnString = fake.OsdOkToStopOutput(1, []int{1, 2})
  150. returnOkResult = true
  151. osds, err := OSDOkToStop(context, clusterInfo, 1, 2)
  152. assert.NoError(t, err)
  153. assert.ElementsMatch(t, osds, []int{1, 2})
  154. assert.Equal(t, "1", seenArgs[2])
  155. assert.Equal(t, "--max=2", seenArgs[3])
  156. })
  157. t.Run("output not ok to stop", func(t *testing.T) {
  158. doSetup()
  159. clusterInfo.CephVersion = version.Reef
  160. returnString = fake.OsdOkToStopOutput(3, []int{})
  161. returnOkResult = false
  162. _, err := OSDOkToStop(context, clusterInfo, 3, 5)
  163. assert.Error(t, err)
  164. assert.Equal(t, "3", seenArgs[2])
  165. assert.Equal(t, "--max=5", seenArgs[3])
  166. })
  167. t.Run("handle maxReturned=0", func(t *testing.T) {
  168. doSetup()
  169. clusterInfo.CephVersion = version.Reef
  170. returnString = fake.OsdOkToStopOutput(4, []int{4, 8})
  171. returnOkResult = true
  172. osds, err := OSDOkToStop(context, clusterInfo, 4, 0)
  173. assert.NoError(t, err)
  174. assert.ElementsMatch(t, osds, []int{4, 8})
  175. assert.Equal(t, "4", seenArgs[2])
  176. // should just pass through as --max=0; don't do any special processing
  177. assert.Equal(t, "--max=0", seenArgs[3])
  178. })
  179. }