Skip to content

Disallow triggering a cancelled downtime #9935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/icinga/downtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ std::set<Downtime::Ptr> Downtime::GetChildren() const

bool Downtime::CanBeTriggered()
{
if (!GetActive() || GetWasCancelled())
return false;

if (IsInEffect() && IsTriggered())
return false;

Expand Down Expand Up @@ -468,6 +471,8 @@ void Downtime::SetupCleanupTimer()

void Downtime::TriggerDowntime(double triggerTime)
{
ObjectLock oLock (this);

if (!CanBeTriggered())
return;

Expand All @@ -480,10 +485,9 @@ void Downtime::TriggerDowntime(double triggerTime)
SetTriggerTime(triggerTime);
}

{
ObjectLock olock (this);
SetupCleanupTimer();
}
SetupCleanupTimer();
OnDowntimeTriggered(this);
oLock.Unlock();

Array::Ptr triggers = GetTriggers();

Expand All @@ -498,8 +502,6 @@ void Downtime::TriggerDowntime(double triggerTime)
downtime->TriggerDowntime(triggerTime);
}
}

OnDowntimeTriggered(this);
}

void Downtime::SetRemovalInfo(const String& removedBy, double removeTime, const MessageOrigin::Ptr& origin) {
Expand Down
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()