1 #!/usr/bin/env python2.6
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 # This file is used to test timeperiods
28 from shinken_test
import *
29 from shinken
.objects
.timeperiod
import Timeperiod
31 class TestTimeperiods(ShinkenTest
):
33 def test_simple_timeperiod(self
):
37 #Get the 12 of july 2010 at 15:00, monday
38 july_the_12
= time
.mktime(time
.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
41 #First a false test, no results
43 t
.timeperiod_name
= ''
44 t
.resolve_daterange(t
.dateranges
, '1999-01-28 00:00-24:00')
45 t_next
= t
.get_next_valid_time_from_t(now
)
46 self
.assert_(t_next
is None)
48 #Then a simple same day
50 t
.timeperiod_name
= ''
51 t
.resolve_daterange(t
.dateranges
, 'tuesday 16:30-24:00')
52 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
53 t_next
= time
.asctime(time
.localtime(t_next
))
55 self
.assert_(t_next
== "Tue Jul 13 16:30:00 2010")
60 def test_simple_timeperiod_with_exclude(self
):
64 #Get the 12 of july 2010 at 15:00, monday
65 july_the_12
= time
.mktime(time
.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
68 #First a false test, no results
70 t
.timeperiod_name
= ''
71 t
.resolve_daterange(t
.dateranges
, '1999-01-28 00:00-24:00')
72 t_next
= t
.get_next_valid_time_from_t(now
)
73 self
.assert_(t_next
is None)
75 #Then a simple same day
77 t
.timeperiod_name
= ''
78 t
.resolve_daterange(t
.dateranges
, 'tuesday 16:30-24:00')
79 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
80 t_next
= time
.asctime(time
.localtime(t_next
))
82 self
.assert_(t_next
== "Tue Jul 13 16:30:00 2010")
84 #Now we add this timeperiod an exception
86 t2
.timeperiod_name
= ''
87 t2
.resolve_daterange(t2
.dateranges
, 'tuesday 08:30-21:00')
89 #So the next will be after 16:30 and not before 21:00. So
90 #It will be 21:00:01 (first second after invalid is valid)
92 #we clean the cache of previous calc of t ;)
94 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
95 t_next
= time
.asctime(time
.localtime(t_next
))
96 print "T nxt with exclude:", t_next
97 self
.assert_(t_next
== "Tue Jul 13 21:00:01 2010")
100 def test_dayweek_timeperiod_with_exclude(self
):
103 #Get the 12 of july 2010 at 15:00, monday
104 july_the_12
= time
.mktime(time
.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
106 #Then a simple same day
108 t
.timeperiod_name
= 'T1'
109 t
.resolve_daterange(t
.dateranges
, 'tuesday 2 16:30-24:00')
110 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
111 t_next
= time
.asctime(time
.localtime(t_next
))
112 print "T next", t_next
113 self
.assert_(t_next
== "Tue Jul 13 16:30:00 2010")
115 #Now we add this timeperiod an exception
117 t2
.timeperiod_name
= 'T2'
118 t2
.resolve_daterange(t2
.dateranges
, 'tuesday 00:00-23:58')
120 #We are a bad boy : first time period want a tuesday
121 #but exclude do not want it until 23:58. So next is 58 + 1second :)
123 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
124 t_exclude
= t2
.get_next_valid_time_from_t(july_the_12
)
125 t_exclude_inv
= t2
.get_next_invalid_time_from_t(july_the_12
)
127 print "T next raw", t_next
128 t_next
= time
.asctime(time
.localtime(t_next
))
129 print "TOTO T next", t_next
131 self
.assert_(t_next
== 'Tue Jul 13 23:58:01 2010')
134 def test_mondayweek_timeperiod_with_exclude(self
):
137 #Get the 12 of july 2010 at 15:00, monday
138 july_the_12
= time
.mktime(time
.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
140 #Then a simple same day
142 t
.timeperiod_name
= 'T1'
143 t
.resolve_daterange(t
.dateranges
, 'tuesday 2 16:30-24:00')
144 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
145 t_next
= time
.asctime(time
.localtime(t_next
))
146 self
.assert_(t_next
== "Tue Jul 13 16:30:00 2010")
148 #Now we add this timeperiod an exception
149 #And a good one : from april (so before so agust (after), and full time.
150 #But the 17 is a tuesday, but the 3 of august, so the next 2 tuesday is
151 #..... the Tue Sep 14 :) Yes, we should wait quite a lot :)
153 t2
.timeperiod_name
= 'T2'
154 t2
.resolve_daterange(t2
.dateranges
, 'april 1 - august 16 00:00-24:00')
157 #We are a bad boy : first time period want a tuesday
158 #but exclude do not want it until 23:58. So next is 59 :)
160 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
161 #print "Check from", time.asctime(time.localtime(july_the_12))
162 #t_exclude = t2.get_next_valid_time_from_t(july_the_12)
163 t_exclude_inv
= t2
.get_next_invalid_time_from_t(july_the_12
)
164 #print "T2 next valid", time.asctime(time.localtime(t_exclude))
165 print "Next invalid T2", time
.asctime(time
.localtime(t_exclude_inv
))
167 print "T next raw JEAN", t_next
168 print "T next?", time
.asctime(time
.localtime(t_next
))
169 t_next
= time
.asctime(time
.localtime(t_next
))
171 self
.assert_(t_next
== 'Tue Sep 14 16:30:00 2010')
177 def test_mondayweek_timeperiod_with_exclude_bis(self
):
180 #Get the 12 of july 2010 at 15:00, monday
181 july_the_12
= time
.mktime(time
.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
183 #Then a funny daterange
184 print "Testing daterange", 'tuesday -1 - monday 1 16:30-24:00'
186 t
.timeperiod_name
= 'T1'
187 t
.resolve_daterange(t
.dateranges
, 'tuesday -1 - monday 1 16:30-24:00')
188 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
189 t_next
= time
.asctime(time
.localtime(t_next
))
190 print "Next without exclude", t_next
191 self
.assert_(t_next
== "Tue Jul 27 16:30:00 2010")
193 #Now we add this timeperiod an exception
194 #And a good one : from april (so before so agust (after), and full time.
195 #But the 27 is nw not possible? So what next? Add a month!
196 #last tuesday of august, the 31 :)
198 t2
.timeperiod_name
= 'T2'
199 t2
.resolve_daterange(t2
.dateranges
, 'april 1 - august 16 00:00-24:00')
202 #We are a bad boy : first time period want a tuesday
203 #but exclude do not want it until 23:58. So next is 59 :)
205 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
206 #print "Check from", time.asctime(time.localtime(july_the_12))
207 #t_exclude = t2.get_next_valid_time_from_t(july_the_12)
208 t_exclude_inv
= t2
.get_next_invalid_time_from_t(july_the_12
)
209 #print "T2 next valid", time.asctime(time.localtime(t_exclude))
210 print "Next invalid T2", time
.asctime(time
.localtime(t_exclude_inv
))
212 print "T next raw JEAN2", t_next
213 print "T next?", time
.asctime(time
.localtime(t_next
))
214 t_next
= time
.asctime(time
.localtime(t_next
))
216 self
.assert_(t_next
== 'Tue Aug 31 16:30:00 2010')
220 def test_funky_mondayweek_timeperiod_with_exclude_and_multiple_daterange(self
):
223 #Get the 12 of july 2010 at 15:00, monday
224 july_the_12
= time
.mktime(time
.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
226 #Then a funny daterange
227 print "Testing daterange", 'tuesday -1 - monday 1 16:30-24:00'
229 t
.timeperiod_name
= 'T1'
230 t
.resolve_daterange(t
.dateranges
, 'tuesday -1 - monday 1 16:30-24:00')
231 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
232 t_next
= time
.asctime(time
.localtime(t_next
))
233 print "Next without exclude", t_next
234 self
.assert_(t_next
== "Tue Jul 27 16:30:00 2010")
236 #Now we add this timeperiod an exception
237 #And a good one : from april (so before so agust (after), and full time.
238 #But the 27 is nw not possible? So what next? Add a month!
239 #But maybe it's not enoutgth? :)
240 #The withoutthe 2nd exclude, it's the Tues Aug 31, btu it's inside
241 #saturday -1 - monday 1 because saturday -1 is the 28 august, so no.
242 #in september saturday -1 is the 25, and tuesday -1 is 28, so still no
243 #A month again! So now tuesday -1 is 26 and saturday -1 is 30. So ok
244 #for this one! that was quite long isn't it? And funky ! :)
246 t2
.timeperiod_name
= 'T2'
247 t2
.resolve_daterange(t2
.dateranges
, 'april 1 - august 16 00:00-24:00')
248 #Oups, I add a inner daterange ;)
249 t2
.resolve_daterange(t2
.dateranges
, 'saturday -1 - monday 1 16:00-24:00')
251 #We are a bad boy : first time period want a tuesday
252 #but exclude do not want it until 23:58. So next is 59 :)
254 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
255 #print "Check from", time.asctime(time.localtime(july_the_12))
256 #t_exclude = t2.get_next_valid_time_from_t(july_the_12)
257 t_exclude_inv
= t2
.get_next_invalid_time_from_t(july_the_12
)
258 #print "T2 next valid", time.asctime(time.localtime(t_exclude))
259 print "Next invalid T2", time
.asctime(time
.localtime(t_exclude_inv
))
261 print "T next raw", t_next
262 print "T next?", time
.asctime(time
.localtime(t_next
))
263 t_next
= time
.asctime(time
.localtime(t_next
))
265 self
.assert_(t_next
== 'Tue Oct 26 16:30:00 2010')
266 print "Finish this Funky test :)"
270 def test_monweekday_timeperiod_with_exclude(self
):
273 #Get the 12 of july 2010 at 15:00, monday
274 july_the_12
= time
.mktime(time
.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
276 #Then a funny daterange
277 print "Testing daterange", 'tuesday -1 july - monday 1 august 16:30-24:00'
279 t
.timeperiod_name
= 'T1'
280 t
.resolve_daterange(t
.dateranges
, 'tuesday -1 july - monday 1 september 16:30-24:00')
281 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
282 t_next
= time
.asctime(time
.localtime(t_next
))
283 print "Next without exclude", t_next
284 self
.assert_(t_next
== "Tue Jul 27 16:30:00 2010")
286 #Now we add this timeperiod an exception
287 #and from april (before) to august monday 3 (monday 16 august),
288 #so Jul 17 is no more possible. So just after it, Tue 17
290 t2
.timeperiod_name
= 'T2'
291 t2
.resolve_daterange(t2
.dateranges
, 'thursday 1 april - monday 3 august 00:00-24:00')
292 print t2
.dateranges
[0].__dict
__
294 #We are a bad boy : first time period want a tuesday
295 #but exclude do not want it until 23:58. So next is 59 :)
297 t_next
= t
.get_next_valid_time_from_t(july_the_12
)
298 #print "Check from", time.asctime(time.localtime(july_the_12))
299 #t_exclude = t2.get_next_valid_time_from_t(july_the_12)
300 t_exclude_inv
= t2
.get_next_invalid_time_from_t(july_the_12
)
301 #print "T2 next valid", time.asctime(time.localtime(t_exclude))
302 print "Next invalid T2", time
.asctime(time
.localtime(t_exclude_inv
))
304 print "T next raw", t_next
305 print "T next?", time
.asctime(time
.localtime(t_next
))
306 t_next
= time
.asctime(time
.localtime(t_next
))
308 self
.assert_(t_next
== 'Tue Aug 17 16:30:00 2010')
312 if __name__
== '__main__':