]> granicus.if.org Git - icinga2/blob - test/icinga-checkresult.cpp
Release version 2.7.1
[icinga2] / test / icinga-checkresult.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "icinga/host.hpp"
21 #include <BoostTestTargetConfig.h>
22
23 using namespace icinga;
24
25 BOOST_AUTO_TEST_SUITE(icinga_checkresult)
26
27 static CheckResult::Ptr MakeCheckResult(ServiceState state)
28 {
29         CheckResult::Ptr cr = new CheckResult();
30
31         cr->SetState(state);
32
33         double now = Utility::GetTime();
34         cr->SetScheduleStart(now);
35         cr->SetScheduleEnd(now);
36         cr->SetExecutionStart(now);
37         cr->SetExecutionEnd(now);
38
39         return cr;
40 }
41
42 static void NotificationHandler(const Checkable::Ptr& checkable, NotificationType type)
43 {
44         std::cout << "Notification triggered: " << Notification::NotificationTypeToString(type) << std::endl;
45
46         checkable->SetExtension("requested_notifications", true);
47         checkable->SetExtension("notification_type", type);
48 }
49
50 static void CheckNotification(const Checkable::Ptr& checkable, bool expected, NotificationType type = NotificationRecovery)
51 {
52         BOOST_CHECK((expected && checkable->GetExtension("requested_notifications").ToBool()) || (!expected && !checkable->GetExtension("requested_notifications").ToBool()));
53
54         if (expected && checkable->GetExtension("requested_notifications").ToBool())
55                 BOOST_CHECK(checkable->GetExtension("notification_type") == type);
56
57         checkable->SetExtension("requested_notifications", false);
58 }
59
60 BOOST_AUTO_TEST_CASE(host_1attempt)
61 {
62         boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
63
64         Host::Ptr host = new Host();
65         host->SetMaxCheckAttempts(1);
66         host->Activate();
67         host->SetAuthority(true);
68         host->SetStateRaw(ServiceOK);
69         host->SetStateType(StateTypeHard);
70
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);
76
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);
83
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);
90
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);
97
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);
104
105         c.disconnect();
106 }
107
108 BOOST_AUTO_TEST_CASE(host_2attempts)
109 {
110         boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
111
112         Host::Ptr host = new Host();
113         host->SetMaxCheckAttempts(2);
114         host->Activate();
115         host->SetAuthority(true);
116         host->SetStateRaw(ServiceOK);
117         host->SetStateType(StateTypeHard);
118
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);
124
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);
131
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);
138
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);
145
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);
152
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);
159
160         c.disconnect();
161 }
162
163 BOOST_AUTO_TEST_CASE(host_3attempts)
164 {
165         boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
166
167         Host::Ptr host = new Host();
168         host->SetMaxCheckAttempts(3);
169         host->Activate();
170         host->SetAuthority(true);
171         host->SetStateRaw(ServiceOK);
172         host->SetStateType(StateTypeHard);
173
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);
179
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);
186
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);
193
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);
200
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);
207
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);
214
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);
221
222         c.disconnect();
223 }
224
225 BOOST_AUTO_TEST_CASE(service_1attempt)
226 {
227         boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
228
229         Service::Ptr service = new Service();
230         service->SetMaxCheckAttempts(1);
231         service->Activate();
232         service->SetAuthority(true);
233         service->SetStateRaw(ServiceOK);
234         service->SetStateType(StateTypeHard);
235
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);
241
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);
248
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);
255
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);
262
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);
269
270         c.disconnect();
271 }
272
273 BOOST_AUTO_TEST_CASE(service_2attempts)
274 {
275         boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
276
277         Service::Ptr service = new Service();
278         service->SetMaxCheckAttempts(2);
279         service->Activate();
280         service->SetAuthority(true);
281         service->SetStateRaw(ServiceOK);
282         service->SetStateType(StateTypeHard);
283
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);
289
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);
296
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);
303
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);
310
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);
317
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);
324
325         c.disconnect();
326 }
327
328 BOOST_AUTO_TEST_CASE(service_3attempts)
329 {
330         boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
331
332         Service::Ptr service = new Service();
333         service->SetMaxCheckAttempts(3);
334         service->Activate();
335         service->SetAuthority(true);
336         service->SetStateRaw(ServiceOK);
337         service->SetStateType(StateTypeHard);
338
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);
344
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);
351
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);
358
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);
365
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);
372
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);
379
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);
386
387         c.disconnect();
388 }
389
390 BOOST_AUTO_TEST_CASE(host_flapping_notification)
391 {
392 #ifndef I2_DEBUG
393         BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
394 #else /* I2_DEBUG */
395         boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
396
397         int softStateCount = 20;
398         int timeStepInterval = 60;
399
400         Host::Ptr host = new Host();
401         host->SetMaxCheckAttempts(softStateCount);
402         host->Activate();
403         host->SetAuthority(true);
404         host->SetStateRaw(ServiceOK);
405         host->SetStateType(StateTypeHard);
406         host->SetEnableFlapping(true);
407
408         /* Initialize start time */
409         Utility::SetTime(0);
410
411         std::cout << "Before first check result (ok, hard)" << std::endl;
412         BOOST_CHECK(host->GetState() == HostUp);
413         BOOST_CHECK(host->GetStateType() == StateTypeHard);
414         BOOST_CHECK(host->GetCheckAttempt() == 1);
415
416         Utility::IncrementTime(timeStepInterval);
417
418         std::cout << "Inserting flapping check results" << std::endl;
419
420         for (int i = 0; i < softStateCount; i++) {
421                 ServiceState state = (i % 2 == 0 ? ServiceOK : ServiceCritical);
422                 host->ProcessCheckResult(MakeCheckResult(state));
423                 Utility::IncrementTime(timeStepInterval);
424         }
425
426         std::cout << "Checking host state (must be flapping in SOFT state)" << std::endl;
427         BOOST_CHECK(host->GetStateType() == StateTypeSoft);
428         BOOST_CHECK(host->IsFlapping() == true);
429
430         std::cout << "No FlappingStart notification type must have been triggered in a SOFT state" << std::endl;
431         CheckNotification(host, false, NotificationFlappingStart);
432
433         c.disconnect();
434
435 #endif /* I2_DEBUG */
436 }
437
438 BOOST_AUTO_TEST_CASE(service_flapping_notification)
439 {
440 #ifndef I2_DEBUG
441         BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
442 #else /* I2_DEBUG */
443         boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
444
445         int softStateCount = 20;
446         int timeStepInterval = 60;
447
448         Host::Ptr service = new Host();
449         service->SetMaxCheckAttempts(softStateCount);
450         service->Activate();
451         service->SetAuthority(true);
452         service->SetStateRaw(ServiceOK);
453         service->SetStateType(StateTypeHard);
454         service->SetEnableFlapping(true);
455
456         /* Initialize start time */
457         Utility::SetTime(0);
458
459         std::cout << "Before first check result (ok, hard)" << std::endl;
460         BOOST_CHECK(service->GetState() == HostUp);
461         BOOST_CHECK(service->GetStateType() == StateTypeHard);
462         BOOST_CHECK(service->GetCheckAttempt() == 1);
463
464         Utility::IncrementTime(timeStepInterval);
465
466         std::cout << "Inserting flapping check results" << std::endl;
467
468         for (int i = 0; i < softStateCount; i++) {
469                 ServiceState state = (i % 2 == 0 ? ServiceOK : ServiceCritical);
470                 service->ProcessCheckResult(MakeCheckResult(state));
471                 Utility::IncrementTime(timeStepInterval);
472         }
473
474         std::cout << "Checking service state (must be flapping in SOFT state)" << std::endl;
475         BOOST_CHECK(service->GetStateType() == StateTypeSoft);
476         BOOST_CHECK(service->IsFlapping() == true);
477
478         std::cout << "No FlappingStart notification type must have been triggered in a SOFT state" << std::endl;
479         CheckNotification(service, false, NotificationFlappingStart);
480
481         c.disconnect();
482
483 #endif /* I2_DEBUG */
484 }
485 BOOST_AUTO_TEST_SUITE_END()