test_provider_compute_service.py 6.6 KB

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