From a862aa0f3d81ea63e1d00d57f1163b2bde70cfd8 Mon Sep 17 00:00:00 2001 From: hussainchachuliya Date: Thu, 29 Sep 2016 19:59:03 +0530 Subject: [PATCH] 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 --- HACKING.rst | 2 ++ nova/hacking/checks.py | 20 ++++++++++++++++++++ nova/tests/unit/test_hacking.py | 16 ++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index ed417c9b468e..45ed114b60dd 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -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 ------------------- diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index 15e3d48ae0a0..963a910d30a2 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -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) diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index 96a4616032cb..6c4ff7d94b6e 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -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)