From 235e4f53ee05dc4c4226bd7b327068ed031e76c5 Mon Sep 17 00:00:00 2001 From: Marc Koderer Date: Tue, 22 Jul 2014 10:15:08 +0200 Subject: [PATCH] Use python abc in auth class Instead of using NotImplementedError the abc class has the advantage that it fails faster if a class doesn't implement the needed interface. Adapt unit test and test class. Change-Id: Ide914a4bb577a28a3d202cbf088c404323a29b21 Partial-Bug: #1346797 --- tempest/auth.py | 24 ++++++++++++++++------- tempest/tests/test_auth.py | 39 ++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/tempest/auth.py b/tempest/auth.py index 830dca9bf1..6dad3a4e36 100644 --- a/tempest/auth.py +++ b/tempest/auth.py @@ -13,10 +13,12 @@ # License for the specific language governing permissions and limitations # under the License. +import abc import copy import datetime import exceptions import re +import six import urlparse from tempest import config @@ -31,6 +33,7 @@ CONF = config.CONF LOG = logging.getLogger(__name__) +@six.add_metaclass(abc.ABCMeta) class AuthProvider(object): """ Provide authentication @@ -70,18 +73,21 @@ class AuthProvider(object): interface=self.interface, cache=self.cache ) + @abc.abstractmethod def _decorate_request(self, filters, method, url, headers=None, body=None, auth_data=None): """ Decorate request with authentication data """ - raise NotImplementedError + return + @abc.abstractmethod def _get_auth(self): - raise NotImplementedError + return + @abc.abstractmethod def _fill_credentials(self, auth_data_body): - raise NotImplementedError + return def fill_credentials(self): """ @@ -130,8 +136,9 @@ class AuthProvider(object): self.cache = None self.credentials.reset() + @abc.abstractmethod def is_expired(self, auth_data): - raise NotImplementedError + return def auth_request(self, method, url, headers=None, body=None, filters=None): """ @@ -188,11 +195,12 @@ class AuthProvider(object): self.alt_part = request_part self.alt_auth_data = auth_data + @abc.abstractmethod def base_url(self, filters, auth_data=None): """ Extracts the base_url based on provided filters """ - raise NotImplementedError + return class KeystoneAuthProvider(AuthProvider): @@ -225,11 +233,13 @@ class KeystoneAuthProvider(AuthProvider): # no change to method or body return str(_url), _headers, body + @abc.abstractmethod def _auth_client(self): - raise NotImplementedError + return + @abc.abstractmethod def _auth_params(self): - raise NotImplementedError + return def _get_auth(self): # Bypasses the cache diff --git a/tempest/tests/test_auth.py b/tempest/tests/test_auth.py index 1dcddad87b..6a2e3358a0 100644 --- a/tempest/tests/test_auth.py +++ b/tempest/tests/test_auth.py @@ -59,12 +59,24 @@ class TestBaseAuthProvider(BaseAuthTestsSetUp): obviously don't test not implemented method or the ones which strongly depends on them. """ - _auth_provider_class = auth.AuthProvider - def test_check_credentials_class(self): - self.assertRaises(NotImplementedError, - self.auth_provider.check_credentials, - auth.Credentials()) + class FakeAuthProviderImpl(auth.AuthProvider): + def _decorate_request(): + pass + + def _fill_credentials(): + pass + + def _get_auth(): + pass + + def base_url(): + pass + + def is_expired(): + pass + + _auth_provider_class = FakeAuthProviderImpl def test_check_credentials_bad_type(self): self.assertFalse(self.auth_provider.check_credentials([])) @@ -74,16 +86,6 @@ class TestBaseAuthProvider(BaseAuthTestsSetUp): auth_provider = self._auth(credentials={}) self.assertIsInstance(auth_provider.credentials, auth.Credentials) - def test_instantiate_with_bad_credentials_type(self): - """ - Assure that credentials with bad type fail with TypeError - """ - self.assertRaises(TypeError, self._auth, []) - - def test_auth_data_property(self): - self.assertRaises(NotImplementedError, getattr, self.auth_provider, - 'auth_data') - def test_auth_data_property_when_cache_exists(self): self.auth_provider.cache = 'foo' self.useFixture(mockpatch.PatchObject(self.auth_provider, @@ -110,9 +112,10 @@ class TestBaseAuthProvider(BaseAuthTestsSetUp): self.assertIsNone(self.auth_provider.alt_part) self.assertIsNone(self.auth_provider.alt_auth_data) - def test_fill_credentials(self): - self.assertRaises(NotImplementedError, - self.auth_provider.fill_credentials) + def test_auth_class(self): + self.assertRaises(TypeError, + auth.AuthProvider, + fake_credentials.FakeCredentials) class TestKeystoneV2AuthProvider(BaseAuthTestsSetUp):