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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 'is_mapped_luks_device',
  25. 'kv'])
  26. self.is_device_mounted.return_value = False
  27. self.is_block_device.return_value = True
  28. self.is_active_bluestore_device.return_value = False
  29. self.is_mapped_luks_device.return_value = False
  30. self.kv.return_value = self.kv
  31. self.hookenv.local_unit.return_value = "ceph-osd-test/0"
  32. @mock.patch.object(zap_disk, 'zap_disk')
  33. def test_authorized_zap_single_disk(self,
  34. _zap_disk):
  35. """Will zap disk with extra config set"""
  36. def side_effect(arg):
  37. return {
  38. 'devices': '/dev/vdb',
  39. 'i-really-mean-it': True,
  40. }.get(arg)
  41. self.hookenv.action_get.side_effect = side_effect
  42. self.kv.get.return_value = ['/dev/vdb', '/dev/vdz']
  43. zap_disk.zap()
  44. _zap_disk.assert_called_with('/dev/vdb')
  45. self.kv.get.assert_called_with('osd-devices', [])
  46. self.kv.set.assert_called_with('osd-devices', ['/dev/vdz'])
  47. self.hookenv.action_set.assert_called_with({
  48. 'message': "1 disk(s) have been zapped, to use "
  49. "them as OSDs, run: \njuju "
  50. "run-action ceph-osd-test/0 add-disk "
  51. "osd-devices=\"/dev/vdb\""
  52. })
  53. @mock.patch.object(zap_disk, 'zap_disk')
  54. def test_authorized_zap_multiple_disks(self,
  55. _zap_disk):
  56. """Will zap disk with extra config set"""
  57. def side_effect(arg):
  58. return {
  59. 'devices': '/dev/vdb /dev/vdc',
  60. 'i-really-mean-it': True,
  61. }.get(arg)
  62. self.hookenv.action_get.side_effect = side_effect
  63. self.kv.get.return_value = ['/dev/vdb', '/dev/vdz']
  64. zap_disk.zap()
  65. _zap_disk.assert_has_calls([
  66. mock.call('/dev/vdb'),
  67. mock.call('/dev/vdc'),
  68. ])
  69. self.kv.get.assert_called_with('osd-devices', [])
  70. self.kv.set.assert_called_with('osd-devices', ['/dev/vdz'])
  71. self.hookenv.action_set.assert_called_with({
  72. 'message': "2 disk(s) have been zapped, to use "
  73. "them as OSDs, run: \njuju "
  74. "run-action ceph-osd-test/0 add-disk "
  75. "osd-devices=\"/dev/vdb /dev/vdc\""
  76. })
  77. @mock.patch.object(zap_disk, 'zap_disk')
  78. def test_wont_zap_non_block_device(self,
  79. _zap_disk,):
  80. """Will not zap a disk that isn't a block device"""
  81. def side_effect(arg):
  82. return {
  83. 'devices': '/dev/vdb',
  84. 'i-really-mean-it': True,
  85. }.get(arg)
  86. self.hookenv.action_get.side_effect = side_effect
  87. self.is_block_device.return_value = False
  88. zap_disk.zap()
  89. _zap_disk.assert_not_called()
  90. self.hookenv.action_fail.assert_called_with(
  91. "1 devices are not block devices: /dev/vdb")
  92. @mock.patch.object(zap_disk, 'zap_disk')
  93. def test_wont_zap_mounted_block_device(self,
  94. _zap_disk):
  95. """Will not zap a disk that is mounted"""
  96. def side_effect(arg):
  97. return {
  98. 'devices': '/dev/vdb',
  99. 'i-really-mean-it': True,
  100. }.get(arg)
  101. self.hookenv.action_get.side_effect = side_effect
  102. self.is_device_mounted.return_value = True
  103. zap_disk.zap()
  104. _zap_disk.assert_not_called()
  105. self.hookenv.action_fail.assert_called_with(
  106. "1 devices are mounted: /dev/vdb")
  107. @mock.patch.object(zap_disk, 'zap_disk')
  108. def test_wont_zap__mounted_bluestore_device(self,
  109. _zap_disk):
  110. """Will not zap a disk that is mounted"""
  111. def side_effect(arg):
  112. return {
  113. 'devices': '/dev/vdb',
  114. 'i-really-mean-it': True,
  115. }.get(arg)
  116. self.hookenv.action_get.side_effect = side_effect
  117. self.is_active_bluestore_device.return_value = True
  118. zap_disk.zap()
  119. _zap_disk.assert_not_called()
  120. self.hookenv.action_fail.assert_called_with(
  121. "1 devices are mounted: /dev/vdb")
  122. @mock.patch.object(zap_disk, 'zap_disk')
  123. def test_wont_zap__mapped_luks_device(self, _zap_disk):
  124. """Will not zap a disk that has a LUKS header"""
  125. def side_effect(arg):
  126. return {
  127. 'devices': '/dev/vdb',
  128. 'i-really-mean-it': True,
  129. }.get(arg)
  130. self.hookenv.action_get.side_effect = side_effect
  131. self.is_active_bluestore_device.return_value = False
  132. self.is_mapped_luks_device.return_value = True
  133. zap_disk.zap()
  134. _zap_disk.assert_not_called()
  135. self.hookenv.action_fail.assert_called_with(
  136. "1 devices are mounted: /dev/vdb")
  137. @mock.patch.object(zap_disk, 'zap_disk')
  138. def test_zap_luks_not_mapped(self, _zap_disk):
  139. """Will zap disk with extra config set"""
  140. def side_effect(arg):
  141. return {
  142. 'devices': '/dev/vdb',
  143. 'i-really-mean-it': True,
  144. }.get(arg)
  145. self.is_active_bluestore_device.return_value = False
  146. self.is_mapped_luks_device.return_value = False
  147. self.hookenv.action_get.side_effect = side_effect
  148. self.kv.get.return_value = ['/dev/vdb', '/dev/vdz']
  149. zap_disk.zap()
  150. _zap_disk.assert_called_with('/dev/vdb')
  151. self.kv.get.assert_called_with('osd-devices', [])
  152. self.kv.set.assert_called_with('osd-devices', ['/dev/vdz'])
  153. self.hookenv.action_set.assert_called_with({
  154. 'message': "1 disk(s) have been zapped, to use "
  155. "them as OSDs, run: \njuju "
  156. "run-action ceph-osd-test/0 add-disk "
  157. "osd-devices=\"/dev/vdb\""
  158. })