1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/host.hpp"
21 #include <boost/test/unit_test.hpp>
23 using namespace icinga;
25 BOOST_AUTO_TEST_SUITE(icinga_checkresult)
27 static CheckResult::Ptr MakeCheckResult(ServiceState state)
29 CheckResult::Ptr cr = new CheckResult();
33 double now = Utility::GetTime();
34 cr->SetScheduleStart(now);
35 cr->SetScheduleEnd(now);
36 cr->SetExecutionStart(now);
37 cr->SetExecutionEnd(now);
42 static void NotificationHandler(const Checkable::Ptr& checkable, NotificationType type)
44 std::cout << "Notification triggered: " << Notification::NotificationTypeToString(type) << std::endl;
46 checkable->SetExtension("requested_notifications", true);
47 checkable->SetExtension("notification_type", type);
50 static void CheckNotification(const Checkable::Ptr& checkable, bool expected, NotificationType type = NotificationRecovery)
52 BOOST_CHECK((expected && checkable->GetExtension("requested_notifications").ToBool()) || (!expected && !checkable->GetExtension("requested_notifications").ToBool()));
54 if (expected && checkable->GetExtension("requested_notifications").ToBool())
55 BOOST_CHECK(checkable->GetExtension("notification_type") == type);
57 checkable->SetExtension("requested_notifications", false);
60 BOOST_AUTO_TEST_CASE(host_1attempt)
62 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
64 Host::Ptr host = new Host();
65 host->SetMaxCheckAttempts(1);
67 host->SetAuthority(true);
68 host->SetStateRaw(ServiceOK);
69 host->SetStateType(StateTypeHard);
71 std::cout << "Before first check result (ok, hard)" << std::endl;
72 BOOST_CHECK(host->GetState() == HostUp);
73 BOOST_CHECK(host->GetStateType() == StateTypeHard);
74 BOOST_CHECK(host->GetCheckAttempt() == 1);
75 CheckNotification(host, false);
77 std::cout << "First check result (unknown)" << std::endl;
78 host->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
79 BOOST_CHECK(host->GetState() == HostDown);
80 BOOST_CHECK(host->GetStateType() == StateTypeHard);
81 BOOST_CHECK(host->GetCheckAttempt() == 1);
82 CheckNotification(host, true, NotificationProblem);
84 std::cout << "Second check result (ok)" << std::endl;
85 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
86 BOOST_CHECK(host->GetState() == HostUp);
87 BOOST_CHECK(host->GetStateType() == StateTypeHard);
88 BOOST_CHECK(host->GetCheckAttempt() == 1);
89 CheckNotification(host, true, NotificationRecovery);
91 std::cout << "Third check result (critical)" << std::endl;
92 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
93 BOOST_CHECK(host->GetState() == HostDown);
94 BOOST_CHECK(host->GetStateType() == StateTypeHard);
95 BOOST_CHECK(host->GetCheckAttempt() == 1);
96 CheckNotification(host, true, NotificationProblem);
98 std::cout << "Fourth check result (ok)" << std::endl;
99 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
100 BOOST_CHECK(host->GetState() == HostUp);
101 BOOST_CHECK(host->GetStateType() == StateTypeHard);
102 BOOST_CHECK(host->GetCheckAttempt() == 1);
103 CheckNotification(host, true, NotificationRecovery);
108 BOOST_AUTO_TEST_CASE(host_2attempts)
110 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
112 Host::Ptr host = new Host();
113 host->SetMaxCheckAttempts(2);
115 host->SetAuthority(true);
116 host->SetStateRaw(ServiceOK);
117 host->SetStateType(StateTypeHard);
119 std::cout << "Before first check result (ok, hard)" << std::endl;
120 BOOST_CHECK(host->GetState() == HostUp);
121 BOOST_CHECK(host->GetStateType() == StateTypeHard);
122 BOOST_CHECK(host->GetCheckAttempt() == 1);
123 CheckNotification(host, false);
125 std::cout << "First check result (unknown)" << std::endl;
126 host->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
127 BOOST_CHECK(host->GetState() == HostDown);
128 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
129 BOOST_CHECK(host->GetCheckAttempt() == 1);
130 CheckNotification(host, false);
132 std::cout << "Second check result (critical)" << std::endl;
133 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
134 BOOST_CHECK(host->GetState() == HostDown);
135 BOOST_CHECK(host->GetStateType() == StateTypeHard);
136 BOOST_CHECK(host->GetCheckAttempt() == 1);
137 CheckNotification(host, true, NotificationProblem);
139 std::cout << "Third check result (ok)" << std::endl;
140 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
141 BOOST_CHECK(host->GetState() == HostUp);
142 BOOST_CHECK(host->GetStateType() == StateTypeHard);
143 BOOST_CHECK(host->GetCheckAttempt() == 1);
144 CheckNotification(host, true, NotificationRecovery);
146 std::cout << "Fourth check result (critical)" << std::endl;
147 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
148 BOOST_CHECK(host->GetState() == HostDown);
149 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
150 BOOST_CHECK(host->GetCheckAttempt() == 1);
151 CheckNotification(host, false);
153 std::cout << "Fifth check result (ok)" << std::endl;
154 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
155 BOOST_CHECK(host->GetState() == HostUp);
156 BOOST_CHECK(host->GetStateType() == StateTypeHard);
157 BOOST_CHECK(host->GetCheckAttempt() == 1);
158 CheckNotification(host, false);
163 BOOST_AUTO_TEST_CASE(host_3attempts)
165 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
167 Host::Ptr host = new Host();
168 host->SetMaxCheckAttempts(3);
170 host->SetAuthority(true);
171 host->SetStateRaw(ServiceOK);
172 host->SetStateType(StateTypeHard);
174 std::cout << "Before first check result (ok, hard)" << std::endl;
175 BOOST_CHECK(host->GetState() == HostUp);
176 BOOST_CHECK(host->GetStateType() == StateTypeHard);
177 BOOST_CHECK(host->GetCheckAttempt() == 1);
178 CheckNotification(host, false);
180 std::cout << "First check result (unknown)" << std::endl;
181 host->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
182 BOOST_CHECK(host->GetState() == HostDown);
183 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
184 BOOST_CHECK(host->GetCheckAttempt() == 1);
185 CheckNotification(host, false);
187 std::cout << "Second check result (critical)" << std::endl;
188 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
189 BOOST_CHECK(host->GetState() == HostDown);
190 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
191 BOOST_CHECK(host->GetCheckAttempt() == 2);
192 CheckNotification(host, false);
194 std::cout << "Third check result (critical)" << std::endl;
195 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
196 BOOST_CHECK(host->GetState() == HostDown);
197 BOOST_CHECK(host->GetStateType() == StateTypeHard);
198 BOOST_CHECK(host->GetCheckAttempt() == 1);
199 CheckNotification(host, true, NotificationProblem);
201 std::cout << "Fourth check result (ok)" << std::endl;
202 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
203 BOOST_CHECK(host->GetState() == HostUp);
204 BOOST_CHECK(host->GetStateType() == StateTypeHard);
205 BOOST_CHECK(host->GetCheckAttempt() == 1);
206 CheckNotification(host, true, NotificationRecovery);
208 std::cout << "Fifth check result (critical)" << std::endl;
209 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
210 BOOST_CHECK(host->GetState() == HostDown);
211 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
212 BOOST_CHECK(host->GetCheckAttempt() == 1);
213 CheckNotification(host, false);
215 std::cout << "Sixth check result (ok)" << std::endl;
216 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
217 BOOST_CHECK(host->GetState() == HostUp);
218 BOOST_CHECK(host->GetStateType() == StateTypeHard);
219 BOOST_CHECK(host->GetCheckAttempt() == 1);
220 CheckNotification(host, false);
225 BOOST_AUTO_TEST_CASE(service_1attempt)
227 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
229 Service::Ptr service = new Service();
230 service->SetMaxCheckAttempts(1);
232 service->SetAuthority(true);
233 service->SetStateRaw(ServiceOK);
234 service->SetStateType(StateTypeHard);
236 std::cout << "Before first check result (ok, hard)" << std::endl;
237 BOOST_CHECK(service->GetState() == ServiceOK);
238 BOOST_CHECK(service->GetStateType() == StateTypeHard);
239 BOOST_CHECK(service->GetCheckAttempt() == 1);
240 CheckNotification(service, false);
242 std::cout << "First check result (unknown)" << std::endl;
243 service->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
244 BOOST_CHECK(service->GetState() == ServiceUnknown);
245 BOOST_CHECK(service->GetStateType() == StateTypeHard);
246 BOOST_CHECK(service->GetCheckAttempt() == 1);
247 CheckNotification(service, true, NotificationProblem);
249 std::cout << "Second check result (ok)" << std::endl;
250 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
251 BOOST_CHECK(service->GetState() == ServiceOK);
252 BOOST_CHECK(service->GetStateType() == StateTypeHard);
253 BOOST_CHECK(service->GetCheckAttempt() == 1);
254 CheckNotification(service, true, NotificationRecovery);
256 std::cout << "Third check result (critical)" << std::endl;
257 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
258 BOOST_CHECK(service->GetState() == ServiceCritical);
259 BOOST_CHECK(service->GetStateType() == StateTypeHard);
260 BOOST_CHECK(service->GetCheckAttempt() == 1);
261 CheckNotification(service, true, NotificationProblem);
263 std::cout << "Fourth check result (ok)" << std::endl;
264 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
265 BOOST_CHECK(service->GetState() == ServiceOK);
266 BOOST_CHECK(service->GetStateType() == StateTypeHard);
267 BOOST_CHECK(service->GetCheckAttempt() == 1);
268 CheckNotification(service, true, NotificationRecovery);
273 BOOST_AUTO_TEST_CASE(service_2attempts)
275 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
277 Service::Ptr service = new Service();
278 service->SetMaxCheckAttempts(2);
280 service->SetAuthority(true);
281 service->SetStateRaw(ServiceOK);
282 service->SetStateType(StateTypeHard);
284 std::cout << "Before first check result (ok, hard)" << std::endl;
285 BOOST_CHECK(service->GetState() == ServiceOK);
286 BOOST_CHECK(service->GetStateType() == StateTypeHard);
287 BOOST_CHECK(service->GetCheckAttempt() == 1);
288 CheckNotification(service, false);
290 std::cout << "First check result (unknown)" << std::endl;
291 service->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
292 BOOST_CHECK(service->GetState() == ServiceUnknown);
293 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
294 BOOST_CHECK(service->GetCheckAttempt() == 1);
295 CheckNotification(service, false);
297 std::cout << "Second check result (critical)" << std::endl;
298 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
299 BOOST_CHECK(service->GetState() == ServiceCritical);
300 BOOST_CHECK(service->GetStateType() == StateTypeHard);
301 BOOST_CHECK(service->GetCheckAttempt() == 1);
302 CheckNotification(service, true, NotificationProblem);
304 std::cout << "Third check result (ok)" << std::endl;
305 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
306 BOOST_CHECK(service->GetState() == ServiceOK);
307 BOOST_CHECK(service->GetStateType() == StateTypeHard);
308 BOOST_CHECK(service->GetCheckAttempt() == 1);
309 CheckNotification(service, true, NotificationRecovery);
311 std::cout << "Fourth check result (critical)" << std::endl;
312 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
313 BOOST_CHECK(service->GetState() == ServiceCritical);
314 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
315 BOOST_CHECK(service->GetCheckAttempt() == 1);
316 CheckNotification(service, false);
318 std::cout << "Fifth check result (ok)" << std::endl;
319 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
320 BOOST_CHECK(service->GetState() == ServiceOK);
321 BOOST_CHECK(service->GetStateType() == StateTypeHard);
322 BOOST_CHECK(service->GetCheckAttempt() == 1);
323 CheckNotification(service, false);
328 BOOST_AUTO_TEST_CASE(service_3attempts)
330 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
332 Service::Ptr service = new Service();
333 service->SetMaxCheckAttempts(3);
335 service->SetAuthority(true);
336 service->SetStateRaw(ServiceOK);
337 service->SetStateType(StateTypeHard);
339 std::cout << "Before first check result (ok, hard)" << std::endl;
340 BOOST_CHECK(service->GetState() == ServiceOK);
341 BOOST_CHECK(service->GetStateType() == StateTypeHard);
342 BOOST_CHECK(service->GetCheckAttempt() == 1);
343 CheckNotification(service, false);
345 std::cout << "First check result (unknown)" << std::endl;
346 service->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
347 BOOST_CHECK(service->GetState() == ServiceUnknown);
348 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
349 BOOST_CHECK(service->GetCheckAttempt() == 1);
350 CheckNotification(service, false);
352 std::cout << "Second check result (critical)" << std::endl;
353 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
354 BOOST_CHECK(service->GetState() == ServiceCritical);
355 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
356 BOOST_CHECK(service->GetCheckAttempt() == 2);
357 CheckNotification(service, false);
359 std::cout << "Third check result (critical)" << std::endl;
360 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
361 BOOST_CHECK(service->GetState() == ServiceCritical);
362 BOOST_CHECK(service->GetStateType() == StateTypeHard);
363 BOOST_CHECK(service->GetCheckAttempt() == 1);
364 CheckNotification(service, true, NotificationProblem);
366 std::cout << "Fourth check result (ok)" << std::endl;
367 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
368 BOOST_CHECK(service->GetState() == ServiceOK);
369 BOOST_CHECK(service->GetStateType() == StateTypeHard);
370 BOOST_CHECK(service->GetCheckAttempt() == 1);
371 CheckNotification(service, true, NotificationRecovery);
373 std::cout << "Fifth check result (critical)" << std::endl;
374 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
375 BOOST_CHECK(service->GetState() == ServiceCritical);
376 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
377 BOOST_CHECK(service->GetCheckAttempt() == 1);
378 CheckNotification(service, false);
380 std::cout << "Sixth check result (ok)" << std::endl;
381 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
382 BOOST_CHECK(service->GetState() == ServiceOK);
383 BOOST_CHECK(service->GetStateType() == StateTypeHard);
384 BOOST_CHECK(service->GetCheckAttempt() == 1);
385 CheckNotification(service, false);
390 BOOST_AUTO_TEST_SUITE_END()