monasca-notification/tests/test_notification.py
Doug Szumski efc6e28edc Fix periodic notifications for webhooks
- Removes the hard-coded magic number of 60 seconds allowing users to
  choose the period that they require.
- Standardise on strings for DictOpt dict keys. When loaded from a config
  file, the DictOpt keys are parsed as strings, which was conflicting with
  the default integer dict keys. This caused the periodic engine to silently
  fail to load when configured via a config file.
- Remove unused variable

Story: 2006783
Task: 37313
Change-Id: Ibd61c45fc1ade37022150d34a5b00c56fdf69814
2019-12-11 11:13:17 +00:00

157 lines
6.2 KiB
Python

# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP
#
# 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.
"""Tests the notification class."""
import json
from monasca_notification import notification
def test_json():
"""Test the to_json method to verify it behaves as expected.
"""
ts = 1429029121239
alarm = {'alarmId': 'alarmId',
'alarmName': 'alarmName',
'timestamp': ts,
'stateChangeReason': 'stateChangeReason',
'newState': 'newState',
'severity': 'LOW',
"link": "some-link",
"lifecycleState": "OPEN",
'tenantId': 'tenantId',
'metrics': 'cpu_util'}
test_notification = notification.Notification(1, 'ntype', 'name',
'address', 0, 0, alarm)
expected_dict = {u'id': 1,
u'name': u'name',
u'type': u'ntype',
u'notification_timestamp': None,
u'tenant_id': u'tenantId',
u'alarm_name': u'alarmName',
u'alarm_id': u'alarmId',
u'state': u'newState',
u'severity': u'LOW',
u'link': u'some-link',
u'lifecycle_state': u'OPEN',
u'alarm_timestamp': ts / 1000,
u'address': u'address',
u'message': u'stateChangeReason',
u'period': 0,
u'retry_count': 0,
u'raw_alarm': {
u'alarmId': u'alarmId',
u'alarmName': u'alarmName',
u'timestamp': ts,
u'stateChangeReason': u'stateChangeReason',
u'newState': u'newState',
u'severity': u'LOW',
u'link': u'some-link',
u'lifecycleState': u'OPEN',
u'tenantId': u'tenantId',
u'metrics': u'cpu_util'}}
# Compare as dicts so ordering is not an issue
assert json.loads(test_notification.to_json()) == expected_dict
def test_json_non_zero_period():
"""Test the to_json method to verify it behaves as expected.
"""
ts = 1429029121239
alarm = {'alarmId': 'alarmId',
'alarmName': 'alarmName',
'timestamp': ts,
'stateChangeReason': 'stateChangeReason',
'newState': 'newState',
'severity': 'LOW',
"link": "some-link",
"lifecycleState": "OPEN",
'tenantId': 'tenantId',
'metrics': 'cpu_util'}
test_notification = notification.Notification(1, 'ntype', 'name',
'address', 60, 0, alarm)
expected_dict = {u'id': 1,
u'name': u'name',
u'type': u'ntype',
u'notification_timestamp': None,
u'tenant_id': u'tenantId',
u'alarm_name': u'alarmName',
u'alarm_id': u'alarmId',
u'state': u'newState',
u'severity': u'LOW',
u'link': u'some-link',
u'lifecycle_state': u'OPEN',
u'alarm_timestamp': ts / 1000,
u'address': u'address',
u'message': u'stateChangeReason',
u'period': 60,
u'retry_count': 0,
u'raw_alarm': {
u'alarmId': u'alarmId',
u'alarmName': u'alarmName',
u'timestamp': ts,
u'stateChangeReason': u'stateChangeReason',
u'newState': u'newState',
u'severity': u'LOW',
u'link': u'some-link',
u'lifecycleState': u'OPEN',
u'tenantId': u'tenantId',
u'metrics': u'cpu_util'}}
# Compare as dicts so ordering is not an issue
assert json.loads(test_notification.to_json()) == expected_dict
def test_equal():
alarm = {'alarmId': 'alarmId',
'alarmName': 'alarmName',
'timestamp': 1429029121239,
'stateChangeReason': 'stateChangeReason',
'newState': 'newState',
'severity': 'LOW',
"link": "some-link",
"lifecycleState": "OPEN",
'tenantId': 'tenantId',
'metrics': 'cpu_util'}
test_notification = notification.Notification(0, 'ntype', 'name',
'address', 0, 0, alarm)
test_notification2 = notification.Notification(0, 'ntype', 'name',
'address', 0, 0, alarm)
assert(test_notification == test_notification2)
def test_unequal():
alarm = {'alarmId': 'alarmId',
'alarmName': 'alarmName',
'timestamp': 1429029121239,
'stateChangeReason': 'stateChangeReason',
'newState': 'newState',
'severity': 'LOW',
"link": "some-link",
"lifecycleState": "OPEN",
'tenantId': 'tenantId',
'metrics': 'cpu_util'}
test_notification = notification.Notification(0, 'ntype', 'name',
'address', 0, 0, alarm)
test_notification2 = notification.Notification(1, 'ntype', 'name',
'address', 0, 0, alarm)
test_notification2.alarm_id = None
assert(test_notification != test_notification2)