test_block_store_service.py 15 KB

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