From 25fe81e298095fcd2cd2d3ba9884eeb475dc0615 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Thu, 3 Feb 2022 18:13:08 +0000 Subject: [PATCH] lint fixes --- charms/ovn-central-k8s/src/charm.py | 57 ++++++++++++---------- charms/ovn-central-k8s/src/ovn.py | 12 +++-- charms/ovn-central-k8s/src/ovsdb.py | 6 ++- charms/ovn-central-k8s/tests/test_charm.py | 33 ------------- 4 files changed, 42 insertions(+), 66 deletions(-) diff --git a/charms/ovn-central-k8s/src/charm.py b/charms/ovn-central-k8s/src/charm.py index de7c5c1c..24968c57 100755 --- a/charms/ovn-central-k8s/src/charm.py +++ b/charms/ovn-central-k8s/src/charm.py @@ -4,19 +4,15 @@ This charm provide Glance services as part of an OpenStack deployment """ -import os import ovn import ovsdb as ch_ovsdb -import ipaddress import logging -import itertools from typing import List import ops.charm from ops.framework import StoredState from ops.main import main -import advanced_sunbeam_openstack.cprocess as sunbeam_cprocess import advanced_sunbeam_openstack.charm as sunbeam_charm import advanced_sunbeam_openstack.core as sunbeam_core import advanced_sunbeam_openstack.relation_handlers as sunbeam_rhandlers @@ -33,8 +29,7 @@ logger = logging.getLogger(__name__) OVN_SB_DB_CONTAINER = "ovn-sb-db-server" OVN_NB_DB_CONTAINER = "ovn-nb-db-server" OVN_NORTHD_CONTAINER = "ovn-northd" - - +OVN_DB_CONTAINERS = [OVN_SB_DB_CONTAINER, OVN_NB_DB_CONTAINER] class OVNDBConfigContext(sunbeam_ctxts.ConfigContext): @@ -275,13 +270,15 @@ class OVNCentralOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): def get_pebble_executor(self, container_name): container = self.unit.get_container( container_name) + def _run_via_pebble(*args): process = container.exec(list(args), timeout=5*60) out, warnings = process.wait_output() if warnings: - for line in warnings.splitlines(): - logger.warning('CMD Out: %s', line.strip()) + for line in warnings.splitlines(): + logger.warning('CMD Out: %s', line.strip()) return out + return _run_via_pebble def cluster_status(self, db, cmd_executor): @@ -318,9 +315,12 @@ class OVNCentralOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): executor = self.get_pebble_executor(OVN_NB_DB_CONTAINER) elif db == 'sb': executor = self.get_pebble_executor(OVN_SB_DB_CONTAINER) - status = self.cluster_status('ovn{}_db'.format(db), cmd_executor=executor) + status = self.cluster_status( + 'ovn{}_db'.format(db), + cmd_executor=executor) if status and status.is_cluster_leader: - logging.debug('configure_ovn_listener is_cluster_leader {}'.format(db)) + logging.debug( + 'configure_ovn_listener is_cluster_leader {}'.format(db)) connections = ch_ovsdb.SimpleOVSDB( 'ovn-{}ctl'.format(db), cmd_executor=executor).connection @@ -376,7 +376,8 @@ class OVNCentralOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): self.unit.status = ops.model.WaitingStatus( "Waiting for data from leader") return - logging.debug("Remote leader is ready and has supplied all data needed") + logging.debug( + "Remote leader is ready and has supplied all data needed") if not self.relation_handlers_ready(): logging.debug("Aborting charm relations not ready") @@ -396,7 +397,7 @@ class OVNCentralOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): if self.unit.is_leader(): # Start services in North/South containers on lead unit logging.debug("Starting services in DB containers") - for ph in self.get_named_pebble_handlers([OVN_SB_DB_CONTAINER, OVN_NB_DB_CONTAINER]): + for ph in self.get_named_pebble_handlers(OVN_DB_CONTAINERS): ph.start_service() # Attempt to setup listers etc self.configure_ovn() @@ -413,10 +414,11 @@ class OVNCentralOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): 'sb_cid': str(sb_status.cluster_id), }) self.unit.status = ops.model.ActiveStatus() - else: + else: logging.debug("Attempting to join OVN_Northbound cluster") container = self.unit.get_container(OVN_NB_DB_CONTAINER) - process = container.exec(['bash', '/root/ovn-nb-cluster-join.sh'], timeout=5*60) + process = container.exec( + ['bash', '/root/ovn-nb-cluster-join.sh'], timeout=5*60) out, warnings = process.wait_output() if warnings: for line in warnings.splitlines(): @@ -424,39 +426,40 @@ class OVNCentralOperatorCharm(sunbeam_charm.OSBaseOperatorCharm): logging.debug("Attempting to join OVN_Southbound cluster") container = self.unit.get_container(OVN_SB_DB_CONTAINER) - process = container.exec(['bash', '/root/ovn-sb-cluster-join.sh'], timeout=5*60) + process = container.exec( + ['bash', '/root/ovn-sb-cluster-join.sh'], timeout=5*60) out, warnings = process.wait_output() if warnings: for line in warnings.splitlines(): logger.warning('CMD Out: %s', line.strip()) logging.debug("Starting services in DB containers") - for ph in self.get_named_pebble_handlers([OVN_SB_DB_CONTAINER, OVN_NB_DB_CONTAINER]): + for ph in self.get_named_pebble_handlers(OVN_DB_CONTAINERS): ph.start_service() # Attempt to setup listers etc self.configure_ovn() self.unit.status = ops.model.ActiveStatus() - + def configure_ovn(self): inactivity_probe = int( self.config['ovsdb-server-inactivity-probe']) * 1000 self.configure_ovn_listener( 'nb', { - self.ovsdb_cms.db_nb_port: { - 'inactivity_probe': inactivity_probe, - }, + self.ovsdb_cms.db_nb_port: { + 'inactivity_probe': inactivity_probe, + }, }) self.configure_ovn_listener( 'sb', { - self.ovsdb_cms.db_sb_port: { - 'role': 'ovn-controller', - 'inactivity_probe': inactivity_probe, - }, + self.ovsdb_cms.db_sb_port: { + 'role': 'ovn-controller', + 'inactivity_probe': inactivity_probe, + }, }) self.configure_ovn_listener( 'sb', { - self.ovsdb_cms.db_sb_admin_port: { - 'inactivity_probe': inactivity_probe, - }, + self.ovsdb_cms.db_sb_admin_port: { + 'inactivity_probe': inactivity_probe, + }, }) diff --git a/charms/ovn-central-k8s/src/ovn.py b/charms/ovn-central-k8s/src/ovn.py index e9458a51..4aed794f 100644 --- a/charms/ovn-central-k8s/src/ovn.py +++ b/charms/ovn-central-k8s/src/ovn.py @@ -22,7 +22,8 @@ OVN_RUNDIR = '/var/run/ovn' OVN_SYSCONFDIR = '/etc/ovn' -def ovn_appctl(target, args, rundir=None, use_ovs_appctl=False, cmd_executor=None): +def ovn_appctl(target, args, rundir=None, use_ovs_appctl=False, + cmd_executor=None): """Run ovn/ovs-appctl for target with args and return output. :param target: Name of daemon to contact. Unless target begins with '/', @@ -127,7 +128,8 @@ class OVNClusterStatus(object): self.vote == other.vote and self.election_timer == other.election_timer and self.log == other.log and - self.entries_not_yet_committed == other.entries_not_yet_committed and + self.entries_not_yet_committed == + other.entries_not_yet_committed and self.entries_not_yet_applied == other.entries_not_yet_applied and self.connections == other.connections and self.servers == other.servers) @@ -142,7 +144,8 @@ class OVNClusterStatus(object): return self.leader == 'self' -def cluster_status(target, schema=None, use_ovs_appctl=False, rundir=None, cmd_executor=None): +def cluster_status(target, schema=None, use_ovs_appctl=False, rundir=None, + cmd_executor=None): """Retrieve status information from clustered OVSDB. :param target: Usually one of 'ovsdb-server', 'ovnnb_db', 'ovnsb_db', can @@ -228,7 +231,8 @@ def is_northd_active(cmd_executor=None): :rtype: bool """ try: - for line in ovn_appctl('ovn-northd', ('status',), cmd_executor=cmd_executor).splitlines(): + for line in ovn_appctl('ovn-northd', ('status',), + cmd_executor=cmd_executor).splitlines(): if line.startswith('Status:') and 'active' in line: return True except subprocess.CalledProcessError: diff --git a/charms/ovn-central-k8s/src/ovsdb.py b/charms/ovn-central-k8s/src/ovsdb.py index 1955b477..458517fa 100644 --- a/charms/ovn-central-k8s/src/ovsdb.py +++ b/charms/ovn-central-k8s/src/ovsdb.py @@ -147,7 +147,8 @@ class SimpleOVSDB(object): raise AttributeError( 'table "{}" not known for use with "{}"' .format(table, self._tool)) - return self.Table(self._tool, table, args=self._args, cmd_executor=self.cmd_executor) + return self.Table( + self._tool, table, args=self._args, cmd_executor=self.cmd_executor) class Table(object): """Methods to interact with contents of OVSDB tables. @@ -250,7 +251,8 @@ class SimpleOVSDB(object): return self._find_tbl(condition=condition) def remove(self, rec, col, value): - self.cmd_executor(self._tool, 'remove', self._table, rec, col, value) + self.cmd_executor( + self._tool, 'remove', self._table, rec, col, value) def set(self, rec, col, value): self.cmd_executor(self._tool, 'set', self._table, rec, diff --git a/charms/ovn-central-k8s/tests/test_charm.py b/charms/ovn-central-k8s/tests/test_charm.py index af9f756f..fa1ebfeb 100644 --- a/charms/ovn-central-k8s/tests/test_charm.py +++ b/charms/ovn-central-k8s/tests/test_charm.py @@ -7,7 +7,6 @@ import unittest from unittest.mock import Mock from charm import SunbeamOvnCentralOperatorCharm -from ops.model import ActiveStatus from ops.testing import Harness @@ -32,35 +31,3 @@ class TestCharm(unittest.TestCase): def test_action_fail(self): action_event = Mock(params={"fail": "fail this"}) self.harness.charm._on_fortune_action(action_event) - - self.assertEqual(action_event.fail.call_args, [("fail this",)]) - - def test_httpbin_pebble_ready(self): - # Check the initial Pebble plan is empty - initial_plan = self.harness.get_container_pebble_plan("httpbin") - self.assertEqual(initial_plan.to_yaml(), "{}\n") - # Expected plan after Pebble ready with default config - expected_plan = { - "services": { - "httpbin": { - "override": "replace", - "summary": "httpbin", - "command": "gunicorn -b 0.0.0.0:80 httpbin:app -k gevent", - "startup": "enabled", - "environment": {"thing": "🎁"}, - } - }, - } - # Get the httpbin container from the model - container = self.harness.model.unit.get_container("httpbin") - # Emit the PebbleReadyEvent carrying the httpbin container - self.harness.charm.on.httpbin_pebble_ready.emit(container) - # Get the plan now we've run PebbleReady - updated_plan = self.harness.get_container_pebble_plan("httpbin").to_dict() - # Check we've got the plan we expected - self.assertEqual(expected_plan, updated_plan) - # Check the service was started - service = self.harness.model.unit.get_container("httpbin").get_service("httpbin") - self.assertTrue(service.is_running()) - # Ensure we set an ActiveStatus with no message - self.assertEqual(self.harness.model.unit.status, ActiveStatus())