test_block_store_service.py 15 KB

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