Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2020-present Open Networking Foundation |
| 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 | |
| 17 | import unittest |
| 18 | import edge_monitoring_server as ems |
| 19 | import datetime |
| 20 | import pytz |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 21 | import json |
| 22 | import time |
| 23 | |
| 24 | |
| 25 | test_edge = { |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 26 | 'name': 'ace-menlo-pixel', |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 27 | 'status': { |
| 28 | 'control_plane': 'connected', |
| 29 | 'user_plane': 'connected' |
| 30 | }, |
| 31 | 'last_update': time.time() |
| 32 | } |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class MyEvent: |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 36 | def __init__ (self, location = "", description = "", summary = "", start = None, end = None, all_day = False): |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 37 | self.location = location |
| 38 | self.description = description |
| 39 | self.summary = summary |
| 40 | self.start = start |
| 41 | self.end = end |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 42 | self.all_day = all_day |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 43 | |
| 44 | class MyEventNoLoc: |
| 45 | def __init__ (self, description = "", summary = ""): |
| 46 | self.description = description |
| 47 | self.summary = summary |
| 48 | |
| 49 | |
| 50 | class TestEdgeMonitoringServer(unittest.TestCase): |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 51 | def setUp(self): |
| 52 | self.app = ems.app.test_client() |
| 53 | |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 54 | def test_match_location(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 55 | event = MyEvent(location = "ace-menlo-pixel-production") |
| 56 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
| 57 | event = MyEvent(location = "(Compute)-MP-1-Aether Production") |
| 58 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 59 | |
| 60 | def test_match_description(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 61 | event = MyEvent(description = "ace-menlo-pixel-production") |
| 62 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
| 63 | event = MyEvent(description = "(Compute)-MP-1-Aether Production") |
| 64 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 65 | |
| 66 | def test_match_summary(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 67 | event = MyEvent(summary = "ace-menlo-pixel-production") |
| 68 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
| 69 | event = MyEvent(summary = "(Compute)-MP-1-Aether Production") |
| 70 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 71 | |
| 72 | def test_no_match(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 73 | event = MyEvent(summary = "ace-menlo-pixel-production, (Compute)-MP-1-Aether Production") |
| 74 | self.assertFalse(ems.is_my_event(event, "ace-intel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 75 | self.assertFalse(ems.is_my_event(event, "(Compute)-MP-1-Aether Staging")) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 76 | self.assertFalse(ems.is_my_event(event, "ace-menlo")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 77 | |
| 78 | def test_missing_field(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 79 | event = MyEventNoLoc(description = "(Compute)-MP-1-Aether Production") |
| 80 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 81 | |
| 82 | def test_in_window(self): |
| 83 | events = [] |
| 84 | now = datetime.datetime.now(pytz.utc) |
| 85 | events.append(MyEvent(location = "(Compute)-MP-1-Aether Production", |
| 86 | start = now - datetime.timedelta(hours=1), |
| 87 | end = now + datetime.timedelta(hours=1))) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 88 | self.assertTrue(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
| 89 | self.assertFalse(ems.in_maintenance_window(events, "ace-tucson", now)) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 90 | |
| 91 | def test_not_in_window(self): |
| 92 | events = [] |
| 93 | now = datetime.datetime.now(pytz.utc) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 94 | events.append(MyEvent(location = "ace-menlo-pixel-production", |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 95 | start = now + datetime.timedelta(hours=1), |
| 96 | end = now + datetime.timedelta(hours=2))) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 97 | self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 98 | |
| 99 | def test_no_events(self): |
| 100 | events = [] |
| 101 | now = datetime.datetime.now(pytz.utc) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 102 | self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 103 | |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 104 | def test_all_day_events(self): |
| 105 | events = [] |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 106 | events.append(MyEvent(location = "ace-menlo-pixel-production", |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 107 | start = datetime.datetime(2020, 9, 2, 0, 0), |
| 108 | end = datetime.datetime(2020, 9, 3, 0, 0), |
| 109 | all_day = True)) |
| 110 | |
| 111 | ems.process_all_day_events(events) |
| 112 | |
| 113 | now = datetime.datetime(2020, 9, 2, 12, 0, tzinfo=pytz.utc) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 114 | self.assertTrue(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 115 | |
| 116 | now = datetime.datetime(2020, 9, 3, 12, 0, tzinfo=pytz.utc) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 117 | self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 118 | |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 119 | def test_get_edges(self): |
| 120 | response = self.app.get('/edges') |
| 121 | data = json.loads(response.get_data(as_text=True)) |
| 122 | self.assertEqual(len(data['edges']), 1) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 123 | self.assertEqual(data['edges'][0]['name'], 'ace-example') |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 124 | |
| 125 | def test_create_and_delete_edge(self): |
| 126 | response = self.app.post('/edges', json=test_edge) |
| 127 | data = json.loads(response.get_data(as_text=True)) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 128 | self.assertEqual(data['edge']['name'], 'ace-menlo-pixel') |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 129 | |
| 130 | response = self.app.get('/edges') |
| 131 | data = json.loads(response.get_data(as_text=True)) |
| 132 | self.assertEqual(len(data['edges']), 2) |
| 133 | |
Andy Bavier | e47157d | 2020-12-11 14:13:12 -0700 | [diff] [blame] | 134 | response = self.app.get('/edges/metrics') |
| 135 | data = response.get_data(as_text=True) |
Andy Bavier | 5b4e28f | 2021-03-09 15:48:20 -0700 | [diff] [blame] | 136 | print(data) |
Andy Bavier | e47157d | 2020-12-11 14:13:12 -0700 | [diff] [blame] | 137 | self.assertTrue('aetheredge_status_control_plane{name="ace-menlo-pixel"} 2.0' in data) |
| 138 | self.assertTrue('aetheredge_status_user_plane{name="ace-menlo-pixel"} 2.0' in data) |
| 139 | self.assertTrue('aetheredge_last_update{name="ace-menlo-pixel"}' in data) |
Andy Bavier | a0c40aa | 2021-03-10 12:09:12 -0700 | [diff] [blame^] | 140 | self.assertTrue('aetheredge_connect_test_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 141 | self.assertTrue('aetheredge_ping_test_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 142 | self.assertTrue('aetheredge_connect_test_down{name="ace-menlo-pixel"} 0.0' in data) |
| 143 | self.assertTrue('aetheredge_ping_test_down{name="ace-menlo-pixel"} 0.0' in data) |
Andy Bavier | e47157d | 2020-12-11 14:13:12 -0700 | [diff] [blame] | 144 | |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 145 | response = self.app.delete('/edges/ace-menlo-pixel') |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 146 | data = json.loads(response.get_data(as_text=True)) |
| 147 | self.assertEqual(data['result'], True) |
Andy Bavier | e47157d | 2020-12-11 14:13:12 -0700 | [diff] [blame] | 148 | |
| 149 | response = self.app.get('/edges/metrics') |
| 150 | data = response.get_data(as_text=True) |
Andy Bavier | 5b4e28f | 2021-03-09 15:48:20 -0700 | [diff] [blame] | 151 | print(data) |
| 152 | self.assertFalse('ace-menlo-pixel' in data) |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 153 | |
| 154 | response = self.app.get('/edges') |
| 155 | data = json.loads(response.get_data(as_text=True)) |
| 156 | self.assertEqual(len(data['edges']), 1) |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 157 | |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 158 | |
| 159 | if __name__ == '__main__': |
| 160 | suite = unittest.TestLoader().loadTestsFromTestCase(TestEdgeMonitoringServer) |
| 161 | unittest.TextTestRunner(verbosity=2).run(suite) |