test_provider_block_store_service.py 7.0 KB

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