blob: cf26f882ca34967c10a50b08c0844a69ba63c93c [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
21
22
23class MyEvent:
Andy Bavierc41cf0c2020-09-02 14:49:21 -070024 def __init__ (self, location = "", description = "", summary = "", start = None, end = None, all_day = False):
Andy Bavier614af142020-08-07 14:49:56 -070025 self.location = location
26 self.description = description
27 self.summary = summary
28 self.start = start
29 self.end = end
Andy Bavierc41cf0c2020-09-02 14:49:21 -070030 self.all_day = all_day
Andy Bavier614af142020-08-07 14:49:56 -070031
32class MyEventNoLoc:
33 def __init__ (self, description = "", summary = ""):
34 self.description = description
35 self.summary = summary
36
37
38class TestEdgeMonitoringServer(unittest.TestCase):
39 def test_match_location(self):
40 event = MyEvent(location = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
41 self.assertTrue(ems.is_my_event(event, "production-edge-onf-menlo"))
42 self.assertTrue(ems.is_my_event(event, "(Compute)-MP-1-Aether Production"))
43
44 def test_match_description(self):
45 event = MyEvent(description = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
46 self.assertTrue(ems.is_my_event(event, "production-edge-onf-menlo"))
47 self.assertTrue(ems.is_my_event(event, "(Compute)-MP-1-Aether Production"))
48
49 def test_match_summary(self):
50 event = MyEvent(summary = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
51 self.assertTrue(ems.is_my_event(event, "production-edge-onf-menlo"))
52 self.assertTrue(ems.is_my_event(event, "(Compute)-MP-1-Aether Production"))
53
54 def test_no_match(self):
55 event = MyEvent(summary = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
56 self.assertFalse(ems.is_my_event(event, "production-edge-intel"))
57 self.assertFalse(ems.is_my_event(event, "(Compute)-MP-1-Aether Staging"))
58
59 def test_missing_field(self):
60 event = MyEventNoLoc(description = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
61 self.assertTrue(ems.is_my_event(event, "production-edge-onf-menlo"))
62
63 def test_in_window(self):
64 events = []
65 now = datetime.datetime.now(pytz.utc)
66 events.append(MyEvent(location = "(Compute)-MP-1-Aether Production",
67 start = now - datetime.timedelta(hours=1),
68 end = now + datetime.timedelta(hours=1)))
69 self.assertTrue(ems.in_maintenance_window(events, "production-edge-onf-menlo", now))
70 self.assertFalse(ems.in_maintenance_window(events, "production-edge-onf-tucson", now))
71
72 def test_not_in_window(self):
73 events = []
74 now = datetime.datetime.now(pytz.utc)
75 events.append(MyEvent(location = "production-edge-onf-menlo",
76 start = now + datetime.timedelta(hours=1),
77 end = now + datetime.timedelta(hours=2)))
78 self.assertFalse(ems.in_maintenance_window(events, "production-edge-onf-menlo", now))
79
80 def test_no_events(self):
81 events = []
82 now = datetime.datetime.now(pytz.utc)
83 self.assertFalse(ems.in_maintenance_window(events, "production-edge-onf-menlo", now))
84
Andy Bavierc41cf0c2020-09-02 14:49:21 -070085 def test_all_day_events(self):
86 events = []
87 events.append(MyEvent(location = "production-edge-onf-menlo",
88 start = datetime.datetime(2020, 9, 2, 0, 0),
89 end = datetime.datetime(2020, 9, 3, 0, 0),
90 all_day = True))
91
92 ems.process_all_day_events(events)
93
94 now = datetime.datetime(2020, 9, 2, 12, 0, tzinfo=pytz.utc)
95 self.assertTrue(ems.in_maintenance_window(events, "production-edge-onf-menlo", now))
96
97 now = datetime.datetime(2020, 9, 3, 12, 0, tzinfo=pytz.utc)
98 self.assertFalse(ems.in_maintenance_window(events, "production-edge-onf-menlo", now))
99
100
Andy Bavier614af142020-08-07 14:49:56 -0700101
102if __name__ == '__main__':
103 suite = unittest.TestLoader().loadTestsFromTestCase(TestEdgeMonitoringServer)
104 unittest.TextTestRunner(verbosity=2).run(suite)