test_compute_service.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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()
  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. deleted_inst = self.provider.compute.get_instance(inst.instance_id)
  28. self.assertTrue(
  29. deleted_inst is None or deleted_inst.state in (
  30. InstanceState.TERMINATED,
  31. InstanceState.UNKNOWN),
  32. "Instance %s should have been deleted but still exists." %
  33. name)
  34. def _is_valid_ip(self, address):
  35. try:
  36. ipaddress.ip_address(address)
  37. except ValueError:
  38. return False
  39. return True
  40. def test_instance_properties(self):
  41. instance_name = "CBInstProps-{0}-{1}".format(
  42. self.provider.name,
  43. uuid.uuid4())
  44. test_instance = helpers.get_test_instance(self.provider,
  45. instance_name)
  46. with helpers.exception_action(lambda: test_instance.terminate()):
  47. self.assertEqual(
  48. test_instance.name, instance_name,
  49. "Instance name {0} is not equal to the expected name"
  50. " {1}".format(test_instance.name, instance_name))
  51. image_id = helpers.get_provider_test_data(self.provider, "image")
  52. self.assertEqual(test_instance.image_id, image_id,
  53. "Image id {0} is not equal to the expected id"
  54. " {1}".format(test_instance.image_id, image_id))
  55. self.assertIsInstance(test_instance.public_ips, list)
  56. self.assertIsInstance(test_instance.private_ips, list)
  57. # Must have either a public or a private ip
  58. ip_private = test_instance.private_ips[0] \
  59. if test_instance.private_ips else None
  60. ip_address = test_instance.public_ips[0] \
  61. if test_instance.public_ips else ip_private
  62. self.assertIsNotNone(
  63. ip_address,
  64. "Instance must have either a public IP or a private IP")
  65. self.assertTrue(
  66. self._is_valid_ip(ip_address),
  67. "Instance must have a valid IP address")