test_provider_compute_service.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import uuid
  2. import ipaddress
  3. from cloudbridge.cloud.interfaces \
  4. import InvalidConfigurationException
  5. from cloudbridge.cloud.interfaces import InstanceState
  6. from cloudbridge.cloud.interfaces.resources import WaitStateException
  7. from test.helpers import ProviderTestBase
  8. import test.helpers as helpers
  9. class ProviderComputeServiceTestCase(ProviderTestBase):
  10. def __init__(self, methodName, provider):
  11. super(ProviderComputeServiceTestCase, self).__init__(
  12. methodName=methodName, provider=provider)
  13. def test_crud_instance(self):
  14. name = "CBInstCrud-{0}-{1}".format(
  15. self.provider.name,
  16. uuid.uuid4())
  17. inst = helpers.create_test_instance(self.provider, name)
  18. def cleanup_inst(instance):
  19. instance.terminate()
  20. instance.wait_for(
  21. [InstanceState.TERMINATED, InstanceState.UNKNOWN],
  22. terminal_states=[InstanceState.ERROR],
  23. interval=self.get_test_wait_interval())
  24. with helpers.cleanup_action(lambda: cleanup_inst(inst)):
  25. inst.wait_till_ready(interval=self.get_test_wait_interval())
  26. all_instances = self.provider.compute.instances.list()
  27. found_instances = [i for i in all_instances if i.name == name]
  28. self.assertTrue(
  29. len(found_instances) == 1,
  30. "List instances does not return the expected instance %s" %
  31. name)
  32. get_inst = self.provider.compute.instances.get(
  33. inst.instance_id)
  34. self.assertTrue(
  35. found_instances[0].instance_id ==
  36. get_inst.instance_id == inst.instance_id,
  37. "Ids returned by list: {0} and get: {1} are not as "
  38. " expected: {2}" .format(found_instances[0].instance_id,
  39. get_inst.instance_id,
  40. inst.instance_id))
  41. self.assertTrue(
  42. found_instances[0].name ==
  43. get_inst.name == inst.name,
  44. "Names returned by list: {0} and get: {1} are not as "
  45. " expected: {2}" .format(found_instances[0].name,
  46. get_inst.name,
  47. inst.name))
  48. deleted_inst = self.provider.compute.instances.get(
  49. inst.instance_id)
  50. self.assertTrue(
  51. deleted_inst is None or deleted_inst.state in (
  52. InstanceState.TERMINATED,
  53. InstanceState.UNKNOWN),
  54. "Instance %s should have been deleted but still exists." %
  55. name)
  56. def _is_valid_ip(self, address):
  57. try:
  58. ipaddress.ip_address(address)
  59. except ValueError:
  60. return False
  61. return True
  62. def test_instance_properties(self):
  63. instance_name = "CBInstProps-{0}-{1}".format(
  64. self.provider.name,
  65. uuid.uuid4())
  66. test_instance = helpers.get_test_instance(self.provider,
  67. instance_name)
  68. with helpers.cleanup_action(lambda: test_instance.terminate()):
  69. self.assertEqual(
  70. test_instance.name, instance_name,
  71. "Instance name {0} is not equal to the expected name"
  72. " {1}".format(test_instance.name, instance_name))
  73. image_id = helpers.get_provider_test_data(self.provider, "image")
  74. self.assertEqual(test_instance.image_id, image_id,
  75. "Image id {0} is not equal to the expected id"
  76. " {1}".format(test_instance.image_id, image_id))
  77. self.assertIsInstance(test_instance.public_ips, list)
  78. self.assertIsInstance(test_instance.private_ips, list)
  79. # Must have either a public or a private ip
  80. ip_private = test_instance.private_ips[0] \
  81. if test_instance.private_ips else None
  82. ip_address = test_instance.public_ips[0] \
  83. if test_instance.public_ips else ip_private
  84. self.assertIsNotNone(
  85. ip_address,
  86. "Instance must have either a public IP or a private IP")
  87. self.assertTrue(
  88. self._is_valid_ip(ip_address),
  89. "Instance must have a valid IP address")
  90. def test_block_device_mappings(self):
  91. name = "CBInstBlkMap-{0}-{1}".format(
  92. self.provider.name,
  93. uuid.uuid4())
  94. lc = self.provider.compute.instances.create_launch_config()
  95. # specifying an invalid size should raise
  96. # an exception
  97. with self.assertRaises(InvalidConfigurationException):
  98. lc.add_volume_device(-1)
  99. # block_devices should be empty so far
  100. self.assertListEqual(
  101. lc.block_devices, [], "No block devices should have been"
  102. " added to mappings list since the configuration was"
  103. " invalid")
  104. # Add a new volume
  105. lc.add_volume_device(size=1)
  106. # Override root volume size
  107. image_id = helpers.get_provider_test_data(self.provider, "image")
  108. img = self.provider.compute.images.get(image_id)
  109. lc.add_volume_device(is_root=True, source=img, size=3)
  110. # Attempting to add more than one root volume should raise an
  111. # exception.
  112. with self.assertRaises(InvalidConfigurationException):
  113. lc.add_volume_device(size=1, is_root=True)
  114. # Add all available ephemeral devices
  115. # instance_type_name = helpers.get_provider_test_data(
  116. # self.provider,
  117. # "instance_type")
  118. # inst_type = next(self.provider.compute.instance_types.find(
  119. # name=instance_type_name))
  120. # for _ in range(inst_type.num_ephemeral_disks):
  121. # lc.add_ephemeral_device()
  122. # block_devices should be populated
  123. # self.assertTrue(
  124. # len(lc.block_devices) == 2 + inst_type.num_ephemeral_disks,
  125. # "Expected %d total block devices bit found %d" %
  126. # (2 + inst_type.num_ephemeral_disks, len(lc.block_devices)))
  127. inst = helpers.create_test_instance(
  128. self.provider,
  129. name,
  130. launch_config=lc)
  131. with helpers.cleanup_action(lambda: inst.terminate()):
  132. try:
  133. inst.wait_till_ready(
  134. interval=self.get_test_wait_interval())
  135. except WaitStateException as e:
  136. self.fail("The block device mapped launch did not complete"
  137. " successfully: %s" % e)
  138. inst.terminate()
  139. inst.wait_for(
  140. [InstanceState.TERMINATED, InstanceState.UNKNOWN],
  141. terminal_states=[InstanceState.ERROR],
  142. interval=self.get_test_wait_interval())
  143. # TODO: Check instance attachments and make sure they
  144. # correspond to requested mappings