blob: c6a6e034d9cdd15fe7da14a86dd50d4024cb1e24 [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2020-present Open Networking Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import edge_monitoring_server as ems
import datetime
import pytz
class MyEvent:
def __init__ (self, location = "", description = "", summary = "", start = None, end = None):
self.location = location
self.description = description
self.summary = summary
self.start = start
self.end = end
class MyEventNoLoc:
def __init__ (self, description = "", summary = ""):
self.description = description
self.summary = summary
class TestEdgeMonitoringServer(unittest.TestCase):
def test_match_location(self):
event = MyEvent(location = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
self.assertTrue(ems.is_my_event(event, "production-edge-onf-menlo"))
self.assertTrue(ems.is_my_event(event, "(Compute)-MP-1-Aether Production"))
def test_match_description(self):
event = MyEvent(description = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
self.assertTrue(ems.is_my_event(event, "production-edge-onf-menlo"))
self.assertTrue(ems.is_my_event(event, "(Compute)-MP-1-Aether Production"))
def test_match_summary(self):
event = MyEvent(summary = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
self.assertTrue(ems.is_my_event(event, "production-edge-onf-menlo"))
self.assertTrue(ems.is_my_event(event, "(Compute)-MP-1-Aether Production"))
def test_no_match(self):
event = MyEvent(summary = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
self.assertFalse(ems.is_my_event(event, "production-edge-intel"))
self.assertFalse(ems.is_my_event(event, "(Compute)-MP-1-Aether Staging"))
def test_missing_field(self):
event = MyEventNoLoc(description = "production-edge-onf-menlo, (Compute)-MP-1-Aether Production")
self.assertTrue(ems.is_my_event(event, "production-edge-onf-menlo"))
def test_in_window(self):
events = []
now = datetime.datetime.now(pytz.utc)
events.append(MyEvent(location = "(Compute)-MP-1-Aether Production",
start = now - datetime.timedelta(hours=1),
end = now + datetime.timedelta(hours=1)))
self.assertTrue(ems.in_maintenance_window(events, "production-edge-onf-menlo", now))
self.assertFalse(ems.in_maintenance_window(events, "production-edge-onf-tucson", now))
def test_not_in_window(self):
events = []
now = datetime.datetime.now(pytz.utc)
events.append(MyEvent(location = "production-edge-onf-menlo",
start = now + datetime.timedelta(hours=1),
end = now + datetime.timedelta(hours=2)))
self.assertFalse(ems.in_maintenance_window(events, "production-edge-onf-menlo", now))
def test_no_events(self):
events = []
now = datetime.datetime.now(pytz.utc)
self.assertFalse(ems.in_maintenance_window(events, "production-edge-onf-menlo", now))
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestEdgeMonitoringServer)
unittest.TextTestRunner(verbosity=2).run(suite)