def test_issue_1385(self): ''' https://github.com/naparuba/shinken/issues/1385 ''' tp = Timeperiod() tp.timeperiod_name = 'mercredi2-22-02' tp.resolve_daterange( tp.dateranges, 'wednesday 2 00:00-02:00,22:00-24:00') tp.resolve_daterange( tp.dateranges, 'thursday 2 00:00-02:00,22:00-24:00') valid_times = ( (2014, 11, 12, 1, 0), # second wednesday of november @ 01:00 (2014, 11, 12, 23, 0), # same @23:00 (2014, 11, 13, 0, 0), # second thursday @ 00:00 # in december: (2014, 12, 10, 1, 0), (2014, 12, 10, 23, 0), (2014, 12, 11, 1, 0), (2014, 12, 11, 23, 0), ) for valid in valid_times: dt = datetime.datetime(*valid) valid_tm = time.mktime(dt.timetuple()) self.assertTrue(tp.is_time_valid(valid_tm)) invalid_times = ( (2014, 11, 3, 1, 0), # first wednesday .. (2014, 11, 4, 1, 0), # first thursday (2014, 11, 17, 1, 0), # third .. (2014, 11, 18, 1, 0), # in december: (2014, 12, 5, 3, 0), (2014, 12, 17, 1, 0), (2014, 12, 18, 1, 0), (2014, 12, 24, 1, 0), (2014, 12, 25, 1, 0), (2014, 12, 31, 1, 0), ) for invalid in invalid_times: dt = datetime.datetime(*invalid) invalid_tm = time.mktime(dt.timetuple()) self.assertFalse(tp.is_time_valid(invalid_tm))
def test_next_invalid_day(self): self.print_header() # Get the 13 of july 2010 at 15:00, tuesday july_the_13 = time.mktime(time.strptime("13 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_13 t = Timeperiod() t.timeperiod_name = 'test_next_invalid_day' t.resolve_daterange(t.dateranges, 'tuesday 00:00-24:00') t.exclude = [] t_next_invalid = t.get_next_invalid_time_from_t(july_the_13) t_next_invalid = time.asctime(time.localtime(t_next_invalid)) print "T next invalid", t_next_invalid self.assert_(t_next_invalid == "Wed Jul 14 00:00:01 2010")
def test_issue_1385(self): ''' https://github.com/naparuba/shinken/issues/1385 ''' tp = Timeperiod() tp.timeperiod_name = 'mercredi2-22-02' tp.resolve_daterange(tp.dateranges, 'wednesday 2 00:00-02:00,22:00-24:00') tp.resolve_daterange(tp.dateranges, 'thursday 2 00:00-02:00,22:00-24:00') valid_times = ( (2014, 11, 12, 1, 0), # second wednesday of november @ 01:00 (2014, 11, 12, 23, 0), # same @23:00 (2014, 11, 13, 0, 0), # second thursday @ 00:00 # in december: (2014, 12, 10, 1, 0), (2014, 12, 10, 23, 0), (2014, 12, 11, 1, 0), (2014, 12, 11, 23, 0), ) for valid in valid_times: dt = datetime.datetime(*valid) valid_tm = time.mktime(dt.timetuple()) self.assertTrue(tp.is_time_valid(valid_tm)) invalid_times = ( (2014, 11, 3, 1, 0), # first wednesday .. (2014, 11, 4, 1, 0), # first thursday (2014, 11, 17, 1, 0), # third .. (2014, 11, 18, 1, 0), # in december: (2014, 12, 5, 3, 0), (2014, 12, 17, 1, 0), (2014, 12, 18, 1, 0), (2014, 12, 24, 1, 0), (2014, 12, 25, 1, 0), (2014, 12, 31, 1, 0), ) for invalid in invalid_times: dt = datetime.datetime(*invalid) invalid_tm = time.mktime(dt.timetuple()) self.assertFalse(tp.is_time_valid(invalid_tm))
def test_dayweek_exclusion_timeperiod_with_day_range(self): self.print_header() t = Timeperiod() # Get the 13 of july 2010 at 15:00, tuesday july_the_13 = time.mktime(time.strptime("13 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_13 # Now we add this timeperiod an exception t2 = Timeperiod() t2.timeperiod_name = '' t2.resolve_daterange(t2.dateranges, 'tuesday 00:00-24:00') t.exclude = [t2] t.resolve_daterange(t.dateranges, '2010-03-01 - 2020-03-01 00:00-24:00') t_next = t.get_next_valid_time_from_t(july_the_13) t_next = time.asctime(time.localtime(t_next)) now = time.time() now = time.asctime(time.localtime(now)) print "T next", t_next # print "T now", now # self.assert_(t_next == now) self.assert_(t_next == "Wed Jul 14 00:00:01 2010")
def test_check_enter_downtime(self): test_router_0 = self.sched.hosts.find_by_name("test_router_0") test_host_0 = self.sched.hosts.find_by_name("test_host_0") test_nobody = self.sched.hosts.find_by_name("test_nobody") svc1 = self.sched.services.find_srv_by_name_and_hostname( "test_host_0", "test_ok_0") svc2 = self.sched.services.find_srv_by_name_and_hostname( "test_router_0", "test_ok_0") svc3 = self.sched.services.find_srv_by_name_and_hostname( "test_nobody", "test_ok_0") # we want to focus on only one maintenance test_router_0.maintenance_period = None test_host_0.maintenance_period = None test_nobody.maintenance_period = None svc1.maintenance_period = None svc2.maintenance_period = None # be sure we have some time before a new minute begins. # otherwise we get a race condition and a failed test here. x = time.gmtime() while x.tm_sec < 50: time.sleep(1) x = time.gmtime() now = time.time() print "now it is", time.asctime(time.localtime(now)) nowday = time.strftime("%A", time.localtime(now + 60)).lower() soonstart = time.strftime("%H:%M", time.localtime(now + 60)) soonend = time.strftime("%H:%M", time.localtime(now + 180)) range = "%s %s-%s" % (nowday, soonstart, soonend) print "range is ", range t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, range) t_next = t.get_next_valid_time_from_t(now) print "planned start", time.asctime(time.localtime(t_next)) t_next = t.get_next_invalid_time_from_t(t_next + 1) print "planned stop ", time.asctime(time.localtime(t_next)) svc3.maintenance_period = t self.assert_(not hasattr(svc3, 'in_maintenance')) # # now let the scheduler run and wait until the maintenance period begins # self.scheduler_loop(3, [[svc3, 0, 'OK']], do_sleep=True) self.assert_(hasattr(svc3, 'in_maintenance')) self.assert_(len(self.sched.downtimes) == 1) self.assert_(len(svc3.downtimes) == 1) self.assert_(svc3.downtimes[0] in self.sched.downtimes.values()) self.assert_(svc3.in_scheduled_downtime) self.assert_(svc3.downtimes[0].fixed) self.assert_(svc3.downtimes[0].is_in_effect) self.assert_(not svc3.downtimes[0].can_be_deleted) self.assert_(svc3.in_maintenance == svc3.downtimes[0].id) # # now the downtime should expire... # self.scheduler_loop(3, [[svc3, 0, 'OK']], do_sleep=True) self.assert_(len(self.sched.downtimes) == 0) self.assert_(len(svc3.downtimes) == 0) self.assert_(not svc3.in_scheduled_downtime) self.assert_(svc3.in_maintenance == False)
def test_simple_with_multiple_time(self): self.print_header() t = Timeperiod() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_12 # First a false test, no results t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, '1999-01-28 00:00-07:00,21:30-24:00') t_next = t.get_next_valid_time_from_t(now) self.assert_(t_next is None) # Then a simple same day print "Cheking validity for", time.asctime(time.localtime(july_the_12)) t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, 'tuesday 00:00-07:00,21:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "RES:", t_next self.assert_(t_next == "Tue Jul 13 00:00:00 2010") # Now ask about at 00:00 time? july_the_12 = time.mktime( time.strptime("12 Jul 2010 00:00:00", "%d %b %Y %H:%M:%S")) # Then a simple same day t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, 'tuesday 00:00-07:00,21:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next?", t_next self.assert_(t_next == "Tue Jul 13 00:00:00 2010")
def test_monweekday_timeperiod_with_exclude(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a funny daterange print "Testing daterange", 'tuesday -1 july - monday 1 august 16:30-24:00' t = Timeperiod() t.timeperiod_name = 'T1' t.resolve_daterange( t.dateranges, 'tuesday -1 july - monday 1 september 16:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next without exclude", t_next self.assert_(t_next == "Tue Jul 27 16:30:00 2010") # Now we add this timeperiod an exception # and from april (before) to august monday 3 (monday 16 august), # so Jul 17 is no more possible. So just after it, Tue 17 t2 = Timeperiod() t2.timeperiod_name = 'T2' t2.resolve_daterange(t2.dateranges, 'thursday 1 april - monday 3 august 00:00-24:00') print t2.dateranges[0].__dict__ t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 59 :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) #print "Check from", time.asctime(time.localtime(july_the_12)) #t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) #print "T2 next valid", time.asctime(time.localtime(t_exclude)) print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv)) print "T next raw", t_next print "T next?", time.asctime(time.localtime(t_next)) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == 'Tue Aug 17 16:30:00 2010')
def test_simple_timeperiod(self): self.print_header() t = Timeperiod() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_12 # First a false test, no results t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, '1999-01-28 00:00-24:00') t_next = t.get_next_valid_time_from_t(now) self.assert_(t_next is None) # Then a simple same day t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, 'tuesday 16:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print t_next self.assert_(t_next == "Tue Jul 13 16:30:00 2010")
def test_mondayweek_timeperiod_with_exclude(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a simple same day t = Timeperiod() t.timeperiod_name = 'T1' t.resolve_daterange(t.dateranges, 'tuesday 2 16:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == "Tue Jul 13 16:30:00 2010") # Now we add this timeperiod an exception # And a good one: from april (so before so agust (after), and full time. # But the 17 is a tuesday, but the 3 of august, so the next 2 tuesday is # ..... the Tue Sep 14 :) Yes, we should wait quite a lot :) t2 = Timeperiod() t2.timeperiod_name = 'T2' t2.resolve_daterange(t2.dateranges, 'april 1 - august 16 00:00-24:00') #print t2.__dict__ t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 59 :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) #print "Check from", time.asctime(time.localtime(july_the_12)) #t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) #print "T2 next valid", time.asctime(time.localtime(t_exclude)) print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv)) print "T next raw JEAN", t_next print "T next?", time.asctime(time.localtime(t_next)) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == 'Tue Sep 14 16:30:00 2010')
def test_simple_timeperiod_with_exclude(self): self.print_header() t = Timeperiod() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_12 # First a false test, no results t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, '1999-01-28 00:00-24:00') t_next = t.get_next_valid_time_from_t(now) self.assert_(t_next is None) # Then a simple same day t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, 'tuesday 16:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print t_next self.assert_(t_next == "Tue Jul 13 16:30:00 2010") # Now we add this timeperiod an exception t2 = Timeperiod() t2.timeperiod_name = '' t2.resolve_daterange(t2.dateranges, 'tuesday 08:30-21:00') t.exclude = [t2] # So the next will be after 16:30 and not before 21:00. So # It will be 21:00:01 (first second after invalid is valid) # we clean the cache of previous calc of t ;) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "T nxt with exclude:", t_next self.assert_(t_next == "Tue Jul 13 21:00:01 2010")
def test_simple_with_multiple_time(self): self.print_header() t = Timeperiod() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_12 # First a false test, no results t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "1999-01-28 00:00-07:00,21:30-24:00") t_next = t.get_next_valid_time_from_t(now) self.assert_(t_next is None) # Then a simple same day print "Cheking validity for", time.asctime(time.localtime(july_the_12)) t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "tuesday 00:00-07:00,21:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "RES:", t_next self.assert_(t_next == "Tue Jul 13 00:00:00 2010") # Now ask about at 00:00 time? july_the_12 = time.mktime(time.strptime("12 Jul 2010 00:00:00", "%d %b %Y %H:%M:%S")) # Then a simple same day t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "tuesday 00:00-07:00,21:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next?", t_next self.assert_(t_next == "Tue Jul 13 00:00:00 2010")
def test_check_enter_downtime(self): test_router_0 = self.sched.hosts.find_by_name("test_router_0") test_host_0 = self.sched.hosts.find_by_name("test_host_0") test_nobody = self.sched.hosts.find_by_name("test_nobody") svc1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0") svc2 = self.sched.services.find_srv_by_name_and_hostname("test_router_0", "test_ok_0") svc3 = self.sched.services.find_srv_by_name_and_hostname("test_nobody", "test_ok_0") # we want to focus on only one maintenance test_router_0.maintenance_period = None test_host_0.maintenance_period = None test_nobody.maintenance_period = None svc1.maintenance_period = None svc2.maintenance_period = None # be sure we have some time before a new minute begins. # otherwise we get a race condition and a failed test here. now = time.time() x = time.gmtime(now) while x.tm_sec < 50: time.sleep(1) now = time.time() x = time.gmtime(now) now = time.time() print "now it is", time.asctime(time.localtime(now)) nowday = time.strftime("%A", time.localtime(now + 60)).lower() soonstart = time.strftime("%H:%M", time.localtime(now + 60)) soonend = time.strftime("%H:%M", time.localtime(now + 180)) range = "%s %s-%s" % (nowday, soonstart, soonend) print "range is ", range t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, range) t_next = t.get_next_valid_time_from_t(now) print "planned start", time.asctime(time.localtime(t_next)) t_next = t.get_next_invalid_time_from_t(t_next + 1) print "planned stop ", time.asctime(time.localtime(t_next)) svc3.maintenance_period = t self.assertFalse(svc3.in_maintenance) # # now let the scheduler run and wait until the maintenance period begins # it is now 10 seconds before the full minute. run for 30 seconds # in 1-second-intervals. this should be enough to trigger the downtime # in 10 seconds from now the downtime starts print "scheduler_loop start", time.asctime() self.scheduler_loop(30, [[svc3, 0, 'OK']], do_sleep=True, sleep_time=1) print "scheduler_loop end ", time.asctime() self.assertTrue(hasattr(svc3, 'in_maintenance')) self.assertEqual(1, len(self.sched.downtimes)) try: print "........................................." print self.sched.downtimes[1] print "downtime starts", time.asctime(self.sched.downtimes[1].start_time) print "downtime ends ", time.asctime(self.sched.downtimes[1].end_time) except Exception: print "looks like there is no downtime" pass self.assertEqual(1, len(svc3.downtimes)) self.assertIn(svc3.downtimes[0], self.sched.downtimes.values()) self.assertTrue(svc3.in_scheduled_downtime) self.assertTrue(svc3.downtimes[0].fixed) self.assertTrue(svc3.downtimes[0].is_in_effect) self.assertFalse(svc3.downtimes[0].can_be_deleted) self.assertEqual(svc3.downtimes[0].id, svc3.in_maintenance) # # now the downtime should expire... # we already have 20 seconds (after 10 seconds of startup). # the downtime is 120 seconds long. # run the remaining 100 seconds plus 5 seconds just to be sure self.scheduler_loop(105, [[svc3, 0, 'OK']], do_sleep=True, sleep_time=1) self.assertEqual(0, len(self.sched.downtimes)) self.assertEqual(0, len(svc3.downtimes)) self.assertFalse(svc3.in_scheduled_downtime) self.assertIs(None, svc3.in_maintenance)
def test_maintenance_period_check_overlap(self): hst2 = self.sched.hosts.find_by_name("test_host_2") svc22 = self.sched.services.find_srv_by_name_and_hostname("test_host_2", "test_service_2") # be sure we have some time before a new minute begins. # otherwise we get a race condition and a failed test here. now = time.time() x = time.gmtime(now) while x.tm_sec < 50: time.sleep(1) now = time.time() x = time.gmtime(now) now = time.time() start_tuple = time.localtime(now + 60) nowday = time.strftime("%A", start_tuple).lower() soonstart = time.strftime("%H:%M", start_tuple) end_tuple = time.localtime(now + 180) soonend = time.strftime("%H:%M", end_tuple) dt_range = "%s %s-%s" % (nowday, soonstart, soonend) t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, dt_range) t_start = t.get_next_valid_time_from_t(now) t_end = t.get_next_invalid_time_from_t(t_start + 1) - 1 hst2.maintenance_period = t svc22.maintenance_period = t self.assertIsNone(hst2.in_maintenance) self.assertIsNone(svc22.in_maintenance) # Run maintenance check (out of production) self.scheduler_loop(1, [ { "item": hst2, "exit_status": 0, "output": "UP test_host_2" }, { "item": svc22, "exit_status": 0, "output": "OK test_host_2/test_service_2" }, { "item": hst2, "exit_status": 2, "output": "MAINTENANCE test_host_2", "check_variant": "maintenance" }, { "item": svc22, "exit_status": 2, "output": "MAINTENANCE test_host_2/test_service_2", "check_variant": "maintenance" }, ]) # Downtimes are created because of maintenance check result self.check_object_status(hst2, True, False) self.check_object_status(svc22, True, False) self.assertEqual(len(self.sched.downtimes), 2) self.assertIn(hst2.downtimes[0], self.sched.downtimes.values()) self.assertIn(svc22.downtimes[0], self.sched.downtimes.values()) hst2_dt_id = hst2.downtimes[0].id self.assertEqual(hst2_dt_id, hst2.in_maintenance) svc22_dt_id = svc22.downtimes[0].id self.assertEqual(svc22_dt_id, svc22.in_maintenance) self.assertLess(hst2.downtimes[0].end_time, t_end) self.assertLess(svc22.downtimes[0].end_time, t_end) # now let the scheduler run and wait until the maintenance period begins # it is now 10 seconds before the full minute. run for 30 seconds # in 1-second-intervals. this should be enough to trigger the downtime # in 10 seconds from now the downtime starts self.scheduler_loop(30, [ { "item": hst2, "exit_status": 0, "output": "UP test_host_2" }, { "item": svc22, "exit_status": 0, "output": "OK test_host_2/test_service_2" }, { "item": hst2, "exit_status": 2, "output": "MAINTENANCE test_host_2", "check_variant": "maintenance" }, { "item": svc22, "exit_status": 2, "output": "MAINTENANCE test_host_2/test_service_2", "check_variant": "maintenance" }, ], do_sleep=True, sleep_time=1) # Downtimes are set from maintenance period, which has precedence self.check_object_status(hst2, True, False) self.check_object_status(svc22,True, False) self.assertEqual(len(self.sched.downtimes), 2) self.assertIn(hst2.downtimes[0], self.sched.downtimes.values()) self.assertIn(svc22.downtimes[0], self.sched.downtimes.values()) self.assertEqual(hst2_dt_id, hst2.in_maintenance) self.assertEqual(svc22_dt_id, svc22.in_maintenance) self.assertEqual(hst2.downtimes[0].end_time, t_end) self.assertEqual(svc22.downtimes[0].end_time, t_end) #time.sleep(180) self.scheduler_loop(180, [ { "item": hst2, "exit_status": 0, "output": "UP test_host_2" }, { "item": svc22, "exit_status": 0, "output": "OK test_host_2/test_service_2" }, { "item": hst2, "exit_status": 2, "output": "MAINTENANCE test_host_2", "check_variant": "maintenance" }, { "item": svc22, "exit_status": 2, "output": "MAINTENANCE test_host_2/test_service_2", "check_variant": "maintenance" }, ], do_sleep=True, sleep_time=1) # Maintenace period has expired, downtimes are extended because of # maintenance check result self.check_object_status(hst2, True, False) self.check_object_status(svc22, True, False) self.assertEqual(len(self.sched.downtimes), 2) self.assertIn(hst2.downtimes[0], self.sched.downtimes.values()) self.assertIn(svc22.downtimes[0], self.sched.downtimes.values()) self.assertEqual(hst2_dt_id, hst2.in_maintenance) self.assertEqual(svc22_dt_id, svc22.in_maintenance) self.assertGreater(hst2.downtimes[0].end_time, t_end) self.assertGreater(svc22.downtimes[0].end_time, t_end)
def test_funky_mondayweek_timeperiod_with_exclude_and_multiple_daterange(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a funny daterange print "Testing daterange", "tuesday -1 - monday 1 16:30-24:00" t = Timeperiod() t.timeperiod_name = "T1" t.resolve_daterange(t.dateranges, "tuesday -1 - monday 1 16:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next without exclude", t_next self.assert_(t_next == "Tue Jul 27 16:30:00 2010") # Now we add this timeperiod an exception # And a good one: from april (so before so agust (after), and full time. # But the 27 is nw not possible? So what next? Add a month! # But maybe it's not enoutgth? :) # The withoutthe 2nd exclude, it's the Tues Aug 31, btu it's inside # saturday -1 - monday 1 because saturday -1 is the 28 august, so no. # in september saturday -1 is the 25, and tuesday -1 is 28, so still no # A month again! So now tuesday -1 is 26 and saturday -1 is 30. So ok # for this one! that was quite long isn't it? And funky! :) t2 = Timeperiod() t2.timeperiod_name = "T2" t2.resolve_daterange(t2.dateranges, "april 1 - august 16 00:00-24:00") # Oups, I add a inner daterange ;) t2.resolve_daterange(t2.dateranges, "saturday -1 - monday 1 16:00-24:00") t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 59 :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) # print "Check from", time.asctime(time.localtime(july_the_12)) # t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) # print "T2 next valid", time.asctime(time.localtime(t_exclude)) print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv)) print "T next raw", t_next print "T next?", time.asctime(time.localtime(t_next)) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == "Tue Oct 26 16:30:00 2010") print "Finish this Funky test :)"
def test_check_enter_downtime(self): test_router_0 = self.sched.hosts.find_by_name("test_router_0") test_host_0 = self.sched.hosts.find_by_name("test_host_0") test_nobody = self.sched.hosts.find_by_name("test_nobody") svc1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0") svc2 = self.sched.services.find_srv_by_name_and_hostname("test_router_0", "test_ok_0") svc3 = self.sched.services.find_srv_by_name_and_hostname("test_nobody", "test_ok_0") # we want to focus on only one maintenance test_router_0.maintenance_period = None test_host_0.maintenance_period = None test_nobody.maintenance_period = None svc1.maintenance_period = None svc2.maintenance_period = None # be sure we have some time before a new minute begins. # otherwise we get a race condition and a failed test here. now = time.time() x = time.gmtime(now) while x.tm_sec < 50: time.sleep(1) now = time.time() x = time.gmtime(now) now = time.time() print "now it is", time.asctime(time.localtime(now)) nowday = time.strftime("%A", time.localtime(now + 60)).lower() soonstart = time.strftime("%H:%M", time.localtime(now + 60)) soonend = time.strftime("%H:%M", time.localtime(now + 180)) range = "%s %s-%s" % (nowday, soonstart, soonend) print "range is ", range t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, range) t_next = t.get_next_valid_time_from_t(now) print "planned start", time.asctime(time.localtime(t_next)) t_next = t.get_next_invalid_time_from_t(t_next + 1) print "planned stop ", time.asctime(time.localtime(t_next)) svc3.maintenance_period = t self.assert_(not hasattr(svc3, "in_maintenance")) # # now let the scheduler run and wait until the maintenance period begins # it is now 10 seconds before the full minute. run for 30 seconds # in 1-second-intervals. this should be enough to trigger the downtime # in 10 seconds from now the downtime starts print "scheduler_loop start", time.asctime() self.scheduler_loop(30, [[svc3, 0, "OK"]], do_sleep=True, sleep_time=1) print "scheduler_loop end ", time.asctime() self.assert_(hasattr(svc3, "in_maintenance")) self.assert_(len(self.sched.downtimes) == 1) try: print "........................................." print self.sched.downtimes[1] print "downtime starts", time.asctime(self.sched.downtimes[1].start_time) print "downtime ends ", time.asctime(self.sched.downtimes[1].end_time) except: print "looks like there is no downtime" pass self.assert_(len(svc3.downtimes) == 1) self.assert_(svc3.downtimes[0] in self.sched.downtimes.values()) self.assert_(svc3.in_scheduled_downtime) self.assert_(svc3.downtimes[0].fixed) self.assert_(svc3.downtimes[0].is_in_effect) self.assert_(not svc3.downtimes[0].can_be_deleted) self.assert_(svc3.in_maintenance == svc3.downtimes[0].id) # # now the downtime should expire... # we already have 20 seconds (after 10 seconds of startup). # the downtime is 120 seconds long. # run the remaining 100 seconds plus 5 seconds just to be sure self.scheduler_loop(105, [[svc3, 0, "OK"]], do_sleep=True, sleep_time=1) self.assert_(len(self.sched.downtimes) == 0) self.assert_(len(svc3.downtimes) == 0) self.assert_(not svc3.in_scheduled_downtime) self.assert_(svc3.in_maintenance == False)
def test_monweekday_timeperiod_with_exclude(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a funny daterange print "Testing daterange", "tuesday -1 july - monday 1 august 16:30-24:00" t = Timeperiod() t.timeperiod_name = "T1" t.resolve_daterange(t.dateranges, "tuesday -1 july - monday 1 september 16:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next without exclude", t_next self.assert_(t_next == "Tue Jul 27 16:30:00 2010") # Now we add this timeperiod an exception # and from april (before) to august monday 3 (monday 16 august), # so Jul 17 is no more possible. So just after it, Tue 17 t2 = Timeperiod() t2.timeperiod_name = "T2" t2.resolve_daterange(t2.dateranges, "thursday 1 april - monday 3 august 00:00-24:00") print t2.dateranges[0].__dict__ t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 59 :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) # print "Check from", time.asctime(time.localtime(july_the_12)) # t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) # print "T2 next valid", time.asctime(time.localtime(t_exclude)) print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv)) print "T next raw", t_next print "T next?", time.asctime(time.localtime(t_next)) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == "Tue Aug 17 16:30:00 2010")
def setUp(self): from shinken.objects.timeperiod import Timeperiod self.item = Timeperiod()
def test_simple_with_multiple_time_mutltiple_days(self): self.print_header() t = Timeperiod() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_12 # First a false test, no results t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "1999-01-28 00:00-07:00,21:30-24:00") t_next = t.get_next_valid_time_from_t(now) self.assert_(t_next is None) # Then a simple same day t = Timeperiod() t.timeperiod_name = "" # monday 00:00-07:00,21:30-24:00 # tuesday 00:00-07:00,21:30-24:00 print "Cheking validity for", time.asctime(time.localtime(july_the_12)) t.resolve_daterange(t.dateranges, "monday 00:00-07:00,21:30-24:00") t.resolve_daterange(t.dateranges, "tuesday 00:00-07:00,21:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "RES:", t_next self.assert_(t_next == "Mon Jul 12 21:30:00 2010") # what about the next invalid? t_next_inv = t.get_next_invalid_time_from_t(july_the_12) t_next_inv = time.asctime(time.localtime(t_next_inv)) print "RES:", t_next_inv self.assert_(t_next_inv == "Mon Jul 12 15:00:00 2010") # what about a valid time and ask next invalid? Like at 22:00h? print "GO" * 10 july_the_12 = time.mktime(time.strptime("12 Jul 2010 22:00:00", "%d %b %Y %H:%M:%S")) t_next_inv = t.get_next_invalid_time_from_t(july_the_12) t_next_inv = time.asctime(time.localtime(t_next_inv)) print "RES:", t_next_inv # , t.is_time_valid(july_the_12) self.assert_(t_next_inv == "Tue Jul 13 07:01:00 2010") # Now ask about at 00:00 time? july_the_12 = time.mktime(time.strptime("12 Jul 2010 00:00:00", "%d %b %Y %H:%M:%S")) print "Cheking validity for", time.asctime(time.localtime(july_the_12)) # Then a simple same day t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "monday 00:00-07:00,21:30-24:00") t.resolve_daterange(t.dateranges, "tuesday 00:00-07:00,21:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next?", t_next self.assert_(t_next == "Mon Jul 12 00:00:00 2010") # Now look for the never case print "24x7" * 10 t = self.conf.timeperiods.find_by_name("24x7") self.assert_(t is not None) t_next_inv = t.get_next_invalid_time_from_t(july_the_12) t_next_inv = time.asctime(time.localtime(t_next_inv)) print "RES:", t_next_inv # , t.is_time_valid(july_the_12) self.assert_(t_next_inv == "Wed Jul 13 00:01:00 2011")
def test_simple_timeperiod_with_exclude(self): self.print_header() t = Timeperiod() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_12 # First a false test, no results t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "1999-01-28 00:00-24:00") t_next = t.get_next_valid_time_from_t(now) self.assert_(t_next is None) # Then a simple same day t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "tuesday 16:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print t_next self.assert_(t_next == "Tue Jul 13 16:30:00 2010") # Now we add this timeperiod an exception t2 = Timeperiod() t2.timeperiod_name = "" t2.resolve_daterange(t2.dateranges, "tuesday 08:30-21:00") t.exclude = [t2] # So the next will be after 16:30 and not before 21:00. So # It will be 21:00:01 (first second after invalid is valid) # we clean the cache of previous calc of t ;) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "T nxt with exclude:", t_next self.assert_(t_next == "Tue Jul 13 21:00:01 2010")
def test_dayweek_timeperiod_with_exclude(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a simple same day t = Timeperiod() t.timeperiod_name = 'T1' t.resolve_daterange(t.dateranges, 'tuesday 2 16:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "T next", t_next self.assert_(t_next == "Tue Jul 13 16:30:00 2010") # Now we add this timeperiod an exception t2 = Timeperiod() t2.timeperiod_name = 'T2' t2.resolve_daterange(t2.dateranges, 'tuesday 00:00-23:58') t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 58 + 1second :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) print "T next raw", t_next t_next = time.asctime(time.localtime(t_next)) print "TOTO T next", t_next self.assert_(t_next == 'Tue Jul 13 23:58:01 2010')
def test_dayweek_timeperiod_with_exclude(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a simple same day t = Timeperiod() t.timeperiod_name = "T1" t.resolve_daterange(t.dateranges, "tuesday 2 16:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "T next", t_next self.assert_(t_next == "Tue Jul 13 16:30:00 2010") # Now we add this timeperiod an exception t2 = Timeperiod() t2.timeperiod_name = "T2" t2.resolve_daterange(t2.dateranges, "tuesday 00:00-23:58") t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 58 + 1second :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) print "T next raw", t_next t_next = time.asctime(time.localtime(t_next)) print "TOTO T next", t_next self.assert_(t_next == "Tue Jul 13 23:58:01 2010")
def test_mondayweek_timeperiod_with_exclude_bis(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a funny daterange print "Testing daterange", 'tuesday -1 - monday 1 16:30-24:00' t = Timeperiod() t.timeperiod_name = 'T1' t.resolve_daterange(t.dateranges, 'tuesday -1 - monday 1 16:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next without exclude", t_next self.assert_(t_next == "Tue Jul 27 16:30:00 2010") # Now we add this timeperiod an exception # And a good one: from april (so before so agust (after), and full time. # But the 27 is nw not possible? So what next? Add a month! # last tuesday of august, the 31 :) t2 = Timeperiod() t2.timeperiod_name = 'T2' t2.resolve_daterange(t2.dateranges, 'april 1 - august 16 00:00-24:00') #print t2.__dict__ t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 59 :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) #print "Check from", time.asctime(time.localtime(july_the_12)) #t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) #print "T2 next valid", time.asctime(time.localtime(t_exclude)) print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv)) print "T next raw JEAN2", t_next print "T next?", time.asctime(time.localtime(t_next)) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == 'Tue Aug 31 16:30:00 2010')
def test_mondayweek_timeperiod_with_exclude(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a simple same day t = Timeperiod() t.timeperiod_name = "T1" t.resolve_daterange(t.dateranges, "tuesday 2 16:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == "Tue Jul 13 16:30:00 2010") # Now we add this timeperiod an exception # And a good one: from april (so before so agust (after), and full time. # But the 17 is a tuesday, but the 3 of august, so the next 2 tuesday is # ..... the Tue Sep 14 :) Yes, we should wait quite a lot :) t2 = Timeperiod() t2.timeperiod_name = "T2" t2.resolve_daterange(t2.dateranges, "april 1 - august 16 00:00-24:00") # print t2.__dict__ t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 59 :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) # print "Check from", time.asctime(time.localtime(july_the_12)) # t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) # print "T2 next valid", time.asctime(time.localtime(t_exclude)) print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv)) print "T next raw JEAN", t_next print "T next?", time.asctime(time.localtime(t_next)) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == "Tue Sep 14 16:30:00 2010")
def test_funky_mondayweek_timeperiod_with_exclude_and_multiple_daterange( self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a funny daterange print "Testing daterange", 'tuesday -1 - monday 1 16:30-24:00' t = Timeperiod() t.timeperiod_name = 'T1' t.resolve_daterange(t.dateranges, 'tuesday -1 - monday 1 16:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next without exclude", t_next self.assert_(t_next == "Tue Jul 27 16:30:00 2010") # Now we add this timeperiod an exception # And a good one: from april (so before so agust (after), and full time. # But the 27 is nw not possible? So what next? Add a month! # But maybe it's not enoutgth? :) # The withoutthe 2nd exclude, it's the Tues Aug 31, btu it's inside # saturday -1 - monday 1 because saturday -1 is the 28 august, so no. # in september saturday -1 is the 25, and tuesday -1 is 28, so still no # A month again! So now tuesday -1 is 26 and saturday -1 is 30. So ok # for this one! that was quite long isn't it? And funky! :) t2 = Timeperiod() t2.timeperiod_name = 'T2' t2.resolve_daterange(t2.dateranges, 'april 1 - august 16 00:00-24:00') # Oups, I add a inner daterange ;) t2.resolve_daterange(t2.dateranges, 'saturday -1 - monday 1 16:00-24:00') t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 59 :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) #print "Check from", time.asctime(time.localtime(july_the_12)) #t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) #print "T2 next valid", time.asctime(time.localtime(t_exclude)) print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv)) print "T next raw", t_next print "T next?", time.asctime(time.localtime(t_next)) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == 'Tue Oct 26 16:30:00 2010') print "Finish this Funky test :)"
def test_mondayweek_timeperiod_with_exclude_bis(self): self.print_header() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) # Then a funny daterange print "Testing daterange", "tuesday -1 - monday 1 16:30-24:00" t = Timeperiod() t.timeperiod_name = "T1" t.resolve_daterange(t.dateranges, "tuesday -1 - monday 1 16:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next without exclude", t_next self.assert_(t_next == "Tue Jul 27 16:30:00 2010") # Now we add this timeperiod an exception # And a good one: from april (so before so agust (after), and full time. # But the 27 is nw not possible? So what next? Add a month! # last tuesday of august, the 31 :) t2 = Timeperiod() t2.timeperiod_name = "T2" t2.resolve_daterange(t2.dateranges, "april 1 - august 16 00:00-24:00") # print t2.__dict__ t.exclude = [t2] # We are a bad boy: first time period want a tuesday # but exclude do not want it until 23:58. So next is 59 :) t.cache = {} t_next = t.get_next_valid_time_from_t(july_the_12) # print "Check from", time.asctime(time.localtime(july_the_12)) # t_exclude = t2.get_next_valid_time_from_t(july_the_12) t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12) # print "T2 next valid", time.asctime(time.localtime(t_exclude)) print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv)) print "T next raw JEAN2", t_next print "T next?", time.asctime(time.localtime(t_next)) t_next = time.asctime(time.localtime(t_next)) self.assert_(t_next == "Tue Aug 31 16:30:00 2010")
def test_dayweek_exclusion_timeperiod(self): self.print_header() t = Timeperiod() now = time.time() # Get the 13 of july 2010 at 15:00, tuesday july_the_13 = time.mktime( time.strptime("13 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_13 # Now we add this timeperiod an exception t2 = Timeperiod() t2.timeperiod_name = '' t2.resolve_daterange(t2.dateranges, 'tuesday 00:00-24:00') t.exclude = [t2] t.resolve_daterange(t.dateranges, 'monday 00:00-24:00') t.resolve_daterange(t.dateranges, 'tuesday 00:00-24:00') t.resolve_daterange(t.dateranges, 'wednesday 00:00-24:00') t_next = t.get_next_valid_time_from_t(july_the_13) t_next = time.asctime(time.localtime(t_next)) print "T next", t_next self.assert_(t_next == "Wed Jul 14 00:00:00 2010")
def test_simple_timeperiod(self): self.print_header() t = Timeperiod() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_12 # First a false test, no results t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "1999-01-28 00:00-24:00") t_next = t.get_next_valid_time_from_t(now) self.assert_(t_next is None) # Then a simple same day t = Timeperiod() t.timeperiod_name = "" t.resolve_daterange(t.dateranges, "tuesday 16:30-24:00") t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print t_next self.assert_(t_next == "Tue Jul 13 16:30:00 2010")
def test_simple_with_multiple_time_mutltiple_days(self): self.print_header() t = Timeperiod() now = time.time() # Get the 12 of july 2010 at 15:00, monday july_the_12 = time.mktime( time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S")) print july_the_12 # First a false test, no results t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, '1999-01-28 00:00-07:00,21:30-24:00') t_next = t.get_next_valid_time_from_t(now) self.assert_(t_next is None) # Then a simple same day t = Timeperiod() t.timeperiod_name = '' # monday 00:00-07:00,21:30-24:00 # tuesday 00:00-07:00,21:30-24:00 print "Cheking validity for", time.asctime(time.localtime(july_the_12)) t.resolve_daterange(t.dateranges, 'monday 00:00-07:00,21:30-24:00') t.resolve_daterange(t.dateranges, 'tuesday 00:00-07:00,21:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "RES:", t_next self.assert_(t_next == "Mon Jul 12 21:30:00 2010") # what about the next invalid? t_next_inv = t.get_next_invalid_time_from_t(july_the_12) t_next_inv = time.asctime(time.localtime(t_next_inv)) print "RES:", t_next_inv self.assert_(t_next_inv == "Mon Jul 12 15:00:00 2010") # what about a valid time and ask next invalid? Like at 22:00h? print "GO" * 10 july_the_12 = time.mktime( time.strptime("12 Jul 2010 22:00:00", "%d %b %Y %H:%M:%S")) t_next_inv = t.get_next_invalid_time_from_t(july_the_12) t_next_inv = time.asctime(time.localtime(t_next_inv)) print "RES:", t_next_inv #, t.is_time_valid(july_the_12) self.assert_(t_next_inv == "Tue Jul 13 07:01:00 2010") # Now ask about at 00:00 time? july_the_12 = time.mktime( time.strptime("12 Jul 2010 00:00:00", "%d %b %Y %H:%M:%S")) print "Cheking validity for", time.asctime(time.localtime(july_the_12)) # Then a simple same day t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, 'monday 00:00-07:00,21:30-24:00') t.resolve_daterange(t.dateranges, 'tuesday 00:00-07:00,21:30-24:00') t_next = t.get_next_valid_time_from_t(july_the_12) t_next = time.asctime(time.localtime(t_next)) print "Next?", t_next self.assert_(t_next == "Mon Jul 12 00:00:00 2010") # Now look for the never case print "24x7" * 10 t = self.conf.timeperiods.find_by_name('24x7') self.assert_(t is not None) t_next_inv = t.get_next_invalid_time_from_t(july_the_12) t_next_inv = time.asctime(time.localtime(t_next_inv)) print "RES:", t_next_inv #, t.is_time_valid(july_the_12) self.assert_(t_next_inv == 'Wed Jul 13 00:01:00 2011')
def test_check_enter_downtime(self): test_router_0 = self.sched.hosts.find_by_name("test_router_0") test_host_0 = self.sched.hosts.find_by_name("test_host_0") test_nobody = self.sched.hosts.find_by_name("test_nobody") svc1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0") svc2 = self.sched.services.find_srv_by_name_and_hostname("test_router_0", "test_ok_0") svc3 = self.sched.services.find_srv_by_name_and_hostname("test_nobody", "test_ok_0") # we want to focus on only one maintenance test_router_0.maintenance_period = None test_host_0.maintenance_period = None test_nobody.maintenance_period = None svc1.maintenance_period = None svc2.maintenance_period = None # be sure we have some time before a new minute begins. # otherwise we get a race condition and a failed test here. x = time.gmtime() while x.tm_sec < 50: time.sleep(1) x = time.gmtime() now = time.time() print "now it is", time.asctime(time.localtime(now)) nowday = time.strftime("%A", time.localtime(now + 60)).lower() soonstart = time.strftime("%H:%M", time.localtime(now + 60)) soonend = time.strftime("%H:%M", time.localtime(now + 180)) range = "%s %s-%s" % (nowday, soonstart, soonend) print "range is ", range t = Timeperiod() t.timeperiod_name = '' t.resolve_daterange(t.dateranges, range) t_next = t.get_next_valid_time_from_t(now) print "planned start", time.asctime(time.localtime(t_next)) t_next = t.get_next_invalid_time_from_t(t_next + 1) print "planned stop ", time.asctime(time.localtime(t_next)) svc3.maintenance_period = t self.assert_(not hasattr(svc3, 'in_maintenance')) # # now let the scheduler run and wait until the maintenance period begins # self.scheduler_loop(3, [[svc3, 0, 'OK']], do_sleep=True) self.assert_(hasattr(svc3, 'in_maintenance')) self.assert_(len(self.sched.downtimes) == 1) self.assert_(len(svc3.downtimes) == 1) self.assert_(svc3.downtimes[0] in self.sched.downtimes.values()) self.assert_(svc3.in_scheduled_downtime) self.assert_(svc3.downtimes[0].fixed) self.assert_(svc3.downtimes[0].is_in_effect) self.assert_(not svc3.downtimes[0].can_be_deleted) self.assert_(svc3.in_maintenance == svc3.downtimes[0].id) # # now the downtime should expire... # self.scheduler_loop(3, [[svc3, 0, 'OK']], do_sleep=True) self.assert_(len(self.sched.downtimes) == 0) self.assert_(len(svc3.downtimes) == 0) self.assert_(not svc3.in_scheduled_downtime) self.assert_(svc3.in_maintenance == False)