diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:32:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:32:39 +0000 |
commit | 56ae875861ab260b80a030f50c4aff9f9dc8fff0 (patch) | |
tree | 531412110fc901a5918c7f7442202804a83cada9 /test/methods-pluginnotificationtask.cpp | |
parent | Initial commit. (diff) | |
download | icinga2-e6c8b97d844e301093c7e2c03da489629676e2c4.tar.xz icinga2-e6c8b97d844e301093c7e2c03da489629676e2c4.zip |
Adding upstream version 2.14.2.upstream/2.14.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | test/methods-pluginnotificationtask.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/test/methods-pluginnotificationtask.cpp b/test/methods-pluginnotificationtask.cpp new file mode 100644 index 0000000..ec582dc --- /dev/null +++ b/test/methods-pluginnotificationtask.cpp @@ -0,0 +1,88 @@ +/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */ + +#include "base/array.hpp" +#include "icinga/checkresult.hpp" +#include "icinga/host.hpp" +#include "icinga/notification.hpp" +#include "icinga/notificationcommand.hpp" +#include "icinga/service.hpp" +#include "icinga/user.hpp" +#include "methods/pluginnotificationtask.hpp" +#include <BoostTestTargetConfig.h> +#include <future> + +using namespace icinga; + +BOOST_AUTO_TEST_SUITE(methods_pluginnotificationtask) + +BOOST_AUTO_TEST_CASE(truncate_long_output) +{ +#ifdef __linux__ + Host::Ptr h = new Host(); + CheckResult::Ptr hcr = new CheckResult(); + CheckResult::Ptr scr = new CheckResult(); + Service::Ptr s = new Service(); + User::Ptr u = new User(); + NotificationCommand::Ptr nc = new NotificationCommand(); + Notification::Ptr n = new Notification(); + String placeHolder (1024 * 1024, 'x'); + std::promise<String> promise; + auto future (promise.get_future()); + + hcr->SetOutput("H" + placeHolder + "h", true); + scr->SetOutput("S" + placeHolder + "s", true); + + h->SetName("example.com", true); + h->SetLastCheckResult(hcr, true); + h->Register(); + + s->SetHostName("example.com", true); + s->SetShortName("disk", true); + s->SetLastCheckResult(scr, true); + s->OnAllConfigLoaded(); // link Host + + nc->SetCommandLine( + new Array({ + "echo", + "host_output=$host.output$", + "service_output=$service.output$", + "notification_comment=$notification.comment$", + "output=$output$", + "comment=$comment$" + }), + true + ); + + nc->SetName("mail", true); + nc->Register(); + + n->SetFieldByName("host_name", "example.com", false, DebugInfo()); + n->SetFieldByName("service_name", "disk", false, DebugInfo()); + n->SetFieldByName("command", "mail", false, DebugInfo()); + n->OnAllConfigLoaded(); // link Service + + Checkable::ExecuteCommandProcessFinishedHandler = [&promise](const Value&, const ProcessResult& pr) { + promise.set_value(pr.Output); + }; + + PluginNotificationTask::ScriptFunc(n, u, nullptr, NotificationCustom, "jdoe", "C" + placeHolder + "c", nullptr, false); + future.wait(); + + Checkable::ExecuteCommandProcessFinishedHandler = nullptr; + h->Unregister(); + nc->Unregister(); + + auto output (future.get()); + + BOOST_CHECK(output.Contains("host_output=Hx")); + BOOST_CHECK(!output.Contains("xh")); + BOOST_CHECK(output.Contains("x service_output=Sx")); + BOOST_CHECK(!output.Contains("xs")); + BOOST_CHECK(output.Contains("x notification_comment=Cx")); + BOOST_CHECK(!output.Contains("xc")); + BOOST_CHECK(output.Contains("x output=Sx")); + BOOST_CHECK(output.Contains("x comment=Cx")); +#endif /* __linux__ */ +} + +BOOST_AUTO_TEST_SUITE_END() |