summaryrefslogtreecommitdiffstats
path: root/test/methods-pluginnotificationtask.cpp
blob: ec582dc8a0e91ed78a219beae7576c3e2c93469d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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()