test_provider_compute_service.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import uuid
  2. import ipaddress
  3. from cloudbridge.providers.interfaces import InstanceState
  4. from test.helpers import ProviderTestBase
  5. import test.helpers as helpers
  6. class ProviderComputeServiceTestCase(ProviderTestBase):
  7. def __init__(self, methodName, provider):
  8. super(ProviderComputeServiceTestCase, self).__init__(
  9. methodName=methodName, provider=provider)
  10. def test_crud_instance(self):
  11. name = "CBInstCrud-{0}-{1}".format(
  12. self.provider.name,
  13. uuid.uuid4())
  14. inst = helpers.create_test_instance(self.provider, name)
  15. with helpers.exception_action(lambda: inst.terminate()):
  16. inst.wait_till_ready(interval=helpers.TEST_WAIT_INTERVAL)
  17. all_instances = self.provider.compute.list_instances()
  18. found_instances = [i for i in all_instances if i.name == name]
  19. self.assertTrue(
  20. len(found_instances) == 1,
  21. "List instances does not return the expected instance %s" %
  22. name)
  23. inst.terminate()
  24. inst.wait_for(
  25. [InstanceState.TERMINATED, InstanceState.UNKNOWN],
  26. terminal_states=[InstanceState.ERROR],
  27. interval=helpers.TEST_WAIT_INTERVAL)
  28. deleted_inst = self.provider.compute.get_instance(inst.instance_id)
  29. self.assertTrue(
  30. deleted_inst is None or deleted_inst.state in (
  31. InstanceState.TERMINATED,
  32. InstanceState.UNKNOWN),
  33. "Instance %s should have been deleted but still exists." %
  34. name)
  35. def _is_valid_ip(self, address):
  36. try:
  37. ipaddress.ip_address(address)
  38. except ValueError:
  39. return False
  40. return True
  41. def test_instance_properties(self):
  42. instance_name = "CBInstProps-{0}-{1}".format(
  43. self.provider.name,
  44. uuid.uuid4())
  45. test_instance = helpers.get_test_instance(self.provider,
  46. instance_name)
  47. with helpers.exception_action(lambda: test_instance.terminate()):
  48. self.assertEqual(
  49. test_instance.name, instance_name,
  50. "Instance name {0} is not equal to the expected name"
  51. " {1}".format(test_instance.name, instance_name))
  52. image_id = helpers.get_provider_test_data(self.provider, "image")
  53. self.assertEqual(test_instance.image_id, image_id,
  54. "Image id {0} is not equal to the expected id"
  55. " {1}".format(test_instance.image_id, image_id))
  56. self.assertIsInstance(test_instance.public_ips, list)
  57. self.assertIsInstance(test_instance.private_ips, list)
  58. # Must have either a public or a private ip
  59. ip_private = test_instance.private_ips[0] \
  60. if test_instance.private_ips else None
  61. ip_address = test_instance.public_ips[0] \
  62. if test_instance.public_ips else ip_private
  63. self.assertIsNotNone(
  64. ip_address,
  65. "Instance must have either a public IP or a private IP")
  66. self.assertTrue(
  67. self._is_valid_ip(ip_address),
  68. "Instance must have a valid IP address")
  69. test_instance.terminate()