Juju Charm - Ceph OSD
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_actions_zap_disk.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright 2018 Canonical Ltd
  2. #
  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. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import mock
  15. from actions import zap_disk
  16. from test_utils import CharmTestCase
  17. class ZapDiskActionTests(CharmTestCase):
  18. def setUp(self):
  19. super(ZapDiskActionTests, self).setUp(
  20. zap_disk, ['hookenv',
  21. 'is_block_device',
  22. 'is_device_mounted',
  23. 'is_active_bluestore_device',
  24. 'kv'])
  25. self.is_device_mounted.return_value = False
  26. self.is_block_device.return_value = True
  27. self.is_active_bluestore_device.return_value = False
  28. self.kv.return_value = self.kv
  29. self.hookenv.local_unit.return_value = "ceph-osd-test/0"
  30. @mock.patch.object(zap_disk, 'zap_disk')
  31. def test_authorized_zap_single_disk(self,
  32. _zap_disk):
  33. """Will zap disk with extra config set"""
  34. def side_effect(arg):
  35. return {
  36. 'devices': '/dev/vdb',
  37. 'i-really-mean-it': True,
  38. }.get(arg)
  39. self.hookenv.action_get.side_effect = side_effect
  40. self.kv.get.return_value = ['/dev/vdb', '/dev/vdz']
  41. zap_disk.zap()
  42. _zap_disk.assert_called_with('/dev/vdb')
  43. self.kv.get.assert_called_with('osd-devices', [])
  44. self.kv.set.assert_called_with('osd-devices', ['/dev/vdz'])
  45. self.hookenv.action_set.assert_called_with({
  46. 'message': "1 disk(s) have been zapped, to use "
  47. "them as OSDs, run: \njuju "
  48. "run-action ceph-osd-test/0 add-disk "
  49. "osd-devices=\"/dev/vdb\""
  50. })
  51. @mock.patch.object(zap_disk, 'zap_disk')
  52. def test_authorized_zap_multiple_disks(self,
  53. _zap_disk):
  54. """Will zap disk with extra config set"""
  55. def side_effect(arg):
  56. return {
  57. 'devices': '/dev/vdb /dev/vdc',
  58. 'i-really-mean-it': True,
  59. }.get(arg)
  60. self.hookenv.action_get.side_effect = side_effect
  61. self.kv.get.return_value = ['/dev/vdb', '/dev/vdz']
  62. zap_disk.zap()
  63. _zap_disk.assert_has_calls([
  64. mock.call('/dev/vdb'),
  65. mock.call('/dev/vdc'),
  66. ])
  67. self.kv.get.assert_called_with('osd-devices', [])
  68. self.kv.set.assert_called_with('osd-devices', ['/dev/vdz'])
  69. self.hookenv.action_set.assert_called_with({
  70. 'message': "2 disk(s) have been zapped, to use "
  71. "them as OSDs, run: \njuju "
  72. "run-action ceph-osd-test/0 add-disk "
  73. "osd-devices=\"/dev/vdb /dev/vdc\""
  74. })
  75. @mock.patch.object(zap_disk, 'zap_disk')
  76. def test_wont_zap_non_block_device(self,
  77. _zap_disk,):
  78. """Will not zap a disk that isn't a block device"""
  79. def side_effect(arg):
  80. return {
  81. 'devices': '/dev/vdb',
  82. 'i-really-mean-it': True,
  83. }.get(arg)
  84. self.hookenv.action_get.side_effect = side_effect
  85. self.is_block_device.return_value = False
  86. zap_disk.zap()
  87. _zap_disk.assert_not_called()
  88. self.hookenv.action_fail.assert_called_with(
  89. "1 devices are not block devices: /dev/vdb")
  90. @mock.patch.object(zap_disk, 'zap_disk')
  91. def test_wont_zap_mounted_block_device(self,
  92. _zap_disk):
  93. """Will not zap a disk that is mounted"""
  94. def side_effect(arg):
  95. return {
  96. 'devices': '/dev/vdb',
  97. 'i-really-mean-it': True,
  98. }.get(arg)
  99. self.hookenv.action_get.side_effect = side_effect
  100. self.is_device_mounted.return_value = True
  101. zap_disk.zap()
  102. _zap_disk.assert_not_called()
  103. self.hookenv.action_fail.assert_called_with(
  104. "1 devices are mounted: /dev/vdb")
  105. @mock.patch.object(zap_disk, 'zap_disk')
  106. def test_wont_zap__mounted_bluestore_device(self,
  107. _zap_disk):
  108. """Will not zap a disk that is mounted"""
  109. def side_effect(arg):
  110. return {
  111. 'devices': '/dev/vdb',
  112. 'i-really-mean-it': True,
  113. }.get(arg)
  114. self.hookenv.action_get.side_effect = side_effect
  115. self.is_active_bluestore_device.return_value = True
  116. zap_disk.zap()
  117. _zap_disk.assert_not_called()
  118. self.hookenv.action_fail.assert_called_with(
  119. "1 devices are mounted: /dev/vdb")