393 Commits

Author SHA1 Message Date
Yonas Habteab
b462028b4f Add basic unittests for bulk group registration 2025-03-19 16:28:00 +01:00
Yonas Habteab
6a0ec70131 Fix & adjust dependencies unittests 2025-03-19 16:28:00 +01:00
Yonas Habteab
2616c99891 tests: Add unittests for the redundancy groups registry 2025-03-12 16:32:01 +01:00
Yonas Habteab
ff0dabe287 Checkable: Store dependencies grouped by their redundancy group 2025-03-12 16:31:59 +01:00
Julian Brost
784867b3f7 Avoid undefined behavior in string/vector_move test
vec[1] is equivalent to vec[vec.size()] at that point and thus not a valid
element of the vector, making the use of operator[] undefined behavior here.
With some compiler flags (like those used in package builds on RHEL and
similar), the compiler (rightfully) aborts the program on this out of bounds
access:

     68/178 Test  #68: base-base_string/vector_move ............................................***Failed    0.01 sec
    /usr/include/c++/14/bits/stl_vector.h:1130: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = icinga::String; _Alloc = std::allocator<icinga::String>; reference = icinga::String&; size_type = long unsigned int]: Assertion '__n < this->size()' failed.
    Running 1 test case...
    unknown location(0): fatal error: in "base_string/vector_move": signal: SIGABRT (application abort requested)
    /builds/packages/icinga2/packaging/fedora/41/BUILD/icinga2-2.14.5+467.g206d7cda1-build/icinga2-2.14.5+467.g206d7cda1/test/base-string.cpp(120): last checkpoint
    *** 1 failure is detected in the test module "icinga2"

This commit fixes this by taking the indirection through .data() and using
plain pointer arithmetic instead.
2025-03-10 09:28:33 +01:00
Yonas Habteab
3e9292a349 Value: Add a specialized rvalue reference of Get()
The move `String(Value&&)` constructor tries to partially move `String`
values from a `Value` type. However, since there was no an appropriate
`Value::Get<T>()` implementation that binds to the requested move
operation, the compiler will actually not move the value but copy it
instead as the only available implementation of `Value::Get<T>()`
returns a const reference `const T&`. This commit adds a new overload
that returns a non-const reference and allows to optionally move the string
value of a Value type.
2025-03-07 10:16:31 +01:00
Julian Brost
e308552ecc Add test that std::vector<icinga::String> uses move overloads 2025-03-06 13:02:40 +01:00
Julian Brost
f1f10fdd9e tests: fix FormatDateTime with 32-bit time_t
With a 32-bit time_t, two checks in the FormatDateTime test case didn't work
properly so far:

1. Every time_t value can be represented by struct tm, hence the test makes no
   sense on such platforms and is now disabled there similar to how it's
   already done with other checks in the same function.
2. std::nextafter(2147483647, +double_limit::infinity())) results in something
   like 2147483647.000000238 which simply results in the limit when cast back
   to an integer type, so it didn't actually test the overflow. This is fixed
   by an additional std::ceil()/std::floor().
2025-02-05 11:21:15 +01:00
Lorenz Kästle
e7381193c8
Reject infinite performance data values
Some fault monitoring plugins may return "inf" or "-inf" as
values due to a failure to initialize or other errors.

This patch introduces a check on whether the parse value is infinite
(or negative infinite) and rejects the data point if that is the case.

The reasoning here is: There is no possible way a value of "inf" is ever
a true measuring or even useful. Furthermore, when passed to the
performance data writers, it may be rejected by the backend and lead
to further complications.
2025-01-09 11:46:34 +01:00
Alexander A. Klimov
27e0e236cb Move Timeout instances from heap to stack 2025-01-07 18:20:50 +01:00
Alexander A. Klimov
959b162913 Timeout#~Timeout(), #Cancel(): support boost::asio::io_context running on multiple threads 2025-01-07 18:19:42 +01:00
Alexander A. Klimov
cb51649363 Timeout#Timeout(): drop unnecessary template parameters 2025-01-07 18:19:39 +01:00
Alexander A. Klimov
8cdbea303b Test Timeout 2025-01-07 18:18:39 +01:00
Yonas Habteab
eb97676d69 Add basic test cases for Type::GetConfigTypesSortedByLoadDependencies() 2024-09-20 16:18:12 +02:00
Yonas Habteab
b8932e67fc tests: Fix test FormatDateTime with invalid formats on macOS/*BSD 2024-09-10 17:11:20 +02:00
Julian Brost
39ae2e8ca4 Utility::FormatDateTime(): provide an overload for tm*
This allows the function to be used both with a double timestamp or a pointer
to a tm struct. With this, a similar implementation inside the tests can simply
use our regular function.
2024-08-23 12:48:50 +02:00
Julian Brost
d5b3ffaa6d Utility::FormatDateTime(): handle invalid format strings on Windows
On Windows, the strftime() function family invokes an invalid parameter handler
when the format string is invalid (see the "Remarks" section in their
documentation). std::put_time() shows the same behavior as it uses
_wcsftime_l() internally. The default invalid parameter handler may terminate
the process, which can be a problem given that the format string can be
specified by the user from the Icinga DSL.

Thus, temporarily set a thread-local no-op handler to disable the default one
allowing the program to continue. This then simply results in the function
returning an error which then results in an exception as we ask the stream to
throw one.

See also:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strftime-wcsftime-strftime-l-wcsftime-l?view=msvc-170
https://learn.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation?view=msvc-170
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/set-invalid-parameter-handler-set-thread-local-invalid-parameter-handler?view=msvc-170
2024-08-23 12:48:50 +02:00
Julian Brost
0285028689 Utility::FormatDateTime(): handle errors from strftime()
So far, the return value of strftime() was simply ignored and the output buffer
passed to the icinga::String constructor. However, there are error conditions
where strftime() returns 0 to signal an error, like if the buffer was too small
for the output. In that case, there's no guarantee on the buffer contents and
reading it can result in undefined behavior. Unfortunately, returning 0 can
also indicate success and strftime() doesn't set errno, so there's no reliable
way to distinguish both situations. Thus, the implementation now returns the
empty string in both cases.

I attempted to use std::put_time() at first as that allows for better error
handling, however, there were problems with the implementation on Windows (see
inline comment), so I put that plan on hold at left strftime() there for the
time being.
2024-08-23 12:42:54 +02:00
Julian Brost
704acdc698 Utility::FormatDateTime(): use boost::numeric_cast<>()
The previous implementation actually had undefined behavior when called with a
double that can't be represented as time_t. With boost::numeric_cast, there's a
convenient cast available that avoids this and throws an exceptions on
overflow.

It's undefined behavior ([0], where the implicit conversion rule comes into
play because the C-style cast uses static_cast [1] which in turn uses the
imlicit conversion as per rule 5 of [2]):

> A prvalue of floating-point type can be converted to a prvalue of any integer
> type. The fractional part is truncated, that is, the fractional part is
> discarded.
>
> * If the truncated value cannot fit into the destination type, the behavior
>   is undefined (even when the destination type is unsigned, modulo arithmetic
>   does not apply).

Note that on Linux amd64, the undefined behavior typically manifests itself in
the result being the minimal value of time_t which then results in localtime_r
failing with EOVERFLOW.

[0]: https://en.cppreference.com/w/cpp/language/implicit_conversion#Floating.E2.80.93integral_conversions
[1]: https://en.cppreference.com/w/cpp/language/explicit_cast
[2]: https://en.cppreference.com/w/cpp/language/static_cast
2024-08-23 12:42:30 +02:00
Julian Brost
090dcfd70f Add tests for Utility::FormatDateTime() 2024-08-22 12:23:22 +02:00
Julian Brost
4c83d793a6
Merge pull request #9983 from Icinga/broken-timeperiod
Fix broken `TimePeriod/ScheduledDowntime`s
2024-08-20 10:05:59 +02:00
Julian Brost
c45829b59f Timeperiods: fix off by one when calculating n-th last weekday of the month
A day specification like "monday -1" refers to the last Monday of the month.
However, there was an off by one if the first day of the next month is the same
day of the week, i.e. a Monday in this example.

LegacyTimePeriod::FindNthWeekday() picks a day to start the search for the day
in question. When given a negative n to search for the n-th last day, it
wrongly used the first day of the following month as the start and counted it
as if it was within the current month. This resulted in a 1/7 chance that the
result was one week too late.

This is fixed by using the last day of the current month instead.
2024-08-07 12:06:05 +02:00
Yonas Habteab
4f94891b52 Add advanced timeperiod range,include/exclude test cases 2024-08-02 11:12:51 +02:00
Yonas Habteab
ddf7143777 tests: Add some basic tests cases for LegacyTimePeriod::IsInTimeRange() 2024-08-02 11:12:51 +02:00
Maciej Dems
2bb5cc62e2 Fix missing values in PerfData normalization 2024-04-25 17:41:12 +02:00
Maciej Dems
91789b2888 Correct tests checks for icinga_perfdata 2024-04-25 17:41:12 +02:00
Alexander Aleksandrovič Klimov
48eb563ca0
Merge pull request #9736 from Icinga/stream-read-allow_partial
Stream#Read(): remove de facto unused param allow_partial
2024-02-13 13:04:15 +01:00
Alexander Aleksandrovič Klimov
949d983a76
Merge pull request #9895 from Icinga/targeted-api-filter
FilterUtility::GetFilterTargets(): don't run filter for specific object(s) for all objects
2023-12-19 15:18:41 +01:00
Alexander Aleksandrovič Klimov
8b2e28a869
Merge pull request #9891 from Icinga/renew-the-ca-9890
ApiListener#Start(): auto-renew CA on its owner
2023-12-19 14:57:47 +01:00
Alexander A. Klimov
175153ce6a PluginNotificationTask::ScriptFunc(): on Linux truncate output and comment
not to run into an exec(3) error E2BIG due to a too long argument.
This sends a notification with truncated output instead of not sending.
2023-12-19 12:21:03 +01:00
Alexander A. Klimov
4424d57e7a Tests: config_apply/gettargetservices_*: use BOOST_CHECK_EQUAL_COLLECTIONS()
to show the value diff in case of mismatch.

Co-authored-by: Yonas Habteab <yonas.habteab@icinga.com>
2023-12-18 16:01:31 +01:00
Alexander A. Klimov
dc338a406a Test IsCertUptodate() and IsCaUptodate() 2023-12-18 15:28:11 +01:00
Alexander A. Klimov
191bf93f2e Test ApplyRule::GetTarget*s() 2023-12-18 13:07:31 +01:00
Alexander A. Klimov
97cd05db7a Notification#BeginExecuteNotification(): on recovery clear last_notified_state_per_user 2023-12-13 13:21:22 +01:00
Alexander A. Klimov
44e9c6f40d Notification#BeginExecuteNotification(): discard likely duplicate problem notifications 2023-12-13 13:21:19 +01:00
Alexander A. Klimov
1308ad62af Stream#Read(): remove de facto unused param allow_partial
The only caller passes true, so no one forbids partial reads (even implicitly).
All usages in the implementation just assert it being true (allowed).
2023-07-13 16:55:48 +02:00
Julian Brost
0e25644151
Merge pull request #8969 from Icinga/bugfix/perfdata-dont-get-parsed-correctly-8912
PluginUtility: Fix PerfData parsing for values separated with multiple spaces
2023-05-22 17:16:31 +02:00
Julian Brost
af9d67b262
Merge pull request #9726 from Icinga/43624b
Remove -and notify- expired downtimes immediately, not every 60s II
2023-05-02 11:25:03 +02:00
Yonas Habteab
e7c5554d87 Test: Add multiline PerfData test case 2023-04-05 15:44:13 +02:00
Alexander A. Klimov
21b68455ce Use Timer::Create() instead of new Timer()
git ls-files -z |xargs -0 perl -pi -e 's/\bnew Timer\b/Timer::Create/g'

ex. in Timer::Create() itself.
2023-04-04 10:35:20 +02:00
Alexander A. Klimov
396a71c6a0 Repair unit tests 2023-02-21 17:49:26 +01:00
Julian Brost
90848f602b Checkable: Add test for state notifications after a suppression ends 2022-03-03 14:25:23 +01:00
Julian Brost
7b2667400b Add tests for LegacyTimePeriod::ScriptFunc when used by TimePeriod::IsInside 2021-11-17 13:09:10 +01:00
Noah Hilverling
10bde2075a Dictionary: Make sure underlaying map is ordered 2021-11-12 13:34:57 +01:00
Alexander Aleksandrovič Klimov
02db80d6f3
Merge pull request #8815 from Icinga/feature/make-base-value-format-test-easier-to-understand
Make base_value/format test easier to understand
2021-08-03 11:37:09 +02:00
Alexander A. Klimov
c666f81361 De-couple package and stage name validation 2021-08-02 15:40:14 +02:00
Julian Brost
d07d48b169 Add tests for DST handling in TimePeriods and ScheduledDowntimes 2021-07-26 16:09:20 +02:00
Alexander A. Klimov
ea5411a6e0 PluginUtility::FormatPerfdata(): normalize UoMs if desired 2021-07-05 19:05:32 +02:00
Alexander Aleksandrovič Klimov
6048d0e800
Merge pull request #8373 from Icinga/feature/improve-crashlog
Improve crashlog
2021-06-29 17:52:25 +02:00
Alexander Aleksandrovič Klimov
9f24107a08
Merge pull request #8748 from Icinga/bugfix/icingadb-cv-function
JsonEncode(): encode non-[]/{} objects as strings, not null
2021-06-23 18:01:21 +02:00