test_block_store_service.py 15 KB

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