diff --git a/HACKING.rst b/HACKING.rst index d8c634edfc..3045daf3a0 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -26,6 +26,7 @@ Manila Specific Commandments - [M336] Must use a dict comprehension instead of a dict constructor with a sequence of key-value pairs. - [M337] Ensure to not use xrange(). +- [M354] Use oslo_utils.uuidutils to generate UUID instead of uuid4(). LOG Translations ---------------- diff --git a/manila/hacking/checks.py b/manila/hacking/checks.py index 1778293ae2..068026a3e3 100644 --- a/manila/hacking/checks.py +++ b/manila/hacking/checks.py @@ -342,6 +342,24 @@ def validate_assertIsNone(logical_line): yield(0, msg) +def check_uuid4(logical_line): + """Generating UUID + + Use oslo_utils.uuidutils to generate UUID instead of uuid4(). + + M354 + """ + + msg = ("M354: Use oslo_utils.uuidutils to generate UUID instead " + "of uuid4().") + + if "uuid4()." in logical_line: + return + + if "uuid4()" in logical_line: + yield (0, msg) + + def factory(register): register(validate_log_translations) register(check_explicit_underscore_import) @@ -354,3 +372,4 @@ def factory(register): register(no_xrange) register(validate_assertTrue) register(validate_assertIsNone) + register(check_uuid4) diff --git a/manila/tests/test_hacking.py b/manila/tests/test_hacking.py index b6093d319b..bfe773c3c5 100644 --- a/manila/tests/test_hacking.py +++ b/manila/tests/test_hacking.py @@ -316,3 +316,16 @@ class HackingTestCase(test.TestCase): "assertIsNone(None)")))) self.assertEqual(1, len(list(checks.validate_assertIsNone( "assertEqual(None, %s)" % test_value)))) + + def test_check_uuid4(self): + code = """ + fake_uuid = uuid.uuid4() + """ + errors = [(1, 0, 'M354')] + self._assert_has_errors(code, checks.check_uuid4, + expected_errors=errors) + + code = """ + hex_uuid = uuid.uuid4().hex + """ + self._assert_has_no_errors(code, checks.check_uuid4)