test_compute_service.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import socket
  2. import uuid
  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. all_instances = self.provider.compute.list_instances()
  28. found_instances = [i for i in all_instances if i.name == name]
  29. self.assertTrue(
  30. len(found_instances) == 0,
  31. "List instances does not return the expected instance %s" %
  32. name)
  33. def _is_valid_ip(self, address):
  34. try:
  35. socket.inet_pton(socket.AF_INET, address)
  36. except socket.error: # not a valid address
  37. try:
  38. socket.inet_pton(socket.AF_INET, address)
  39. except socket.error: # still not a valid address
  40. return False
  41. return True
  42. def test_instance_properties(self):
  43. instance_name = "CBInstProps-{0}-{1}".format(
  44. self.provider.name,
  45. uuid.uuid4())
  46. test_instance = helpers.get_test_instance(self.provider,
  47. instance_name)
  48. with helpers.exception_action(lambda: test_instance.terminate()):
  49. self.assertEqual(
  50. test_instance.name, instance_name,
  51. "Instance name {0} is not equal to the expected name"
  52. " {1}".format(test_instance.name, instance_name))
  53. image_id = helpers.get_provider_test_data(self.provider, "image")
  54. self.assertEqual(test_instance.image_id, image_id,
  55. "Image id {0} is not equal to the expected id"
  56. " {1}".format(test_instance.image_id, image_id))
  57. self.assertIsInstance(test_instance.public_ips, list)
  58. self.assertIsInstance(test_instance.private_ips, list)
  59. self.assertTrue(
  60. self._is_valid_ip(self.test_instance.private_ips[0]))