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 | }, |
Jeremy Ronquillo | f420025 | 2021-02-13 16:11:04 -0800 | [diff] [blame^] | 31 | 'speedtest': { |
| 32 | 'ping': { |
| 33 | 'dns': { |
| 34 | 'min': 2.0, |
| 35 | 'avg': 4.0, |
| 36 | 'max': 6.0, |
| 37 | 'stddev': 1.0 |
| 38 | } |
| 39 | } |
| 40 | }, |
| 41 | 'last_update': time.time() |
| 42 | } |
| 43 | |
| 44 | test_edge_no_speedtest = { |
| 45 | 'name': 'ace-menlo-pixel', |
| 46 | 'status': { |
| 47 | 'control_plane': 'connected', |
| 48 | 'user_plane': 'connected' |
| 49 | }, |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 50 | 'last_update': time.time() |
| 51 | } |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 52 | |
| 53 | |
| 54 | class MyEvent: |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 55 | 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] | 56 | self.location = location |
| 57 | self.description = description |
| 58 | self.summary = summary |
| 59 | self.start = start |
| 60 | self.end = end |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 61 | self.all_day = all_day |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 62 | |
| 63 | class MyEventNoLoc: |
| 64 | def __init__ (self, description = "", summary = ""): |
| 65 | self.description = description |
| 66 | self.summary = summary |
| 67 | |
| 68 | |
| 69 | class TestEdgeMonitoringServer(unittest.TestCase): |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 70 | def setUp(self): |
| 71 | self.app = ems.app.test_client() |
| 72 | |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 73 | def test_match_location(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 74 | event = MyEvent(location = "ace-menlo-pixel-production") |
| 75 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
| 76 | event = MyEvent(location = "(Compute)-MP-1-Aether Production") |
| 77 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 78 | |
| 79 | def test_match_description(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 80 | event = MyEvent(description = "ace-menlo-pixel-production") |
| 81 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
| 82 | event = MyEvent(description = "(Compute)-MP-1-Aether Production") |
| 83 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 84 | |
| 85 | def test_match_summary(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 86 | event = MyEvent(summary = "ace-menlo-pixel-production") |
| 87 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
| 88 | event = MyEvent(summary = "(Compute)-MP-1-Aether Production") |
| 89 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 90 | |
| 91 | def test_no_match(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 92 | event = MyEvent(summary = "ace-menlo-pixel-production, (Compute)-MP-1-Aether Production") |
| 93 | self.assertFalse(ems.is_my_event(event, "ace-intel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 94 | self.assertFalse(ems.is_my_event(event, "(Compute)-MP-1-Aether Staging")) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 95 | self.assertFalse(ems.is_my_event(event, "ace-menlo")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 96 | |
| 97 | def test_missing_field(self): |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 98 | event = MyEventNoLoc(description = "(Compute)-MP-1-Aether Production") |
| 99 | self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel")) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 100 | |
| 101 | def test_in_window(self): |
| 102 | events = [] |
| 103 | now = datetime.datetime.now(pytz.utc) |
| 104 | events.append(MyEvent(location = "(Compute)-MP-1-Aether Production", |
| 105 | start = now - datetime.timedelta(hours=1), |
| 106 | end = now + datetime.timedelta(hours=1))) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 107 | self.assertTrue(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
| 108 | self.assertFalse(ems.in_maintenance_window(events, "ace-tucson", now)) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 109 | |
| 110 | def test_not_in_window(self): |
| 111 | events = [] |
| 112 | now = datetime.datetime.now(pytz.utc) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 113 | events.append(MyEvent(location = "ace-menlo-pixel-production", |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 114 | start = now + datetime.timedelta(hours=1), |
| 115 | end = now + datetime.timedelta(hours=2))) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 116 | self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 117 | |
| 118 | def test_no_events(self): |
| 119 | events = [] |
| 120 | now = datetime.datetime.now(pytz.utc) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 121 | self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 122 | |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 123 | def test_all_day_events(self): |
| 124 | events = [] |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 125 | events.append(MyEvent(location = "ace-menlo-pixel-production", |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 126 | start = datetime.datetime(2020, 9, 2, 0, 0), |
| 127 | end = datetime.datetime(2020, 9, 3, 0, 0), |
| 128 | all_day = True)) |
| 129 | |
| 130 | ems.process_all_day_events(events) |
| 131 | |
| 132 | now = datetime.datetime(2020, 9, 2, 12, 0, tzinfo=pytz.utc) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 133 | self.assertTrue(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 134 | |
| 135 | now = datetime.datetime(2020, 9, 3, 12, 0, tzinfo=pytz.utc) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 136 | self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now)) |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 137 | |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 138 | def test_get_edges(self): |
| 139 | response = self.app.get('/edges') |
| 140 | data = json.loads(response.get_data(as_text=True)) |
| 141 | self.assertEqual(len(data['edges']), 1) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 142 | self.assertEqual(data['edges'][0]['name'], 'ace-example') |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 143 | |
Andy Bavier | f872e9a | 2021-03-22 12:06:25 -0700 | [diff] [blame] | 144 | def test_create_and_delete_edge_legacy(self): |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 145 | response = self.app.post('/edges', json=test_edge) |
| 146 | data = json.loads(response.get_data(as_text=True)) |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 147 | self.assertEqual(data['edge']['name'], 'ace-menlo-pixel') |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 148 | |
| 149 | response = self.app.get('/edges') |
| 150 | data = json.loads(response.get_data(as_text=True)) |
| 151 | self.assertEqual(len(data['edges']), 2) |
| 152 | |
Andy Bavier | e47157d | 2020-12-11 14:13:12 -0700 | [diff] [blame] | 153 | response = self.app.get('/edges/metrics') |
| 154 | data = response.get_data(as_text=True) |
Andy Bavier | 5b4e28f | 2021-03-09 15:48:20 -0700 | [diff] [blame] | 155 | print(data) |
Andy Bavier | e47157d | 2020-12-11 14:13:12 -0700 | [diff] [blame] | 156 | self.assertTrue('aetheredge_status_control_plane{name="ace-menlo-pixel"} 2.0' in data) |
| 157 | self.assertTrue('aetheredge_status_user_plane{name="ace-menlo-pixel"} 2.0' in data) |
| 158 | self.assertTrue('aetheredge_last_update{name="ace-menlo-pixel"}' in data) |
Andy Bavier | a0c40aa | 2021-03-10 12:09:12 -0700 | [diff] [blame] | 159 | self.assertTrue('aetheredge_connect_test_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 160 | self.assertTrue('aetheredge_ping_test_ok{name="ace-menlo-pixel"} 1.0' in data) |
Andy Bavier | 3c7b78d | 2021-03-11 14:16:43 -0700 | [diff] [blame] | 161 | self.assertTrue('aetheredge_e2e_tests_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 162 | self.assertTrue('aetheredge_e2e_tests_down{name="ace-menlo-pixel"} 0.0' in data) |
Andy Bavier | e47157d | 2020-12-11 14:13:12 -0700 | [diff] [blame] | 163 | |
Andy Bavier | 8a5c987 | 2020-10-21 13:17:53 -0700 | [diff] [blame] | 164 | response = self.app.delete('/edges/ace-menlo-pixel') |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 165 | data = json.loads(response.get_data(as_text=True)) |
| 166 | self.assertEqual(data['result'], True) |
Andy Bavier | e47157d | 2020-12-11 14:13:12 -0700 | [diff] [blame] | 167 | |
| 168 | response = self.app.get('/edges/metrics') |
| 169 | data = response.get_data(as_text=True) |
Andy Bavier | 5b4e28f | 2021-03-09 15:48:20 -0700 | [diff] [blame] | 170 | print(data) |
| 171 | self.assertFalse('ace-menlo-pixel' in data) |
Hyunsun Moon | 5f237ec | 2020-09-29 14:45:52 -0700 | [diff] [blame] | 172 | |
| 173 | response = self.app.get('/edges') |
| 174 | data = json.loads(response.get_data(as_text=True)) |
| 175 | self.assertEqual(len(data['edges']), 1) |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame] | 176 | |
Andy Bavier | f872e9a | 2021-03-22 12:06:25 -0700 | [diff] [blame] | 177 | def test_create_and_delete_edge(self): |
| 178 | response = self.app.post('/testresults', json=test_edge) |
| 179 | data = json.loads(response.get_data(as_text=True)) |
| 180 | self.assertEqual(data['edge']['name'], 'ace-menlo-pixel') |
| 181 | |
| 182 | response = self.app.get('/edges') |
| 183 | data = json.loads(response.get_data(as_text=True)) |
| 184 | self.assertEqual(len(data['edges']), 2) |
| 185 | |
| 186 | response = self.app.get('/edges/metrics') |
| 187 | data = response.get_data(as_text=True) |
| 188 | print(data) |
| 189 | self.assertTrue('aetheredge_status_control_plane{name="ace-menlo-pixel"} 2.0' in data) |
| 190 | self.assertTrue('aetheredge_status_user_plane{name="ace-menlo-pixel"} 2.0' in data) |
| 191 | self.assertTrue('aetheredge_last_update{name="ace-menlo-pixel"}' in data) |
| 192 | self.assertTrue('aetheredge_connect_test_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 193 | self.assertTrue('aetheredge_ping_test_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 194 | self.assertTrue('aetheredge_e2e_tests_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 195 | self.assertTrue('aetheredge_e2e_tests_down{name="ace-menlo-pixel"} 0.0' in data) |
| 196 | |
| 197 | response = self.app.delete('/testresults/ace-menlo-pixel') |
| 198 | data = json.loads(response.get_data(as_text=True)) |
| 199 | self.assertEqual(data['result'], True) |
| 200 | |
| 201 | response = self.app.get('/edges/metrics') |
| 202 | data = response.get_data(as_text=True) |
| 203 | print(data) |
| 204 | self.assertFalse('ace-menlo-pixel' in data) |
| 205 | |
| 206 | response = self.app.get('/edges') |
| 207 | data = json.loads(response.get_data(as_text=True)) |
| 208 | self.assertEqual(len(data['edges']), 1) |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 209 | |
Jeremy Ronquillo | f420025 | 2021-02-13 16:11:04 -0800 | [diff] [blame^] | 210 | def test_create_and_delete_edge_speed_test(self): |
| 211 | response = self.app.post('/testresults', json=test_edge) |
| 212 | data = json.loads(response.get_data(as_text=True)) |
| 213 | self.assertEqual(data['edge']['name'], 'ace-menlo-pixel') |
| 214 | |
| 215 | response = self.app.get('/edges/metrics') |
| 216 | data = response.get_data(as_text=True) |
| 217 | print(data) |
| 218 | |
| 219 | self.assertTrue('aetheredge_ping_dns_test_min{name="ace-menlo-pixel"} ' + str(test_edge['speedtest']['ping']['dns']['min']) in data) |
| 220 | self.assertTrue('aetheredge_ping_dns_test_avg{name="ace-menlo-pixel"} ' + str(test_edge['speedtest']['ping']['dns']['avg']) in data) |
| 221 | self.assertTrue('aetheredge_ping_dns_test_max{name="ace-menlo-pixel"} ' + str(test_edge['speedtest']['ping']['dns']['max']) in data) |
| 222 | self.assertTrue('aetheredge_ping_dns_test_stddev{name="ace-menlo-pixel"} ' + str(test_edge['speedtest']['ping']['dns']['stddev']) in data) |
| 223 | |
| 224 | response = self.app.delete('/testresults/ace-menlo-pixel') |
| 225 | data = json.loads(response.get_data(as_text=True)) |
| 226 | self.assertEqual(data['result'], True) |
| 227 | |
| 228 | def test_backwards_compatible_no_speedtest(self): |
| 229 | response = self.app.post('/testresults', json=test_edge_no_speedtest) |
| 230 | data = json.loads(response.get_data(as_text=True)) |
| 231 | self.assertEqual(data['edge']['name'], 'ace-menlo-pixel') |
| 232 | |
| 233 | response = self.app.get('/edges/metrics') |
| 234 | data = response.get_data(as_text=True) |
| 235 | print(data) |
| 236 | |
| 237 | self.assertFalse('aetheredge_ping_dns_test_min{name="ace-menlo-pixel"}' in data) |
| 238 | self.assertFalse('aetheredge_ping_dns_test_avg{name="ace-menlo-pixel"}' in data) |
| 239 | self.assertFalse('aetheredge_ping_dns_test_max{name="ace-menlo-pixel"}' in data) |
| 240 | self.assertFalse('aetheredge_ping_dns_test_stddev{name="ace-menlo-pixel"}' in data) |
| 241 | |
| 242 | self.assertTrue('aetheredge_status_control_plane{name="ace-menlo-pixel"} 2.0' in data) |
| 243 | self.assertTrue('aetheredge_status_user_plane{name="ace-menlo-pixel"} 2.0' in data) |
| 244 | self.assertTrue('aetheredge_last_update{name="ace-menlo-pixel"}' in data) |
| 245 | self.assertTrue('aetheredge_connect_test_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 246 | self.assertTrue('aetheredge_ping_test_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 247 | self.assertTrue('aetheredge_e2e_tests_ok{name="ace-menlo-pixel"} 1.0' in data) |
| 248 | self.assertTrue('aetheredge_e2e_tests_down{name="ace-menlo-pixel"} 0.0' in data) |
| 249 | |
| 250 | response = self.app.delete('/testresults/ace-menlo-pixel') |
| 251 | data = json.loads(response.get_data(as_text=True)) |
| 252 | self.assertEqual(data['result'], True) |
| 253 | |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 254 | if __name__ == '__main__': |
| 255 | suite = unittest.TestLoader().loadTestsFromTestCase(TestEdgeMonitoringServer) |
| 256 | unittest.TextTestRunner(verbosity=2).run(suite) |