From 8d0b16292d0bb00f9fb61c6a05cb7f01ff610f34 Mon Sep 17 00:00:00 2001 From: Tin Lam Date: Thu, 10 Mar 2016 19:10:46 -0600 Subject: [PATCH] Add hacking check to ensure not use xrange() Added hacking check to ensure not to use xrange. Also, fixed issues with EoF missing newline [W391]. Change-Id: Iba8d240c042e46cb34eb6ed057534d62efb6f903 Closes-Bug: #1538118 --- HACKING.rst | 1 + octavia/api/app.py | 2 +- octavia/api/v1/controllers/load_balancer.py | 2 +- .../controller/healthmanager/update_serializer.py | 2 +- octavia/hacking/checks.py | 12 ++++++++++++ .../tests/unit/certificates/generator/local_csr.py | 2 +- .../tests/unit/certificates/generator/test_local.py | 2 +- .../healthmanager/test_update_serializer.py | 2 +- .../unit/controller/worker/tasks/test_cert_task.py | 2 +- .../controller/worker/tasks/test_database_tasks.py | 2 +- octavia/tests/unit/test_hacking.py | 7 +++++++ 11 files changed, 28 insertions(+), 8 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index cb0e6c44dd..6411036753 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -23,6 +23,7 @@ Octavia Specific Commandments assertEqual(A in B, False) or assertEqual(False, A in B) to the more specific assertIn/NotIn(A, B) - [O339] LOG.warn() is not allowed. Use LOG.warning() +- [O340] Don't use xrange() Creating Unit Tests ------------------- diff --git a/octavia/api/app.py b/octavia/api/app.py index 2ea9f855c3..4d6e742abc 100644 --- a/octavia/api/app.py +++ b/octavia/api/app.py @@ -37,4 +37,4 @@ def setup_app(pecan_config=None, debug=False): debug=debug, hooks=app_hooks, wsme=pecan_config.wsme - ) \ No newline at end of file + ) diff --git a/octavia/api/v1/controllers/load_balancer.py b/octavia/api/v1/controllers/load_balancer.py index dfb5982853..8ef9b7eedf 100644 --- a/octavia/api/v1/controllers/load_balancer.py +++ b/octavia/api/v1/controllers/load_balancer.py @@ -199,4 +199,4 @@ class LBCascadeDeleteController(LoadBalancersController): @wsme_pecan.wsexpose(None, status_code=202) def delete(self): """Deletes a load balancer.""" - return self._delete(self.lb_id, cascade=True) \ No newline at end of file + return self._delete(self.lb_id, cascade=True) diff --git a/octavia/controller/healthmanager/update_serializer.py b/octavia/controller/healthmanager/update_serializer.py index 1d5f3e83e8..45bb841b12 100644 --- a/octavia/controller/healthmanager/update_serializer.py +++ b/octavia/controller/healthmanager/update_serializer.py @@ -44,4 +44,4 @@ class InfoContainer(object): return True def __ne__(self, other): - return not self == other \ No newline at end of file + return not self == other diff --git a/octavia/hacking/checks.py b/octavia/hacking/checks.py index 79e38b2624..6bbfdbd9b4 100644 --- a/octavia/hacking/checks.py +++ b/octavia/hacking/checks.py @@ -73,6 +73,8 @@ assert_not_equal_end_with_none_re = re.compile( r"(.)*assertNotEqual\(.+, None\)") assert_not_equal_start_with_none_re = re.compile( r"(.)*assertNotEqual\(None, .+\)") +assert_no_xrange_re = re.compile( + r"\s*xrange\s*\(") def _directory_to_check_translation(filename): @@ -209,6 +211,15 @@ def no_log_warn(logical_line): yield(0, "O339:Use LOG.warning() rather than LOG.warn()") +def no_xrange(logical_line): + """Disallow 'xrange()' + + O340 + """ + if assert_no_xrange_re.match(logical_line): + yield(0, "O340: Do not use xrange().") + + def factory(register): register(assert_true_instance) register(assert_equal_or_not_none) @@ -220,3 +231,4 @@ def factory(register): register(no_mutable_default_args) register(assert_equal_in) register(no_log_warn) + register(no_xrange) diff --git a/octavia/tests/unit/certificates/generator/local_csr.py b/octavia/tests/unit/certificates/generator/local_csr.py index 88c7d72289..e6e86b7cd2 100644 --- a/octavia/tests/unit/certificates/generator/local_csr.py +++ b/octavia/tests/unit/certificates/generator/local_csr.py @@ -112,4 +112,4 @@ class BaseLocalCSRTestCase(base.TestCase): cn=cn, validity=2 * 365 * 24 * 60 * 60, ) - self.assertTrue(m.called) \ No newline at end of file + self.assertTrue(m.called) diff --git a/octavia/tests/unit/certificates/generator/test_local.py b/octavia/tests/unit/certificates/generator/test_local.py index 9a45d84a6d..db310fbb2b 100644 --- a/octavia/tests/unit/certificates/generator/test_local.py +++ b/octavia/tests/unit/certificates/generator/test_local.py @@ -135,4 +135,4 @@ class TestLocalGenerator(local_csr.BaseLocalCSRTestCase): data=cert_object.private_key, password=cert_object.private_key_passphrase, backend=backends.default_backend()) - self.assertIsNotNone(key) \ No newline at end of file + self.assertIsNotNone(key) diff --git a/octavia/tests/unit/controller/healthmanager/test_update_serializer.py b/octavia/tests/unit/controller/healthmanager/test_update_serializer.py index 0b81842470..b5bf2464da 100644 --- a/octavia/tests/unit/controller/healthmanager/test_update_serializer.py +++ b/octavia/tests/unit/controller/healthmanager/test_update_serializer.py @@ -36,4 +36,4 @@ class TestUpdateSerializer(base.TestCase): obj_payload = {'test': [3, 2, 1, 6], id: obj_id, 'x': {'y': 1}} obj = update_serializer.InfoContainer(obj_id, obj_type, obj_payload) cloned_obj = update_serializer.InfoContainer.from_dict(obj.to_dict()) - self.assertEqual(obj, cloned_obj) \ No newline at end of file + self.assertEqual(obj, cloned_obj) diff --git a/octavia/tests/unit/controller/worker/tasks/test_cert_task.py b/octavia/tests/unit/controller/worker/tasks/test_cert_task.py index 4548c95e74..d32a360604 100644 --- a/octavia/tests/unit/controller/worker/tasks/test_cert_task.py +++ b/octavia/tests/unit/controller/worker/tasks/test_cert_task.py @@ -30,4 +30,4 @@ class TestCertTasks(base.TestCase): self.assertEqual( pem, dummy_cert.get_certificate() + dummy_cert.get_private_key()) mock_driver.generate_cert_key_pair.assert_called_once_with( - cn='123', validity=cert_task.CERT_VALIDITY) \ No newline at end of file + cn='123', validity=cert_task.CERT_VALIDITY) diff --git a/octavia/tests/unit/controller/worker/tasks/test_database_tasks.py b/octavia/tests/unit/controller/worker/tasks/test_database_tasks.py index 86b2ed9c78..bcbfc4b92f 100644 --- a/octavia/tests/unit/controller/worker/tasks/test_database_tasks.py +++ b/octavia/tests/unit/controller/worker/tasks/test_database_tasks.py @@ -1448,4 +1448,4 @@ class TestDatabaseTasks(base.TestCase): # Test the revert mock_listener_repo_update.reset_mock() - update_server_group_info.revert(LB_ID, SERVER_GROUP_ID) \ No newline at end of file + update_server_group_info.revert(LB_ID, SERVER_GROUP_ID) diff --git a/octavia/tests/unit/test_hacking.py b/octavia/tests/unit/test_hacking.py index f8af9d93a3..c8077b555e 100644 --- a/octavia/tests/unit/test_hacking.py +++ b/octavia/tests/unit/test_hacking.py @@ -135,3 +135,10 @@ class HackingTestCase(base.BaseTestCase): self.assertEqual(0, len(list(checks.no_log_warn( "LOG.warning()")))) + + def test_no_xrange(self): + self.assertEqual(1, len(list(checks.no_xrange( + "xrange(45)")))) + + self.assertEqual(0, len(list(checks.no_xrange( + "range(45)"))))