]> xenbits.xensource.com Git - osstest/openstack-nova.git/commitdiff
hacking: Use uuidutils or uuidsentinel to generate UUID
authorhussainchachuliya <hussain.chachuliya@nttdata.com>
Thu, 29 Sep 2016 14:29:03 +0000 (19:59 +0530)
committerhussainchachuliya <hussain.chachuliya@nttdata.com>
Tue, 29 Nov 2016 06:19:24 +0000 (11:49 +0530)
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
nova/hacking/checks.py
nova/tests/unit/test_hacking.py

index ed417c9b468e4937b5372a8360fcc68cbc307197..45ed114b60dd03ece508ae45178e5ac5db34fdfb 100644 (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
 -------------------
index 15e3d48ae0a0e62bf7e47ce9ef37a807c85c8a1b..963a910d30a2c5da1ea2815a58ae30d9b9a8a928 100644 (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)
index 96a4616032cb5fb3bdfeb3dae2f3fc007d0dd2e9..6c4ff7d94b6e33a8ac4fb047a00804aab2f48eec 100644 (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)