From 8a0fd9898be3b59d351524b367a5858952768f8e Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 22 Nov 2023 12:15:51 +0100 Subject: [PATCH] Test Downtime#CanBeTriggered() --- lib/icinga/downtime.hpp | 3 +- test/CMakeLists.txt | 8 +++++ test/icinga-downtime.cpp | 70 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 test/icinga-downtime.cpp diff --git a/lib/icinga/downtime.hpp b/lib/icinga/downtime.hpp index 15aa0af5d3d..57288b1ab04 100644 --- a/lib/icinga/downtime.hpp +++ b/lib/icinga/downtime.hpp @@ -40,6 +40,7 @@ class Downtime final : public ObjectImpl bool IsInEffect() const; bool IsTriggered() const; bool IsExpired() const; + bool CanBeTriggered(); bool HasValidConfigOwner() const; static void StaticInitialize(); @@ -86,8 +87,6 @@ class Downtime final : public ObjectImpl Timer::Ptr m_CleanupTimer; - bool CanBeTriggered(); - void SetupCleanupTimer(); static void DowntimesStartTimerHandler(); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8919de304dc..668f5805baa 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -27,6 +27,7 @@ set(base_test_SOURCES config-ops.cpp icinga-checkresult.cpp icinga-dependencies.cpp + icinga-downtime.cpp icinga-legacytimeperiod.cpp icinga-macros.cpp icinga-notification.cpp @@ -135,6 +136,13 @@ add_boost_test(base icinga_checkresult/service_flapping_notification icinga_checkresult/suppressed_notification icinga_dependencies/multi_parent + icinga_downtime/canbetriggered_fixed + icinga_downtime/canbetriggered_flexible + icinga_downtime/canbetriggered_inactive + icinga_downtime/canbetriggered_removed + icinga_downtime/canbetriggered_triggered + icinga_downtime/canbetriggered_expired + icinga_downtime/canbetriggered_tooearly icinga_notification/strings icinga_notification/state_filter icinga_notification/type_filter diff --git a/test/icinga-downtime.cpp b/test/icinga-downtime.cpp new file mode 100644 index 00000000000..b2ffa3c9907 --- /dev/null +++ b/test/icinga-downtime.cpp @@ -0,0 +1,70 @@ +/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */ + +#include "base/utility.hpp" +#include "icinga/downtime.hpp" +#include + +using namespace icinga; + +static void CanBeTriggeredHelper( + bool active, bool fixed, double relStart, double relEnd, double duration, Value relTriggered, Value relRemoved, bool canBeTriggered +) +{ + Downtime::Ptr dt = new Downtime(); + auto now (Utility::GetTime()); + + dt->SetActive(active, true); + dt->SetFixed(fixed, true); + dt->SetStartTime(now + relStart, true); + dt->SetEndTime(now + relEnd, true); + dt->SetDuration(duration, true); + + if (!relTriggered.IsEmpty()) { + dt->SetTriggerTime(now + relTriggered, true); + } + + if (!relRemoved.IsEmpty()) { + dt->SetRemoveTime(now + relRemoved, true); + } + + BOOST_CHECK(dt->CanBeTriggered() == canBeTriggered); +} + +BOOST_AUTO_TEST_SUITE(icinga_downtime) + +BOOST_AUTO_TEST_CASE(canbetriggered_fixed) +{ + CanBeTriggeredHelper(true, true, -2, 8, 0, Empty, Empty, true); +} + +BOOST_AUTO_TEST_CASE(canbetriggered_flexible) +{ + CanBeTriggeredHelper(true, false, -2, 8, 20, Empty, Empty, true); +} + +BOOST_AUTO_TEST_CASE(canbetriggered_inactive) +{ + CanBeTriggeredHelper(false, true, -2, 8, 0, Empty, Empty, false); +} + +BOOST_AUTO_TEST_CASE(canbetriggered_removed) +{ + CanBeTriggeredHelper(true, true, -2, 8, 0, Empty, -4, false); +} + +BOOST_AUTO_TEST_CASE(canbetriggered_triggered) +{ + CanBeTriggeredHelper(true, true, -2, 8, 0, -1, Empty, false); +} + +BOOST_AUTO_TEST_CASE(canbetriggered_expired) +{ + CanBeTriggeredHelper(true, true, -12, -2, 0, Empty, Empty, false); +} + +BOOST_AUTO_TEST_CASE(canbetriggered_tooearly) +{ + CanBeTriggeredHelper(true, true, 2, 12, 0, Empty, Empty, false); +} + +BOOST_AUTO_TEST_SUITE_END()