test_provider_compute_service.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.id)
  34. self.assertTrue(
  35. found_instances[0].id ==
  36. get_inst.id == inst.id,
  37. "Ids returned by list: {0} and get: {1} are not as "
  38. " expected: {2}" .format(found_instances[0].id,
  39. get_inst.id,
  40. inst.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.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.assertTrue(
  70. test_instance.id in repr(test_instance),
  71. "repr(obj) should contain the object id so that the object"
  72. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  73. self.assertEqual(
  74. test_instance.name, instance_name,
  75. "Instance name {0} is not equal to the expected name"
  76. " {1}".format(test_instance.name, instance_name))
  77. image_id = helpers.get_provider_test_data(self.provider, "image")
  78. self.assertEqual(test_instance.image_id, image_id,
  79. "Image id {0} is not equal to the expected id"
  80. " {1}".format(test_instance.image_id, image_id))
  81. self.assertIsInstance(test_instance.public_ips, list)
  82. self.assertIsInstance(test_instance.private_ips, list)
  83. # Must have either a public or a private ip
  84. ip_private = test_instance.private_ips[0] \
  85. if test_instance.private_ips else None
  86. ip_address = test_instance.public_ips[0] \
  87. if test_instance.public_ips else ip_private
  88. self.assertIsNotNone(
  89. ip_address,
  90. "Instance must have either a public IP or a private IP")
  91. self.assertTrue(
  92. self._is_valid_ip(ip_address),
  93. "Instance must have a valid IP address")
  94. def test_block_device_mappings(self):
  95. name = "CBInstBlkMap-{0}-{1}".format(
  96. self.provider.name,
  97. uuid.uuid4())
  98. lc = self.provider.compute.instances.create_launch_config()
  99. # specifying an invalid size should raise
  100. # an exception
  101. with self.assertRaises(InvalidConfigurationException):
  102. lc.add_volume_device(-1)
  103. # block_devices should be empty so far
  104. self.assertListEqual(
  105. lc.block_devices, [], "No block devices should have been"
  106. " added to mappings list since the configuration was"
  107. " invalid")
  108. # Add a new volume
  109. lc.add_volume_device(size=1)
  110. # Override root volume size
  111. image_id = helpers.get_provider_test_data(self.provider, "image")
  112. img = self.provider.compute.images.get(image_id)
  113. lc.add_volume_device(is_root=True, source=img, size=3)
  114. # Attempting to add more than one root volume should raise an
  115. # exception.
  116. with self.assertRaises(InvalidConfigurationException):
  117. lc.add_volume_device(size=1, is_root=True)
  118. # Add all available ephemeral devices
  119. # instance_type_name = helpers.get_provider_test_data(
  120. # self.provider,
  121. # "instance_type")
  122. # inst_type = next(self.provider.compute.instance_types.find(
  123. # name=instance_type_name))
  124. # for _ in range(inst_type.num_ephemeral_disks):
  125. # lc.add_ephemeral_device()
  126. # block_devices should be populated
  127. # self.assertTrue(
  128. # len(lc.block_devices) == 2 + inst_type.num_ephemeral_disks,
  129. # "Expected %d total block devices bit found %d" %
  130. # (2 + inst_type.num_ephemeral_disks, len(lc.block_devices)))
  131. inst = helpers.create_test_instance(
  132. self.provider,
  133. name,
  134. launch_config=lc)
  135. with helpers.cleanup_action(lambda: inst.terminate()):
  136. try:
  137. inst.wait_till_ready(
  138. interval=self.get_test_wait_interval())
  139. except WaitStateException as e:
  140. self.fail("The block device mapped launch did not complete"
  141. " successfully: %s" % e)
  142. inst.terminate()
  143. inst.wait_for(
  144. [InstanceState.TERMINATED, InstanceState.UNKNOWN],
  145. terminal_states=[InstanceState.ERROR],
  146. interval=self.get_test_wait_interval())
  147. # TODO: Check instance attachments and make sure they
  148. # correspond to requested mappings