From 0deba34db64fb9aee7bae6b4468ce5ee39c5d054 Mon Sep 17 00:00:00 2001 From: lipan Date: Mon, 29 Jan 2018 17:23:05 +0800 Subject: [PATCH] add "severity" value for alarm_history alarm_history table has a field "severity",but aodh-evaluator service's _record_service() function in /aodh/evaluator/__init__.py lack this field.So this bug caused "severity" field in alarm_history is NULL. Change-Id: Ifd9f7b2fa9101ecbb6084dc363d7a14d3e67fed6 Closes-Bug: #1745946 --- aodh/evaluator/__init__.py | 4 ++- aodh/storage/impl_sqlalchemy.py | 3 +- .../storage/test_storage_scenarios.py | 30 +++++++++++++++++-- aodh/tests/unit/evaluator/test_gnocchi.py | 5 +++- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/aodh/evaluator/__init__.py b/aodh/evaluator/__init__.py index f8f7798d1..78bbdfa4e 100644 --- a/aodh/evaluator/__init__.py +++ b/aodh/evaluator/__init__.py @@ -94,6 +94,7 @@ class Evaluator(object): user_id, project_id = self.ks_client.user_id, self.ks_client.project_id on_behalf_of = alarm.project_id now = timeutils.utcnow() + severity = alarm.severity payload = dict(event_id=uuidutils.generate_uuid(), alarm_id=alarm.alarm_id, type=type, @@ -101,7 +102,8 @@ class Evaluator(object): user_id=user_id, project_id=project_id, on_behalf_of=on_behalf_of, - timestamp=now) + timestamp=now, + severity=severity) try: self._storage_conn.record_alarm_change(payload) diff --git a/aodh/storage/impl_sqlalchemy.py b/aodh/storage/impl_sqlalchemy.py index bff6e12c3..f168c208f 100644 --- a/aodh/storage/impl_sqlalchemy.py +++ b/aodh/storage/impl_sqlalchemy.py @@ -297,7 +297,8 @@ class Connection(base.Connection): user_id=row.user_id, project_id=row.project_id, on_behalf_of=row.on_behalf_of, - timestamp=row.timestamp) + timestamp=row.timestamp, + severity=row.severity) def query_alarms(self, filter_expr=None, orderby=None, limit=None): """Yields a lists of alarms that match filter.""" diff --git a/aodh/tests/functional/storage/test_storage_scenarios.py b/aodh/tests/functional/storage/test_storage_scenarios.py index 256b20e2d..2fffe29f9 100644 --- a/aodh/tests/functional/storage/test_storage_scenarios.py +++ b/aodh/tests/functional/storage/test_storage_scenarios.py @@ -75,7 +75,8 @@ class AlarmTestBase(DBTestBase): aggregation_method='count', evaluation_periods=1, granularity=60, - metrics=METRIC_IDS) + metrics=METRIC_IDS), + severity='low' ), alarm_models.Alarm(alarm_id='0r4ng3', enabled=True, @@ -99,7 +100,8 @@ class AlarmTestBase(DBTestBase): aggregation_method='avg', evaluation_periods=1, granularity=60, - metrics=METRIC_IDS) + metrics=METRIC_IDS), + severity='low' ), alarm_models.Alarm(alarm_id='y3ll0w', enabled=False, @@ -123,7 +125,8 @@ class AlarmTestBase(DBTestBase): aggregation_method='min', evaluation_periods=1, granularity=60, - metrics=METRIC_IDS) + metrics=METRIC_IDS), + severity='low' )] for a in alarms: @@ -302,6 +305,27 @@ class AlarmHistoryTest(AlarmTestBase): history = list(self.alarm_conn.query_alarm_history()) self.assertEqual(0, len(history)) + def test_record_severity_when_alarm_change(self): + alarm = list(self.alarm_conn.get_alarms(name='orange-alert'))[0] + severity = "low" + alarm_change = { + "event_id": "3d22800c-a3ca-4991-b34b-d97efb6047d9", + "alarm_id": alarm.alarm_id, + "type": alarm_models.AlarmChange.STATE_TRANSITION, + "detail": "detail %s" % alarm.name, + "user_id": alarm.user_id, + "project_id": alarm.project_id, + "on_behalf_of": alarm.project_id, + "severity": severity, + "timestamp": datetime.datetime(2014, 4, 7, 7, 34) + } + self.alarm_conn.record_alarm_change(alarm_change=alarm_change) + filter_expr = {"=": {"severity": "low"}} + history = list(self.alarm_conn.query_alarm_history( + filter_expr=filter_expr)) + self.assertEqual(1, len(history)) + self.assertEqual("low", history[0].severity) + class ComplexAlarmQueryTest(AlarmTestBase): diff --git a/aodh/tests/unit/evaluator/test_gnocchi.py b/aodh/tests/unit/evaluator/test_gnocchi.py index b07221621..a771ae83e 100644 --- a/aodh/tests/unit/evaluator/test_gnocchi.py +++ b/aodh/tests/unit/evaluator/test_gnocchi.py @@ -56,6 +56,7 @@ class TestGnocchiEvaluatorBase(base.TestEvaluatorBase): alarm_actions=[], repeat_actions=False, time_constraints=[], + severity='low', rule=dict( comparison_operator='gt', threshold=80.0, @@ -235,6 +236,7 @@ class TestGnocchiResourceThresholdEvaluate(TestGnocchiEvaluatorBase): detail = json.dumps({'state': alarm.state, 'transition_reason': reasons[num]}) on_behalf_of = alarm.project_id + severity = alarm.severity payload = dict( event_id='fake_event_id_%s' % num, alarm_id=alarm.alarm_id, @@ -243,7 +245,8 @@ class TestGnocchiResourceThresholdEvaluate(TestGnocchiEvaluatorBase): user_id='fake_user_id', project_id='fake_project_id', on_behalf_of=on_behalf_of, - timestamp=datetime.datetime(2015, 7, 26, 3, 33, 21, 876795)) + timestamp=datetime.datetime(2015, 7, 26, 3, 33, 21, 876795), + severity=severity) payloads.append(payload) return payloads