test_block_store_service.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import time
  2. import six
  3. from cloudbridge.base import helpers as cb_helpers
  4. from cloudbridge.factory import ProviderList
  5. from cloudbridge.interfaces import SnapshotState
  6. from cloudbridge.interfaces import VolumeState
  7. from cloudbridge.interfaces.provider import TestMockHelperMixin
  8. from cloudbridge.interfaces.resources import AttachmentInfo
  9. from cloudbridge.interfaces.resources import Snapshot
  10. from cloudbridge.interfaces.resources import Volume
  11. from tests import helpers
  12. from tests.helpers import ProviderTestBase
  13. from tests.helpers import standard_interface_tests as sit
  14. class CloudBlockStoreServiceTestCase(ProviderTestBase):
  15. _multiprocess_can_split_ = True
  16. @helpers.skipIfNoService(['storage.volumes', 'storage.volumes'])
  17. def test_storage_services_event_pattern(self):
  18. # pylint:disable=protected-access
  19. self.assertEqual(
  20. self.provider.storage.volumes._service_event_pattern,
  21. "provider.storage.volumes",
  22. "Event pattern for {} service should be '{}', "
  23. "but found '{}'.".format("volumes",
  24. "provider.storage.volumes",
  25. self.provider.storage.volumes.
  26. _service_event_pattern))
  27. # pylint:disable=protected-access
  28. self.assertEqual(
  29. self.provider.storage.snapshots._service_event_pattern,
  30. "provider.storage.snapshots",
  31. "Event pattern for {} service should be '{}', "
  32. "but found '{}'.".format("snapshots",
  33. "provider.storage.snapshots",
  34. self.provider.storage.snapshots.
  35. _service_event_pattern))
  36. @helpers.skipIfNoService(['storage.volumes'])
  37. def test_crud_volume(self):
  38. def create_vol(label):
  39. return self.provider.storage.volumes.create(
  40. label, 1)
  41. def cleanup_vol(vol):
  42. if vol:
  43. vol.delete()
  44. vol.wait_for([VolumeState.DELETED, VolumeState.UNKNOWN],
  45. terminal_states=[VolumeState.ERROR])
  46. vol.refresh()
  47. self.assertTrue(
  48. vol.state == VolumeState.UNKNOWN,
  49. "Volume.state must be unknown when refreshing after a "
  50. "delete but got %s"
  51. % vol.state)
  52. sit.check_crud(self, self.provider.storage.volumes, Volume,
  53. "cb-createvol", create_vol, cleanup_vol)
  54. @helpers.skipIfNoService(['storage.volumes'])
  55. def test_attach_detach_volume(self):
  56. label = "cb-attachvol-{0}".format(helpers.get_uuid())
  57. # Declare these variables and late binding will allow
  58. # the cleanup method access to the most current values
  59. test_instance = None
  60. with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  61. test_instance)):
  62. subnet = helpers.get_or_create_default_subnet(
  63. self.provider)
  64. test_instance = helpers.get_test_instance(
  65. self.provider, label, subnet=subnet)
  66. test_vol = self.provider.storage.volumes.create(
  67. label, 1)
  68. with cb_helpers.cleanup_action(lambda: test_vol.delete()):
  69. test_vol.wait_till_ready()
  70. test_vol.attach(test_instance, '/dev/sda2')
  71. test_vol.wait_for(
  72. [VolumeState.IN_USE],
  73. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  74. test_vol.detach()
  75. test_vol.wait_for(
  76. [VolumeState.AVAILABLE],
  77. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  78. @helpers.skipIfNoService(['storage.volumes'])
  79. def test_volume_properties(self):
  80. label = "cb-volprops-{0}".format(helpers.get_uuid())
  81. vol_desc = 'newvoldesc1'
  82. # Declare these variables and late binding will allow
  83. # the cleanup method access to the most current values
  84. test_instance = None
  85. with cb_helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  86. test_instance)):
  87. subnet = helpers.get_or_create_default_subnet(
  88. self.provider)
  89. test_instance = helpers.get_test_instance(
  90. self.provider, label, subnet=subnet)
  91. test_vol = self.provider.storage.volumes.create(
  92. label, 1, description=vol_desc)
  93. with cb_helpers.cleanup_action(lambda: test_vol.delete()):
  94. test_vol.wait_till_ready()
  95. self.assertTrue(
  96. isinstance(test_vol.size, six.integer_types) and
  97. test_vol.size >= 0,
  98. "Volume.size must be a positive number, but got %s"
  99. % test_vol.size)
  100. self.assertTrue(
  101. test_vol.description is None or
  102. isinstance(test_vol.description, six.string_types),
  103. "Volume.description must be None or a string. Got: %s"
  104. % test_vol.description)
  105. self.assertIsNone(test_vol.source)
  106. self.assertIsNone(test_vol.source)
  107. self.assertIsNotNone(test_vol.create_time)
  108. self.assertIsNotNone(test_vol.zone_id)
  109. self.assertIsNone(test_vol.attachments)
  110. test_vol.attach(test_instance, '/dev/sda2')
  111. test_vol.wait_for(
  112. [VolumeState.IN_USE],
  113. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  114. self.assertIsNotNone(test_vol.attachments)
  115. self.assertIsInstance(test_vol.attachments, AttachmentInfo)
  116. self.assertEqual(test_vol.attachments.volume, test_vol)
  117. self.assertEqual(test_vol.attachments.instance_id,
  118. test_instance.id)
  119. if (self.provider.PROVIDER_ID != 'azure' and
  120. self.provider.PROVIDER_ID != 'gcp'):
  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, six.integer_types))
  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, six.string_types),
  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()