hacking: Use uuidutils or uuidsentinel to generate UUID

Added hacking check to ensure that UUID is being generated from
oslo_utils.uuidutils or uuidsentinel(in case of test cases)
instead of uuid4().

Change-Id: I73ee63fbd4f451d3aa5dc1a2a734d68c308b4440
This commit is contained in:
hussainchachuliya 2016-09-29 19:59:03 +05:30
parent fd99bd7e9b
commit a862aa0f3d
3 changed files with 38 additions and 0 deletions

View File

@ -67,6 +67,8 @@ Nova Specific Commandments
- [N354] String interpolation should be delayed at logging calls.
- [N355] Enforce use of assertTrue/assertFalse
- [N356] Enforce use of assertIs/assertIsNot
- [N357] Use oslo_utils.uuidutils or uuidsentinel(in case of test cases) to
generate UUID instead of uuid4().
Creating Unit Tests
-------------------

View File

@ -856,6 +856,25 @@ def no_assert_true_false_is_not(logical_line):
"Use assertIs(A, B) or assertIsNot(A, B) instead")
def check_uuid4(logical_line):
"""Generating UUID
Use oslo_utils.uuidutils or uuidsentinel(in case of test cases) to generate
UUID instead of uuid4().
N357
"""
msg = ("N357: Use oslo_utils.uuidutils or uuidsentinel(in case of test "
"cases) to generate UUID instead of uuid4().")
if "uuid4()." in logical_line:
return
if "uuid4()" in logical_line:
yield (0, msg)
def factory(register):
register(import_no_db_in_virt)
register(no_db_session_in_public_api)
@ -899,3 +918,4 @@ def factory(register):
register(check_delayed_string_interpolation)
register(no_assert_equal_true_false)
register(no_assert_true_false_is_not)
register(check_uuid4)

View File

@ -877,3 +877,19 @@ class HackingTestCase(test.NoDBTestCase):
(4, 0, 'N356')]
self._assert_has_errors(code, checks.no_assert_true_false_is_not,
expected_errors=errors)
def test_check_uuid4(self):
code = """
fake_uuid = uuid.uuid4()
"""
errors = [(1, 0, 'N357')]
self._assert_has_errors(code, checks.check_uuid4,
expected_errors=errors)
code = """
hex_uuid = uuid.uuid4().hex
int_uuid = uuid.uuid4().int
urn_uuid = uuid.uuid4().urn
variant_uuid = uuid.uuid4().variant
version_uuid = uuid.uuid4().version
"""
self._assert_has_no_errors(code, checks.check_uuid4)