From 747fa2d68d91411992871be9ef6b8fff757afb3d Mon Sep 17 00:00:00 2001 From: James Page Date: Tue, 26 May 2020 09:22:25 +0100 Subject: [PATCH] Misc fixes for charm infra Update pydev project definition to use python 3. Drop upper bound on flake8. Tidy misc lint across codebase with newer flake8. Change-Id: I9637ac603cb3801c9e3ffa8c2b0897968d42ada5 --- .pydevproject | 19 +++++++++---- actions/hugepagereport.py | 2 ++ actions/openstack_upgrade.py | 2 ++ actions/pause_resume.py | 1 + actions/security_checklist.py | 2 ++ hooks/nova_compute_context.py | 40 +++++++++++++-------------- hooks/nova_compute_utils.py | 4 +-- test-requirements.txt | 2 +- tox.ini | 2 +- unit_tests/test_nova_compute_utils.py | 2 +- unit_tests/test_utils.py | 6 ++-- 11 files changed, 48 insertions(+), 34 deletions(-) diff --git a/.pydevproject b/.pydevproject index a69fbde8..494f7310 100644 --- a/.pydevproject +++ b/.pydevproject @@ -1,9 +1,16 @@ -python 2.7 -Default - -/nova-compute/hooks -/nova-compute/unit_tests - + + python interpreter + + python3 + + + + /nova-compute/hooks + + /nova-compute/unit_tests + + + diff --git a/actions/hugepagereport.py b/actions/hugepagereport.py index 3966e3a6..ec2f2206 100755 --- a/actions/hugepagereport.py +++ b/actions/hugepagereport.py @@ -25,6 +25,7 @@ def _add_path(path): if path not in sys.path: sys.path.insert(1, path) + _add_path(_hooks) @@ -58,5 +59,6 @@ def hugepages_report(): return hookenv.action_set(outmap) + if __name__ == '__main__': hugepages_report() diff --git a/actions/openstack_upgrade.py b/actions/openstack_upgrade.py index f8d9702c..63df71f9 100755 --- a/actions/openstack_upgrade.py +++ b/actions/openstack_upgrade.py @@ -26,6 +26,7 @@ def _add_path(path): if path not in sys.path: sys.path.insert(1, path) + _add_path(_hooks) @@ -65,5 +66,6 @@ def openstack_upgrade(): # upgrade) then the config_changed() function is a no-op config_changed() + if __name__ == '__main__': openstack_upgrade() diff --git a/actions/pause_resume.py b/actions/pause_resume.py index 54e3a54c..cddb2864 100755 --- a/actions/pause_resume.py +++ b/actions/pause_resume.py @@ -25,6 +25,7 @@ def _add_path(path): if path not in sys.path: sys.path.insert(1, path) + _add_path(_hooks) diff --git a/actions/security_checklist.py b/actions/security_checklist.py index 4552e697..8fdb7925 100755 --- a/actions/security_checklist.py +++ b/actions/security_checklist.py @@ -26,6 +26,7 @@ def _add_path(path): if path not in sys.path: sys.path.insert(1, path) + _add_path(_hooks) @@ -84,5 +85,6 @@ def main(): [config['config_path'], config['config_file']]) return audits.action_parse_results(audits.run(config)) + if __name__ == "__main__": sys.exit(main()) diff --git a/hooks/nova_compute_context.py b/hooks/nova_compute_context.py index 7fb04f4e..a67afd92 100644 --- a/hooks/nova_compute_context.py +++ b/hooks/nova_compute_context.py @@ -97,32 +97,32 @@ def _get_availability_zone(): def _neutron_security_groups(): - ''' - Inspects current cloud-compute relation and determine if nova-c-c has - instructed us to use neutron security groups. - ''' - for rid in relation_ids('cloud-compute'): - for unit in related_units(rid): - groups = [ - relation_get('neutron_security_groups', - rid=rid, unit=unit), - relation_get('quantum_security_groups', - rid=rid, unit=unit) - ] - if ('yes' in groups or 'Yes' in groups): - return True - return False + ''' + Inspects current cloud-compute relation and determine if nova-c-c has + instructed us to use neutron security groups. + ''' + for rid in relation_ids('cloud-compute'): + for unit in related_units(rid): + groups = [ + relation_get('neutron_security_groups', + rid=rid, unit=unit), + relation_get('quantum_security_groups', + rid=rid, unit=unit) + ] + if ('yes' in groups or 'Yes' in groups): + return True + return False def _neutron_plugin(): - from nova_compute_utils import neutron_plugin - return neutron_plugin() + from nova_compute_utils import neutron_plugin + return neutron_plugin() def _neutron_url(rid, unit): - # supports legacy relation settings. - return (relation_get('neutron_url', rid=rid, unit=unit) or - relation_get('quantum_url', rid=rid, unit=unit)) + # supports legacy relation settings. + return (relation_get('neutron_url', rid=rid, unit=unit) or + relation_get('quantum_url', rid=rid, unit=unit)) def nova_metadata_requirement(): diff --git a/hooks/nova_compute_utils.py b/hooks/nova_compute_utils.py index 6ab27015..f027adb0 100644 --- a/hooks/nova_compute_utils.py +++ b/hooks/nova_compute_utils.py @@ -573,7 +573,7 @@ def public_ssh_key(user='root'): try: with open(os.path.join(home, '.ssh', 'id_rsa.pub')) as key: return key.read().strip() - except: + except OSError: return None @@ -751,7 +751,7 @@ def _libvirt_network_exec(netname, action): return for line in out[2:]: - res = re.search("^\s+{} ".format(netname), line) + res = re.search(r"^\s+{} ".format(netname), line) if res: check_call(['virsh', 'net-{}'.format(action), netname]) return diff --git a/test-requirements.txt b/test-requirements.txt index 7d9c2587..44b50231 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -10,7 +10,7 @@ charm-tools>=2.4.4 requests>=2.18.4 mock>=1.2 -flake8>=2.2.4,<=2.4.1 +flake8>=2.2.4 stestr>=2.2.0 coverage>=4.5.2 pyudev # for ceph-* charm unit tests (need to fix the ceph-* charm unit tests/mocking) diff --git a/tox.ini b/tox.ini index b835733a..8080ba6d 100644 --- a/tox.ini +++ b/tox.ini @@ -116,5 +116,5 @@ commands = functest-run-suite --keep-model --bundle {posargs} [flake8] -ignore = E402,E226 +ignore = E402,E226,W504 exclude = */charmhelpers diff --git a/unit_tests/test_nova_compute_utils.py b/unit_tests/test_nova_compute_utils.py index 8ed99543..123a68e4 100644 --- a/unit_tests/test_nova_compute_utils.py +++ b/unit_tests/test_nova_compute_utils.py @@ -534,7 +534,7 @@ class NovaComputeUtilsTests(CharmTestCase): @patch('builtins.open') @patch('pwd.getpwnam') def test_public_ssh_key_not_found(self, getpwnam, _open): - _open.side_effect = Exception + _open.side_effect = OSError getpwnam.return_value = self.fake_user('foo') self.assertEqual(None, utils.public_ssh_key()) diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index d4d5fb80..77e40e5a 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -101,9 +101,9 @@ class TestConfig(object): return self.config def set(self, attr, value): - if attr not in self.config: - raise KeyError - self.config[attr] = value + if attr not in self.config: + raise KeyError + self.config[attr] = value class TestRelation(object):