Fix : fix hot module under windows test. (at leat I hope...)
[shinken.git] / test / test_dispatcher.py
blobb35d9c112f15ba36dfdcefcb2b48d06c4dc8ebb2
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/>.
22 # This file is used to test reading and processing of config files
25 #It's ugly I know....
26 from shinken_test import *
29 class GoodArbiter(ArbiterLink):
30 # To lie about satellites
31 def ping(self):
32 print "Dummy OK for", self.get_name()
33 self.set_alive()
35 def have_conf(self, i):
36 return True
38 def do_not_run(self):
39 pass
42 class GoodScheduler(SchedulerLink):
43 # To lie about satellites
44 def ping(self):
45 print "Dummy OK for", self.get_name()
46 self.set_alive()
48 def have_conf(self, i):
49 return True
51 def put_conf(self, conf):
52 return True
55 class BadScheduler(SchedulerLink):
56 def ping(self):
57 print "Dummy bad ping", self.get_name()
58 self.add_failed_check_attempt()
60 def have_conf(self, i):
61 return False
63 class GoodPoller(PollerLink):
64 # To lie about satellites
65 def ping(self):
66 print "Dummy OK for", self.get_name()
67 self.set_alive()
69 def put_conf(self, conf):
70 return True
73 class BadPoller(PollerLink):
74 def ping(self):
75 print "Dummy bad ping", self.get_name()
76 self.add_failed_check_attempt()
78 class GoodReactionner(ReactionnerLink):
79 # To lie about satellites
80 def ping(self):
81 print "Dummy OK for", self.get_name()
82 self.set_alive()
84 def put_conf(self, conf):
85 return True
87 class BadReactionner(ReactionnerLink):
88 def ping(self):
89 print "Dummy bad ping", self.get_name()
90 self.add_failed_check_attempt()
92 class GoodBroker(BrokerLink):
93 # To lie about satellites
94 def ping(self):
95 print "Dummy OK for", self.get_name()
96 self.set_alive()
98 def put_conf(self, conf):
99 return True
101 class BadBroker(BrokerLink):
102 def ping(self):
103 print "Dummy bad ping", self.get_name()
104 self.add_failed_check_attempt()
107 class TestDispatcher(ShinkenTest):
108 #Uncomment this is you want to use a specific configuration
109 #for your test
110 def setUp(self):
111 self.setup_with_file('etc/nagios_dispatcher.cfg')
114 #Change ME :)
115 def test_simple_dispatch(self):
116 print "The dispatcher", self.dispatcher
117 # dummy for the arbiter
118 for a in self.conf.arbiterlinks:
119 a.__class__ = GoodArbiter
120 print "Preparing schedulers"
121 scheduler1 = self.conf.schedulerlinks.find_by_name('scheduler-all-1')
122 self.assert_(scheduler1 is not None)
123 scheduler1.__class__ = GoodScheduler
124 scheduler2 = self.conf.schedulerlinks.find_by_name('scheduler-all-2')
125 self.assert_(scheduler2 is not None)
126 scheduler2.__class__ = BadScheduler
128 print "Preparing pollers"
129 poller1 = self.conf.pollers.find_by_name('poller-all-1')
130 self.assert_(poller1 is not None)
131 poller1.__class__ = GoodPoller
132 poller2 = self.conf.pollers.find_by_name('poller-all-2')
133 self.assert_(poller2 is not None)
134 poller2.__class__ = BadPoller
136 print "Preparing reactionners"
137 reactionner1 = self.conf.reactionners.find_by_name('reactionner-all-1')
138 self.assert_(reactionner1 is not None)
139 reactionner1.__class__ = GoodReactionner
140 reactionner2 = self.conf.reactionners.find_by_name('reactionner-all-2')
141 self.assert_(reactionner2 is not None)
142 reactionner2.__class__ = BadReactionner
144 print "Preparing brokers"
145 broker1 = self.conf.brokers.find_by_name('broker-all-1')
146 self.assert_(broker1 is not None)
147 broker1.__class__ = GoodBroker
148 broker2 = self.conf.brokers.find_by_name('broker-all-2')
149 self.assert_(broker2 is not None)
150 broker2.__class__ = BadBroker
152 # Ping all elements. Should have 1 as OK, 2 as
153 # one bad attempt (3 max)
154 self.dispatcher.check_alive()
156 # Check good values
157 self.assert_(scheduler1.alive == True)
158 self.assert_(scheduler1.attempt == 0)
159 self.assert_(scheduler1.reachable == True)
160 # still alive, just unreach
161 self.assert_(scheduler2.alive == True)
162 self.assert_(scheduler2.attempt == 1)
163 self.assert_(scheduler2.reachable == False)
165 #and others satellites too
166 self.assert_(poller1.alive == True)
167 self.assert_(poller1.attempt == 0)
168 self.assert_(poller1.reachable == True)
169 # still alive, just unreach
170 self.assert_(poller2.alive == True)
171 self.assert_(poller2.attempt == 1)
172 self.assert_(poller2.reachable == False)
174 #and others satellites too
175 self.assert_(reactionner1.alive == True)
176 self.assert_(reactionner1.attempt == 0)
177 self.assert_(reactionner1.reachable == True)
178 # still alive, just unreach
179 self.assert_(reactionner2.alive == True)
180 self.assert_(reactionner2.attempt == 1)
181 self.assert_(reactionner2.reachable == False)
183 #and others satellites too
184 self.assert_(broker1.alive == True)
185 self.assert_(broker1.attempt == 0)
186 self.assert_(broker1.reachable == True)
187 # still alive, just unreach
188 self.assert_(broker2.alive == True)
189 self.assert_(broker2.attempt == 1)
190 self.assert_(broker2.reachable == False)
192 ### Now add another attempt, still alive, but attemp=2/3
193 self.dispatcher.check_alive()
195 # Check good values
196 self.assert_(scheduler1.alive == True)
197 self.assert_(scheduler1.attempt == 0)
198 self.assert_(scheduler1.reachable == True)
199 # still alive, just unreach
200 self.assert_(scheduler2.alive == True)
201 self.assert_(scheduler2.attempt == 2)
202 self.assert_(scheduler2.reachable == False)
204 #and others satellites too
205 self.assert_(poller1.alive == True)
206 self.assert_(poller1.attempt == 0)
207 self.assert_(poller1.reachable == True)
208 # still alive, just unreach
209 self.assert_(poller2.alive == True)
210 self.assert_(poller2.attempt == 2)
211 self.assert_(poller2.reachable == False)
213 #and others satellites too
214 self.assert_(reactionner1.alive == True)
215 self.assert_(reactionner1.attempt == 0)
216 self.assert_(reactionner1.reachable == True)
217 # still alive, just unreach
218 self.assert_(reactionner2.alive == True)
219 self.assert_(reactionner2.attempt == 2)
220 self.assert_(reactionner2.reachable == False)
222 #and others satellites too
223 self.assert_(broker1.alive == True)
224 self.assert_(broker1.attempt == 0)
225 self.assert_(broker1.reachable == True)
226 # still alive, just unreach
227 self.assert_(broker2.alive == True)
228 self.assert_(broker2.attempt == 2)
229 self.assert_(broker2.reachable == False)
231 ### Now we get BAD, We go DEAD for N2 !
232 self.dispatcher.check_alive()
234 # Check good values
235 self.assert_(scheduler1.alive == True)
236 self.assert_(scheduler1.attempt == 0)
237 self.assert_(scheduler1.reachable == True)
238 # still alive, just unreach
239 self.assert_(scheduler2.alive == False)
240 self.assert_(scheduler2.attempt == 3)
241 self.assert_(scheduler2.reachable == False)
243 #and others satellites too
244 self.assert_(poller1.alive == True)
245 self.assert_(poller1.attempt == 0)
246 self.assert_(poller1.reachable == True)
247 # still alive, just unreach
248 self.assert_(poller2.alive == False)
249 self.assert_(poller2.attempt == 3)
250 self.assert_(poller2.reachable == False)
252 #and others satellites too
253 self.assert_(reactionner1.alive == True)
254 self.assert_(reactionner1.attempt == 0)
255 self.assert_(reactionner1.reachable == True)
256 # still alive, just unreach
257 self.assert_(reactionner2.alive == False)
258 self.assert_(reactionner2.attempt == 3)
259 self.assert_(reactionner2.reachable == False)
261 #and others satellites too
262 self.assert_(broker1.alive == True)
263 self.assert_(broker1.attempt == 0)
264 self.assert_(broker1.reachable == True)
265 # still alive, just unreach
266 self.assert_(broker2.alive == False)
267 self.assert_(broker2.attempt == 3)
268 self.assert_(broker2.reachable == False)
270 # Now we check how we should dispatch confs
271 self.dispatcher.check_dispatch()
272 # the conf should not be in a good shape
273 self.assert_(self.dispatcher.dispatch_ok == False)
275 # Now we really dispatch them!
276 self.dispatcher.dispatch()
277 self.assert_(self.any_log_match('Dispatch OK of for conf in scheduler scheduler-all-1'))
278 self.assert_(self.any_log_match('Dispatch OK of for configuration 0 to reactionner reactionner-all-1'))
279 self.assert_(self.any_log_match('Dispatch OK of for configuration 0 to poller poller-all-1'))
280 self.assert_(self.any_log_match('Dispatch OK of for configuration 0 to broker broker-all-1'))
281 self.clear_logs()
283 # And look if we really dispatch conf as we should
284 for r in self.conf.realms:
285 for cfg in r.confs.values():
286 self.assert_(cfg.is_assigned == True)
287 self.assert_(cfg.assigned_to == scheduler1)
291 class TestDispatcherMultiBroker(ShinkenTest):
292 #Uncomment this is you want to use a specific configuration
293 #for your test
294 def setUp(self):
295 self.setup_with_file('etc/nagios_dispatcher_multibrokers.cfg')
298 #Change ME :)
299 def test_simple_dispatch(self):
300 print "The dispatcher", self.dispatcher
301 # dummy for the arbiter
302 for a in self.conf.arbiterlinks:
303 a.__class__ = GoodArbiter
304 print "Preparing schedulers"
305 scheduler1 = self.conf.schedulerlinks.find_by_name('scheduler-all-1')
306 self.assert_(scheduler1 is not None)
307 scheduler1.__class__ = GoodScheduler
308 scheduler2 = self.conf.schedulerlinks.find_by_name('scheduler-all-2')
309 self.assert_(scheduler2 is not None)
310 scheduler2.__class__ = GoodScheduler
312 print "Preparing pollers"
313 poller1 = self.conf.pollers.find_by_name('poller-all-1')
314 self.assert_(poller1 is not None)
315 poller1.__class__ = GoodPoller
316 poller2 = self.conf.pollers.find_by_name('poller-all-2')
317 self.assert_(poller2 is not None)
318 poller2.__class__ = BadPoller
320 print "Preparing reactionners"
321 reactionner1 = self.conf.reactionners.find_by_name('reactionner-all-1')
322 self.assert_(reactionner1 is not None)
323 reactionner1.__class__ = GoodReactionner
324 reactionner2 = self.conf.reactionners.find_by_name('reactionner-all-2')
325 self.assert_(reactionner2 is not None)
326 reactionner2.__class__ = BadReactionner
328 print "Preparing brokers"
329 broker1 = self.conf.brokers.find_by_name('broker-all-1')
330 self.assert_(broker1 is not None)
331 broker1.__class__ = GoodBroker
332 broker2 = self.conf.brokers.find_by_name('broker-all-2')
333 self.assert_(broker2 is not None)
334 broker2.__class__ = GoodBroker
336 # Ping all elements. Should have 1 as OK, 2 as
337 # one bad attempt (3 max)
338 self.dispatcher.check_alive()
340 # Check good values
341 self.assert_(scheduler1.alive == True)
342 self.assert_(scheduler1.attempt == 0)
343 self.assert_(scheduler1.reachable == True)
344 # still alive, just unreach
345 self.assert_(scheduler2.alive == True)
346 self.assert_(scheduler2.attempt == 0)
347 self.assert_(scheduler2.reachable == True)
349 #and others satellites too
350 self.assert_(poller1.alive == True)
351 self.assert_(poller1.attempt == 0)
352 self.assert_(poller1.reachable == True)
353 # still alive, just unreach
354 self.assert_(poller2.alive == True)
355 self.assert_(poller2.attempt == 1)
356 self.assert_(poller2.reachable == False)
358 #and others satellites too
359 self.assert_(reactionner1.alive == True)
360 self.assert_(reactionner1.attempt == 0)
361 self.assert_(reactionner1.reachable == True)
362 # still alive, just unreach
363 self.assert_(reactionner2.alive == True)
364 self.assert_(reactionner2.attempt == 1)
365 self.assert_(reactionner2.reachable == False)
367 #and others satellites too
368 self.assert_(broker1.alive == True)
369 self.assert_(broker1.attempt == 0)
370 self.assert_(broker1.reachable == True)
371 # still alive, just unreach
372 self.assert_(broker2.alive == True)
373 self.assert_(broker2.attempt == 0)
374 self.assert_(broker2.reachable == True)
376 ### Now add another attempt, still alive, but attemp=2/3
377 self.dispatcher.check_alive()
379 # Check good values
380 self.assert_(scheduler1.alive == True)
381 self.assert_(scheduler1.attempt == 0)
382 self.assert_(scheduler1.reachable == True)
383 # still alive, just unreach
384 self.assert_(scheduler2.alive == True)
385 self.assert_(scheduler2.attempt == 0)
386 self.assert_(scheduler2.reachable == True)
388 #and others satellites too
389 self.assert_(poller1.alive == True)
390 self.assert_(poller1.attempt == 0)
391 self.assert_(poller1.reachable == True)
392 # still alive, just unreach
393 self.assert_(poller2.alive == True)
394 self.assert_(poller2.attempt == 2)
395 self.assert_(poller2.reachable == False)
397 #and others satellites too
398 self.assert_(reactionner1.alive == True)
399 self.assert_(reactionner1.attempt == 0)
400 self.assert_(reactionner1.reachable == True)
401 # still alive, just unreach
402 self.assert_(reactionner2.alive == True)
403 self.assert_(reactionner2.attempt == 2)
404 self.assert_(reactionner2.reachable == False)
406 #and others satellites too
407 self.assert_(broker1.alive == True)
408 self.assert_(broker1.attempt == 0)
409 self.assert_(broker1.reachable == True)
410 # still alive, just unreach
411 self.assert_(broker2.alive == True)
412 self.assert_(broker2.attempt == 0)
413 self.assert_(broker2.reachable == True)
415 ### Now we get BAD, We go DEAD for N2 !
416 self.dispatcher.check_alive()
418 # Check good values
419 self.assert_(scheduler1.alive == True)
420 self.assert_(scheduler1.attempt == 0)
421 self.assert_(scheduler1.reachable == True)
422 # still alive, just unreach
423 self.assert_(scheduler2.alive == True)
424 self.assert_(scheduler2.attempt == 0)
425 self.assert_(scheduler2.reachable == True)
427 #and others satellites too
428 self.assert_(poller1.alive == True)
429 self.assert_(poller1.attempt == 0)
430 self.assert_(poller1.reachable == True)
431 # still alive, just unreach
432 self.assert_(poller2.alive == False)
433 self.assert_(poller2.attempt == 3)
434 self.assert_(poller2.reachable == False)
436 #and others satellites too
437 self.assert_(reactionner1.alive == True)
438 self.assert_(reactionner1.attempt == 0)
439 self.assert_(reactionner1.reachable == True)
440 # still alive, just unreach
441 self.assert_(reactionner2.alive == False)
442 self.assert_(reactionner2.attempt == 3)
443 self.assert_(reactionner2.reachable == False)
445 #and others satellites too
446 self.assert_(broker1.alive == True)
447 self.assert_(broker1.attempt == 0)
448 self.assert_(broker1.reachable == True)
449 # still alive, just unreach
450 self.assert_(broker2.alive == True)
451 self.assert_(broker2.attempt == 0)
452 self.assert_(broker2.reachable == True)
454 # Now we check how we should dispatch confs
455 self.dispatcher.check_dispatch()
456 # the conf should not be in a good shape
457 self.assert_(self.dispatcher.dispatch_ok == False)
459 # Now we really dispatch them!
460 self.dispatcher.dispatch()
461 self.assert_(self.any_log_match('Dispatch OK of for conf in scheduler scheduler-all-1'))
462 self.assert_(self.any_log_match('Dispatch OK of for configuration 0 to reactionner reactionner-all-1'))
463 self.assert_(self.any_log_match('Dispatch OK of for configuration 0 to poller poller-all-1'))
465 self.assert_(self.any_log_match('Dispatch OK of for configuration 1 to broker broker-all-1'))
466 self.assert_(self.any_log_match('Dispatch OK of for configuration 0 to broker broker-all-2'))
467 self.clear_logs()
470 # And look if we really dispatch conf as we should
471 for r in self.conf.realms:
472 for cfg in r.confs.values():
473 self.assert_(cfg.is_assigned == True)
474 self.assert_(cfg.assigned_to in [scheduler1, scheduler2])
479 if __name__ == '__main__':
480 unittest.main()