From fb46d312e52a5f282d0d199110a7af528fc4bdf6 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 12 Jun 2024 18:17:44 +0200 Subject: [PATCH] Add advanced timeperiod range,include/exclude test cases --- test/CMakeLists.txt | 2 + test/icinga-legacytimeperiod.cpp | 148 +++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a4066e2cb5a..cff86ff8df5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -146,6 +146,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 diff --git a/test/icinga-legacytimeperiod.cpp b/test/icinga-legacytimeperiod.cpp index 132ffd233d8..781913e4dea 100644 --- a/test/icinga-legacytimeperiod.cpp +++ b/test/icinga-legacytimeperiod.cpp @@ -257,6 +257,154 @@ 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:00 - 24:00:00 UTC + + BOOST_CHECK_EQUAL(true, tp->IsInside(1718200800)); // 2024-06-12 14:00:00 UTC + { + Array::Ptr segments = tp->GetSegments(); + 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:00 - 24:00:00 UTC + + BOOST_CHECK_EQUAL(false, tp->IsInside(1718287200)); // 2024-06-13 14:00:00 UTC + { + Array::Ptr segments = tp->GetSegments(); + 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", "10:00-12:00"}}), true); + tp->UpdateRegion(1718150400, 1718236800, true); // 2024-06-12 00:00:00 - 24:00:00 UTC + + BOOST_CHECK_EQUAL(true, tp->IsInside(1718190000)); // 2024-06-12 11:00:00 UTC + { + Array::Ptr segments = tp->GetSegments(); + 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:00 - 24:00:00 UTC + + BOOST_CHECK_EQUAL(false, tp->IsInside(1718287200)); // 2024-06-13 14:00:00 UTC + BOOST_CHECK_EQUAL(0, tp->GetSegments()->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:00 - 2024-06-13 24:00:00 UTC + + BOOST_CHECK_EQUAL(1, excludedTp->GetSegments()->GetLength()); + BOOST_CHECK_EQUAL(true, excludedTp->IsInside(1718114400)); // 2024-06-11 14:00:00 UTC + 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(1718064000, 1718323200, true); // 2024-06-11 00:00:00 - 2024-06-13 24:00:00 UTC + + BOOST_CHECK_EQUAL(false, tp->IsInside(1718114400)); // 2024-06-11 14: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(); + // The updated region is 2024-06-11 - 13, so there should only be 2 segements, when the excludes works correctly. + BOOST_REQUIRE_EQUAL(2, 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 + + BOOST_CHECK_EQUAL(true, tp->IsInside(segment->Get("begin"))); + BOOST_CHECK_EQUAL(false, tp->IsInside(segment->Get("end"))); + + 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(true, tp->IsInside(segment->Get("begin"))); + BOOST_CHECK_EQUAL(false, tp->IsInside(segment->Get("end"))); + + BOOST_CHECK_EQUAL(false, excludedTp->IsInside(segment->Get("begin"))); + BOOST_CHECK_EQUAL(false, excludedTp->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:00 - 2024-06-13 24:00:00 UTC + + BOOST_CHECK_EQUAL(1, includedTp->GetSegments()->GetLength()); + BOOST_CHECK_EQUAL(true, includedTp->IsInside(1718114400)); // 2024-06-11 14:00:00 UTC + 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:00 - 2024-06-13 24:00:00 UTC + { + Array::Ptr segments = tp->GetSegments(); + // The updated region is 2024-06-11 - 13, so there should be 3 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 + + BOOST_CHECK_EQUAL(true, tp->IsInside(segment->Get("begin"))); + BOOST_CHECK_EQUAL(true, includedTp->IsInside(segment->Get("begin"))); + BOOST_CHECK_EQUAL(false, tp->IsInside(segment->Get("end"))); + BOOST_CHECK_EQUAL(false, includedTp->IsInside(segment->Get("end"))); + } +} + struct DateTime { struct {