blob: 9aad25900fe5b359293cb984c1914db5c5f58557 [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 },
31 'last_update': time.time()
32}
Andy Bavier614af142020-08-07 14:49:56 -070033
34
35class MyEvent:
Andy Bavierc41cf0c2020-09-02 14:49:21 -070036 def __init__ (self, location = "", description = "", summary = "", start = None, end = None, all_day = False):
Andy Bavier614af142020-08-07 14:49:56 -070037 self.location = location
38 self.description = description
39 self.summary = summary
40 self.start = start
41 self.end = end
Andy Bavierc41cf0c2020-09-02 14:49:21 -070042 self.all_day = all_day
Andy Bavier614af142020-08-07 14:49:56 -070043
44class MyEventNoLoc:
45 def __init__ (self, description = "", summary = ""):
46 self.description = description
47 self.summary = summary
48
49
50class TestEdgeMonitoringServer(unittest.TestCase):
Hyunsun Moon5f237ec2020-09-29 14:45:52 -070051 def setUp(self):
52 self.app = ems.app.test_client()
53
Andy Bavier614af142020-08-07 14:49:56 -070054 def test_match_location(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070055 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 Bavier614af142020-08-07 14:49:56 -070059
60 def test_match_description(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070061 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 Bavier614af142020-08-07 14:49:56 -070065
66 def test_match_summary(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070067 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 Bavier614af142020-08-07 14:49:56 -070071
72 def test_no_match(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070073 event = MyEvent(summary = "ace-menlo-pixel-production, (Compute)-MP-1-Aether Production")
74 self.assertFalse(ems.is_my_event(event, "ace-intel"))
Andy Bavier614af142020-08-07 14:49:56 -070075 self.assertFalse(ems.is_my_event(event, "(Compute)-MP-1-Aether Staging"))
Andy Bavier8a5c9872020-10-21 13:17:53 -070076 self.assertFalse(ems.is_my_event(event, "ace-menlo"))
Andy Bavier614af142020-08-07 14:49:56 -070077
78 def test_missing_field(self):
Andy Bavier8a5c9872020-10-21 13:17:53 -070079 event = MyEventNoLoc(description = "(Compute)-MP-1-Aether Production")
80 self.assertTrue(ems.is_my_event(event, "ace-menlo-pixel"))
Andy Bavier614af142020-08-07 14:49:56 -070081
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 Bavier8a5c9872020-10-21 13:17:53 -070088 self.assertTrue(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
89 self.assertFalse(ems.in_maintenance_window(events, "ace-tucson", now))
Andy Bavier614af142020-08-07 14:49:56 -070090
91 def test_not_in_window(self):
92 events = []
93 now = datetime.datetime.now(pytz.utc)
Andy Bavier8a5c9872020-10-21 13:17:53 -070094 events.append(MyEvent(location = "ace-menlo-pixel-production",
Andy Bavier614af142020-08-07 14:49:56 -070095 start = now + datetime.timedelta(hours=1),
96 end = now + datetime.timedelta(hours=2)))
Andy Bavier8a5c9872020-10-21 13:17:53 -070097 self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
Andy Bavier614af142020-08-07 14:49:56 -070098
99 def test_no_events(self):
100 events = []
101 now = datetime.datetime.now(pytz.utc)
Andy Bavier8a5c9872020-10-21 13:17:53 -0700102 self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
Andy Bavier614af142020-08-07 14:49:56 -0700103
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700104 def test_all_day_events(self):
105 events = []
Andy Bavier8a5c9872020-10-21 13:17:53 -0700106 events.append(MyEvent(location = "ace-menlo-pixel-production",
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700107 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 Bavier8a5c9872020-10-21 13:17:53 -0700114 self.assertTrue(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700115
116 now = datetime.datetime(2020, 9, 3, 12, 0, tzinfo=pytz.utc)
Andy Bavier8a5c9872020-10-21 13:17:53 -0700117 self.assertFalse(ems.in_maintenance_window(events, "ace-menlo-pixel", now))
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700118
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700119 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 Bavier8a5c9872020-10-21 13:17:53 -0700123 self.assertEqual(data['edges'][0]['name'], 'ace-example')
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700124
Andy Bavierf872e9a2021-03-22 12:06:25 -0700125 def test_create_and_delete_edge_legacy(self):
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700126 response = self.app.post('/edges', json=test_edge)
127 data = json.loads(response.get_data(as_text=True))
Andy Bavier8a5c9872020-10-21 13:17:53 -0700128 self.assertEqual(data['edge']['name'], 'ace-menlo-pixel')
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700129
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 Baviere47157d2020-12-11 14:13:12 -0700134 response = self.app.get('/edges/metrics')
135 data = response.get_data(as_text=True)
Andy Bavier5b4e28f2021-03-09 15:48:20 -0700136 print(data)
Andy Baviere47157d2020-12-11 14:13:12 -0700137 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 Baviera0c40aa2021-03-10 12:09:12 -0700140 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)
Andy Bavier3c7b78d2021-03-11 14:16:43 -0700142 self.assertTrue('aetheredge_e2e_tests_ok{name="ace-menlo-pixel"} 1.0' in data)
143 self.assertTrue('aetheredge_e2e_tests_down{name="ace-menlo-pixel"} 0.0' in data)
Andy Baviere47157d2020-12-11 14:13:12 -0700144
Andy Bavier8a5c9872020-10-21 13:17:53 -0700145 response = self.app.delete('/edges/ace-menlo-pixel')
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700146 data = json.loads(response.get_data(as_text=True))
147 self.assertEqual(data['result'], True)
Andy Baviere47157d2020-12-11 14:13:12 -0700148
149 response = self.app.get('/edges/metrics')
150 data = response.get_data(as_text=True)
Andy Bavier5b4e28f2021-03-09 15:48:20 -0700151 print(data)
152 self.assertFalse('ace-menlo-pixel' in data)
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700153
154 response = self.app.get('/edges')
155 data = json.loads(response.get_data(as_text=True))
156 self.assertEqual(len(data['edges']), 1)
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700157
Andy Bavierf872e9a2021-03-22 12:06:25 -0700158 def test_create_and_delete_edge(self):
159 response = self.app.post('/testresults', json=test_edge)
160 data = json.loads(response.get_data(as_text=True))
161 self.assertEqual(data['edge']['name'], 'ace-menlo-pixel')
162
163 response = self.app.get('/edges')
164 data = json.loads(response.get_data(as_text=True))
165 self.assertEqual(len(data['edges']), 2)
166
167 response = self.app.get('/edges/metrics')
168 data = response.get_data(as_text=True)
169 print(data)
170 self.assertTrue('aetheredge_status_control_plane{name="ace-menlo-pixel"} 2.0' in data)
171 self.assertTrue('aetheredge_status_user_plane{name="ace-menlo-pixel"} 2.0' in data)
172 self.assertTrue('aetheredge_last_update{name="ace-menlo-pixel"}' in data)
173 self.assertTrue('aetheredge_connect_test_ok{name="ace-menlo-pixel"} 1.0' in data)
174 self.assertTrue('aetheredge_ping_test_ok{name="ace-menlo-pixel"} 1.0' in data)
175 self.assertTrue('aetheredge_e2e_tests_ok{name="ace-menlo-pixel"} 1.0' in data)
176 self.assertTrue('aetheredge_e2e_tests_down{name="ace-menlo-pixel"} 0.0' in data)
177
178 response = self.app.delete('/testresults/ace-menlo-pixel')
179 data = json.loads(response.get_data(as_text=True))
180 self.assertEqual(data['result'], True)
181
182 response = self.app.get('/edges/metrics')
183 data = response.get_data(as_text=True)
184 print(data)
185 self.assertFalse('ace-menlo-pixel' in data)
186
187 response = self.app.get('/edges')
188 data = json.loads(response.get_data(as_text=True))
189 self.assertEqual(len(data['edges']), 1)
Andy Bavier614af142020-08-07 14:49:56 -0700190
191if __name__ == '__main__':
192 suite = unittest.TestLoader().loadTestsFromTestCase(TestEdgeMonitoringServer)
193 unittest.TextTestRunner(verbosity=2).run(suite)