From 6d4e305fe32019f851719d4b4bda9a2bef6e98d3 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 26 Aug 2016 12:37:20 +0200 Subject: [PATCH] Add more unit tests fixes #12530 --- test/CMakeLists.txt | 8 +- test/icinga-checkresult.cpp | 210 +++++++++++++++++++++++++++++++++++- 2 files changed, 214 insertions(+), 4 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e895b0ea7..918fe5b8c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -89,8 +89,12 @@ add_boost_test(base base_value/format config_ops/simple config_ops/advanced - icinga_checkresult/host - icinga_checkresult/service + icinga_checkresult/host_1attempt + icinga_checkresult/host_2attempts + icinga_checkresult/host_3attempts + icinga_checkresult/service_1attempt + icinga_checkresult/service_2attempts + icinga_checkresult/service_3attempts icinga_macros/simple icinga_perfdata/empty icinga_perfdata/simple diff --git a/test/icinga-checkresult.cpp b/test/icinga-checkresult.cpp index db6494fbd..5241d43e2 100644 --- a/test/icinga-checkresult.cpp +++ b/test/icinga-checkresult.cpp @@ -57,7 +57,110 @@ static void CheckNotification(const Checkable::Ptr& checkable, bool expected, No checkable->SetExtension("requested_notifications", false); } -BOOST_AUTO_TEST_CASE(host) +BOOST_AUTO_TEST_CASE(host_1attempt) +{ + boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2)); + + Host::Ptr host = new Host(); + host->SetMaxCheckAttempts(1); + host->Activate(); + host->SetAuthority(true); + host->SetStateRaw(ServiceOK); + host->SetStateType(StateTypeHard); + + std::cout << "Before first check result (ok, hard)" << std::endl; + BOOST_CHECK(host->GetState() == HostUp); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, false); + + std::cout << "First check result (unknown)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceUnknown)); + BOOST_CHECK(host->GetState() == HostDown); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, true, NotificationProblem); + + std::cout << "Second check result (ok)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceOK)); + BOOST_CHECK(host->GetState() == HostUp); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, true, NotificationRecovery); + + std::cout << "Third check result (critical)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceCritical)); + BOOST_CHECK(host->GetState() == HostDown); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, true, NotificationProblem); + + std::cout << "Fourth check result (ok)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceOK)); + BOOST_CHECK(host->GetState() == HostUp); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, true, NotificationRecovery); + + c.disconnect(); +} + +BOOST_AUTO_TEST_CASE(host_2attempts) +{ + boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2)); + + Host::Ptr host = new Host(); + host->SetMaxCheckAttempts(2); + host->Activate(); + host->SetAuthority(true); + host->SetStateRaw(ServiceOK); + host->SetStateType(StateTypeHard); + + std::cout << "Before first check result (ok, hard)" << std::endl; + BOOST_CHECK(host->GetState() == HostUp); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, false); + + std::cout << "First check result (unknown)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceUnknown)); + BOOST_CHECK(host->GetState() == HostDown); + BOOST_CHECK(host->GetStateType() == StateTypeSoft); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, false); + + std::cout << "Second check result (critical)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceCritical)); + BOOST_CHECK(host->GetState() == HostDown); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, true, NotificationProblem); + + std::cout << "Third check result (ok)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceOK)); + BOOST_CHECK(host->GetState() == HostUp); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, true, NotificationRecovery); + + std::cout << "Fourth check result (critical)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceCritical)); + BOOST_CHECK(host->GetState() == HostDown); + BOOST_CHECK(host->GetStateType() == StateTypeSoft); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, false); + + std::cout << "Fifth check result (ok)" << std::endl; + host->ProcessCheckResult(MakeCheckResult(ServiceOK)); + BOOST_CHECK(host->GetState() == HostUp); + BOOST_CHECK(host->GetStateType() == StateTypeHard); + BOOST_CHECK(host->GetCheckAttempt() == 1); + CheckNotification(host, false); + + c.disconnect(); +} + +BOOST_AUTO_TEST_CASE(host_3attempts) { boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2)); @@ -119,7 +222,110 @@ BOOST_AUTO_TEST_CASE(host) c.disconnect(); } -BOOST_AUTO_TEST_CASE(service) +BOOST_AUTO_TEST_CASE(service_1attempt) +{ + boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2)); + + Service::Ptr service = new Service(); + service->SetMaxCheckAttempts(1); + service->Activate(); + service->SetAuthority(true); + service->SetStateRaw(ServiceOK); + service->SetStateType(StateTypeHard); + + std::cout << "Before first check result (ok, hard)" << std::endl; + BOOST_CHECK(service->GetState() == ServiceOK); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, false); + + std::cout << "First check result (unknown)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceUnknown)); + BOOST_CHECK(service->GetState() == ServiceUnknown); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, true, NotificationProblem); + + std::cout << "Second check result (ok)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceOK)); + BOOST_CHECK(service->GetState() == ServiceOK); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, true, NotificationRecovery); + + std::cout << "Third check result (critical)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceCritical)); + BOOST_CHECK(service->GetState() == ServiceCritical); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, true, NotificationProblem); + + std::cout << "Fourth check result (ok)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceOK)); + BOOST_CHECK(service->GetState() == ServiceOK); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, true, NotificationRecovery); + + c.disconnect(); +} + +BOOST_AUTO_TEST_CASE(service_2attempts) +{ + boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2)); + + Service::Ptr service = new Service(); + service->SetMaxCheckAttempts(2); + service->Activate(); + service->SetAuthority(true); + service->SetStateRaw(ServiceOK); + service->SetStateType(StateTypeHard); + + std::cout << "Before first check result (ok, hard)" << std::endl; + BOOST_CHECK(service->GetState() == ServiceOK); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, false); + + std::cout << "First check result (unknown)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceUnknown)); + BOOST_CHECK(service->GetState() == ServiceUnknown); + BOOST_CHECK(service->GetStateType() == StateTypeSoft); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, false); + + std::cout << "Second check result (critical)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceCritical)); + BOOST_CHECK(service->GetState() == ServiceCritical); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, true, NotificationProblem); + + std::cout << "Third check result (ok)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceOK)); + BOOST_CHECK(service->GetState() == ServiceOK); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, true, NotificationRecovery); + + std::cout << "Fourth check result (critical)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceCritical)); + BOOST_CHECK(service->GetState() == ServiceCritical); + BOOST_CHECK(service->GetStateType() == StateTypeSoft); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, false); + + std::cout << "Fifth check result (ok)" << std::endl; + service->ProcessCheckResult(MakeCheckResult(ServiceOK)); + BOOST_CHECK(service->GetState() == ServiceOK); + BOOST_CHECK(service->GetStateType() == StateTypeHard); + BOOST_CHECK(service->GetCheckAttempt() == 1); + CheckNotification(service, false); + + c.disconnect(); +} + +BOOST_AUTO_TEST_CASE(service_3attempts) { boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2)); -- 2.40.0