-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PluginNotificationTask::ScriptFunc(): on Linux truncate output and co…
…mment 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.
- Loading branch information
Showing
3 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* 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<Value> 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); | ||
((ConfigObject*)s.get())->OnAllConfigLoaded(); // link Host | ||
|
||
nc->SetName("mail", true); | ||
nc->SetCommandLine(new Array({"echo", "$host.output$", "$service.output$", "$notification.comment$"}), true); | ||
nc->Register(); | ||
|
||
n->SetFieldByName("host_name", "example.com", false, DebugInfo()); | ||
n->SetFieldByName("service_name", "disk", false, DebugInfo()); | ||
n->SetFieldByName("command", "mail", false, DebugInfo()); | ||
((ConfigObject*)n.get())->OnAllConfigLoaded(); // link Service | ||
|
||
Checkable::ExecuteCommandProcessFinishedHandler = [&promise](const Value& commandline, const ProcessResult&) { | ||
promise.set_value(commandline); | ||
}; | ||
|
||
PluginNotificationTask::ScriptFunc(n, u, nullptr, NotificationCustom, "jdoe", "C" + placeHolder + "c", nullptr, false); | ||
future.wait(); | ||
|
||
Checkable::ExecuteCommandProcessFinishedHandler = nullptr; | ||
h->Unregister(); | ||
nc->Unregister(); | ||
|
||
String commandline = Array::Ptr(future.get())->Join(" "); | ||
|
||
BOOST_CHECK(commandline.Contains("echo Hx")); | ||
BOOST_CHECK(!commandline.Contains("xh")); | ||
BOOST_CHECK(commandline.Contains("x Sx")); | ||
BOOST_CHECK(!commandline.Contains("xs")); | ||
BOOST_CHECK(commandline.Contains("x Cx")); | ||
BOOST_CHECK(!commandline.Contains("xc")); | ||
#endif /* __linux__ */ | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |