test_interface.py 2.3 KB

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