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 |
| 21 | |
| 22 | |
| 23 | class MyEvent: |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame^] | 24 | 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] | 25 | self.location = location |
| 26 | self.description = description |
| 27 | self.summary = summary |
| 28 | self.start = start |
| 29 | self.end = end |
Andy Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame^] | 30 | self.all_day = all_day |
Andy Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 31 | |
| 32 | class MyEventNoLoc: |
| 33 | def __init__ (self, description = "", summary = ""): |
| 34 | self.description = description |
| 35 | self.summary = summary |
| 36 | |
| 37 | |
| 38 | class 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 Bavier | c41cf0c | 2020-09-02 14:49:21 -0700 | [diff] [blame^] | 85 | 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 Bavier | 614af14 | 2020-08-07 14:49:56 -0700 | [diff] [blame] | 101 | |
| 102 | if __name__ == '__main__': |
| 103 | suite = unittest.TestLoader().loadTestsFromTestCase(TestEdgeMonitoringServer) |
| 104 | unittest.TextTestRunner(verbosity=2).run(suite) |