|
| 1 | +/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */ |
| 2 | + |
| 3 | +#include "base/array.hpp" |
| 4 | +#include "icinga/checkresult.hpp" |
| 5 | +#include "icinga/host.hpp" |
| 6 | +#include "icinga/notification.hpp" |
| 7 | +#include "icinga/notificationcommand.hpp" |
| 8 | +#include "icinga/service.hpp" |
| 9 | +#include "icinga/user.hpp" |
| 10 | +#include "methods/pluginnotificationtask.hpp" |
| 11 | +#include <BoostTestTargetConfig.h> |
| 12 | +#include <future> |
| 13 | + |
| 14 | +using namespace icinga; |
| 15 | + |
| 16 | +BOOST_AUTO_TEST_SUITE(methods_pluginnotificationtask) |
| 17 | + |
| 18 | +BOOST_AUTO_TEST_CASE(truncate_long_output) |
| 19 | +{ |
| 20 | +#ifdef __linux__ |
| 21 | + Host::Ptr h = new Host(); |
| 22 | + CheckResult::Ptr hcr = new CheckResult(); |
| 23 | + CheckResult::Ptr scr = new CheckResult(); |
| 24 | + Service::Ptr s = new Service(); |
| 25 | + User::Ptr u = new User(); |
| 26 | + NotificationCommand::Ptr nc = new NotificationCommand(); |
| 27 | + Notification::Ptr n = new Notification(); |
| 28 | + String placeHolder (1024 * 1024, 'x'); |
| 29 | + std::promise<Value> promise; |
| 30 | + auto future (promise.get_future()); |
| 31 | + |
| 32 | + hcr->SetOutput("H" + placeHolder + "h", true); |
| 33 | + scr->SetOutput("S" + placeHolder + "s", true); |
| 34 | + |
| 35 | + h->SetName("example.com", true); |
| 36 | + h->SetLastCheckResult(hcr, true); |
| 37 | + h->Register(); |
| 38 | + |
| 39 | + s->SetHostName("example.com", true); |
| 40 | + s->SetShortName("disk", true); |
| 41 | + s->SetLastCheckResult(scr, true); |
| 42 | + ((ConfigObject*)s.get())->OnAllConfigLoaded(); // link Host |
| 43 | + |
| 44 | + nc->SetName("mail", true); |
| 45 | + nc->SetCommandLine(new Array({"echo", "$host.output$", "$service.output$", "$notification.comment$"}), true); |
| 46 | + nc->Register(); |
| 47 | + |
| 48 | + n->SetFieldByName("host_name", "example.com", false, DebugInfo()); |
| 49 | + n->SetFieldByName("service_name", "disk", false, DebugInfo()); |
| 50 | + n->SetFieldByName("command", "mail", false, DebugInfo()); |
| 51 | + ((ConfigObject*)n.get())->OnAllConfigLoaded(); // link Service |
| 52 | + |
| 53 | + Checkable::ExecuteCommandProcessFinishedHandler = [&promise](const Value& commandline, const ProcessResult&) { |
| 54 | + promise.set_value(commandline); |
| 55 | + }; |
| 56 | + |
| 57 | + PluginNotificationTask::ScriptFunc(n, u, nullptr, NotificationCustom, "jdoe", "C" + placeHolder + "c", nullptr, false); |
| 58 | + future.wait(); |
| 59 | + |
| 60 | + Checkable::ExecuteCommandProcessFinishedHandler = nullptr; |
| 61 | + h->Unregister(); |
| 62 | + nc->Unregister(); |
| 63 | + |
| 64 | + String commandline = Array::Ptr(future.get())->Join(" "); |
| 65 | + |
| 66 | + BOOST_CHECK(commandline.Contains("echo Hx")); |
| 67 | + BOOST_CHECK(!commandline.Contains("xh")); |
| 68 | + BOOST_CHECK(commandline.Contains("x Sx")); |
| 69 | + BOOST_CHECK(!commandline.Contains("xs")); |
| 70 | + BOOST_CHECK(commandline.Contains("x Cx")); |
| 71 | + BOOST_CHECK(!commandline.Contains("xc")); |
| 72 | +#endif /* __linux__ */ |
| 73 | +} |
| 74 | + |
| 75 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments