storage_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. Copyright 2018 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 v1
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestNodeExists(t *testing.T) {
  19. t.Run("does not exist - no nodes specified", func(t *testing.T) {
  20. spec := StorageScopeSpec{}
  21. assert.False(t, spec.NodeExists("does-not-exist"))
  22. })
  23. t.Run("exists - single node specified", func(t *testing.T) {
  24. spec := StorageScopeSpec{
  25. Nodes: []Node{
  26. {Name: "node1"}, // node gets nothing but its name set
  27. },
  28. }
  29. assert.True(t, spec.NodeExists("node1"))
  30. })
  31. t.Run("exists and not exists - multiple nodes specified", func(t *testing.T) {
  32. spec := StorageScopeSpec{
  33. Nodes: []Node{
  34. {Name: "node1"}, // node gets nothing but its name set
  35. {Name: "node3"},
  36. {Name: "node4"},
  37. },
  38. }
  39. assert.True(t, spec.NodeExists("node1"))
  40. assert.False(t, spec.NodeExists("node2"))
  41. assert.True(t, spec.NodeExists("node3"))
  42. assert.True(t, spec.NodeExists("node4"))
  43. assert.False(t, spec.NodeExists("node5"))
  44. assert.False(t, spec.NodeExists("does-not-exist"))
  45. })
  46. }
  47. func TestResolveNodeNotExist(t *testing.T) {
  48. // a nonexistent node should return nil
  49. storageSpec := StorageScopeSpec{}
  50. node := storageSpec.ResolveNode("fake node")
  51. assert.Nil(t, node)
  52. }
  53. func TestResolveNodeDefaultValues(t *testing.T) {
  54. // a node with no properties and none defined in the cluster storage spec should get the default values
  55. storageSpec := StorageScopeSpec{
  56. Nodes: []Node{
  57. {Name: "node1"}, // node gets nothing but its name set
  58. },
  59. }
  60. node := storageSpec.ResolveNode("node1")
  61. assert.NotNil(t, node)
  62. assert.Equal(t, "", node.Selection.DeviceFilter)
  63. assert.Equal(t, "", node.Selection.DevicePathFilter)
  64. assert.False(t, node.Selection.GetUseAllDevices())
  65. assert.Equal(t, storageSpec.Devices, node.Devices)
  66. }
  67. func TestResolveNodeInherentFromCluster(t *testing.T) {
  68. // a node with no properties defined should inherit them from the cluster storage spec
  69. storageSpec := StorageScopeSpec{
  70. Selection: Selection{
  71. DeviceFilter: "^sd.",
  72. DevicePathFilter: "^/dev/disk/by-path/pci-.*",
  73. Devices: []Device{{Name: "sda"}},
  74. },
  75. Config: map[string]string{
  76. "foo": "bar",
  77. },
  78. Nodes: []Node{
  79. {Name: "node1"}, // node gets nothing but its name set
  80. },
  81. }
  82. node := storageSpec.ResolveNode("node1")
  83. assert.NotNil(t, node)
  84. assert.Equal(t, "^sd.", node.Selection.DeviceFilter)
  85. assert.Equal(t, "^/dev/disk/by-path/pci-.*", node.Selection.DevicePathFilter)
  86. assert.False(t, node.Selection.GetUseAllDevices())
  87. assert.Equal(t, "bar", node.Config["foo"])
  88. assert.Equal(t, []Device{{Name: "sda"}}, node.Devices)
  89. }
  90. func TestResolveNodeSpecificProperties(t *testing.T) {
  91. // a node with its own specific properties defined should keep those values, regardless of what the global cluster config is
  92. storageSpec := StorageScopeSpec{
  93. Selection: Selection{
  94. DeviceFilter: "^sd.",
  95. DevicePathFilter: "^/dev/disk/by-path/pci-.*",
  96. },
  97. Config: map[string]string{
  98. "foo": "bar",
  99. "baz": "biz",
  100. },
  101. Nodes: []Node{
  102. {
  103. Name: "node1", // node has its own config that should override cluster level config
  104. Selection: Selection{
  105. DeviceFilter: "nvme.*",
  106. DevicePathFilter: "^/dev/disk/by-id/.*foo.*",
  107. Devices: []Device{{Name: "device026"}},
  108. },
  109. Config: map[string]string{
  110. "foo": "node1bar",
  111. },
  112. },
  113. },
  114. }
  115. node := storageSpec.ResolveNode("node1")
  116. assert.NotNil(t, node)
  117. assert.False(t, node.Selection.GetUseAllDevices())
  118. assert.Equal(t, "nvme.*", node.Selection.DeviceFilter)
  119. assert.Equal(t, "^/dev/disk/by-id/.*foo.*", node.Selection.DevicePathFilter)
  120. assert.Equal(t, []Device{{Name: "device026"}}, node.Devices)
  121. assert.Equal(t, "node1bar", node.Config["foo"])
  122. assert.Equal(t, "biz", node.Config["baz"])
  123. }
  124. func TestResolveNodeUseAllDevices(t *testing.T) {
  125. storageSpec := StorageScopeSpec{
  126. Selection: Selection{UseAllDevices: newBool(true)}, // UseAllDevices is set to true on the storage spec
  127. Nodes: []Node{
  128. {Name: "node1"}, // node gets nothing but its name set
  129. },
  130. }
  131. node := storageSpec.ResolveNode("node1")
  132. assert.NotNil(t, node)
  133. assert.True(t, node.Selection.GetUseAllDevices())
  134. }
  135. func TestUseAllDevices(t *testing.T) {
  136. storageSpec := StorageScopeSpec{}
  137. assert.False(t, storageSpec.AnyUseAllDevices())
  138. storageSpec = StorageScopeSpec{
  139. Selection: Selection{
  140. UseAllDevices: newBool(true)}, // UseAllDevices is set to true on the storage spec
  141. }
  142. assert.True(t, storageSpec.AnyUseAllDevices())
  143. storageSpec = StorageScopeSpec{
  144. Selection: Selection{UseAllDevices: newBool(false)},
  145. Nodes: []Node{
  146. {
  147. Name: "node1",
  148. Selection: Selection{UseAllDevices: newBool(true)},
  149. },
  150. },
  151. }
  152. assert.True(t, storageSpec.AnyUseAllDevices())
  153. }
  154. func TestClearUseAllDevices(t *testing.T) {
  155. // create a storage spec with use all devices set to true for the cluster and for all nodes
  156. storageSpec := StorageScopeSpec{
  157. Selection: Selection{UseAllDevices: newBool(true)},
  158. Nodes: []Node{
  159. {
  160. Name: "node1",
  161. Selection: Selection{UseAllDevices: newBool(true)},
  162. },
  163. },
  164. }
  165. assert.True(t, storageSpec.AnyUseAllDevices())
  166. // now clear the use all devices field, it should be cleared from the entire cluster and its nodes
  167. storageSpec.ClearUseAllDevices()
  168. assert.False(t, storageSpec.AnyUseAllDevices())
  169. }
  170. func TestClusterDirsDevsInherit(t *testing.T) {
  171. // test for no directories or devices given
  172. storageSpec := StorageScopeSpec{
  173. Nodes: []Node{
  174. {
  175. Name: "node1",
  176. },
  177. },
  178. }
  179. node := storageSpec.ResolveNode("node1")
  180. assert.NotNil(t, node)
  181. assert.Equal(t, storageSpec.Devices, node.Devices)
  182. // test if cluster wide devices are inherited to no-directories/devices node
  183. storageSpec = StorageScopeSpec{
  184. Selection: Selection{
  185. Devices: []Device{{Name: "device1"}},
  186. },
  187. Nodes: []Node{
  188. {
  189. Name: "node1",
  190. },
  191. },
  192. }
  193. node = storageSpec.ResolveNode("node1")
  194. assert.NotNil(t, node)
  195. assert.Equal(t, []Device{{Name: "device1"}}, node.Devices)
  196. // test if node directories and devices are used
  197. storageSpec = StorageScopeSpec{
  198. Nodes: []Node{
  199. {
  200. Name: "node1",
  201. Selection: Selection{
  202. Devices: []Device{{Name: "device2"}},
  203. },
  204. },
  205. },
  206. }
  207. node = storageSpec.ResolveNode("node1")
  208. assert.NotNil(t, node)
  209. assert.Equal(t, []Device{{Name: "device2"}}, node.Devices)
  210. // test if cluster wide devices are and aren't inherited to nodes with and without directories/devices
  211. storageSpec = StorageScopeSpec{
  212. Selection: Selection{
  213. Devices: []Device{{Name: "device4"}},
  214. },
  215. Nodes: []Node{
  216. {
  217. Name: "node1",
  218. Selection: Selection{
  219. Devices: []Device{{Name: "device3"}},
  220. },
  221. },
  222. {
  223. Name: "node2",
  224. },
  225. },
  226. }
  227. // node1 keeps its specified devices
  228. node = storageSpec.ResolveNode("node1")
  229. assert.NotNil(t, node)
  230. assert.Equal(t, []Device{{Name: "device3"}}, node.Devices)
  231. // node2 inherits the cluster wide devices since it specified none of its own
  232. node = storageSpec.ResolveNode("node2")
  233. assert.NotNil(t, node)
  234. assert.Equal(t, []Device{{Name: "device4"}}, node.Devices)
  235. }
  236. func TestStorageScopeSpec_NodeWithNameExists(t *testing.T) {
  237. spec := &StorageScopeSpec{
  238. Nodes: []Node{},
  239. }
  240. assert.False(t, spec.NodeWithNameExists("node0"))
  241. spec.Nodes = []Node{
  242. {Name: "node0-hostname"},
  243. {Name: "node1"},
  244. {Name: "node2"}}
  245. assert.True(t, spec.NodeWithNameExists("node0-hostname"))
  246. assert.False(t, spec.NodeWithNameExists("node0"))
  247. assert.True(t, spec.NodeWithNameExists("node1"))
  248. assert.True(t, spec.NodeWithNameExists("node2"))
  249. }
  250. func TestIsOnPVCEncrypted(t *testing.T) {
  251. s := &StorageScopeSpec{}
  252. assert.False(t, s.IsOnPVCEncrypted())
  253. s.StorageClassDeviceSets = []StorageClassDeviceSet{
  254. {Encrypted: true},
  255. }
  256. assert.True(t, s.IsOnPVCEncrypted())
  257. }