test_block_store_service.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import time
  2. from cloudbridge.base import helpers as cb_helpers
  3. from cloudbridge.factory import ProviderList
  4. from cloudbridge.interfaces import SnapshotState
  5. from cloudbridge.interfaces import VolumeState
  6. from cloudbridge.interfaces.provider import TestMockHelperMixin
  7. from cloudbridge.interfaces.resources import AttachmentInfo
  8. from cloudbridge.interfaces.resources import Snapshot
  9. from cloudbridge.interfaces.resources import Volume
  10. from tests import helpers
  11. from tests.helpers import ProviderTestBase
  12. from tests.helpers import standard_interface_tests as sit
  13. class CloudBlockStoreServiceTestCase(ProviderTestBase):
  14. _multiprocess_can_split_ = True
  15. @helpers.skipIfNoService(['storage.volumes', 'storage.volumes'])
  16. def test_storage_services_event_pattern(self):
  17. # pylint:disable=protected-access
  18. self.assertEqual(
  19. self.provider.storage.volumes._service_event_pattern,
  20. "provider.storage.volumes",
  21. "Event pattern for {} service should be '{}', "
  22. "but found '{}'.".format("volumes",
  23. "provider.storage.volumes",
  24. self.provider.storage.volumes.
  25. _service_event_pattern))
  26. # pylint:disable=protected-access
  27. self.assertEqual(
  28. self.provider.storage.snapshots._service_event_pattern,
  29. "provider.storage.snapshots",
  30. "Event pattern for {} service should be '{}', "
  31. "but found '{}'.".format("snapshots",
  32. "provider.storage.snapshots",
  33. self.provider.storage.snapshots.
  34. _service_event_pattern))
  35. @helpers.skipIfNoService(['storage.volumes'])
  36. def test_crud_volume(self):
  37. def create_vol(label):
  38. return self.provider.storage.volumes.create(
  39. label, 1)
  40. def cleanup_vol(vol):
  41. if vol:
  42. vol.delete()
  43. vol.wait_for([VolumeState.DELETED, VolumeState.UNKNOWN],
  44. terminal_states=[VolumeState.ERROR])
  45. vol.refresh()
  46. self.assertTrue(
  47. vol.state == VolumeState.UNKNOWN,
  48. "Volume.state must be unknown when refreshing after a "
  49. "delete but got %s"
  50. % vol.state)
  51. sit.check_crud(self, self.provider.storage.volumes, Volume,
  52. "cb-createvol", create_vol, cleanup_vol)
  53. @helpers.skipIfNoService(['storage.volumes'])
  54. def test_attach_detach_volume(self):
  55. label = "cb-attachvol-{0}".format(helpers.get_uuid())
  56. # Declare these variables and late binding will allow
  57. # the cleanup method access to the most current values
  58. test_instance = None
  59. with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  60. test_instance)):
  61. subnet = helpers.get_or_create_default_subnet(
  62. self.provider)
  63. test_instance = helpers.get_test_instance(
  64. self.provider, label, subnet=subnet)
  65. test_vol = self.provider.storage.volumes.create(
  66. label, 1)
  67. with cb_helpers.cleanup_action(lambda: test_vol.delete()):
  68. test_vol.wait_till_ready()
  69. test_vol.attach(test_instance, '/dev/sda2')
  70. test_vol.wait_for(
  71. [VolumeState.IN_USE],
  72. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  73. test_vol.detach()
  74. test_vol.wait_for(
  75. [VolumeState.AVAILABLE],
  76. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  77. @helpers.skipIfNoService(['storage.volumes'])
  78. def test_volume_properties(self):
  79. label = "cb-volprops-{0}".format(helpers.get_uuid())
  80. vol_desc = 'newvoldesc1'
  81. # Declare these variables and late binding will allow
  82. # the cleanup method access to the most current values
  83. test_instance = None
  84. with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  85. test_instance)):
  86. subnet = helpers.get_or_create_default_subnet(
  87. self.provider)
  88. test_instance = helpers.get_test_instance(
  89. self.provider, label, subnet=subnet)
  90. test_vol = self.provider.storage.volumes.create(
  91. label, 1, description=vol_desc)
  92. with cb_helpers.cleanup_action(lambda: test_vol.delete()):
  93. test_vol.wait_till_ready()
  94. self.assertTrue(
  95. isinstance(test_vol.size, int) and
  96. test_vol.size >= 0,
  97. "Volume.size must be a positive number, but got %s"
  98. % test_vol.size)
  99. self.assertTrue(
  100. test_vol.description is None or
  101. isinstance(test_vol.description, str),
  102. "Volume.description must be None or a string. Got: %s"
  103. % test_vol.description)
  104. self.assertIsNone(test_vol.source)
  105. self.assertIsNone(test_vol.source)
  106. self.assertIsNotNone(test_vol.create_time)
  107. self.assertIsNotNone(test_vol.zone_id)
  108. self.assertIsNone(test_vol.attachments)
  109. test_vol.attach(test_instance, '/dev/sda2')
  110. test_vol.wait_for(
  111. [VolumeState.IN_USE],
  112. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  113. self.assertIsNotNone(test_vol.attachments)
  114. self.assertIsInstance(test_vol.attachments, AttachmentInfo)
  115. self.assertEqual(test_vol.attachments.volume, test_vol)
  116. self.assertEqual(test_vol.attachments.instance_id,
  117. test_instance.id)
  118. if (self.provider.PROVIDER_ID != 'azure' and
  119. self.provider.PROVIDER_ID != 'gcp' and
  120. self.provider.PROVIDER_ID != 'openstack'):
  121. self.assertEqual(test_vol.attachments.device,
  122. "/dev/sda2")
  123. test_vol.detach()
  124. test_vol.label = 'newvolname1'
  125. test_vol.wait_for(
  126. [VolumeState.AVAILABLE],
  127. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  128. self.assertEqual(test_vol.label, 'newvolname1')
  129. self.assertEqual(test_vol.description, vol_desc)
  130. self.assertIsNone(test_vol.attachments)
  131. test_vol.wait_for(
  132. [VolumeState.AVAILABLE],
  133. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  134. @helpers.skipIfNoService(['storage.snapshots'])
  135. def test_crud_snapshot(self):
  136. # Create a new volume, create a snapshot of the volume, and check
  137. # whether list_snapshots properly detects the new snapshot.
  138. # Delete everything afterwards.
  139. label = "cb-crudsnap-{0}".format(helpers.get_uuid())
  140. test_vol = self.provider.storage.volumes.create(
  141. label, 1)
  142. with cb_helpers.cleanup_action(lambda: test_vol.delete()):
  143. test_vol.wait_till_ready()
  144. def create_snap(label):
  145. return test_vol.create_snapshot(label=label,
  146. description=label)
  147. def cleanup_snap(snap):
  148. if snap:
  149. snap.delete()
  150. snap.wait_for([SnapshotState.UNKNOWN],
  151. terminal_states=[SnapshotState.ERROR])
  152. snap.refresh()
  153. self.assertTrue(
  154. snap.state == SnapshotState.UNKNOWN,
  155. "Snapshot.state must be unknown when refreshing after "
  156. "a delete but got %s"
  157. % snap.state)
  158. sit.check_crud(self, self.provider.storage.snapshots, Snapshot,
  159. "cb-snap", create_snap, cleanup_snap)
  160. # Test creation of a snap via SnapshotService
  161. def create_snap2(label):
  162. return self.provider.storage.snapshots.create(
  163. label=label, volume=test_vol, description=label)
  164. if (self.provider.PROVIDER_ID == ProviderList.AWS and
  165. not isinstance(self.provider, TestMockHelperMixin)):
  166. time.sleep(15) # Or get SnapshotCreationPerVolumeRateExceeded
  167. sit.check_crud(self, self.provider.storage.snapshots, Snapshot,
  168. "cb-snaptwo", create_snap2, cleanup_snap)
  169. @helpers.skipIfNoService(['storage.snapshots'])
  170. def test_snapshot_properties(self):
  171. label = "cb-snapprop-{0}".format(helpers.get_uuid())
  172. test_vol = self.provider.storage.volumes.create(
  173. label, 1)
  174. with cb_helpers.cleanup_action(lambda: test_vol.delete()):
  175. test_vol.wait_till_ready()
  176. snap_label = "cb-snap-{0}".format(label)
  177. test_snap = test_vol.create_snapshot(label=snap_label,
  178. description=snap_label)
  179. def cleanup_snap(snap):
  180. if snap:
  181. snap.delete()
  182. snap.wait_for([SnapshotState.UNKNOWN],
  183. terminal_states=[SnapshotState.ERROR])
  184. with cb_helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
  185. test_snap.wait_till_ready()
  186. self.assertTrue(isinstance(test_vol.size, int))
  187. self.assertEqual(
  188. test_snap.size, test_vol.size,
  189. "Snapshot.size must match original volume's size: %s"
  190. " but is: %s" % (test_vol.size, test_snap.size))
  191. self.assertTrue(
  192. test_vol.description is None or
  193. isinstance(test_vol.description, str),
  194. "Snapshot.description must be None or a string. Got: %s"
  195. % test_vol.description)
  196. self.assertEqual(test_vol.id, test_snap.volume_id)
  197. self.assertIsNotNone(test_vol.create_time)
  198. test_snap.label = 'snapnewname1'
  199. test_snap.description = 'snapnewdescription1'
  200. test_snap.refresh()
  201. self.assertEqual(test_snap.label, 'snapnewname1')
  202. self.assertEqual(test_snap.description, 'snapnewdescription1')
  203. # Test volume creation from a snapshot (via VolumeService)
  204. sv_label = "cb-snapvol-{0}".format(test_snap.name)
  205. snap_vol = self.provider.storage.volumes.create(
  206. sv_label, 1, snapshot=test_snap)
  207. with cb_helpers.cleanup_action(lambda: snap_vol.delete()):
  208. snap_vol.wait_till_ready()
  209. # Test volume creation from a snapshot (via Snapshot)
  210. snap_vol2 = test_snap.create_volume()
  211. with cb_helpers.cleanup_action(lambda: snap_vol2.delete()):
  212. snap_vol2.wait_till_ready()