From 7665143afa500dd589546665124293b9c1206265 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 3 Jul 2019 15:40:43 +0200 Subject: [PATCH] Tests: Rewrite TimePeriod tests using Boost::DateTime fixes #7257 --- lib/icinga/legacytimeperiod.cpp | 4 +- test/icinga-legacytimeperiod.cpp | 256 ++++++++++++++++++++++--------- tools/win32/test.ps1 | 2 +- 3 files changed, 186 insertions(+), 76 deletions(-) diff --git a/lib/icinga/legacytimeperiod.cpp b/lib/icinga/legacytimeperiod.cpp index 2ae72e651..1275ae351 100644 --- a/lib/icinga/legacytimeperiod.cpp +++ b/lib/icinga/legacytimeperiod.cpp @@ -170,7 +170,7 @@ void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end, begin->tm_min = 0; begin->tm_sec = 0; - /* Negative days are relative to the next month. */ + /* day -1: Negative days are relative to the next month. */ if (mday < 0) { begin->tm_mday = mday * -1 - 1; begin->tm_mon++; @@ -185,7 +185,7 @@ void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end, end->tm_min = 0; end->tm_sec = 0; - /* Negative days are relative to the next month. */ + /* day -1: Negative days are relative to the next month. */ if (mday < 0) { end->tm_mday = mday * -1 - 1; end->tm_mon++; diff --git a/test/icinga-legacytimeperiod.cpp b/test/icinga-legacytimeperiod.cpp index 45babcee3..f27a56477 100644 --- a/test/icinga-legacytimeperiod.cpp +++ b/test/icinga-legacytimeperiod.cpp @@ -2,6 +2,11 @@ #include "base/utility.hpp" #include "icinga/legacytimeperiod.hpp" +#include +#include +#include +#include +#include #include using namespace icinga; @@ -44,105 +49,210 @@ BOOST_GLOBAL_FIXTURE(GlobalTimezoneFixture); BOOST_AUTO_TEST_CASE(simple) { - tm beg, end, ref; + tm tm_beg, tm_end, tm_ref; + String timestamp; + boost::posix_time::ptime begin; + boost::posix_time::ptime end; + boost::posix_time::ptime expectedBegin; + boost::posix_time::ptime expectedEnd; + //----------------------------------------------------- // check parsing of "YYYY-MM-DD" specs - LegacyTimePeriod::ParseTimeSpec("2016-01-01", &beg, &end, &ref); - BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1451606400); - BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1451692800); + timestamp = "2016-01-01"; - LegacyTimePeriod::ParseTimeSpec("2015-12-31", &beg, &end, &ref); - BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1451520000); - BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1451606400); + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2016, 1, 1), boost::posix_time::time_duration(0, 0, 0)); - BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-12-32", &beg, &end, &ref), + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2016, 1, 2), boost::posix_time::time_duration(0, 0, 0)); + + // Run test + LegacyTimePeriod::ParseTimeSpec(timestamp, &tm_beg, &tm_end, &tm_ref); + + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); + + //----------------------------------------------------- + timestamp = "2015-12-31"; + + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2015, 12, 31), boost::posix_time::time_duration(0, 0, 0)); + + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2016, 1, 1), boost::posix_time::time_duration(0, 0, 0)); + + // Run test + LegacyTimePeriod::ParseTimeSpec(timestamp, &tm_beg, &tm_end, &tm_ref); + + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); + + //----------------------------------------------------- + // Break things forcefully + BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-12-32", &tm_beg, &tm_end, &tm_ref), std::invalid_argument); - BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-28-01", &beg, &end, &ref), + BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-28-01", &tm_beg, &tm_end, &tm_ref), std::invalid_argument); + //----------------------------------------------------- // check parsing of "day X" and "day -X" specs - ref.tm_year = 2016 - 1900; - ref.tm_mon = 1; - LegacyTimePeriod::ParseTimeSpec("day 2", &beg, &end, &ref); - BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1454371200); // 2016-02-02 - BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1454457600); // 2016-02-03 + timestamp = "day 2"; + tm_ref.tm_year = 2016 - 1900; + tm_ref.tm_mon = 2 - 1; - ref.tm_year = 2018 - 1900; - ref.tm_mon = 11; - LegacyTimePeriod::ParseTimeSpec("day 31", &beg, &end, &ref); - BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1546214400); // 2018-12-31 - BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1546300800); // 2019-01-01 + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2016, 2, 2), boost::posix_time::time_duration(0, 0, 0)); - ref.tm_year = 2012 - 1900; - ref.tm_mon = 6; - LegacyTimePeriod::ParseTimeSpec("day -1", &beg, &end, &ref); - BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1343692800); // 2012-07-31 - BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1343779200); // 2012-08-01 + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2016, 2, 3), boost::posix_time::time_duration(0, 0, 0)); - ref.tm_year = 2016 - 1900; // leap year - ref.tm_mon = 1; - LegacyTimePeriod::ParseTimeSpec("day -1", &beg, &end, &ref); - BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1456704000); // 2016-02-29 - BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1456790400); // 2016-03-01 + // Run Tests + LegacyTimePeriod::ParseTimeSpec(timestamp, &tm_beg, &tm_end, &tm_ref); + + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); + + //----------------------------------------------------- + timestamp = "day 31"; + tm_ref.tm_year = 2018 - 1900; + tm_ref.tm_mon = 12 - 1; + + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2018, 12, 31), boost::posix_time::time_duration(0, 0, 0)); + + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2019, 1, 1), boost::posix_time::time_duration(0, 0, 0)); + + // Run Tests + LegacyTimePeriod::ParseTimeSpec(timestamp, &tm_beg, &tm_end, &tm_ref); + + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); + + //----------------------------------------------------- + // Last day of the month + timestamp = "day -1"; + tm_ref.tm_year = 2012 - 1900; + tm_ref.tm_mon = 7 - 1; + + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2012, 7, 31), boost::posix_time::time_duration(0, 0, 0)); + + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2012, 8, 1), boost::posix_time::time_duration(0, 0, 0)); + + // Run Tests + LegacyTimePeriod::ParseTimeSpec(timestamp, &tm_beg, &tm_end, &tm_ref); + + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); + + //----------------------------------------------------- + // Leap year with the last day of the month + timestamp = "day -1"; + tm_ref.tm_year = 2016 - 1900; // leap year + tm_ref.tm_mon = 2 - 1; + + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2016, 2, 29), boost::posix_time::time_duration(0, 0, 0)); + + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2016, 3, 1), boost::posix_time::time_duration(0, 0, 0)); + + // Run Tests + LegacyTimePeriod::ParseTimeSpec("day -1", &tm_beg, &tm_end, &tm_ref); + + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); } -BOOST_AUTO_TEST_CASE(advanced) { - tm beg, end, ref; - time_t ts_beg, ts_end, ts_beg_exp, ts_end_exp; +BOOST_AUTO_TEST_CASE(advanced) +{ + tm tm_beg, tm_end, tm_ref; + String timestamp; + boost::posix_time::ptime begin; + boost::posix_time::ptime end; + boost::posix_time::ptime expectedBegin; + boost::posix_time::ptime expectedEnd; - //2019-05-06 where Icinga celebrates 10 years #monitoringlove - String timestamp = "22:00-06:00"; - ref.tm_year = 2019 - 1900; - ref.tm_mon = 5 - 1; - ref.tm_mday = 6; + //----------------------------------------------------- + // 2019-05-06 where Icinga celebrates 10 years #monitoringlove + // 2019-05-06 22:00:00 - 2019-05-07 06:00:00 + timestamp = "22:00-06:00"; + tm_ref.tm_year = 2019 - 1900; + tm_ref.tm_mon = 5 - 1; + tm_ref.tm_mday = 6; - LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &ref, &beg, &end); - ts_beg = mktime(&beg); - ts_end = mktime(&end); - ts_beg_exp = 1557180000; // 2019-05-06 22:00:00 - ts_end_exp = 1557208800; // 2019-05-07 06:00:00 + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2019, 5, 6), boost::posix_time::time_duration(22, 0, 0)); - BOOST_CHECK_EQUAL(ts_beg, ts_beg_exp); - BOOST_TEST_MESSAGE("Begin date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg_exp)); - BOOST_CHECK_EQUAL(ts_end, ts_end_exp); - BOOST_TEST_MESSAGE("End date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end_exp)); + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2019, 5, 7), boost::posix_time::time_duration(6, 0, 0)); + // Run test + LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &tm_ref, &tm_beg, &tm_end); - //2019-05-06 Icinga is unleashed. + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); + + //----------------------------------------------------- + // 2019-05-06 Icinga is unleashed. + // 09:00:00 - 17:00:00 timestamp = "09:00-17:00"; - ref.tm_year = 2009 - 1900; - ref.tm_mon = 5 - 1; - ref.tm_mday = 6; + tm_ref.tm_year = 2009 - 1900; + tm_ref.tm_mon = 5 - 1; + tm_ref.tm_mday = 6; - LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &ref, &beg, &end); - ts_beg = mktime(&beg); - ts_end = mktime(&end); - ts_beg_exp = 1241600400; // 2009-05-06 09:00:00 - ts_end_exp = 1241629200; // 2009-05-06 17:00:00 + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2009, 5, 6), boost::posix_time::time_duration(9, 0, 0)); - BOOST_CHECK_EQUAL(ts_beg, ts_beg_exp); - BOOST_TEST_MESSAGE("Begin date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg_exp)); - BOOST_CHECK_EQUAL(ts_end, ts_end_exp); - BOOST_TEST_MESSAGE("End date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end_exp)); + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2009, 5, 6), boost::posix_time::time_duration(17, 0, 0)); - //At our first Icinga Camp in SFO 2014 at GitHub HQ, we partied all night long with an overflow. + // Run test + LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &tm_ref, &tm_beg, &tm_end); + + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); + + //----------------------------------------------------- + // At our first Icinga Camp in SFO 2014 at GitHub HQ, we partied all night long with an overflow. + // 2014-09-24 09:00:00 - 2014-09-25 06:00:00 timestamp = "09:00-30:00"; - ref.tm_year = 2014 - 1900; - ref.tm_mon = 9 - 1; - ref.tm_mday = 24; + tm_ref.tm_year = 2014 - 1900; + tm_ref.tm_mon = 9 - 1; + tm_ref.tm_mday = 24; - LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &ref, &beg, &end); - ts_beg = mktime(&beg); - ts_end = mktime(&end); - ts_beg_exp = 1411549200; // 2014-09-24 09:00:00 - ts_end_exp = 1411624800; // 2014-09-25 06:00:00 + expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2014, 9, 24), boost::posix_time::time_duration(9, 0, 0)); - BOOST_CHECK_EQUAL(ts_beg, ts_beg_exp); - BOOST_TEST_MESSAGE("Begin date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_beg_exp)); - BOOST_CHECK_EQUAL(ts_end, ts_end_exp); - BOOST_TEST_MESSAGE("End date: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end) << " expected " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", ts_end_exp)); + expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2014, 9, 25), boost::posix_time::time_duration(6, 0, 0)); - } + // Run test + LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &tm_ref, &tm_beg, &tm_end); + + // Compare times + begin = boost::posix_time::ptime_from_tm(tm_beg); + end = boost::posix_time::ptime_from_tm(tm_end); + + BOOST_CHECK_EQUAL(begin, expectedBegin); + BOOST_CHECK_EQUAL(end, expectedEnd); +} BOOST_AUTO_TEST_SUITE_END() diff --git a/tools/win32/test.ps1 b/tools/win32/test.ps1 index 3d3c12540..d7ad90ce2 100644 --- a/tools/win32/test.ps1 +++ b/tools/win32/test.ps1 @@ -24,7 +24,7 @@ if (-not ($env:PATH -contains $env:CMAKE_PATH)) { cd "$env:ICINGA2_BUILDPATH" -ctest.exe -C "${env:CMAKE_BUILD_TYPE}" -T test -O $env:ICINGA2_BUILDPATH/Test.xml --output-on-failure +ctest.exe -C "${env:CMAKE_BUILD_TYPE}" -T test -O $env:ICINGA2_BUILDPATH/Test.xml --output-on-failure --log_level=all if ($lastexitcode -ne 0) { cd .. exit $lastexitcode