test_interface.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import unittest
  2. import cloudbridge
  3. from cloudbridge.cloud import interfaces
  4. from test.helpers import ProviderTestBase
  5. from cloudbridge.cloud.interfaces.resources import ProviderConnectionException
  6. from cloudbridge.cloud.interfaces import TestMockHelperMixin
  7. from cloudbridge.cloud.factory import CloudProviderFactory
  8. class CloudInterfaceTestCase(ProviderTestBase):
  9. def __init__(self, methodName, provider):
  10. super(CloudInterfaceTestCase, self).__init__(
  11. methodName=methodName, provider=provider)
  12. def test_name_property(self):
  13. """
  14. Name should always return a value and should not raise an exception
  15. """
  16. assert self.provider.name
  17. def test_has_service_valid_service_type(self):
  18. """
  19. has_service with a valid service type should return
  20. a boolean and raise no exceptions
  21. """
  22. for key, value in interfaces.CloudServiceType.__dict__.items():
  23. if not key.startswith("__"):
  24. self.provider.has_service(value)
  25. def test_has_service_invalid_service_type(self):
  26. """
  27. has_service with an invalid service type should return False
  28. """
  29. self.assertFalse(
  30. self.provider.has_service("NON_EXISTENT_SERVICE"),
  31. "has_service should not return True for a non-existent service")
  32. def test_library_version(self):
  33. """
  34. Check that the library version can be retrieved.
  35. """
  36. self.assertIsNotNone(cloudbridge.get_version(),
  37. "Did not get library version.")
  38. def test_authenticate_success(self):
  39. self.assertTrue(self.provider.authenticate())
  40. def test_authenticate_failure(self):
  41. if isinstance(self.provider, TestMockHelperMixin):
  42. raise unittest.SkipTest(
  43. "Mock providers are not expected to"
  44. " authenticate correctly")
  45. cloned_provider = CloudProviderFactory().create_provider(
  46. self.provider.PROVIDER_ID, self.provider.config)
  47. with self.assertRaises(ProviderConnectionException):
  48. # Mock up test by clearing credentials on a per provider basis
  49. if cloned_provider.PROVIDER_ID == 'aws':
  50. cloned_provider.a_key = "dummy_a_key"
  51. cloned_provider.s_key = "dummy_s_key"
  52. elif cloned_provider.PROVIDER_ID == 'openstack':
  53. cloned_provider.username = "cb_dummy"
  54. cloned_provider.password = "cb_dummy"
  55. cloned_provider.authenticate()