From be42d54170ca40d8d52e2c9fc8d888621585d6cf Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Mon, 14 Nov 2022 00:29:47 +0100 Subject: [PATCH] Enable black and pylint in tox.ini Change-Id: I1571ce8b1a32aa67be1ada2864233c6f5ce9616b Signed-off-by: garciadeblas --- devops-stages/stage-test.sh | 32 ++--- osm_policy_module/alarming/service.py | 79 ++++++++--- osm_policy_module/autoscaling/service.py | 8 +- osm_policy_module/common/lcm_client.py | 37 +++-- osm_policy_module/common/mon_client.py | 2 +- osm_policy_module/core/agent.py | 41 ++++-- osm_policy_module/core/database.py | 24 ++-- osm_policy_module/healing/service.py | 132 +++++++++--------- .../migrations/006_add_heal_alarm.py | 4 +- .../tests/integration/test_policy_agent.py | 6 +- .../unit/alarming/test_alarming_service.py | 61 ++++---- .../tests/unit/core/test_policy_agent.py | 8 +- .../unit/healing/test_healing_service.py | 2 +- tox.ini | 8 +- 14 files changed, 263 insertions(+), 181 deletions(-) diff --git a/devops-stages/stage-test.sh b/devops-stages/stage-test.sh index 75a3c61..2802151 100755 --- a/devops-stages/stage-test.sh +++ b/devops-stages/stage-test.sh @@ -1,20 +1,20 @@ -# Copyright 2017 Intel Research and Development Ireland Limited -# ************************************************************* - -# This file is part of OSM Monitoring module -# All Rights Reserved to Intel Corporation - -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at +#!/bin/bash -# http://www.apache.org/licenses/LICENSE-2.0 +# Copyright 2017 Intel Research and Development Ireland Limited +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# # Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +echo "Launching tox" +TOX_PARALLEL_NO_SPINNER=1 tox --parallel=auto -OUTPUT=$(TOX_PARALLEL_NO_SPINNER=1 tox --parallel=auto) -printf "$OUTPUT" diff --git a/osm_policy_module/alarming/service.py b/osm_policy_module/alarming/service.py index 0e355d8..dbc375e 100644 --- a/osm_policy_module/alarming/service.py +++ b/osm_policy_module/alarming/service.py @@ -111,10 +111,17 @@ class AlarmingService: ) alarm_action = dict() for action_type in ["ok", "insufficient-data", "alarm"]: - if "actions" in alarm_descriptor and action_type in alarm_descriptor["actions"]: - for url in alarm_descriptor["actions"][action_type]: + if ( + "actions" in alarm_descriptor + and action_type in alarm_descriptor["actions"] + ): + for url in alarm_descriptor["actions"][ + action_type + ]: if "webhook" in alarm_action: - alarm_action["webhook"].append(url["url"]) + alarm_action["webhook"].append( + url["url"] + ) else: alarm_action["webhook"] = [url["url"]] alarm_uuid = await self.mon_client.create_alarm( @@ -132,10 +139,10 @@ class AlarmingService: nsr_id=nsr_id, vnf_member_index=vnfr["member-vnf-index-ref"], vdu_name=vdur["name"], - last_action='insufficient-data', + last_action="insufficient-data", id_suffix=0, ok_ack=False, - alarm_ack=False + alarm_ack=False, ) for action_type in ["ok", "insufficient-data", "alarm"]: if ( @@ -211,8 +218,10 @@ class AlarmingService: if vnf_member_index is None: alarm_conditions = VnfAlarm.nsr_id == nsr_id else: - query_list = [VnfAlarm.nsr_id == nsr_id, - VnfAlarm.vnf_member_index == vnf_member_index] + query_list = [ + VnfAlarm.nsr_id == nsr_id, + VnfAlarm.vnf_member_index == vnf_member_index, + ] alarm_conditions = functools.reduce(operator.and_, query_list) for alarm in VnfAlarmRepository.list(alarm_conditions): log.debug("Deleting vnf alarm %s", alarm.alarm_uuid) @@ -236,7 +245,7 @@ class AlarmingService: database.db.close() async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict): - alert_timeout = int(self.conf.get('alert', 'timeout')) + alert_timeout = int(self.conf.get("alert", "timeout")) database.db.connect() try: with database.db.atomic(): @@ -251,14 +260,19 @@ class AlarmingService: If both the status are 'ok', it avoid sending repetitive ok notification. """ if action.type == status: - if bool(self.conf.get('alert', 'enhanced_alarms')): - if ((status != "ok" or (status == "ok" and alarm.ok_ack is False)) and - (status != "alarm" or (status == "alarm" and alarm.alarm_ack is False))): + if bool(self.conf.get("alert", "enhanced_alarms")): + if ( + status != "ok" + or (status == "ok" and alarm.ok_ack is False) + ) and ( + status != "alarm" + or (status == "alarm" and alarm.alarm_ack is False) + ): log.info( "Executing request to url %s for vnf alarm %s with status %s", action.url, alarm.alarm_id, - status + status, ) try: if status == "alarm" and alarm.last_action == "ok": @@ -271,11 +285,18 @@ class AlarmingService: except Exception as e: log.exception(e) - payload["notify_details"]["alarm_number"] = alarm.id_suffix + payload["notify_details"][ + "alarm_number" + ] = alarm.id_suffix headers = {"content-type": "application/json"} try: - resp = requests.post(url=action.url, data=json.dumps(payload), - headers=headers, verify=False, timeout=alert_timeout) + resp = requests.post( + url=action.url, + data=json.dumps(payload), + headers=headers, + verify=False, + timeout=alert_timeout, + ) log.info("Response %s", resp) if resp.status_code == 200: if status == "ok": @@ -288,26 +309,38 @@ class AlarmingService: alarm.alarm_ack = False alarm.ok_ack = False alarm.save() + except ConnectionError: + log.exception( + "Error connecting to url %s", action.url + ) except RequestException as e: - log.info("Error: RequestException while connecting to url %s", action.url) + log.info( + "Error: RequestException while connecting to url %s", + action.url, + ) log.debug("RequestException %s", e) - except ConnectionError: - log.exception("Error connecting to url %s", action.url) else: log.info( "Executing request to url %s for vnf alarm %s with status %s", action.url, alarm.alarm_id, - status + status, ) try: - requests.post(url=action.url, json=json.dumps(payload), timeout=alert_timeout) - except RequestException as e: - log.info("Error: RequestException while connecting to url %s", action.url) - log.debug("RequestException %s", e) + requests.post( + url=action.url, + json=json.dumps(payload), + timeout=alert_timeout, + ) except ConnectionError: log.exception("Error connecting to url %s", action.url) + except RequestException as e: + log.info( + "Error: RequestException while connecting to url %s", + action.url, + ) + log.debug("RequestException %s", e) except VnfAlarm.DoesNotExist: log.debug( diff --git a/osm_policy_module/autoscaling/service.py b/osm_policy_module/autoscaling/service.py index d195bb7..33472fd 100644 --- a/osm_policy_module/autoscaling/service.py +++ b/osm_policy_module/autoscaling/service.py @@ -198,7 +198,7 @@ class AutoscalingService: operation=scaling_criteria[ "scale-in-relational-operation" ], - action="scale_in" + action="scale_in", ) ) alarm = ScalingAlarmRepository.create( @@ -262,8 +262,10 @@ class AutoscalingService: if vnf_member_index is None: scale_conditions = ScalingGroup.nsr_id == nsr_id else: - query_list = [ScalingGroup.nsr_id == nsr_id, - ScalingGroup.vnf_member_index == vnf_member_index] + query_list = [ + ScalingGroup.nsr_id == nsr_id, + ScalingGroup.vnf_member_index == vnf_member_index, + ] scale_conditions = functools.reduce(operator.and_, query_list) for scaling_group in ScalingGroupRepository.list(scale_conditions): for scaling_policy in scaling_group.scaling_policies: diff --git a/osm_policy_module/common/lcm_client.py b/osm_policy_module/common/lcm_client.py index 102b0dc..c94a424 100644 --- a/osm_policy_module/common/lcm_client.py +++ b/osm_policy_module/common/lcm_client.py @@ -133,8 +133,16 @@ class LcmClient: return nslcmop async def heal( - self, nsr_id: str, vnfinstance_id: str, vdur_name: str, vdu_id: str, - vnf_member_index: str, heal_type: str, day1: bool, count_index: int): + self, + nsr_id: str, + vnfinstance_id: str, + vdur_name: str, + vdu_id: str, + vnf_member_index: str, + heal_type: str, + day1: bool, + count_index: int, + ): """ Sends healing action to LCM through the message bus. @@ -159,8 +167,15 @@ class LcmClient: ) nsr = self.db_client.get_nsr(nsr_id) nslcmop = self._generate_nslcmop_heal( - nsr_id, vnfinstance_id, vdur_name, vdu_id, vnf_member_index, heal_type, day1, - count_index, nsr['_admin'] + nsr_id, + vnfinstance_id, + vdur_name, + vdu_id, + vnf_member_index, + heal_type, + day1, + count_index, + nsr["_admin"], ) self.db_client.create_nslcmop(nslcmop) log.debug("Sending heal action message: %s", json.dumps(nslcmop)) @@ -213,12 +228,12 @@ class LcmClient: { "run-day1": day1, "count-index": count_index, - "vdu-id": vdu_id + "vdu-id": vdu_id, } - ] - } + ], + }, } - ] + ], } nslcmop = { @@ -239,8 +254,8 @@ class LcmClient: "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id, }, "_admin": { - "projects_read": admin['projects_read'], - "projects_write": admin['projects_write'] - } + "projects_read": admin["projects_read"], + "projects_write": admin["projects_write"], + }, } return nslcmop diff --git a/osm_policy_module/common/mon_client.py b/osm_policy_module/common/mon_client.py index b8c3779..1d498c2 100644 --- a/osm_policy_module/common/mon_client.py +++ b/osm_policy_module/common/mon_client.py @@ -53,7 +53,7 @@ class MonClient: threshold: int, operation: str, statistic: str = "AVERAGE", - action: str = '', + action: str = "", ): cor_id = random.randint(1, 10e7) msg = self._build_create_alarm_payload( diff --git a/osm_policy_module/core/agent.py b/osm_policy_module/core/agent.py index 9867c0c..56caecd 100644 --- a/osm_policy_module/core/agent.py +++ b/osm_policy_module/core/agent.py @@ -37,7 +37,14 @@ from osm_policy_module.core.config import Config log = logging.getLogger(__name__) -ALLOWED_KAFKA_KEYS = ["instantiated", "scaled", "terminated", "notify_alarm", "policy_updated", "vnf_terminated"] +ALLOWED_KAFKA_KEYS = [ + "instantiated", + "scaled", + "terminated", + "notify_alarm", + "policy_updated", + "vnf_terminated", +] class PolicyModuleAgent: @@ -167,39 +174,49 @@ class PolicyModuleAgent: async def _handle_policy_update(self, content): log.info("_handle_policy_update: %s", content) - nsr_id = content['nsr_id'] - vnf_member_index = content['vnf_member_index'] + nsr_id = content["nsr_id"] + vnf_member_index = content["vnf_member_index"] if ( content["operationState"] == "COMPLETED" or content["operationState"] == "PARTIALLY_COMPLETED" ): log.info( "Updating policies of VNF with nsr_id: %s and vnf-member-index: %s" - % (nsr_id, vnf_member_index)) - await self.autoscaling_service.delete_scaling_groups(nsr_id, vnf_member_index) + % (nsr_id, vnf_member_index) + ) + await self.autoscaling_service.delete_scaling_groups( + nsr_id, vnf_member_index + ) await self.alarming_service.delete_vnf_alarms(nsr_id, vnf_member_index) - await self.autoscaling_service.configure_scaling_groups(nsr_id, vnf_member_index) + await self.autoscaling_service.configure_scaling_groups( + nsr_id, vnf_member_index + ) await self.alarming_service.configure_vnf_alarms(nsr_id, vnf_member_index) else: log.info( "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. " "Current state is %s. Skipping...", - content['operationState']) + content["operationState"], + ) async def _handle_vnf_terminated(self, content): - nsr_id = content['nsr_id'] - vnf_member_index = content['vnf_member_index'] + nsr_id = content["nsr_id"] + vnf_member_index = content["vnf_member_index"] if ( content["operationState"] == "COMPLETED" or content["operationState"] == "PARTIALLY_COMPLETED" ): log.info( "Deleting policies of VNF with nsr_id: %s and vnf-member-index: %s" - % (nsr_id, vnf_member_index)) - await self.autoscaling_service.delete_scaling_groups(nsr_id, vnf_member_index) + % (nsr_id, vnf_member_index) + ) + await self.autoscaling_service.delete_scaling_groups( + nsr_id, vnf_member_index + ) await self.alarming_service.delete_vnf_alarms(nsr_id, vnf_member_index) else: log.info( "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. " "Current state is %s. Skipping...", - content['operationState']) + content["operationState"], + ) diff --git a/osm_policy_module/core/database.py b/osm_policy_module/core/database.py index e596d74..6423b5e 100644 --- a/osm_policy_module/core/database.py +++ b/osm_policy_module/core/database.py @@ -98,7 +98,7 @@ class VnfAlarm(BaseModel): nsr_id = CharField() vnf_member_index = CharField() vdu_name = CharField() - last_action = CharField(default='insufficient-data') + last_action = CharField(default="insufficient-data") id_suffix = IntegerField() ok_ack = BooleanField(default=False) alarm_ack = BooleanField(default=False) @@ -122,7 +122,7 @@ class HealingAction(BaseModel): cooldown_time = IntegerField() count_index = IntegerField() last_heal = DateTimeField(default=datetime.datetime.now) - last_status = CharField(default='insufficient-data') + last_status = CharField(default="insufficient-data") day1 = BooleanField(default=False) @@ -141,7 +141,7 @@ class DatabaseManager: class ScalingAlarmRepository: @staticmethod def list(*expressions) -> Iterable[ScalingAlarm]: - return ScalingAlarm.select().where(*expressions) + return ScalingAlarm.select().where(*expressions).__iter__() @staticmethod def get(*expressions, join_classes: List = None) -> ScalingAlarm: @@ -159,7 +159,7 @@ class ScalingAlarmRepository: class ScalingGroupRepository: @staticmethod def list(*expressions) -> Iterable[ScalingGroup]: - return ScalingGroup.select().where(*expressions) + return ScalingGroup.select().where(*expressions).__iter__() @staticmethod def get(*expressions) -> ScalingGroup: @@ -177,7 +177,7 @@ class ScalingPolicyRepository: if join_classes: for join_class in join_classes: query = query.join(join_class) - return query.where(*expressions) + return query.where(*expressions).__iter__() @staticmethod def get(*expressions, join_classes: List = None) -> ScalingPolicy: @@ -199,7 +199,7 @@ class ScalingCriteriaRepository: if join_classes: for join_class in join_classes: query = query.join(join_class) - return query.where(*expressions) + return query.where(*expressions).__iter__() @staticmethod def get(*expressions, join_classes: List = None) -> ScalingCriteria: @@ -217,7 +217,7 @@ class ScalingCriteriaRepository: class VnfAlarmRepository: @staticmethod def list(*expressions) -> Iterable[VnfAlarm]: - return VnfAlarm.select().where(*expressions) + return VnfAlarm.select().where(*expressions).__iter__() @staticmethod def get(*expressions) -> VnfAlarm: @@ -231,7 +231,7 @@ class VnfAlarmRepository: class AlarmActionRepository: @staticmethod def list(*expressions) -> Iterable[AlarmAction]: - return AlarmAction.select().where(*expressions) + return AlarmAction.select().where(*expressions).__iter__() @staticmethod def get(*expressions) -> AlarmAction: @@ -245,8 +245,12 @@ class AlarmActionRepository: class HealingActionRepository: @staticmethod def list(*expressions) -> Iterable[HealingAction]: - log.info("### Printing healing action db alarm {}".format(HealingAction.select().where(*expressions))) - return HealingAction.select().where(*expressions) + log.info( + "### Printing healing action db alarm {}".format( + HealingAction.select().where(*expressions) + ) + ) + return HealingAction.select().where(*expressions).__iter__() @staticmethod def get(*expressions) -> HealingAction: diff --git a/osm_policy_module/healing/service.py b/osm_policy_module/healing/service.py index 8434c66..1885d05 100644 --- a/osm_policy_module/healing/service.py +++ b/osm_policy_module/healing/service.py @@ -43,7 +43,7 @@ log = logging.getLogger(__name__) class HealingService: def __init__(self, config: Config, loop=None): """ - Initializing the HealingService + Initializing the HealingService """ log.info("HealingService Initialized") self.conf = config @@ -57,8 +57,8 @@ class HealingService: async def configure_healing_alarms(self, nsr_id: str): """ - Configuring the Healing alarms - :param nsr_id: Network service record id + Configuring the Healing alarms + :param nsr_id: Network service record id """ log.info("Configuring Healing alarm for NS %s", nsr_id) alarms_created = [] @@ -67,32 +67,38 @@ class HealingService: with database.db.atomic(): vnfrs = self.db_client.get_vnfrs(nsr_id) for vnfr in vnfrs: - vnfd = self.db_client.get_vnfd(vnfr['vnfd-id']) + vnfd = self.db_client.get_vnfd(vnfr["vnfd-id"]) df = vnfd.get("df", [{}])[0] if "healing-aspect" not in df: log.info("No healing configuration present in vnfd") continue healing_aspects = df["healing-aspect"] for healing_aspect in healing_aspects: - for healing_policy in healing_aspect.get( - "healing-policy", () - ): - vdu_id = healing_policy['vdu-id'] + for healing_policy in healing_aspect.get("healing-policy", ()): + vdu_id = healing_policy["vdu-id"] for vdur in vnfr["vdur"]: if vdu_id == vdur["vdu-id-ref"]: try: HealingActionRepository.get( - HealingAction.alarm_id == healing_policy['event-name'], - HealingAction.vdur_name == vdur['name'], + HealingAction.alarm_id + == healing_policy["event-name"], + HealingAction.vdur_name == vdur["name"], HealingAction.nsr_id == nsr_id, - HealingAction.cooldown_time == healing_policy['cooldown-time'], - HealingAction.recovery_action == healing_policy['action-on-recovery'], - HealingAction.vnfinstance_id == vnfr['id'], - HealingAction.vdu_id == healing_policy['vdu-id'], - HealingAction.count_index == vdur['count-index'] + HealingAction.cooldown_time + == healing_policy["cooldown-time"], + HealingAction.recovery_action + == healing_policy["action-on-recovery"], + HealingAction.vnfinstance_id == vnfr["id"], + HealingAction.vdu_id + == healing_policy["vdu-id"], + HealingAction.count_index + == vdur["count-index"], + ) + log.debug( + "vdu %s already has an alarm configured with same id %s", + healing_policy["vdu-id"], + healing_policy["event-name"], ) - log.debug("vdu %s already has an alarm configured with same id %s", - healing_policy['vdu-id'], healing_policy['event-name']) continue except HealingAction.DoesNotExist: pass @@ -101,28 +107,26 @@ class HealingService: alarm_uuid = await self.mon_client.create_alarm( metric_name=metric_name, ns_id=nsr_id, - vdu_name=vdur['name'], - vnf_member_index=vnfr[ - 'member-vnf-index-ref' - ], + vdu_name=vdur["name"], + vnf_member_index=vnfr["member-vnf-index-ref"], threshold=1, operation="LT", - statistic="AVERAGE" + statistic="AVERAGE", ) alarm = HealingActionRepository.create( - alarm_id=healing_policy['event-name'], + alarm_id=healing_policy["event-name"], alarm_uuid=alarm_uuid, nsr_id=nsr_id, - vnf_member_index=vnfr[ - 'member-vnf-index-ref' + vnf_member_index=vnfr["member-vnf-index-ref"], + vdur_name=vdur["name"], + recovery_action=healing_policy[ + "action-on-recovery" ], - vdur_name=vdur['name'], - recovery_action=healing_policy['action-on-recovery'], - cooldown_time=healing_policy['cooldown-time'], - day1=healing_policy['day1'], - vdu_id=healing_policy['vdu-id'], - vnfinstance_id=vnfr['id'], - count_index=vdur['count-index'] + cooldown_time=healing_policy["cooldown-time"], + day1=healing_policy["day1"], + vdu_id=healing_policy["vdu-id"], + vnfinstance_id=vnfr["id"], + count_index=vdur["count-index"], ) alarms_created.append(alarm) @@ -131,12 +135,16 @@ class HealingService: if len(alarms_created) > 0: for alarm in alarms_created: try: - await self.mon_client.delete_alarm(alarm.nsr_id, - alarm.vnf_member_index, - alarm.vdu_name, - alarm.alarm_uuid) + await self.mon_client.delete_alarm( + alarm.nsr_id, + alarm.vnf_member_index, + alarm.vdu_name, + alarm.alarm_uuid, + ) except ValueError: - log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) + log.exception( + "Error deleting alarm in MON %s", alarm.alarm_uuid + ) raise e finally: database.db.close() @@ -151,26 +159,20 @@ class HealingService: ): try: self.db_client.get_vdur( - nsr_id, - alarm.vnf_member_index, - alarm.vdur_name + nsr_id, alarm.vnf_member_index, alarm.vdur_name ) except VdurNotFound: - log.info( - "Deleting orphaned alarm %s", - alarm.alarm_uuid - ) + log.info("Deleting orphaned alarm %s", alarm.alarm_uuid) try: await self.mon_client.delete_alarm( alarm.nsr_id, alarm.vnf_member_index, alarm.vdur_name, - alarm.alarm_uuid + alarm.alarm_uuid, ) except ValueError: log.exception( - "Error deleting alarm in MON %s", - alarm.alarm_uuid + "Error deleting alarm in MON %s", alarm.alarm_uuid ) alarm.delete_instance() @@ -182,8 +184,8 @@ class HealingService: async def delete_healing_alarms(self, nsr_id): """ - Deleting the healing alarms - :param nsr_id: Network service record id + Deleting the healing alarms + :param nsr_id: Network service record id """ log.info("Deleting healing vnf alarms for network service %s", nsr_id) database.db.connect() @@ -197,12 +199,11 @@ class HealingService: alarm.nsr_id, alarm.vnf_member_index, alarm.vdur_name, - alarm.alarm_uuid + alarm.alarm_uuid, ) except ValueError: log.exception( - "Error deleting alarm in MON %s", - alarm.alarm_uuid + "Error deleting alarm in MON %s", alarm.alarm_uuid ) alarm.delete_instance() @@ -214,9 +215,9 @@ class HealingService: async def update_alarm_status(self, alarm_uuid: str, status: str): """ - For updating the alarm status - :param alarm_uuid: vdu uuid - :param status: Status of an alarm + For updating the alarm status + :param alarm_uuid: vdu uuid + :param status: Status of an alarm """ database.db.connect() try: @@ -227,17 +228,15 @@ class HealingService: alarm.last_status = status alarm.save() except HealingAction.DoesNotExist: - log.debug( - "There is no healing action configured for alarm %s.", alarm_uuid - ) + log.debug("There is no healing action configured for alarm %s.", alarm_uuid) finally: database.db.close() async def handle_alarm(self, alarm_uuid: str, status: str): """ - For Handling the healing alarms - :param alarm_uuid: vdu uuid - :param status: Status of an alarm + For Handling the healing alarms + :param alarm_uuid: vdu uuid + :param status: Status of an alarm """ await self.update_alarm_status(alarm_uuid, status) database.db.connect() @@ -259,13 +258,13 @@ class HealingService: vnfinstance_id = alarm.vnfinstance_id alarms = HealingActionRepository.list( HealingAction.vnf_member_index == vnf_member_index, - HealingAction.vdur_name == vdur_name + HealingAction.vdur_name == vdur_name, ) statuses = [] for alarm in alarms: statuses.append(alarm.last_status) - if ((set(statuses) == {'alarm'}) or ('alarm' in statuses)): + if (set(statuses) == {"alarm"}) or ("alarm" in statuses): delta = datetime.datetime.now() - last_heal if delta.total_seconds() > cooldown_time: await self.lcm_client.heal( @@ -276,7 +275,7 @@ class HealingService: vnf_member_index, heal_type, day1, - count_index + count_index, ) last_heal = datetime.datetime.now() log.info("datetime.datetime.now %s", datetime.datetime.now) @@ -284,9 +283,6 @@ class HealingService: alarm.save() except HealingAction.DoesNotExist: - log.info( - "There is no healing action configured for alarm %s.", - alarm_uuid - ) + log.info("There is no healing action configured for alarm %s.", alarm_uuid) finally: database.db.close() diff --git a/osm_policy_module/migrations/006_add_heal_alarm.py b/osm_policy_module/migrations/006_add_heal_alarm.py index b44f42d..a2d2a69 100644 --- a/osm_policy_module/migrations/006_add_heal_alarm.py +++ b/osm_policy_module/migrations/006_add_heal_alarm.py @@ -66,7 +66,7 @@ def migrate(migrator, database, fake=False, **kwargs): cooldown_time = pw.IntegerField() count_index = pw.IntegerField() last_heal = pw.DateTimeField() - last_status = pw.CharField(max_length=255, default='insufficient-data') + last_status = pw.CharField(max_length=255, default="insufficient-data") day1 = pw.BooleanField(default=False) class Meta: @@ -76,4 +76,4 @@ def migrate(migrator, database, fake=False, **kwargs): def rollback(migrator, database, fake=False, **kwargs): """Write your rollback migrations here.""" - migrator.remove_model('healingaction') + migrator.remove_model("healingaction") diff --git a/osm_policy_module/tests/integration/test_policy_agent.py b/osm_policy_module/tests/integration/test_policy_agent.py index 0329b7c..73db304 100644 --- a/osm_policy_module/tests/integration/test_policy_agent.py +++ b/osm_policy_module/tests/integration/test_policy_agent.py @@ -375,7 +375,7 @@ vnfd_record_mock = { "recovery-type": "automatic", "action-on-recovery": "REDEPLOY_ONLY", "cooldown-time": 180, - "day1": False + "day1": False, } ], } @@ -592,7 +592,7 @@ class PolicyModuleAgentTest(unittest.TestCase): vnf_member_index="1", threshold=20.0, operation="LT", - action="{'webhook': ['localhost:9090', 'localhost:9090', 'localhost:9090']}" + action="{'webhook': ['localhost:9090', 'localhost:9090', 'localhost:9090']}", ) create_alarm.assert_any_call( metric_name="average_memory_utilization", @@ -601,7 +601,7 @@ class PolicyModuleAgentTest(unittest.TestCase): vnf_member_index="2", threshold=20.0, operation="LT", - action="{'webhook': ['localhost:9090', 'localhost:9090', 'localhost:9090']}" + action="{'webhook': ['localhost:9090', 'localhost:9090', 'localhost:9090']}", ) @patch.object(DbMongo, "db_connect", Mock()) diff --git a/osm_policy_module/tests/unit/alarming/test_alarming_service.py b/osm_policy_module/tests/unit/alarming/test_alarming_service.py index 563768f..af672e6 100644 --- a/osm_policy_module/tests/unit/alarming/test_alarming_service.py +++ b/osm_policy_module/tests/unit/alarming/test_alarming_service.py @@ -39,71 +39,84 @@ class TestAlarmingService(TestCase): def setUp(self): self.config = Config() self.loop = asyncio.new_event_loop() - self.payload = { - "notify_details": { - "alarm_number": 0 - } - } + self.payload = {"notify_details": {"alarm_number": 0}} self.headers = {"content-type": "application/json"} @mock.patch.object(VnfAlarmRepository, "get") @mock.patch("requests.post") @mock.patch("osm_policy_module.core.database.db") def test_handle_alarm_suppression(self, database, requests_post, get_alarm): - alert_timeout = int(self.config.get('alert', 'timeout')) + alert_timeout = int(self.config.get("alert", "timeout")) mock_alarm = self._build_mock_alarm("test_id", last_action="ok") get_alarm.return_value = mock_alarm service = AlarmingService(self.config) - if bool(self.config.get('alert', 'enhanced_alarms')): - self.loop.run_until_complete(service.handle_alarm("test_id", "alarm", self.payload)) + if bool(self.config.get("alert", "enhanced_alarms")): + self.loop.run_until_complete( + service.handle_alarm("test_id", "alarm", self.payload) + ) requests_post.assert_called_once_with( - url='http://alarm-url/', data='{"notify_details": {"alarm_number": 1}}', - headers={'content-type': 'application/json'}, verify=False, timeout=alert_timeout + url="http://alarm-url/", + data='{"notify_details": {"alarm_number": 1}}', + headers={"content-type": "application/json"}, + verify=False, + timeout=alert_timeout, ) else: self.loop.run_until_complete(service.handle_alarm("test_id", "alarm", {})) - requests_post.assert_called_once_with(json="{}", url="http://alarm-url/", timeout=alert_timeout) + requests_post.assert_called_once_with( + json="{}", url="http://alarm-url/", timeout=alert_timeout + ) @mock.patch.object(VnfAlarmRepository, "get") @mock.patch("requests.post") @mock.patch("osm_policy_module.core.database.db") def test_handle_ok_suppression(self, database, requests_post, get_alarm): - alert_timeout = int(self.config.get('alert', 'timeout')) + alert_timeout = int(self.config.get("alert", "timeout")) mock_alarm = self._build_mock_alarm("test_id", last_action="alarm") get_alarm.return_value = mock_alarm service = AlarmingService(self.config) - if bool(self.config.get('alert', 'enhanced_alarms')): - self.loop.run_until_complete(service.handle_alarm("test_id", "ok", self.payload)) - requests_post.assert_called_once_with( - url='http://ok-url/', data='{"notify_details": {"alarm_number": 0}}', - headers={'content-type': 'application/json'}, verify=False, timeout=alert_timeout + if bool(self.config.get("alert", "enhanced_alarms")): + self.loop.run_until_complete( + service.handle_alarm("test_id", "ok", self.payload) + ) + requests_post.assert_called_once_with( + url="http://ok-url/", + data='{"notify_details": {"alarm_number": 0}}', + headers={"content-type": "application/json"}, + verify=False, + timeout=alert_timeout, ) else: self.loop.run_until_complete(service.handle_alarm("test_id", "ok", {})) - requests_post.assert_called_once_with(json="{}", url="http://ok-url/", timeout=alert_timeout) + requests_post.assert_called_once_with( + json="{}", url="http://ok-url/", timeout=alert_timeout + ) @mock.patch.object(VnfAlarmRepository, "get") @mock.patch("requests.post") @mock.patch("osm_policy_module.core.database.db") def test_handle_insufficientalarm(self, database, requests_post, get_alarm): - alert_timeout = int(self.config.get('alert', 'timeout')) + alert_timeout = int(self.config.get("alert", "timeout")) mock_alarm = self._build_mock_alarm("test_id") get_alarm.return_value = mock_alarm service = AlarmingService(self.config) - if bool(self.config.get('alert', 'enhanced_alarms')): + if bool(self.config.get("alert", "enhanced_alarms")): self.loop.run_until_complete( service.handle_alarm("test_id", "insufficient-data", self.payload) ) requests_post.assert_called_once_with( - url='http://insufficient-data-url/', data='{"notify_details": {"alarm_number": 0}}', - headers={'content-type': 'application/json'}, verify=False, timeout=alert_timeout + url="http://insufficient-data-url/", + data='{"notify_details": {"alarm_number": 0}}', + headers={"content-type": "application/json"}, + verify=False, + timeout=alert_timeout, ) else: self.loop.run_until_complete( service.handle_alarm("test_id", "insufficient-data", {}) ) requests_post.assert_called_once_with( - json="{}", url='http://insufficient-data-url/', timeout=alert_timeout + json="{}", url="http://insufficient-data-url/", timeout=alert_timeout ) @mock.patch.object(VnfAlarmRepository, "get") @@ -123,7 +136,7 @@ class TestAlarmingService(TestCase): insufficient_data_url="http://insufficient-data-url/", ok_url="http://ok-url/", id_suffix=0, - last_action="insufficient-data" + last_action="insufficient-data", ): mock_alarm = mock.Mock() mock_alarm.alarm_id = alarm_id diff --git a/osm_policy_module/tests/unit/core/test_policy_agent.py b/osm_policy_module/tests/unit/core/test_policy_agent.py index cc17891..43bc508 100644 --- a/osm_policy_module/tests/unit/core/test_policy_agent.py +++ b/osm_policy_module/tests/unit/core/test_policy_agent.py @@ -146,12 +146,12 @@ class PolicyAgentTest(unittest.TestCase): content = { "nsr_id": "test_nsr_id", "vnf_member_index": "1", - "operationState": "COMPLETED" + "operationState": "COMPLETED", } failed_content = { "nsr_id": "test_nsr_id", "vnf_member_index": "1", - "operationState": "FAILED" + "operationState": "FAILED", } configure_scaling_groups.side_effect = mock_configure_scaling_groups configure_vnf_alarms.side_effect = mock_configure_vnf_alarms @@ -250,12 +250,12 @@ class PolicyAgentTest(unittest.TestCase): content = { "nsr_id": "test_nsr_id", "vnf_member_index": "1", - "operationState": "COMPLETED" + "operationState": "COMPLETED", } failed_content = { "nsr_id": "test_nsr_id", "vnf_member_index": "1", - "operationState": "FAILED" + "operationState": "FAILED", } delete_scaling_groups.side_effect = mock_delete_scaling_groups delete_vnf_alarms.side_effect = mock_delete_vnf_alarms diff --git a/osm_policy_module/tests/unit/healing/test_healing_service.py b/osm_policy_module/tests/unit/healing/test_healing_service.py index e99f654..7f48f83 100644 --- a/osm_policy_module/tests/unit/healing/test_healing_service.py +++ b/osm_policy_module/tests/unit/healing/test_healing_service.py @@ -82,7 +82,7 @@ class TestHealscalingService(TestCase): "test_vnf_member_index", "test_heal_type", "test_day1", - "test_count_index" + "test_count_index", ) def _build_mock_alarm( diff --git a/tox.ini b/tox.ini index ee801f6..87f7e21 100644 --- a/tox.ini +++ b/tox.ini @@ -27,14 +27,15 @@ basepython = python3.8 setenv = VIRTUAL_ENV={envdir} PYTHONDONTWRITEBYTECODE = 1 deps = -r{toxinidir}/requirements.txt +parallel_show_output = true ####################################################################################### [testenv:black] deps = black skip_install = true commands = - - black --check --diff osm_policy_module/ - - black --check --diff setup.py + black --check --diff osm_policy_module/ + black --check --diff setup.py ####################################################################################### @@ -66,7 +67,7 @@ deps = {[testenv]deps} -r{toxinidir}/requirements-test.txt pylint commands = - pylint -E osm_policy_module + - pylint -E osm_policy_module ####################################################################################### @@ -115,6 +116,7 @@ whitelist_externals = sh ignore = W291, W293, + W503, E123, E125, E226, -- 2.25.1