disk_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Copyright 2016 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 clusterd
  14. import (
  15. "testing"
  16. exectest "github.com/rook/rook/pkg/util/exec/test"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestDiscoverDevices(t *testing.T) {
  20. executor := &exectest.MockExecutor{
  21. MockExecuteCommand: func(command string, arg ...string) error {
  22. logger.Infof("mock execute. %s", command)
  23. return nil
  24. },
  25. MockExecuteCommandWithOutput: func(command string, arg ...string) (string, error) {
  26. logger.Infof("mock execute with output. %s", command)
  27. return "", nil
  28. },
  29. }
  30. devices, err := DiscoverDevices(executor)
  31. assert.Nil(t, err)
  32. assert.Equal(t, 0, len(devices))
  33. }
  34. func TestDeviceMatchWithFilter(t *testing.T) {
  35. result := deviceMatchWithFilter("nvme0n1p1", "l[o]+p", "")
  36. assert.False(t, result)
  37. result = deviceMatchWithFilter("nvme0n1p1", "nvme[0-1np]+", "")
  38. assert.True(t, result)
  39. result = deviceMatchWithFilter("nvme0n1p1", "all", "")
  40. assert.True(t, result)
  41. result = deviceMatchWithFilter("/dev/test-rook-vg/test-rook-lv", "nvme[0-1np]+", "/dev/test-rook-vg/test-rook-lv")
  42. assert.True(t, result)
  43. result = deviceMatchWithFilter("dm-0", "nvme[0-1np]+", "/dev/test-rook-vg/test-rook-lv")
  44. assert.True(t, result)
  45. result = deviceMatchWithFilter("dm-1", "nvme[0-1np]+", "/dev/test-rook-vg/test-rook-lv")
  46. assert.True(t, result)
  47. }
  48. func TestIgnoreDevice(t *testing.T) {
  49. cases := map[string]bool{
  50. "rbd0": true,
  51. "rbd2": true,
  52. "rbd9913": true,
  53. "rbd32p1": true,
  54. "rbd0a2": false,
  55. "rbd": false,
  56. "arbd0": false,
  57. "rbd0x": false,
  58. }
  59. for dev, expected := range cases {
  60. assert.Equal(t, expected, ignoreDevice(dev), dev)
  61. }
  62. }