Skip to content

Commit

Permalink
Add advanced timeperiod range,include/exclude test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Jun 13, 2024
1 parent 4616938 commit 5734e4d
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ add_boost_test(base
icinga_macros/simple
icinga_legacytimeperiod/simple
icinga_legacytimeperiod/is_in_range
icinga_legacytimeperiod/out_of_range_segments
icinga_legacytimeperiod/include_exclude_timeperiods
icinga_legacytimeperiod/advanced
icinga_legacytimeperiod/dst
icinga_legacytimeperiod/dst_isinside
Expand Down
157 changes: 157 additions & 0 deletions test/icinga-legacytimeperiod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,163 @@ BOOST_AUTO_TEST_CASE(is_in_range)
BOOST_CHECK_EQUAL(true, LegacyTimePeriod::IsInTimeRange(&tm_beg, &tm_end, 2, &reference));
}

BOOST_AUTO_TEST_CASE(out_of_range_segments)
{
TimePeriod::Ptr tp = new TimePeriod();
tp->SetUpdate(new Function("LegacyTimePeriod", LegacyTimePeriod::ScriptFunc, {"tp", "begin", "end"}), true);

// A single day range shouldn't span to the following day too (see https://github.com/Icinga/icinga2/issues/9388).
tp->SetRanges(new Dictionary({{"2024-06-12", "00:00-24:00"}}), true);
tp->UpdateRegion(1718150400, 1718236800, true); // 2024-06-12 00:00 - 24:00:00 UTC

BOOST_CHECK_EQUAL(true, tp->IsInside(1718200800)); // 2024-06-12 14:00:00 UTC
{
Array::Ptr segments = tp->GetSegments();
ObjectLock olock(segments);
BOOST_REQUIRE_EQUAL(1, segments->GetLength());

Dictionary::Ptr segment = segments->Get(0);
BOOST_CHECK_EQUAL(1718150400, segment->Get("begin")); // 2024-06-12 00:00:00 UTC
BOOST_CHECK_EQUAL(1718236800, segment->Get("end")); // 2024-06-12 24:00:00 UTC
}
tp->UpdateRegion(1718236800, 1718323200, true); // 2024-06-13 00:00 - 24:00:00 UTC

BOOST_CHECK_EQUAL(false, tp->IsInside(1718287200)); // 2024-06-13 14:00:00 UTC
{
Array::Ptr segments = tp->GetSegments();
ObjectLock olock(segments);
BOOST_CHECK_EQUAL(0, segments->GetLength()); // There should be no segments at all!
}

// One partially day range shouldn't contain more than a single segment (see https://github.com/Icinga/icinga2/issues/9781).
tp->SetRanges(new Dictionary({{"2024-06-12 - 2024-06-12", "10:00-12:00"}}), true);
tp->UpdateRegion(1718150400, 1718236800, true); // 2024-06-12 00:00 - 24:00:00 UTC

BOOST_CHECK_EQUAL(true, tp->IsInside(1718190000)); // 2024-06-12 12:00:00 UTC
{
Array::Ptr segments = tp->GetSegments();
ObjectLock olock(segments);
BOOST_REQUIRE_EQUAL(1, segments->GetLength());

Dictionary::Ptr segment = segments->Get(0);
BOOST_CHECK_EQUAL(1718186400, segment->Get("begin")); // 2024-06-12 10:00:00 UTC (range start date)
BOOST_CHECK_EQUAL(1718193600, segment->Get("end")); // 2024-06-12 12:00:00 UTC (range end date)
}
tp->UpdateRegion(1718236800, 1718323200, true); // 2024-06-13 00:00 - 24:00:00 UTC

BOOST_CHECK_EQUAL(false, tp->IsInside(1718287200)); // 2024-06-13 14:00:00 UTC
{
Array::Ptr segments = tp->GetSegments();
ObjectLock olock(segments);
BOOST_CHECK_EQUAL(0, segments->GetLength()); // There should be no segments at all!
}
}

BOOST_AUTO_TEST_CASE(include_exclude_timeperiods)
{
Function::Ptr update = new Function("LegacyTimePeriod", LegacyTimePeriod::ScriptFunc, {"tp", "begin", "end"});
TimePeriod::Ptr excludedTp = new TimePeriod();
excludedTp->SetName("excluded", true);
excludedTp->SetUpdate(update, true);
excludedTp->SetRanges(new Dictionary({{"2024-06-11", "00:00-24:00"}}), true);

excludedTp->UpdateRegion(1718064000, 1718323200, true); // 2024-06-11 00:00 - 2024-06-13 24:00:00 UTC

BOOST_CHECK_EQUAL(1, excludedTp->GetSegments()->GetLength());
BOOST_CHECK_EQUAL(false, excludedTp->IsInside(1718200800)); // 2024-06-12 14:00:00 UTC
BOOST_CHECK_EQUAL(false, excludedTp->IsInside(1718287200)); // 2024-06-13 14:00:00 UTC

// Register the excluded timeperiod to make it globally visible.
excludedTp->Register();

Dictionary::Ptr ranges = new Dictionary({
{"2024-06-11", "09:00-17:00"},
{"2024-06-12", "09:00-17:00"},
{"2024-06-13", "09:00-17:00"}
});

TimePeriod::Ptr tp = new TimePeriod();
tp->SetExcludes(new Array({"excluded"}), true);
tp->SetUpdate(update, true);
tp->SetRanges(ranges, true);
tp->UpdateRegion(1718150400, 1718323200, true); // 2024-06-12 00:00 - 2024-06-13 24:00:00 UTC

BOOST_CHECK_EQUAL(false, tp->IsInside(1718150400)); // 2024-06-12 00:00:00 UTC
BOOST_CHECK_EQUAL(true, tp->IsInside(1718200800)); // 2024-06-12 14:00:00 UTC
BOOST_CHECK_EQUAL(false, tp->IsInside(1718323200)); // 2024-06-13 00:00:00 UTC
BOOST_CHECK_EQUAL(true, tp->IsInside(1718287200)); // 2024-06-13 14:00:00 UTC
{
Array::Ptr segments = tp->GetSegments();
ObjectLock olock(segments);
BOOST_REQUIRE_EQUAL(2, segments->GetLength()); // The updated region is 2024-06-12 - 13, so there should be only 2 segements.

Dictionary::Ptr segment = segments->Get(0);
BOOST_CHECK_EQUAL(1718182800, segment->Get("begin")); // 2024-06-12 09:00:00 UTC
BOOST_CHECK_EQUAL(1718211600, segment->Get("end")); // 2024-06-12 17:00:00 UTC

BOOST_CHECK_EQUAL(false, excludedTp->IsInside(segment->Get("begin")));
BOOST_CHECK_EQUAL(false, excludedTp->IsInside(segment->Get("end")));

segment = segments->Get(1);
BOOST_CHECK_EQUAL(1718269200, segment->Get("begin")); // 2024-06-13 09:00:00 UTC
BOOST_CHECK_EQUAL(1718298000, segment->Get("end")); // 2024-06-13 17:00:00 UTC

BOOST_CHECK_EQUAL(false, excludedTp->IsInside(segment->Get("begin")));
BOOST_CHECK_EQUAL(false, excludedTp->IsInside(segment->Get("end")));
}

tp->UpdateRegion(1718064000, 1718323200, true); // 2024-06-11 00:00 - 2024-06-13 24:00:00 UTC
{
Array::Ptr segments = tp->GetSegments();
ObjectLock olock(segments);
// The updated region is 2024-06-11 - 13, so there should still be 2 segements, when the excludes works correctly.
BOOST_REQUIRE_EQUAL(2, segments->GetLength());

Dictionary::Ptr segment = segments->Get(1);
BOOST_CHECK_EQUAL(1718269200, segment->Get("begin")); // 2024-06-13 09:00:00 UTC
BOOST_CHECK_EQUAL(1718298000, segment->Get("end")); // 2024-06-13 17:00:00 UTC

BOOST_CHECK_EQUAL(false, tp->IsInside(segment->Get("begin")));
BOOST_CHECK_EQUAL(false, tp->IsInside(segment->Get("end")));
}

// Include timeperiod test cases ...
TimePeriod::Ptr includedTp = new TimePeriod();
includedTp->SetName("included", true);
includedTp->SetUpdate(update, true);
includedTp->SetRanges(new Dictionary({{"2024-06-11", "08:00-17:00"}}), true);

includedTp->UpdateRegion(1718064000, 1718323200, true); // 2024-06-11 00:00 - 2024-06-13 24:00:00 UTC

BOOST_CHECK_EQUAL(1, includedTp->GetSegments()->GetLength());
BOOST_CHECK_EQUAL(false, includedTp->IsInside(1718200800)); // 2024-06-12 14:00:00 UTC
BOOST_CHECK_EQUAL(false, includedTp->IsInside(1718287200)); // 2024-06-13 14:00:00 UTC

// Register the timeperiod to make it globally visible.
includedTp->Register();

tp->SetIncludes(new Array({"included"}), true);
tp->UpdateRegion(1718064000, 1718323200, true); // 2024-06-11 00:00 - 2024-06-13 24:00:00 UTC
{
Array::Ptr segments = tp->GetSegments();
ObjectLock olock(segments);
// The updated region is 2024-06-11 - 13, so there should still be 2 segements, when the prefer includes works correctly.
BOOST_REQUIRE_EQUAL(3, segments->GetLength());

Dictionary::Ptr segment = segments->Get(0);
BOOST_CHECK_EQUAL(1718182800, segment->Get("begin")); // 2024-06-12 09:00:00 UTC
BOOST_CHECK_EQUAL(1718211600, segment->Get("end")); // 2024-06-12 17:00:00 UTC

segment = segments->Get(1);
BOOST_CHECK_EQUAL(1718269200, segment->Get("begin")); // 2024-06-13 09:00:00 UTC
BOOST_CHECK_EQUAL(1718298000, segment->Get("end")); // 2024-06-13 17:00:00 UTC

segment = segments->Get(2);
BOOST_CHECK_EQUAL(1718092800, segment->Get("begin")); // 2024-06-11 08:00:00 UTC
BOOST_CHECK_EQUAL(1718125200, segment->Get("end")); // 2024-06-11 17:00:00 UTC
}
}

struct DateTime
{
struct {
Expand Down

0 comments on commit 5734e4d

Please sign in to comment.