From 8a036c3f76f0d0ae4cb4c57f902a5e6b90baa9c9 Mon Sep 17 00:00:00 2001 From: Kui Shi Date: Fri, 4 Oct 2013 16:54:03 +0800 Subject: [PATCH] Fix H202 assertRaises Exception Align the hacking version between test-requirement and global requirement. The change of H202 detection from 0.6 to 0.7 in hacking is: - if logical_line.startswith("self.assertRaises(Exception)"): + if logical_line.startswith("self.assertRaises(Exception,"): then more cases are detected by this change. Fix the exposed H202 error. There is a special test case: tests/v3/test_endpoints.py:test_update_invalid_interface ref = self.new_ref(interface=uuid.uuid4().hex) this line can not generate proper parameter for self.manager.update, add a parameter "endpoint" for it, according to the definition in keystoneclient/v3/endpoints.py:EndpointManager.update. Otherwise, there will be following error after changing the Exception to exceptions.ValidationError: TypeError: update() takes at least 2 arguments (6 given) Fixes Bug #1220008 Change-Id: I8f7ed7a6eebf8576a6db5fecd86b9d19a15c8d60 --- keystoneclient/middleware/auth_token.py | 9 +++++---- .../tests/test_auth_token_middleware.py | 15 ++++++++++----- keystoneclient/tests/v3/test_endpoints.py | 8 +++++--- keystoneclient/v3/endpoints.py | 3 ++- tox.ini | 3 +-- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/keystoneclient/middleware/auth_token.py b/keystoneclient/middleware/auth_token.py index 740dff281..48b9f4656 100644 --- a/keystoneclient/middleware/auth_token.py +++ b/keystoneclient/middleware/auth_token.py @@ -439,11 +439,12 @@ class AuthProtocol(object): def _assert_valid_memcache_protection_config(self): if self._memcache_security_strategy: if self._memcache_security_strategy not in ('MAC', 'ENCRYPT'): - raise Exception('memcache_security_strategy must be ' - 'ENCRYPT or MAC') + raise ConfigurationError('memcache_security_strategy must be ' + 'ENCRYPT or MAC') if not self._memcache_secret_key: - raise Exception('mecmache_secret_key must be defined when ' - 'a memcache_security_strategy is defined') + raise ConfigurationError('mecmache_secret_key must be defined ' + 'when a memcache_security_strategy ' + 'is defined') def _init_cache(self, env): cache = self._conf_get('cache') diff --git a/keystoneclient/tests/test_auth_token_middleware.py b/keystoneclient/tests/test_auth_token_middleware.py index 4f4c594a0..037029320 100644 --- a/keystoneclient/tests/test_auth_token_middleware.py +++ b/keystoneclient/tests/test_auth_token_middleware.py @@ -751,31 +751,36 @@ class CommonAuthTokenMiddlewareTest(object): 'memcached_servers': ['localhost:11211'], 'memcache_security_strategy': 'Encrypt' } - self.assertRaises(Exception, self.set_middleware, conf=conf) + self.assertRaises(auth_token.ConfigurationError, self.set_middleware, + conf=conf) # test invalue memcache_security_strategy conf = { 'memcached_servers': ['localhost:11211'], 'memcache_security_strategy': 'whatever' } - self.assertRaises(Exception, self.set_middleware, conf=conf) + self.assertRaises(auth_token.ConfigurationError, self.set_middleware, + conf=conf) # test missing memcache_secret_key conf = { 'memcached_servers': ['localhost:11211'], 'memcache_security_strategy': 'mac' } - self.assertRaises(Exception, self.set_middleware, conf=conf) + self.assertRaises(auth_token.ConfigurationError, self.set_middleware, + conf=conf) conf = { 'memcached_servers': ['localhost:11211'], 'memcache_security_strategy': 'Encrypt', 'memcache_secret_key': '' } - self.assertRaises(Exception, self.set_middleware, conf=conf) + self.assertRaises(auth_token.ConfigurationError, self.set_middleware, + conf=conf) conf = { 'memcached_servers': ['localhost:11211'], 'memcache_security_strategy': 'mAc', 'memcache_secret_key': '' } - self.assertRaises(Exception, self.set_middleware, conf=conf) + self.assertRaises(auth_token.ConfigurationError, self.set_middleware, + conf=conf) def test_config_revocation_cache_timeout(self): conf = { diff --git a/keystoneclient/tests/v3/test_endpoints.py b/keystoneclient/tests/v3/test_endpoints.py index 673452396..050a646ec 100644 --- a/keystoneclient/tests/v3/test_endpoints.py +++ b/keystoneclient/tests/v3/test_endpoints.py @@ -14,6 +14,7 @@ import uuid +from keystoneclient import exceptions from keystoneclient.tests.v3 import utils from keystoneclient.v3 import endpoints @@ -49,7 +50,7 @@ class EndpointTests(utils.TestCase, utils.CrudTests): def test_create_invalid_interface(self): ref = self.new_ref(interface=uuid.uuid4().hex) - self.assertRaises(Exception, self.manager.create, + self.assertRaises(exceptions.ValidationError, self.manager.create, **utils.parameterize(ref)) def test_update_public_interface(self): @@ -66,7 +67,8 @@ class EndpointTests(utils.TestCase, utils.CrudTests): def test_update_invalid_interface(self): ref = self.new_ref(interface=uuid.uuid4().hex) - self.assertRaises(Exception, self.manager.update, + ref['endpoint'] = "fake_endpoint" + self.assertRaises(exceptions.ValidationError, self.manager.update, **utils.parameterize(ref)) def test_list_public_interface(self): @@ -87,5 +89,5 @@ class EndpointTests(utils.TestCase, utils.CrudTests): def test_list_invalid_interface(self): interface = uuid.uuid4().hex expected_path = 'v3/%s?interface=%s' % (self.collection_key, interface) - self.assertRaises(Exception, self.manager.list, + self.assertRaises(exceptions.ValidationError, self.manager.list, expected_path=expected_path, interface=interface) diff --git a/keystoneclient/v3/endpoints.py b/keystoneclient/v3/endpoints.py index 7030d287a..4950fa588 100644 --- a/keystoneclient/v3/endpoints.py +++ b/keystoneclient/v3/endpoints.py @@ -15,6 +15,7 @@ # under the License. from keystoneclient import base +from keystoneclient import exceptions VALID_INTERFACES = ['public', 'admin', 'internal'] @@ -45,7 +46,7 @@ class EndpointManager(base.CrudManager): if interface is not None and interface not in VALID_INTERFACES: msg = '"interface" must be one of: %s' msg = msg % ', '.join(VALID_INTERFACES) - raise Exception(msg) + raise exceptions.ValidationError(msg) def create(self, service, url, interface=None, region=None, enabled=True, **kwargs): diff --git a/tox.ini b/tox.ini index bc0144a25..c41c8b8b2 100644 --- a/tox.ini +++ b/tox.ini @@ -27,9 +27,8 @@ commands = python setup.py testr --coverage --testr-args='{posargs}' downloadcache = ~/cache/pip [flake8] -# H202: assertRaises Exception too broad # F821: undefined name # H304: no relative imports -ignore = F821,H202,H304 +ignore = F821,H304 show-source = True exclude = .venv,.tox,dist,doc,*egg,build