test_compute_service.py 7.5 KB

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