/
test_timeperiods.py
executable file
·460 lines (400 loc) · 19.5 KB
/
test_timeperiods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env python
# Copyright (C) 2009-2012:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
#
# This file is used to test timeperiods
#
from shinken_test import *
from shinken.objects.timeperiod import Timeperiod
class TestTimeperiods(ShinkenTest):
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(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_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_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_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_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_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_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_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")
# short test to check the invalid function of timeranges
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")
if __name__ == '__main__':
unittest.main()