test_provider_block_store_service.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import uuid
  2. from cloudbridge.providers.interfaces import SnapshotState
  3. from cloudbridge.providers.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 setUp(self):
  11. self.instance = helpers.get_test_instance(self.provider)
  12. def tearDown(self):
  13. self.instance.terminate()
  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_volume(
  21. name,
  22. 1,
  23. self.instance.placement_zone)
  24. with helpers.exception_action(lambda x: test_vol.delete()):
  25. test_vol.wait_till_ready()
  26. volumes = self.provider.block_store.volumes.list_volumes()
  27. found_volumes = [vol for vol in volumes if vol.name == name]
  28. self.assertTrue(
  29. len(found_volumes) == 1,
  30. "List volumes does not return the expected volume %s" %
  31. name)
  32. test_vol.delete()
  33. test_vol.wait_for(
  34. [VolumeState.DELETED, VolumeState.UNKNOWN],
  35. terminal_states=[VolumeState.ERROR])
  36. volumes = self.provider.block_store.volumes.list_volumes()
  37. found_volumes = [vol for vol in volumes if vol.name == name]
  38. self.assertTrue(
  39. len(found_volumes) == 0,
  40. "Volume %s should have been deleted but still exists." %
  41. name)
  42. def test_attach_detach_volume(self):
  43. """
  44. Create a new volume, and attempt to attach it to an instance
  45. """
  46. name = "CBUnitTestAttachVol-{0}".format(uuid.uuid4())
  47. test_vol = self.provider.block_store.volumes.create_volume(
  48. name, 1, self.instance.placement_zone)
  49. with helpers.exception_action(lambda x: test_vol.delete()):
  50. test_vol.wait_till_ready()
  51. test_vol.attach(self.instance, '/dev/sda2')
  52. test_vol.wait_for(
  53. [VolumeState.IN_USE], terminal_states=[VolumeState.ERROR,
  54. VolumeState.DELETED])
  55. test_vol.detach()
  56. test_vol.wait_for(
  57. [VolumeState.AVAILABLE], terminal_states=[VolumeState.ERROR,
  58. VolumeState.DELETED])
  59. test_vol.delete()
  60. def test_crud_snapshot(self):
  61. """
  62. Create a new volume, create a snapshot of the volume, and check
  63. whether list_snapshots properly detects the new snapshot.
  64. Delete everything afterwards.
  65. """
  66. name = "CBUnitTestCreateSnap-{0}".format(uuid.uuid4())
  67. test_vol = self.provider.block_store.volumes.create_volume(
  68. name,
  69. 1,
  70. self.instance.placement_zone)
  71. with helpers.exception_action(lambda x: test_vol.delete()):
  72. test_vol.wait_till_ready()
  73. snap_name = "CBSnapshot-{0}".format(name)
  74. test_snap = test_vol.create_snapshot(name=snap_name,
  75. description=snap_name)
  76. def cleanup_snap(snap):
  77. snap.delete()
  78. snap.wait_for(
  79. [SnapshotState.UNKNOWN],
  80. terminal_states=[SnapshotState.ERROR])
  81. with helpers.exception_action(lambda x: cleanup_snap(test_snap)):
  82. test_snap.wait_till_ready()
  83. snaps = self.provider.block_store.snapshots.list_snapshots()
  84. found_snaps = [snap for snap in snaps
  85. if snap.name == snap_name]
  86. self.assertTrue(
  87. len(found_snaps) == 1,
  88. "List snapshots does not return the expected volume %s" %
  89. name)
  90. cleanup_snap(test_snap)
  91. snaps = self.provider.block_store.snapshots.list_snapshots()
  92. found_snaps = [snap for snap in snaps
  93. if snap.name == snap_name]
  94. self.assertTrue(
  95. len(found_snaps) == 0,
  96. "Snapshot %s should have been deleted but still exists." %
  97. snap_name)