diff --git a/cinder/tests/hacking/checks.py b/cinder/tests/hacking/checks.py index 87c515fc8ea..f50853220f2 100644 --- a/cinder/tests/hacking/checks.py +++ b/cinder/tests/hacking/checks.py @@ -372,3 +372,18 @@ def validate_assertTrue(logical_line, filename): msg = ("C313: Unit tests should use assertTrue(value) instead" " of using assertEqual(True, value).") yield(0, msg) + + +third_party_mock = re.compile("^import.mock") +from_third_party_mock = re.compile("^from.mock.import") + + +@core.flake8ext +def no_third_party_mock(logical_line): + # We should only use unittest.mock, not the third party mock library that + # was needed for py2 support. + if (re.match(third_party_mock, logical_line) or + re.match(from_third_party_mock, logical_line)): + msg = ('C337: Unit tests should use the standard library "mock" ' + 'module, not the third party mock lib.') + yield(0, msg) diff --git a/cinder/tests/unit/test_hacking.py b/cinder/tests/unit/test_hacking.py index 6748daacd31..a770ff1345b 100644 --- a/cinder/tests/unit/test_hacking.py +++ b/cinder/tests/unit/test_hacking.py @@ -293,3 +293,13 @@ class HackingTestCase(test.TestCase): def test_no_test_log(self, first, second, third, fourth): self.assertEqual(first, len(list(checks.no_test_log( "%s('arg')" % second, third, fourth)))) + + @ddt.unpack + @ddt.data( + (1, 'import mock'), + (0, 'from unittest import mock'), + (1, 'from mock import patch'), + (0, 'from unittest.mock import patch')) + def test_no_third_party_mock(self, err_count, line): + self.assertEqual(err_count, len(list(checks.no_third_party_mock( + line)))) diff --git a/cinder/tests/unit/volume/drivers/test_prophetstor_dpl.py b/cinder/tests/unit/volume/drivers/test_prophetstor_dpl.py index f8eeeaa1b1e..2fb5c9d8cdd 100644 --- a/cinder/tests/unit/volume/drivers/test_prophetstor_dpl.py +++ b/cinder/tests/unit/volume/drivers/test_prophetstor_dpl.py @@ -14,8 +14,8 @@ import errno import re +from unittest import mock -import mock from oslo_utils import units from six.moves import http_client diff --git a/tox.ini b/tox.ini index be741db7a0d..fc0409fb779 100644 --- a/tox.ini +++ b/tox.ini @@ -213,6 +213,7 @@ extension = C312 = checks:no_translate_logs C313 = checks:validate_assertTrue C336 = checks:dict_constructor_with_list_copy + C337 = checks:no_third_party_mock paths = ./cinder/tests/hacking [doc8]