From c3d10b7a62ad2032cde401f91e764c59d2e11b44 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 8 Jan 2015 11:38:28 -0500 Subject: [PATCH] Do not use deprecated assertRaisesRegexp() The unit test log ends up with DeprecationWarning(s) from the outdated calls to assertRaisesRegexp. We should switch to using assertRaisesRegex instead. This commit eliminates these warnings from the log and the hacking rule N344 ensures that folks don't end up adding fresh code down the line with the outdated assertRaisesRegexp as well Partial-Bug: #1407736 Change-Id: Ifba672f7568d5159c63bf88c534812e4e3a26d5a --- HACKING.rst | 1 + nova/hacking/checks.py | 13 +++++++++++++ nova/tests/unit/compute/test_claims.py | 4 ++-- nova/tests/unit/objects/test_objects.py | 2 +- nova/tests/unit/test_hacking.py | 10 ++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 67cd95218535..0f8b94f9f34d 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -46,6 +46,7 @@ Nova Specific Commandments - [N333] Check for oslo library imports use the non-namespaced packages - [N334] Change assertTrue/False(A in/not in B, message) to the more specific assertIn/NotIn(A, B, message) +- [N335] Check for usage of deprecated assertRaisesRegexp Creating Unit Tests ------------------- diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index 6eb63ce46a6f..d49b6ebc0be6 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -70,6 +70,7 @@ asse_true_false_with_in_or_not_in = re.compile(r"assert(True|False)\(" asse_true_false_with_in_or_not_in_spaces = re.compile(r"assert(True|False)" r"\((\w|[][.'\"])+( not)? in [\[|'|\"](\w|[][.'\", ])+" r"[\[|'|\"](, .*)?\)") +asse_raises_regexp = re.compile(r"assertRaisesRegexp\(") conf_attribute_set_re = re.compile(r"CONF\.[a-z0-9_.]+\s*=\s*\w") log_translation = re.compile( r"(.)*LOG\.(audit|error|critical)\(\s*('|\")") @@ -478,6 +479,17 @@ def assert_true_or_false_with_in(logical_line): "contents.") +def assert_raises_regexp(logical_line): + """Check for usage of deprecated assertRaisesRegexp + + N335 + """ + res = asse_raises_regexp.search(logical_line) + if res: + yield (0, "N335: assertRaisesRegex must be used instead " + "of assertRaisesRegexp") + + def factory(register): register(import_no_db_in_virt) register(no_db_session_in_public_api) @@ -489,6 +501,7 @@ def factory(register): register(assert_true_instance) register(assert_equal_type) register(assert_equal_none) + register(assert_raises_regexp) register(no_translate_debug_logs) register(no_setting_conf_directly_in_tests) register(validate_log_translations) diff --git a/nova/tests/unit/compute/test_claims.py b/nova/tests/unit/compute/test_claims.py index ad9017c01b28..cdcde67f8ea3 100644 --- a/nova/tests/unit/compute/test_claims.py +++ b/nova/tests/unit/compute/test_claims.py @@ -172,14 +172,14 @@ class ClaimTestCase(test.NoDBTestCase): def test_disk_insufficient(self, mock_get): limits = {'disk_gb': 45} - self.assertRaisesRegexp( + self.assertRaisesRegex( exception.ComputeResourcesUnavailable, "disk", self._claim, limits=limits, root_gb=10, ephemeral_gb=40) def test_disk_and_memory_insufficient(self, mock_get): limits = {'disk_gb': 45, 'memory_mb': 8192} - self.assertRaisesRegexp( + self.assertRaisesRegex( exception.ComputeResourcesUnavailable, "memory.*disk", self._claim, limits=limits, root_gb=10, ephemeral_gb=40, diff --git a/nova/tests/unit/objects/test_objects.py b/nova/tests/unit/objects/test_objects.py index 9eb2f7aafbf4..5090265bbfd6 100644 --- a/nova/tests/unit/objects/test_objects.py +++ b/nova/tests/unit/objects/test_objects.py @@ -513,7 +513,7 @@ class _TestObject(object): class Foo(base.NovaObject): fields = {'foobar': fields.Field(fields.Integer())} obj = Foo() - with self.assertRaisesRegexp(NotImplementedError, ".*foobar.*"): + with self.assertRaisesRegex(NotImplementedError, ".*foobar.*"): obj.foobar def test_loaded_in_primitive(self): diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index 2f00182182b3..e20ce8c05f4c 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -410,6 +410,16 @@ class HackingTestCase(test.NoDBTestCase): self._assert_has_errors(code, checks.check_oslo_namespace_imports, expected_errors=[(1, 0, "N333")]) + def test_oslo_assert_raises_regexp(self): + code = """ + self.assertRaisesRegexp(ValueError, + "invalid literal for.*XYZ'$", + int, + 'XYZ') + """ + self._assert_has_errors(code, checks.assert_raises_regexp, + expected_errors=[(1, 0, "N335")]) + def test_trans_add(self): checker = checks.CheckForTransAdd