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
This commit is contained in:
Davanum Srinivas
2015-01-08 11:38:28 -05:00
parent 3c27e2c8f5
commit c3d10b7a62
5 changed files with 27 additions and 3 deletions

View File

@@ -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
-------------------

View File

@@ -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)

View File

@@ -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,

View File

@@ -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):

View File

@@ -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