From 2f0fbe0bf481f84da3ed1ec5148efd3bf6af2e1b Mon Sep 17 00:00:00 2001 From: Anna Waldron Date: Fri, 30 Nov 2018 11:08:50 -0800 Subject: [PATCH] Issue #92 - Adding script for testing new notification options --- test_scripts/__init__.py | 0 test_scripts/notification_test.py | 116 ++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 test_scripts/__init__.py create mode 100644 test_scripts/notification_test.py diff --git a/test_scripts/__init__.py b/test_scripts/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test_scripts/notification_test.py b/test_scripts/notification_test.py new file mode 100644 index 00000000..943c9fda --- /dev/null +++ b/test_scripts/notification_test.py @@ -0,0 +1,116 @@ +import ait.gui +import ait.core +import mock +from collections import defaultdict +from time import sleep +from ait.core import cfg, tlm, limits + +""" +This script tests that notifications are triggered at the threshold +and frequency specified in the configs. To run the test, execute the script, +wait up to 10 seconds for it to run, and then quit the process using Ctrl-C. +""" + + +@mock.patch('ait.core.notify.trigger_notification') +def enable_monitoring_test(mock_notify): + try: + set_notif_options(thrshld=3, freq=2) + print_notif_options() + + ait.gui.init('localhost', 8080) + ait.gui.enable_monitoring() + sleep(1) + add_telemetry() + ait.gui.wait() + sleep(1) + ait.gui.cleanup() + + except KeyboardInterrupt: + ait.core.log.info('Received Ctrl-C. Stopping AIT GUI.') + ait.gui.cleanup() + + except Exception as e: + ait.core.log.error('AIT GUI error: %s' % str(e)) + + # define expected calls + # value 0 should notify 3 times (msg 3, 5, 7) + call_list = [mock.mock.call( + 'limit-error', + 'Field Voltage_A error out of limit with value 0')] * 3 + # value 50 should notify 3 times (msg 3, 5, 7) + call_list.extend([mock.mock.call( + 'limit-error', + 'Field Voltage_A error out of limit with value 50')] * 3) + + print('Notification was triggered {} times.' + .format(mock_notify.call_count)) + + assert mock_notify.call_args_list == call_list + + +def add_telemetry(): + # error value + pkt.Voltage_A = 0 + for i in range(8): + ait.gui.Sessions.addTelemetry(pkt_defn.uid, pkt._data) + sleep(1) + + # good value - no notifs + pkt.Voltage_A = 20 + for i in range(2): + ait.gui.Sessions.addTelemetry(pkt_defn.uid, pkt._data) + sleep(1) + + # error value + pkt.Voltage_A = 50 + for i in range(8): + ait.gui.Sessions.addTelemetry(pkt_defn.uid, pkt._data) + + +def get_packet_and_defn(): + first_stream = ait.config.get('gui.telemetry')[0] + stream = cfg.AitConfig(config=first_stream).get('stream') + name = stream.get('name', '') + pkt_defn = tlm.getDefaultDict().get(name, None) + pkt = tlm.Packet(pkt_defn) + + return pkt_defn, pkt + + +def get_limit_dict(): + limit_dict = defaultdict(dict) + for k, v in limits.getDefaultDict().iteritems(): + packet, field = k.split('.') + limit_dict[packet][field] = v + + return limit_dict + + +def print_notif_options(): + thrshld = ait.config.get('notifications.options.threshold') + freq = ait.config.get('notifications.options.frequency') + print('Threshold and frequency are {} and {} respectively.' + .format(thrshld, freq)) + + +def set_notif_options(thrshld=None, freq=None): + pathvars = {} + if thrshld: + pathvars['notifications.options.threshold'] = thrshld + print('Changing notif threshold to {}.'.format(thrshld)) + if freq: + pathvars['notifications.options.frequency'] = freq + print('Changing notif freq to {}.'.format(freq)) + + ait.config.addPathVariables(pathvars) + ait.config.reload() + + +if __name__ == "__main__": + global pkt_defn, pkt, limit_dict, limit_defn + pkt_defn, pkt = get_packet_and_defn() + limit_dict = get_limit_dict() + limit_defn = limit_dict[pkt_defn.name]['Voltage_A'] + + enable_monitoring_test()