test_compute_service.py 7.9 KB

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