test_block_store_service.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import uuid
  2. import six
  3. from cloudbridge.cloud.interfaces import SnapshotState
  4. from cloudbridge.cloud.interfaces import VolumeState
  5. from cloudbridge.cloud.interfaces.resources import AttachmentInfo
  6. from test.helpers import ProviderTestBase
  7. import test.helpers as helpers
  8. class CloudBlockStoreServiceTestCase(ProviderTestBase):
  9. def __init__(self, methodName, provider):
  10. super(CloudBlockStoreServiceTestCase, self).__init__(
  11. methodName=methodName, provider=provider)
  12. def test_crud_volume(self):
  13. """
  14. Create a new volume, check whether the expected values are set,
  15. and delete it
  16. """
  17. name = "CBUnitTestCreateVol-{0}".format(uuid.uuid4())
  18. test_vol = self.provider.block_store.volumes.create(
  19. name,
  20. 1,
  21. helpers.get_provider_test_data(self.provider, "placement"))
  22. def cleanup_vol(vol):
  23. vol.delete()
  24. vol.wait_for([VolumeState.DELETED, VolumeState.UNKNOWN],
  25. terminal_states=[VolumeState.ERROR])
  26. with helpers.cleanup_action(lambda: cleanup_vol(test_vol)):
  27. test_vol.wait_till_ready()
  28. self.assertTrue(
  29. test_vol.id in repr(test_vol),
  30. "repr(obj) should contain the object id so that the object"
  31. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  32. volumes = self.provider.block_store.volumes.list()
  33. list_volumes = [vol for vol in volumes if vol.name == name]
  34. self.assertTrue(
  35. len(list_volumes) == 1,
  36. "List volumes does not return the expected volume %s" %
  37. name)
  38. # check iteration
  39. iter_volumes = [vol for vol in self.provider.block_store.volumes
  40. if vol.name == name]
  41. self.assertTrue(
  42. len(iter_volumes) == 1,
  43. "Iter volumes does not return the expected volume %s" %
  44. name)
  45. # check find
  46. find_vols = self.provider.block_store.volumes.find(name=name)
  47. self.assertTrue(
  48. len(find_vols) == 1,
  49. "Find volumes does not return the expected volume %s" %
  50. name)
  51. # check non-existent find
  52. # TODO: Moto has a bug with filters causing the following test
  53. # to fail. Need to add tag based filtering support for volumes
  54. # find_vols = self.provider.block_store.volumes.find(
  55. # name="non_existent_vol")
  56. # self.assertTrue(
  57. # len(find_vols) == 0,
  58. # "Find() for a non-existent volume returned %s" % find_vols)
  59. get_vol = self.provider.block_store.volumes.get(
  60. test_vol.id)
  61. self.assertTrue(
  62. list_volumes[0] ==
  63. get_vol == test_vol,
  64. "Ids returned by list: {0} and get: {1} are not as "
  65. " expected: {2}" .format(list_volumes[0].id,
  66. get_vol.id,
  67. test_vol.id))
  68. self.assertTrue(
  69. list_volumes[0].name ==
  70. get_vol.name == test_vol.name,
  71. "Names returned by list: {0} and get: {1} are not as "
  72. " expected: {2}" .format(list_volumes[0].name,
  73. get_vol.name,
  74. test_vol.name))
  75. volumes = self.provider.block_store.volumes.list()
  76. found_volumes = [vol for vol in volumes if vol.name == name]
  77. self.assertTrue(
  78. len(found_volumes) == 0,
  79. "Volume %s should have been deleted but still exists." %
  80. name)
  81. def test_attach_detach_volume(self):
  82. """
  83. Create a new volume, and attempt to attach it to an instance
  84. """
  85. instance_name = "CBVolOps-{0}-{1}".format(
  86. self.provider.name,
  87. uuid.uuid4())
  88. test_instance = helpers.get_test_instance(self.provider, instance_name)
  89. with helpers.cleanup_action(lambda: test_instance.terminate()):
  90. name = "CBUnitTestAttachVol-{0}".format(uuid.uuid4())
  91. test_vol = self.provider.block_store.volumes.create(
  92. name, 1, test_instance.zone_id)
  93. with helpers.cleanup_action(lambda: test_vol.delete()):
  94. test_vol.wait_till_ready()
  95. test_vol.attach(test_instance, '/dev/sda2')
  96. test_vol.wait_for(
  97. [VolumeState.IN_USE],
  98. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  99. test_vol.detach()
  100. test_vol.wait_for(
  101. [VolumeState.AVAILABLE],
  102. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  103. def test_volume_properties(self):
  104. """
  105. Test volume properties
  106. """
  107. instance_name = "CBVolProps-{0}-{1}".format(
  108. self.provider.name,
  109. uuid.uuid4())
  110. vol_desc = 'newvoldesc1'
  111. test_instance = helpers.get_test_instance(self.provider, instance_name)
  112. with helpers.cleanup_action(lambda: test_instance.terminate()):
  113. name = "CBUnitTestVolProps-{0}".format(uuid.uuid4())
  114. test_vol = self.provider.block_store.volumes.create(
  115. name, 1, test_instance.zone_id, description=vol_desc)
  116. with helpers.cleanup_action(lambda: test_vol.delete()):
  117. test_vol.wait_till_ready()
  118. self.assertTrue(
  119. isinstance(test_vol.size, six.integer_types) and
  120. test_vol.size >= 0,
  121. "Volume.size must be a positive number, but got %s"
  122. % test_vol.size)
  123. self.assertTrue(
  124. test_vol.description is None or
  125. isinstance(test_vol.description, six.string_types),
  126. "Volume.description must be None or a string. Got: %s"
  127. % test_vol.description)
  128. self.assertIsNone(test_vol.source)
  129. self.assertIsNone(test_vol.source)
  130. self.assertIsNotNone(test_vol.create_time)
  131. self.assertIsNotNone(test_vol.zone_id)
  132. self.assertIsNone(test_vol.attachments)
  133. test_vol.attach(test_instance, '/dev/sda2')
  134. test_vol.wait_for(
  135. [VolumeState.IN_USE],
  136. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  137. self.assertIsNotNone(test_vol.attachments)
  138. self.assertIsInstance(test_vol.attachments, AttachmentInfo)
  139. self.assertEqual(test_vol.attachments.volume, test_vol)
  140. self.assertEqual(test_vol.attachments.instance_id,
  141. test_instance.id)
  142. self.assertEqual(test_vol.attachments.device,
  143. "/dev/sda2")
  144. test_vol.detach()
  145. test_vol.name = 'newvolname1'
  146. # Force a refresh before checking attachment status
  147. test_vol.refresh()
  148. self.assertEqual(test_vol.name, 'newvolname1')
  149. self.assertEqual(test_vol.description, vol_desc)
  150. self.assertIsNone(test_vol.attachments)
  151. test_vol.wait_for(
  152. [VolumeState.AVAILABLE],
  153. terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
  154. def test_crud_snapshot(self):
  155. """
  156. Create a new volume, create a snapshot of the volume, and check
  157. whether list_snapshots properly detects the new snapshot.
  158. Delete everything afterwards.
  159. """
  160. name = "CBUnitTestCreateSnap-{0}".format(uuid.uuid4())
  161. test_vol = self.provider.block_store.volumes.create(
  162. name,
  163. 1,
  164. helpers.get_provider_test_data(self.provider, "placement"))
  165. with helpers.cleanup_action(lambda: test_vol.delete()):
  166. test_vol.wait_till_ready()
  167. snap_name = "CBSnapshot-{0}".format(name)
  168. test_snap = test_vol.create_snapshot(name=snap_name,
  169. description=snap_name)
  170. def cleanup_snap(snap):
  171. snap.delete()
  172. snap.wait_for(
  173. [SnapshotState.UNKNOWN],
  174. terminal_states=[SnapshotState.ERROR])
  175. with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
  176. test_snap.wait_till_ready()
  177. self.assertTrue(
  178. test_snap.id in repr(test_snap),
  179. "repr(obj) should contain the object id so that the object"
  180. " can be reconstructed, but does not.")
  181. snaps = self.provider.block_store.snapshots.list()
  182. list_snaps = [snap for snap in snaps
  183. if snap.name == snap_name]
  184. self.assertTrue(
  185. len(list_snaps) == 1,
  186. "List snapshots does not return the expected volume %s" %
  187. name)
  188. # check iteration
  189. iter_snaps = [
  190. snap for snap in self.provider.block_store.snapshots
  191. if snap.name == snap_name]
  192. self.assertTrue(
  193. len(iter_snaps) == 1,
  194. "Iter snapshots does not return the expected volume %s" %
  195. name)
  196. # check find
  197. find_snap = self.provider.block_store.snapshots.find(
  198. name=snap_name)
  199. self.assertTrue(
  200. len(find_snap) == 1,
  201. "Find snaps does not return the expected snapshot %s" %
  202. name)
  203. # check non-existent find
  204. # TODO: Moto has a bug with filters causing the following test
  205. # to fail. Need to add tag based filtering support for snaps
  206. # find_snap = self.provider.block_store.snapshots.find(
  207. # name="non_existent_snap")
  208. # self.assertTrue(
  209. # len(find_snap) == 0,
  210. # "Find() for a non-existent snap returned %s" %
  211. # find_snap)
  212. get_snap = self.provider.block_store.snapshots.get(
  213. test_snap.id)
  214. self.assertTrue(
  215. list_snaps[0] ==
  216. get_snap == test_snap,
  217. "Ids returned by list: {0} and get: {1} are not as "
  218. " expected: {2}" .format(list_snaps[0].id,
  219. get_snap.id,
  220. test_snap.id))
  221. self.assertTrue(
  222. list_snaps[0].name ==
  223. get_snap.name == test_snap.name,
  224. "Names returned by list: {0} and get: {1} are not as "
  225. " expected: {2}" .format(list_snaps[0].name,
  226. get_snap.name,
  227. test_snap.name))
  228. # Test volume creation from a snapshot (via VolumeService)
  229. sv_name = "CBUnitTestSnapVol-{0}".format(name)
  230. snap_vol = self.provider.block_store.volumes.create(
  231. sv_name,
  232. 1,
  233. helpers.get_provider_test_data(self.provider, "placement"),
  234. snapshot=test_snap)
  235. with helpers.cleanup_action(lambda: snap_vol.delete()):
  236. snap_vol.wait_till_ready()
  237. # Test volume creation from a snapshot (via Snapshot)
  238. snap_vol2 = test_snap.create_volume(
  239. helpers.get_provider_test_data(self.provider, "placement"))
  240. with helpers.cleanup_action(lambda: snap_vol2.delete()):
  241. snap_vol2.wait_till_ready()
  242. snaps = self.provider.block_store.snapshots.list()
  243. found_snaps = [snap for snap in snaps
  244. if snap.name == snap_name]
  245. self.assertTrue(
  246. len(found_snaps) == 0,
  247. "Snapshot %s should have been deleted but still exists." %
  248. snap_name)
  249. # Test creation of a snap via SnapshotService
  250. snap_too_name = "CBSnapToo-{0}".format(name)
  251. test_snap_too = self.provider.block_store.snapshots.create(
  252. name=snap_too_name, volume=test_vol, description=snap_too_name)
  253. with helpers.cleanup_action(lambda: cleanup_snap(test_snap_too)):
  254. test_snap_too.wait_till_ready()
  255. self.assertTrue(
  256. test_snap_too.id in repr(test_snap_too),
  257. "repr(obj) should contain the object id so that the object"
  258. " can be reconstructed, but does not.")
  259. def test_snapshot_properties(self):
  260. """
  261. Test snapshot properties
  262. """
  263. name = "CBTestSnapProp-{0}".format(uuid.uuid4())
  264. test_vol = self.provider.block_store.volumes.create(
  265. name,
  266. 1,
  267. helpers.get_provider_test_data(self.provider, "placement"))
  268. with helpers.cleanup_action(lambda: test_vol.delete()):
  269. test_vol.wait_till_ready()
  270. snap_name = "CBSnapProp-{0}".format(name)
  271. test_snap = test_vol.create_snapshot(name=snap_name,
  272. description=snap_name)
  273. def cleanup_snap(snap):
  274. snap.delete()
  275. snap.wait_for(
  276. [SnapshotState.UNKNOWN],
  277. terminal_states=[SnapshotState.ERROR])
  278. with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
  279. test_snap.wait_till_ready()
  280. self.assertTrue(isinstance(test_vol.size, six.integer_types))
  281. self.assertEqual(
  282. test_snap.size, test_vol.size,
  283. "Snapshot.size must match original volume's size: %s"
  284. " but is: %s" % (test_vol.size, test_snap.size))
  285. self.assertTrue(
  286. test_vol.description is None or
  287. isinstance(test_vol.description, six.string_types),
  288. "Snapshot.description must be None or a string. Got: %s"
  289. % test_vol.description)
  290. self.assertEqual(test_vol.id, test_snap.volume_id)
  291. self.assertIsNotNone(test_vol.create_time)
  292. test_snap.name = 'snapnewname1'
  293. test_snap.description = 'snapnewdescription1'
  294. test_snap.refresh()
  295. self.assertEqual(test_snap.name, 'snapnewname1')
  296. self.assertEqual(test_snap.description, 'snapnewdescription1')