blob: 423c31055677ac07554c581bb5a1fe04cfcb6cb3 [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
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 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)
136 self.assertTrue('aetheredge_status_control_plane{name="ace-menlo-pixel"} 2.0' in data)
137 self.assertTrue('aetheredge_status_user_plane{name="ace-menlo-pixel"} 2.0' in data)
138 self.assertTrue('aetheredge_last_update{name="ace-menlo-pixel"}' in data)
139
Andy Bavier8a5c9872020-10-21 13:17:53 -0700140 response = self.app.delete('/edges/ace-menlo-pixel')
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700141 data = json.loads(response.get_data(as_text=True))
142 self.assertEqual(data['result'], True)
Andy Baviere47157d2020-12-11 14:13:12 -0700143
144 response = self.app.get('/edges/metrics')
145 data = response.get_data(as_text=True)
146 self.assertFalse('aetheredge_status_control_plane{name="ace-menlo-pixel"} 2.0' in data)
147 self.assertFalse('aetheredge_status_user_plane{name="ace-menlo-pixel"} 2.0' in data)
148 self.assertFalse('aetheredge_last_update{name="ace-menlo-pixel"}' in data)
Hyunsun Moon5f237ec2020-09-29 14:45:52 -0700149
150 response = self.app.get('/edges')
151 data = json.loads(response.get_data(as_text=True))
152 self.assertEqual(len(data['edges']), 1)
Andy Bavierc41cf0c2020-09-02 14:49:21 -0700153
Andy Bavier614af142020-08-07 14:49:56 -0700154
155if __name__ == '__main__':
156 suite = unittest.TestLoader().loadTestsFromTestCase(TestEdgeMonitoringServer)
157 unittest.TextTestRunner(verbosity=2).run(suite)