Skip to content

Commit

Permalink
Test Downtime#CanBeTriggered()
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Nov 22, 2023
1 parent b1d7ff8 commit 8a0fd98
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/icinga/downtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Downtime final : public ObjectImpl<Downtime>
bool IsInEffect() const;
bool IsTriggered() const;
bool IsExpired() const;
bool CanBeTriggered();
bool HasValidConfigOwner() const;

static void StaticInitialize();
Expand Down Expand Up @@ -86,8 +87,6 @@ class Downtime final : public ObjectImpl<Downtime>

Timer::Ptr m_CleanupTimer;

bool CanBeTriggered();

void SetupCleanupTimer();

static void DowntimesStartTimerHandler();
Expand Down
8 changes: 8 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions test/icinga-downtime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */

#include "base/utility.hpp"
#include "icinga/downtime.hpp"
#include <BoostTestTargetConfig.h>

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()

0 comments on commit 8a0fd98

Please sign in to comment.