blob: a7e8cf2fce0c115c0a53ee5f556b3c95839f7046 [file] [log] [blame]
Andy Bavier614af142020-08-07 14:49:56 -07001#!/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
17import unittest
18import edge_monitoring_server as ems
19import datetime
20import pytz
Hyunsun Moon5f237ec2020-09-29 14:45:52 -070021import json
22import time
23
24
25test_edge = {
Andy Bavier8a5c9872020-10-21 13:17:53 -070026 'name': 'ace-menlo-pixel',
Hyunsun Moon5f237ec2020-09-29 14:45:52 -070027 'status': {
28 'control_plane': 'connected',
29 'user_plane': 'connected'
30 },
Jeremy Ronquillof4200252021-02-13 16:11:04 -080031 '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
44test_edge_no_speedtest = {
45 'name': 'ace-menlo-pixel',
46 'status': {
47 'control_plane': 'connected',
48 'user_plane': 'connected'
49 },
Hyunsun Moon5f237ec2020-09-29 14:45:52 -070050 'last_update': time.time()
51}
Andy Bavier614af142020-08-07 14:49:56 -070052
53
54class MyEvent:
Andy Bavierc41cf0c2020-09-02 14:49:21 -070055 def __init__ (self, location = "", description = "", summary = "", start = None, end = None, all_day = False):
Andy Bavier614af142020-08-07 14:49:56 -070056 self.location = location
57 self.description = description
58 self.summary = summary
59 self.start = start
60 self.end = end
Andy Bavierc41cf0c2020-09-02 14:49:21 -070061 self.all_day = all_day
Andy Bavier614af142020-08-07 14:49:56 -070062
63class MyEventNoLoc:
64 def __init__ (self, description = "", summary = ""):
65 self.description = description
66 self.summary = summary
67
68
69class TestEdgeMonitoringServer(unittest.TestCase):
Hyunsun Moon5f237ec2020-09-29 14:45:52 -070070 def setUp(self):
71 self.app = ems.app.test_client()
72
Andy Bavier614af142020-08-07 14:49:56 -070073 def test_match_location(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070074 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 Bavier614af142020-08-07 14:49:56 -070078
79 def test_match_description(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070080 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 Bavier614af142020-08-07 14:49:56 -070084
85 def test_match_summary(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070086 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 Bavier614af142020-08-07 14:49:56 -070090
91 def test_no_match(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070092 event = MyEvent(summary = "ace-menlo-pixel-production, (Compute)-MP-1-Aether Production")
93 self.assertFalse(ems.is_my_event(event, "ace-intel"))
Andy Bavier614af142020-08-07 14:49:56 -070094 self.assertFalse(ems.is_my_event(event, "(Compute)-MP-1-Aether Staging"))
Andy Bavier8a5c9872020-10-21 13:17:53 -070095 self.assertFalse(ems.is_my_event(event, "ace-menlo"))
Andy Bavier614af142020-08-07 14:49:56 -070096
97 def test_missing_field(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070098 event = MyEventNoLoc(description = "(Compute)-MP-1-Aether Production")
99 self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel"))
Andy Bavier614af142020-08-07 14:49:56 -0700100
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 Bavier8a5c9872020-10-21 13:17:53 -0700107 self.assertTrue(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
108 self.assertFalse(ems.in_maintenance_window(events, "ace-tucson", now))
Andy Bavier614af142020-08-07 14:49:56 -0700109
110 def test_not_in_window(self):
111 events = []
112 now = datetime.datetime.now(pytz.utc)
Andy Bavier8a5c9872020-10-21 13:17:53 -0700113 events.append(MyEvent(location = "ace-menlo-pixel-production",
Andy Bavier614af142020-08-07 14:49:56 -0700114 start = now + datetime.timedelta(hours=1),
115 end = now + datetime.timedelta(hours=2)))
Andy Bavier8a5c9872020-10-21 13:17:53 -0700116 self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
Andy Bavier614af142020-08-07 14:49:56 -0700117
118 def test_no_events(self):
119 events = []
120 now = datetime.datetime.now(pytz.utc)
Andy Bavier8a5c9872020-10-21 13:17:53 -0700121 self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
Andy Bavier614af142020-08-07 14:49:56 -0700122
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700123 def test_all_day_events(self):
124 events = []
Andy Bavier8a5c9872020-10-21 13:17:53 -0700125 events.append(MyEvent(location = "ace-menlo-pixel-production",
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700126 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 Bavier8a5c9872020-10-21 13:17:53 -0700133 self.assertTrue(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700134
135 now = datetime.datetime(2020, 9, 3, 12, 0, tzinfo=pytz.utc)
Andy Bavier8a5c9872020-10-21 13:17:53 -0700136 self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700137
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700138 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 Bavier8a5c9872020-10-21 13:17:53 -0700142 self.assertEqual(data['edges'][0]['name'], 'ace-example')
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700143
Andy Bavierf872e9a2021-03-22 12:06:25 -0700144 def test_create_and_delete_edge_legacy(self):
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700145 response = self.app.post('/edges', json=test_edge)
146 data = json.loads(response.get_data(as_text=True))
Andy Bavier8a5c9872020-10-21 13:17:53 -0700147 self.assertEqual(data['edge']['name'], 'ace-menlo-pixel')
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700148
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 Baviere47157d2020-12-11 14:13:12 -0700153 response = self.app.get('/edges/metrics')
154 data = response.get_data(as_text=True)
Andy Bavier5b4e28f2021-03-09 15:48:20 -0700155 print(data)
Andy Baviere47157d2020-12-11 14:13:12 -0700156 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 Baviera0c40aa2021-03-10 12:09:12 -0700159 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 Bavier3c7b78d2021-03-11 14:16:43 -0700161 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 Baviere47157d2020-12-11 14:13:12 -0700163
Andy Bavier8a5c9872020-10-21 13:17:53 -0700164 response = self.app.delete('/edges/ace-menlo-pixel')
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700165 data = json.loads(response.get_data(as_text=True))
166 self.assertEqual(data['result'], True)
Andy Baviere47157d2020-12-11 14:13:12 -0700167
168 response = self.app.get('/edges/metrics')
169 data = response.get_data(as_text=True)
Andy Bavier5b4e28f2021-03-09 15:48:20 -0700170 print(data)
171 self.assertFalse('ace-menlo-pixel' in data)
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700172
173 response = self.app.get('/edges')
174 data = json.loads(response.get_data(as_text=True))
175 self.assertEqual(len(data['edges']), 1)
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700176
Andy Bavierf872e9a2021-03-22 12:06:25 -0700177 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 Bavier614af142020-08-07 14:49:56 -0700209
Jeremy Ronquillof4200252021-02-13 16:11:04 -0800210 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 Bavier614af142020-08-07 14:49:56 -0700254if __name__ == '__main__':
255 suite = unittest.TestLoader().loadTestsFromTestCase(TestEdgeMonitoringServer)
256 unittest.TextTestRunner(verbosity=2).run(suite)