Implement unit tests for state changes

fixes #12530
This commit is contained in:
Gunnar Beutner 2016-08-24 11:11:20 +02:00
parent 209697ea68
commit ae1ab5f865
20 changed files with 221 additions and 29 deletions

View File

@ -44,7 +44,7 @@ set(base_SOURCES
set_property(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/application-version.cpp PROPERTY EXCLUDE_UNITY_BUILD TRUE) set_property(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/application-version.cpp PROPERTY EXCLUDE_UNITY_BUILD TRUE)
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(base base_SOURCES) mkunity_target(base base base_SOURCES)
endif() endif()
add_library(base SHARED ${base_SOURCES}) add_library(base SHARED ${base_SOURCES})

View File

@ -22,7 +22,7 @@ set(checker_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(checker checker_SOURCES) mkunity_target(checker checker checker_SOURCES)
endif() endif()
add_library(checker SHARED ${checker_SOURCES}) add_library(checker SHARED ${checker_SOURCES})

View File

@ -32,7 +32,7 @@ set(cli_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(cli cli_SOURCES) mkunity_target(cli cli cli_SOURCES)
endif() endif()
add_library(cli SHARED ${cli_SOURCES}) add_library(cli SHARED ${cli_SOURCES})

View File

@ -27,7 +27,7 @@ set(compat_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(compat compat_SOURCES) mkunity_target(compat compat compat_SOURCES)
endif() endif()
add_library(compat SHARED ${compat_SOURCES}) add_library(compat SHARED ${compat_SOURCES})

View File

@ -41,7 +41,7 @@ set(config_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(config config_SOURCES) mkunity_target(config config config_SOURCES)
endif() endif()
add_library(config SHARED ${config_SOURCES}) add_library(config SHARED ${config_SOURCES})

View File

@ -29,7 +29,7 @@ set(db_ido_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(db_ido db_ido_SOURCES) mkunity_target(db_ido db_ido db_ido_SOURCES)
endif() endif()
add_library(db_ido SHARED ${db_ido_SOURCES}) add_library(db_ido SHARED ${db_ido_SOURCES})

View File

@ -25,7 +25,7 @@ if(MYSQL_FOUND)
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(db_ido_mysql db_ido_mysql_SOURCES) mkunity_target(db_ido_mysql db_ido_mysql db_ido_mysql_SOURCES)
endif() endif()
add_library(db_ido_mysql SHARED ${db_ido_mysql_SOURCES}) add_library(db_ido_mysql SHARED ${db_ido_mysql_SOURCES})

View File

@ -28,7 +28,7 @@ if(PostgreSQL_FOUND)
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(db_ido_pgsql db_ido_pgsql_SOURCES) mkunity_target(db_ido_pgsql db_ido_pgsql db_ido_pgsql_SOURCES)
endif() endif()
add_library(db_ido_pgsql SHARED ${db_ido_pgsql_SOURCES}) add_library(db_ido_pgsql SHARED ${db_ido_pgsql_SOURCES})

View File

@ -22,7 +22,7 @@ set(demo_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(demo demo_SOURCES) mkunity_target(demo demo demo_SOURCES)
endif() endif()
add_library(demo SHARED ${demo_SOURCES}) add_library(demo SHARED ${demo_SOURCES})

View File

@ -22,7 +22,7 @@ set(hello_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(hello hello_SOURCES) mkunity_target(hello hello hello_SOURCES)
endif() endif()
add_library(hello SHARED ${hello_SOURCES}) add_library(hello SHARED ${hello_SOURCES})

View File

@ -54,7 +54,7 @@ set(icinga_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(icinga icinga_SOURCES) mkunity_target(icinga icinga icinga_SOURCES)
endif() endif()
add_library(icinga SHARED ${icinga_SOURCES}) add_library(icinga SHARED ${icinga_SOURCES})

View File

@ -32,7 +32,7 @@ set(livestatus_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(livestatus livestatus_SOURCES) mkunity_target(livestatus livestatus livestatus_SOURCES)
endif() endif()
add_library(livestatus SHARED ${livestatus_SOURCES}) add_library(livestatus SHARED ${livestatus_SOURCES})

View File

@ -31,7 +31,7 @@ set(methods_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(methods methods_SOURCES) mkunity_target(methods methods methods_SOURCES)
endif() endif()
add_library(methods SHARED ${methods_SOURCES}) add_library(methods SHARED ${methods_SOURCES})

View File

@ -22,7 +22,7 @@ set(notification_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(notification notification_SOURCES) mkunity_target(notification notification notification_SOURCES)
endif() endif()
add_library(notification SHARED ${notification_SOURCES}) add_library(notification SHARED ${notification_SOURCES})

View File

@ -26,7 +26,7 @@ set(perfdata_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(perfdata perfdata_SOURCES) mkunity_target(perfdata perfdata perfdata_SOURCES)
endif() endif()
add_library(perfdata SHARED ${perfdata_SOURCES}) add_library(perfdata SHARED ${perfdata_SOURCES})

View File

@ -34,7 +34,7 @@ set(remote_SOURCES
) )
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(remote remote_SOURCES) mkunity_target(remote remote remote_SOURCES)
endif() endif()
add_library(remote SHARED ${remote_SOURCES}) add_library(remote SHARED ${remote_SOURCES})

View File

@ -22,20 +22,12 @@ set(base_test_SOURCES
base-json.cpp base-match.cpp base-netstring.cpp base-object.cpp base-json.cpp base-match.cpp base-netstring.cpp base-object.cpp
base-serialize.cpp base-shellescape.cpp base-stacktrace.cpp base-serialize.cpp base-shellescape.cpp base-stacktrace.cpp
base-stream.cpp base-string.cpp base-timer.cpp base-type.cpp base-stream.cpp base-string.cpp base-timer.cpp base-type.cpp
base-value.cpp config-ops.cpp icinga-macros.cpp base-value.cpp config-ops.cpp icinga-checkresult.cpp icinga-macros.cpp
icinga-perfdata.cpp base-test.cpp icinga-perfdata.cpp remote-base64.cpp remote-url.cpp
remote-base64.cpp remote-url.cpp
) )
set(livestatus_test_SOURCES
livestatus.cpp
livestatus-test.cpp
)
set_property(SOURCE test.cpp PROPERTY EXCLUDE_UNITY_BUILD TRUE)
if(ICINGA2_UNITY_BUILD) if(ICINGA2_UNITY_BUILD)
mkunity_target(test base_test_SOURCES) mkunity_target(base test base_test_SOURCES)
endif() endif()
add_boost_test(base add_boost_test(base
@ -97,6 +89,8 @@ add_boost_test(base
base_value/format base_value/format
config_ops/simple config_ops/simple
config_ops/advanced config_ops/advanced
icinga_checkresult/host
icinga_checkresult/service
icinga_macros/simple icinga_macros/simple
icinga_perfdata/empty icinga_perfdata/empty
icinga_perfdata/simple icinga_perfdata/simple
@ -116,6 +110,14 @@ add_boost_test(base
) )
if(ICINGA2_WITH_LIVESTATUS) if(ICINGA2_WITH_LIVESTATUS)
set(livestatus_test_SOURCES
livestatus.cpp
)
if(ICINGA2_UNITY_BUILD)
mkunity_target(livestatus test livestatus_test_SOURCES)
endif()
add_boost_test(livestatus add_boost_test(livestatus
SOURCES livestatus-test.cpp ${livestatus_test_SOURCES} SOURCES livestatus-test.cpp ${livestatus_test_SOURCES}
LIBRARIES base config icinga cli livestatus LIBRARIES base config icinga cli livestatus

View File

@ -20,6 +20,7 @@
#define BOOST_TEST_MAIN #define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE icinga2_test #define BOOST_TEST_MODULE icinga2_test
#include "icinga/icingaapplication.hpp"
#include "base/application.hpp" #include "base/application.hpp"
#include "base/timer.hpp" #include "base/timer.hpp"
#include <BoostTestTargetConfig.h> #include <BoostTestTargetConfig.h>
@ -28,9 +29,14 @@ using namespace icinga;
struct InitLibBase struct InitLibBase
{ {
IcingaApplication::Ptr appInst;
InitLibBase(void) InitLibBase(void)
{ {
Application::InitializeBase(); Application::InitializeBase();
appInst = new IcingaApplication();
static_pointer_cast<ConfigObject>(appInst)->OnConfigLoaded();
} }
~InitLibBase(void) ~InitLibBase(void)

184
test/icinga-checkresult.cpp Normal file
View File

@ -0,0 +1,184 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "icinga/host.hpp"
#include <boost/test/unit_test.hpp>
using namespace icinga;
BOOST_AUTO_TEST_SUITE(icinga_checkresult)
static CheckResult::Ptr MakeCheckResult(ServiceState state)
{
CheckResult::Ptr cr = new CheckResult();
cr->SetState(state);
double now = Utility::GetTime();
cr->SetScheduleStart(now);
cr->SetScheduleEnd(now);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
return cr;
}
static void NotificationHandler(const Checkable::Ptr& checkable, NotificationType type)
{
std::cout << "Notification triggered: " << Notification::NotificationTypeToString(type) << std::endl;
checkable->SetExtension("requested_notifications", true);
checkable->SetExtension("notification_type", type);
}
static void CheckNotification(const Checkable::Ptr& checkable, bool expected, NotificationType type = NotificationRecovery)
{
BOOST_CHECK((expected && checkable->GetExtension("requested_notifications").ToBool()) || (!expected && !checkable->GetExtension("requested_notifications").ToBool()));
if (expected && checkable->GetExtension("requested_notifications").ToBool())
BOOST_CHECK(checkable->GetExtension("notification_type") == type);
checkable->SetExtension("requested_notifications", false);
}
BOOST_AUTO_TEST_CASE(host)
{
boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
Host::Ptr host = new Host();
host->SetMaxCheckAttempts(3);
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() == StateTypeSoft);
BOOST_CHECK(host->GetCheckAttempt() == 2);
CheckNotification(host, false);
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);
std::cout << "Fifth 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 << "Sixth 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(service)
{
boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
Service::Ptr service = new Service();
service->SetMaxCheckAttempts(3);
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() == StateTypeSoft);
BOOST_CHECK(service->GetCheckAttempt() == 2);
CheckNotification(service, false);
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);
std::cout << "Fifth 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 << "Sixth 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_SUITE_END()

View File

@ -30,9 +30,9 @@ if(ICINGA2_UNITY_BUILD)
FOLDER Bin FOLDER Bin
) )
function(MKUNITY_TARGET Prefix UnityInputRef) function(MKUNITY_TARGET Target Prefix UnityInputRef)
set(UnityInput ${${UnityInputRef}}) set(UnityInput ${${UnityInputRef}})
set(UnityOutput ${CMAKE_CURRENT_BINARY_DIR}/${Prefix}_unity.cpp) set(UnityOutput ${CMAKE_CURRENT_BINARY_DIR}/${Target}_unity.cpp)
set(RealSources "") set(RealSources "")
set(UnitySources "") set(UnitySources "")
foreach(UnitySource ${UnityInput}) foreach(UnitySource ${UnityInput})