test_block_store_service.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import itertools
  2. import uuid
  3. from cloudbridge.cloud.interfaces import SnapshotState
  4. from cloudbridge.cloud.interfaces import VolumeState
  5. from test.helpers import ProviderTestBase
  6. import test.helpers as helpers
  7. class CloudBlockStoreServiceTestCase(ProviderTestBase):
  8. def __init__(self, methodName, provider):
  9. super(CloudBlockStoreServiceTestCase, self).__init__(
  10. methodName=methodName, provider=provider)
  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(
  24. [VolumeState.DELETED, VolumeState.UNKNOWN],
  25. terminal_states=[VolumeState.ERROR],
  26. interval=self.get_test_wait_interval())
  27. with helpers.cleanup_action(lambda: cleanup_vol(test_vol)):
  28. test_vol.wait_till_ready(interval=self.get_test_wait_interval())
  29. self.assertTrue(
  30. test_vol.id in repr(test_vol),
  31. "repr(obj) should contain the object id so that the object"
  32. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  33. volumes = self.provider.block_store.volumes.list()
  34. # check iteration
  35. iter_volumes = list(itertools.islice(
  36. self.provider.block_store.volumes,
  37. len(volumes)))
  38. self.assertListEqual(iter_volumes, volumes)
  39. found_volumes = [vol for vol in volumes if vol.name == name]
  40. self.assertTrue(
  41. len(found_volumes) == 1,
  42. "List volumes does not return the expected volume %s" %
  43. name)
  44. get_vol = self.provider.block_store.volumes.get(
  45. test_vol.id)
  46. self.assertTrue(
  47. found_volumes[0].id ==
  48. get_vol.id == test_vol.id,
  49. "Ids returned by list: {0} and get: {1} are not as "
  50. " expected: {2}" .format(found_volumes[0].id,
  51. get_vol.id,
  52. test_vol.id))
  53. self.assertTrue(
  54. found_volumes[0].name ==
  55. get_vol.name == test_vol.name,
  56. "Names returned by list: {0} and get: {1} are not as "
  57. " expected: {2}" .format(found_volumes[0].name,
  58. get_vol.name,
  59. test_vol.name))
  60. volumes = self.provider.block_store.volumes.list()
  61. found_volumes = [vol for vol in volumes if vol.name == name]
  62. self.assertTrue(
  63. len(found_volumes) == 0,
  64. "Volume %s should have been deleted but still exists." %
  65. name)
  66. def test_attach_detach_volume(self):
  67. """
  68. Create a new volume, and attempt to attach it to an instance
  69. """
  70. instance_name = "CBVolOps-{0}-{1}".format(
  71. self.provider.name,
  72. uuid.uuid4())
  73. test_instance = helpers.get_test_instance(self.provider, instance_name)
  74. with helpers.cleanup_action(lambda: test_instance.terminate()):
  75. name = "CBUnitTestAttachVol-{0}".format(uuid.uuid4())
  76. test_vol = self.provider.block_store.volumes.create(
  77. name, 1, test_instance.placement_zone)
  78. with helpers.cleanup_action(lambda: test_vol.delete()):
  79. test_vol.wait_till_ready(
  80. interval=self.get_test_wait_interval())
  81. test_vol.attach(test_instance, '/dev/sda2')
  82. test_vol.wait_for(
  83. [VolumeState.IN_USE],
  84. terminal_states=[VolumeState.ERROR, VolumeState.DELETED],
  85. interval=self.get_test_wait_interval())
  86. test_vol.detach()
  87. test_vol.wait_for(
  88. [VolumeState.AVAILABLE],
  89. terminal_states=[VolumeState.ERROR, VolumeState.DELETED],
  90. interval=self.get_test_wait_interval())
  91. def test_crud_snapshot(self):
  92. """
  93. Create a new volume, create a snapshot of the volume, and check
  94. whether list_snapshots properly detects the new snapshot.
  95. Delete everything afterwards.
  96. """
  97. name = "CBUnitTestCreateSnap-{0}".format(uuid.uuid4())
  98. test_vol = self.provider.block_store.volumes.create(
  99. name,
  100. 1,
  101. helpers.get_provider_test_data(self.provider, "placement"))
  102. with helpers.cleanup_action(lambda: test_vol.delete()):
  103. test_vol.wait_till_ready(interval=self.get_test_wait_interval())
  104. snap_name = "CBSnapshot-{0}".format(name)
  105. test_snap = test_vol.create_snapshot(name=snap_name,
  106. description=snap_name)
  107. def cleanup_snap(snap):
  108. snap.delete()
  109. snap.wait_for(
  110. [SnapshotState.UNKNOWN],
  111. terminal_states=[SnapshotState.ERROR],
  112. interval=self.get_test_wait_interval())
  113. with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
  114. test_snap.wait_till_ready(
  115. interval=self.get_test_wait_interval())
  116. self.assertTrue(
  117. test_snap.id in repr(test_snap),
  118. "repr(obj) should contain the object id so that the object"
  119. " can be reconstructed, but does not.")
  120. snaps = self.provider.block_store.snapshots.list()
  121. # check iteration
  122. iter_snaps = list(itertools.islice(
  123. self.provider.block_store.snapshots,
  124. len(snaps)))
  125. self.assertListEqual(iter_snaps, snaps)
  126. found_snaps = [snap for snap in snaps
  127. if snap.name == snap_name]
  128. self.assertTrue(
  129. len(found_snaps) == 1,
  130. "List snapshots does not return the expected volume %s" %
  131. name)
  132. get_snap = self.provider.block_store.snapshots.get(
  133. test_snap.id)
  134. self.assertTrue(
  135. found_snaps[0].id ==
  136. get_snap.id == test_snap.id,
  137. "Ids returned by list: {0} and get: {1} are not as "
  138. " expected: {2}" .format(found_snaps[0].id,
  139. get_snap.id,
  140. test_snap.id))
  141. self.assertTrue(
  142. found_snaps[0].name ==
  143. get_snap.name == test_snap.name,
  144. "Names returned by list: {0} and get: {1} are not as "
  145. " expected: {2}" .format(found_snaps[0].name,
  146. get_snap.name,
  147. test_snap.name))
  148. snaps = self.provider.block_store.snapshots.list()
  149. found_snaps = [snap for snap in snaps
  150. if snap.name == snap_name]
  151. self.assertTrue(
  152. len(found_snaps) == 0,
  153. "Snapshot %s should have been deleted but still exists." %
  154. snap_name)