test_interface.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import unittest
  2. from test.helpers import ProviderTestBase
  3. import cloudbridge
  4. from cloudbridge.cloud import interfaces
  5. from cloudbridge.cloud.factory import CloudProviderFactory
  6. from cloudbridge.cloud.interfaces import TestMockHelperMixin
  7. from cloudbridge.cloud.interfaces.exceptions import ProviderConnectionException
  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. # Mock up test by clearing credentials on a per provider basis
  43. cloned_config = self.provider.config.copy()
  44. if self.provider.PROVIDER_ID == 'aws':
  45. cloned_config['aws_access_key'] = "dummy_a_key"
  46. cloned_config['aws_secret_key'] = "dummy_s_key"
  47. elif self.provider.PROVIDER_ID == 'openstack':
  48. cloned_config['os_username'] = "cb_dummy"
  49. cloned_config['os_password'] = "cb_dummy"
  50. with self.assertRaises(ProviderConnectionException):
  51. cloned_provider = CloudProviderFactory().create_provider(
  52. self.provider.PROVIDER_ID, cloned_config)
  53. cloned_provider.authenticate()