blob: 0cde0cef5aa49614806ac7bbd3d731f3b12ab5df [file] [log] [blame]
Lott, Christopher (cl778h)81084bc2020-06-01 20:53:12 -04001# =================================================================================2
2# Copyright (c) 2020 AT&T Intellectual Property.
3# Copyright (c) 2020 Nokia
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16# ==================================================================================
17import json
Lott, Christopher (cl778h)a03c5172020-07-06 15:13:07 -040018import pytest
Lott, Christopher (cl778h)81084bc2020-06-01 20:53:12 -040019import time
20from ricxappframe.alarm import alarm
Lott, Christopher (cl778h)a03c5172020-07-06 15:13:07 -040021from ricxappframe.alarm.alarm import AlarmAction, AlarmDetail, AlarmManager, AlarmSeverity, ALARM_MGR_SERVICE_NAME_ENV, ALARM_MGR_SERVICE_PORT_ENV
22from ricxappframe.alarm.exceptions import InitFailed
Lott, Christopher (cl778h)81084bc2020-06-01 20:53:12 -040023from ricxappframe.rmr import rmr
24
25MRC_SEND = None
26MRC_RCV = None
27SIZE = 256
28
29
30def setup_module():
31 """
32 test alarm module setup
33 """
34 global MRC_SEND
35 MRC_SEND = rmr.rmr_init(b"4566", rmr.RMR_MAX_RCV_BYTES, 0x00)
36 while rmr.rmr_ready(MRC_SEND) == 0:
37 time.sleep(1)
38
39 global MRC_RCV
40 MRC_RCV = rmr.rmr_init(b"4567", rmr.RMR_MAX_RCV_BYTES, 0x00)
41 while rmr.rmr_ready(MRC_RCV) == 0:
42 time.sleep(1)
43
44
45def teardown_module():
46 """
47 test alarm module teardown
48 """
49 rmr.rmr_close(MRC_SEND)
50
51
Lott, Christopher (cl778h)a03c5172020-07-06 15:13:07 -040052def test_alarm_set_get(monkeypatch):
Lott, Christopher (cl778h)81084bc2020-06-01 20:53:12 -040053 """
54 test set functions
55 """
56 act = AlarmAction.RAISE
57 assert act is not None
58
59 sev = AlarmSeverity.CRITICAL
60 assert sev is not None
61
62 det = AlarmDetail("1", "2", 3, AlarmSeverity.MINOR, "4", "5")
63 assert det[alarm.KEY_MANAGED_OBJECT_ID] == "1"
64 assert det[alarm.KEY_APPLICATION_ID] == "2"
65 assert det[alarm.KEY_SPECIFIC_PROBLEM] == 3
66 assert det[alarm.KEY_PERCEIVED_SEVERITY] == AlarmSeverity.MINOR.name
67 assert det[alarm.KEY_IDENTIFYING_INFO] == "4"
68 assert det[alarm.KEY_ADDITIONAL_INFO] == "5"
69
Lott, Christopher (cl778h)a03c5172020-07-06 15:13:07 -040070 # missing environment variables
71 with pytest.raises(InitFailed):
72 alarm.AlarmManager(MRC_SEND, "missing", "envvars")
73
74 # invalid environment variables
75 monkeypatch.setenv(ALARM_MGR_SERVICE_NAME_ENV, "0")
76 monkeypatch.setenv(ALARM_MGR_SERVICE_PORT_ENV, "a")
77 with pytest.raises(InitFailed):
78 alarm.AlarmManager(MRC_SEND, "bogus", "envvars")
79
80 # good environment variables
81 monkeypatch.setenv(ALARM_MGR_SERVICE_NAME_ENV, "localhost")
82 monkeypatch.setenv(ALARM_MGR_SERVICE_PORT_ENV, "4567") # any int is ok here
Lott, Christopher (cl778h)81084bc2020-06-01 20:53:12 -040083 mgr = alarm.AlarmManager(MRC_SEND, "moid2", "appid2")
84 assert mgr is not None
85 assert mgr.managed_object_id == "moid2"
86 assert mgr.application_id == "appid2"
87
88
89def _receive_alarm_msg(action: AlarmAction):
90 """
91 delays briefly, receives a message, checks the message type and action
92 """
93 time.sleep(0.5)
94 sbuf_rcv = rmr.rmr_alloc_msg(MRC_RCV, SIZE)
95 sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv, 2000)
96 rcv_summary = rmr.message_summary(sbuf_rcv)
97 assert rcv_summary[rmr.RMR_MS_MSG_STATE] == rmr.RMR_OK
98 assert rcv_summary[rmr.RMR_MS_MSG_TYPE] == alarm.RIC_ALARM_UPDATE
99 # parse JSON
100 data = json.loads(rcv_summary[rmr.RMR_MS_PAYLOAD].decode())
101 assert data[alarm.KEY_ALARM_ACTION] == action.name
102
103
Lott, Christopher (cl778h)a03c5172020-07-06 15:13:07 -0400104def test_alarm_manager(monkeypatch):
Lott, Christopher (cl778h)81084bc2020-06-01 20:53:12 -0400105 """
106 test send functions and ensure a message arrives
107 """
Lott, Christopher (cl778h)a03c5172020-07-06 15:13:07 -0400108 monkeypatch.setenv(ALARM_MGR_SERVICE_NAME_ENV, "localhost")
109 monkeypatch.setenv(ALARM_MGR_SERVICE_PORT_ENV, "4567") # must match rcv port above
Lott, Christopher (cl778h)81084bc2020-06-01 20:53:12 -0400110 mgr = AlarmManager(MRC_SEND, "moid", "appid")
111 assert mgr is not None
112
113 det = mgr.create_alarm(3, AlarmSeverity.DEFAULT, "identifying", "additional")
114 assert det is not None
115
116 success = mgr.raise_alarm(det)
117 assert success
118 _receive_alarm_msg(AlarmAction.RAISE)
119
120 success = mgr.clear_alarm(det)
121 assert success
122 _receive_alarm_msg(AlarmAction.CLEAR)
123
124 success = mgr.reraise_alarm(det)
125 assert success
126 _receive_alarm_msg(AlarmAction.CLEAR)
127 _receive_alarm_msg(AlarmAction.RAISE)
128
129 success = mgr.clear_all_alarms()
130 assert success
131 _receive_alarm_msg(AlarmAction.CLEARALL)