Merge branch 'master' of ssh://naparuba@shinken.git.sourceforge.net/gitroot/shinken...
[shinken.git] / test / test_eventids.py
blobd6b2084f965da47ab07027e90e89c9b5d317a3fb
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 current_event_id, last_event_id,
24 # current_problem_id and last_problem_id which are used for
25 # $HOSTEVENTID$, $HOSTPROBLEMID$ etc.
29 #It's ugly I know....
30 from shinken_test import *
31 from shinken.schedulingitem import SchedulingItem
34 class TestConfig(ShinkenTest):
37 def print_ids(self, host, svc, router):
38 print "global: cei,lei,cpi,lpi = %d,%d" % (SchedulingItem.current_event_id, SchedulingItem.current_problem_id)
39 print "service: cei,lei,cpi,lpi = %d,%d,%d,%d" % (svc.current_event_id, svc.last_event_id, svc.current_problem_id, svc.last_problem_id)
40 print "host: cei,lei,cpi,lpi = %d,%d,%d,%d" % (host.current_event_id, host.last_event_id, host.current_problem_id, host.last_problem_id)
41 print "router: cei,lei,cpi,lpi = %d,%d,%d,%d" % (router.current_event_id, router.last_event_id, router.current_problem_id, router.last_problem_id)
43 def test_global_counters(self):
44 self.print_header()
45 # retry_interval 2
46 # critical notification
47 # run loop -> another notification
48 now = time.time()
49 host = self.sched.hosts.find_by_name("test_host_0")
50 host.checks_in_progress = []
51 host.act_depend_of = [] # ignore the router
52 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
53 router = self.sched.hosts.find_by_name("test_router_0")
54 router.checks_in_progress = []
55 router.act_depend_of = [] # ignore the router
57 svc.checks_in_progress = []
58 svc.act_depend_of = [] # no hostchecks on critical checkresults
59 self.print_ids(host, svc, router)
60 #--------------------------------------------------------------
61 # initialize host/service state
62 #--------------------------------------------------------------
63 self.scheduler_loop(1, [[host, 0, 'UP']], do_sleep=False)
64 print "- 1 x OK -------------------------------------"
65 self.scheduler_loop(1, [[svc, 0, 'OK']], do_sleep=False)
66 self.print_ids(host, svc, router)
67 self.assert_(host.current_event_id == 0)
68 self.assert_(host.last_event_id == 0)
69 self.assert_(host.current_problem_id == 0)
70 self.assert_(host.last_problem_id == 0)
71 self.assert_(svc.current_event_id == 0)
72 self.assert_(svc.last_event_id == 0)
73 self.assert_(svc.current_problem_id == 0)
74 self.assert_(svc.last_problem_id == 0)
75 #--------------------------------------------------------------
76 # service reaches soft;1
77 # svc: 1,0,1,0
78 #--------------------------------------------------------------
79 print "- 1 x BAD get soft -------------------------------------"
80 self.scheduler_loop(1, [[svc, 2, 'BAD']], do_sleep=False)
81 self.print_ids(host, svc, router)
82 self.assert_(host.current_event_id == 0)
83 self.assert_(host.last_event_id == 0)
84 self.assert_(host.current_problem_id == 0)
85 self.assert_(host.last_problem_id == 0)
86 self.assert_(svc.current_event_id == 1)
87 self.assert_(svc.last_event_id == 0)
88 self.assert_(svc.current_problem_id == 1)
89 self.assert_(svc.last_problem_id == 0)
90 #--------------------------------------------------------------
91 # service reaches hard;2
92 # svc: 1,0,1,0
93 #--------------------------------------------------------------
94 print "- 1 x BAD get hard -------------------------------------"
95 self.scheduler_loop(1, [[svc, 2, 'BAD']], do_sleep=False)
96 self.print_ids(host, svc, router)
97 self.assert_(host.current_event_id == 0)
98 self.assert_(host.last_event_id == 0)
99 self.assert_(host.current_problem_id == 0)
100 self.assert_(host.last_problem_id == 0)
101 self.assert_(svc.current_event_id == 1)
102 self.assert_(svc.last_event_id == 0)
103 self.assert_(svc.current_problem_id == 1)
104 self.assert_(svc.last_problem_id == 0)
105 print "- 5 x BAD repeat -------------------------------------"
106 self.scheduler_loop(5, [[svc, 2, 'BAD']], do_sleep=False)
107 self.print_ids(host, svc, router)
108 self.assert_(host.current_event_id == 0)
109 self.assert_(host.last_event_id == 0)
110 self.assert_(host.current_problem_id == 0)
111 self.assert_(host.last_problem_id == 0)
112 self.assert_(svc.current_event_id == 1)
113 self.assert_(svc.last_event_id == 0)
114 self.assert_(svc.current_problem_id == 1)
115 self.assert_(svc.last_problem_id == 0)
116 #--------------------------------------------------------------
117 # now recover.
118 #--------------------------------------------------------------
119 self.scheduler_loop(1, [[svc, 0, 'GOOD']], do_sleep=False)
120 self.print_ids(host, svc, router)
121 self.assert_(host.current_event_id == 0)
122 self.assert_(host.last_event_id == 0)
123 self.assert_(host.current_problem_id == 0)
124 self.assert_(host.last_problem_id == 0)
125 self.assert_(svc.current_event_id == 2)
126 self.assert_(svc.last_event_id == 1)
127 self.assert_(svc.current_problem_id == 0)
128 self.assert_(svc.last_problem_id == 1)
129 #--------------------------------------------------------------
130 # service fails again, ok->w->c
131 #--------------------------------------------------------------
132 print "- 4 x BAD get hard with non-ok statechange -------------"
133 self.scheduler_loop(2, [[svc, 1, 'BAD']], do_sleep=False)
134 self.print_ids(host, svc, router)
135 self.assert_(host.current_event_id == 0)
136 self.assert_(host.last_event_id == 0)
137 self.assert_(host.current_problem_id == 0)
138 self.assert_(host.last_problem_id == 0)
139 self.assert_(svc.current_event_id == 3)
140 self.assert_(svc.last_event_id == 2)
141 self.assert_(svc.current_problem_id == 2)
142 self.assert_(svc.last_problem_id == 0)
143 # another statechange
144 self.scheduler_loop(2, [[svc, 2, 'BAD']], do_sleep=False)
145 self.print_ids(host, svc, router)
146 self.assert_(host.current_event_id == 0)
147 self.assert_(host.last_event_id == 0)
148 self.assert_(host.current_problem_id == 0)
149 self.assert_(host.last_problem_id == 0)
150 self.assert_(svc.current_event_id == 4)
151 self.assert_(svc.last_event_id == 3)
152 self.assert_(svc.current_problem_id == 2)
153 self.assert_(svc.last_problem_id == 0)
154 #--------------------------------------------------------------
155 # now recover.
156 #--------------------------------------------------------------
157 self.scheduler_loop(1, [[svc, 0, 'GOOD']], do_sleep=False)
158 self.print_ids(host, svc, router)
159 self.assert_(host.current_event_id == 0)
160 self.assert_(host.last_event_id == 0)
161 self.assert_(host.current_problem_id == 0)
162 self.assert_(host.last_problem_id == 0)
163 self.assert_(svc.current_event_id == 5)
164 self.assert_(svc.last_event_id == 4)
165 self.assert_(svc.current_problem_id == 0)
166 self.assert_(svc.last_problem_id == 2)
167 #--------------------------------------------------------------
168 # mix in two hosts
169 #--------------------------------------------------------------
170 print "- 4 x BAD get hard with non-ok statechange -------------"
171 self.scheduler_loop(2, [[router, 2, 'DOWN']], do_sleep=False)
172 self.print_ids(host, svc, router)
173 self.assert_(SchedulingItem.current_event_id == 6)
174 self.assert_(SchedulingItem.current_problem_id == 3)
175 self.assert_(host.current_event_id == 0)
176 self.assert_(host.last_event_id == 0)
177 self.assert_(host.current_problem_id == 0)
178 self.assert_(host.last_problem_id == 0)
179 self.assert_(svc.current_event_id == 5)
180 self.assert_(svc.last_event_id == 4)
181 self.assert_(svc.current_problem_id == 0)
182 self.assert_(svc.last_problem_id == 2)
183 self.assert_(router.current_event_id == 6)
184 self.assert_(router.last_event_id == 0)
185 self.assert_(router.current_problem_id == 3)
186 self.assert_(router.last_problem_id == 0)
187 # add chaos
188 self.scheduler_loop(1, [[svc, 2, 'BAD']], do_sleep=False)
189 self.scheduler_loop(2, [[router, 0, 'UP']], do_sleep=False)
190 self.scheduler_loop(5, [[host, 2, 'DOWN']], do_sleep=False)
191 self.print_ids(host, svc, router)
192 self.assert_(SchedulingItem.current_event_id == 9)
193 self.assert_(SchedulingItem.current_problem_id == 5)
194 self.assert_(host.current_event_id == 9)
195 self.assert_(host.last_event_id == 0)
196 self.assert_(host.current_problem_id == 5)
197 self.assert_(host.last_problem_id == 0)
198 self.assert_(svc.current_event_id == 7)
199 self.assert_(svc.last_event_id == 5)
200 self.assert_(svc.current_problem_id == 4)
201 self.assert_(svc.last_problem_id == 0)
202 self.assert_(router.current_event_id == 8)
203 self.assert_(router.last_event_id == 6)
204 self.assert_(router.current_problem_id == 0)
205 self.assert_(router.last_problem_id == 3)
208 if __name__ == '__main__':
209 unittest.main()