Add : receiver launching scripts.
[shinken.git] / test / test_poller_addition.py
blob2abdaaf803aad223ac3d14d8f7f6bed4f36becf0
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 TestPollerAddition(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_and_addition(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)
290 cmd = "[%lu] ADD_SIMPLE_POLLER;All;newpoller;localhost;7771" % int(time.time())
291 ext_cmd = ExternalCommand(cmd)
292 self.external_command_dispatcher.resolve_command(ext_cmd)
294 # Look for the poller now
295 newpoller = self.conf.pollers.find_by_name('newpoller')
296 self.assert_(newpoller is not None)
297 newpoller.__class__ = GoodPoller
299 ### Wht now with our new poller object?
300 self.dispatcher.check_alive()
302 # Check good values
303 self.assert_(newpoller.alive == True)
304 self.assert_(newpoller.attempt == 0)
305 self.assert_(newpoller.reachable == True)
307 # Now we check how we should dispatch confs
308 self.dispatcher.check_bad_dispatch()
309 self.dispatcher.dispatch()
312 if __name__ == '__main__':
313 unittest.main()