[icinga-checkins] icinga.org: icinga2/master: Implement unit tests for state changes

git at icinga.org git at icinga.org
Wed Aug 24 11:51:25 CEST 2016


Module: icinga2
Branch: master
Commit: a7c7a53c3e1ea3fc7996c65d0446168ce2fd34bd
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=a7c7a53c3e1ea3fc7996c65d0446168ce2fd34bd

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Wed Aug 24 11:11:20 2016 +0200

Implement unit tests for state changes

fixes #12530

---

 test/CMakeLists.txt         |    4 +-
 test/base-test.cpp          |    6 ++
 test/icinga-checkresult.cpp |  184 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 193 insertions(+), 1 deletion(-)

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index e68bdf5..8e5caa2 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -22,7 +22,7 @@ set(base_test_SOURCES
   base-json.cpp base-match.cpp base-netstring.cpp base-object.cpp
   base-serialize.cpp base-shellescape.cpp base-stacktrace.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 
   remote-base64.cpp remote-url.cpp
 )
@@ -97,6 +97,8 @@ add_boost_test(base
         base_value/format
         config_ops/simple
         config_ops/advanced
+        icinga_checkresult/host
+        icinga_checkresult/service
         icinga_macros/simple
         icinga_perfdata/empty
         icinga_perfdata/simple
diff --git a/test/base-test.cpp b/test/base-test.cpp
index 77a5a71..0ed1d89 100644
--- a/test/base-test.cpp
+++ b/test/base-test.cpp
@@ -20,6 +20,7 @@
 #define BOOST_TEST_MAIN
 #define BOOST_TEST_MODULE icinga2_test
 
+#include "icinga/icingaapplication.hpp"
 #include "base/application.hpp"
 #include "base/timer.hpp"
 #include <BoostTestTargetConfig.h>
@@ -28,9 +29,14 @@ using namespace icinga;
 
 struct InitLibBase
 {
+	IcingaApplication::Ptr appInst;
+
 	InitLibBase(void)
 	{
 		Application::InitializeBase();
+
+		appInst = new IcingaApplication();
+		static_pointer_cast<ConfigObject>(appInst)->OnConfigLoaded();
 	}
 
 	~InitLibBase(void)
diff --git a/test/icinga-checkresult.cpp b/test/icinga-checkresult.cpp
new file mode 100644
index 0000000..db6494f
--- /dev/null
+++ b/test/icinga-checkresult.cpp
@@ -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()



More information about the icinga-checkins mailing list