summaryrefslogtreecommitdiffstats
path: root/test/icinga-notification.cpp
blob: a0aeb7df859f6be1edcac11c34610f216bb84cfa (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "icinga/host.hpp"
#include "icinga/notification.hpp"
#include "icinga/notificationcommand.hpp"
#include "icinga/service.hpp"
#include "icinga/user.hpp"
#include <BoostTestTargetConfig.h>
#include <iostream>

using namespace icinga;

struct DuplicateDueToFilterHelper
{
	Host::Ptr h = new Host();
	Service::Ptr s = new Service();
	User::Ptr u = new User();
	NotificationCommand::Ptr nc = new NotificationCommand();
	Notification::Ptr n = new Notification();
	unsigned int called = 0;

	DuplicateDueToFilterHelper(int typeFilter, int stateFilter)
	{
		h->SetName("example.com", true);
		h->Register();

		s->SetShortName("disk", true);
		h->AddService(s);

		u->SetName("jdoe", true);
		u->SetTypeFilter(~0);
		u->SetStateFilter(~0);
		u->Register();

		nc->SetName("mail", true);
		nc->SetExecute(new Function("", [this]() { ++called; }), 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->SetUsersRaw(new Array({"jdoe"}), true);
		n->SetTypeFilter(typeFilter);
		n->SetStateFilter(stateFilter);
		n->OnAllConfigLoaded(); // link Service
	}

	~DuplicateDueToFilterHelper()
	{
		h->Unregister();
		u->Unregister();
		nc->Unregister();
	}

	void SendStateNotification(ServiceState state, bool isSent)
	{
		auto calledBefore (called);

		s->SetStateRaw(state, true);
		Application::GetTP().Start();

		n->BeginExecuteNotification(
			state == ServiceOK ? NotificationRecovery : NotificationProblem,
			nullptr, false, false, "", ""
		);

		Application::GetTP().Stop();
		BOOST_CHECK_EQUAL(called > calledBefore, isSent);
	}
};

BOOST_AUTO_TEST_SUITE(icinga_notification)

BOOST_AUTO_TEST_CASE(strings)
{
	// States
	BOOST_CHECK("OK" == Notification::NotificationServiceStateToString(ServiceOK));
	BOOST_CHECK("Critical" == Notification::NotificationServiceStateToString(ServiceCritical));
	BOOST_CHECK("Up" == Notification::NotificationHostStateToString(HostUp));

	// Types
	BOOST_CHECK("DowntimeStart" == Notification::NotificationTypeToString(NotificationDowntimeStart));
	BOOST_CHECK("Problem" == Notification::NotificationTypeToString(NotificationProblem));

	// Compat
	BOOST_CHECK("DOWNTIMECANCELLED" == Notification::NotificationTypeToStringCompat(NotificationDowntimeRemoved));
}

BOOST_AUTO_TEST_CASE(state_filter)
{
	unsigned long fstate;

	Array::Ptr states = new Array();
	states->Add("OK");
	states->Add("Warning");

	Notification::Ptr notification = new Notification();

	notification->SetStateFilter(FilterArrayToInt(states, notification->GetStateFilterMap(), ~0));
	notification->Activate();
	notification->SetAuthority(true);

	/* Test passing notification state */
	fstate = StateFilterWarning;
	std::cout << "#1 Notification state: " << fstate << " against " << notification->GetStateFilter() << " must pass. " << std::endl;
	BOOST_CHECK(notification->GetStateFilter() & fstate);

	/* Test filtered notification state */
	fstate = StateFilterUnknown;
	std::cout << "#2 Notification state: " << fstate << " against " << notification->GetStateFilter() << " must fail." << std::endl;
	BOOST_CHECK(!(notification->GetStateFilter() & fstate));

	/* Test unset states filter configuration */
	notification->SetStateFilter(FilterArrayToInt(Array::Ptr(), notification->GetStateFilterMap(), ~0));

	fstate = StateFilterOK;
	std::cout << "#3 Notification state: " << fstate << " against " << notification->GetStateFilter() << " must pass." << std::endl;
	BOOST_CHECK(notification->GetStateFilter() & fstate);

	/* Test empty states filter configuration */
	states->Clear();
	notification->SetStateFilter(FilterArrayToInt(states, notification->GetStateFilterMap(), ~0));

	fstate = StateFilterCritical;
	std::cout << "#4 Notification state: " << fstate << " against " << notification->GetStateFilter() << " must fail." << std::endl;
	BOOST_CHECK(!(notification->GetStateFilter() & fstate));
}
BOOST_AUTO_TEST_CASE(type_filter)
{
	unsigned long ftype;

	Array::Ptr types = new Array();
	types->Add("Problem");
	types->Add("DowntimeStart");
	types->Add("DowntimeEnd");

	Notification::Ptr notification = new Notification();

	notification->SetTypeFilter(FilterArrayToInt(types, notification->GetTypeFilterMap(), ~0));
	notification->Activate();
	notification->SetAuthority(true);

	/* Test passing notification type */
	ftype = NotificationProblem;
	std::cout << "#1 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must pass." << std::endl;
	BOOST_CHECK(notification->GetTypeFilter() & ftype);

	/* Test filtered notification type */
	ftype = NotificationCustom;
	std::cout << "#2 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must fail." << std::endl;
	BOOST_CHECK(!(notification->GetTypeFilter() & ftype));

	/* Test unset types filter configuration */
	notification->SetTypeFilter(FilterArrayToInt(Array::Ptr(), notification->GetTypeFilterMap(), ~0));

	ftype = NotificationRecovery;
	std::cout << "#3 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must pass." << std::endl;
	BOOST_CHECK(notification->GetTypeFilter() & ftype);

	/* Test empty types filter configuration */
	types->Clear();
	notification->SetTypeFilter(FilterArrayToInt(types, notification->GetTypeFilterMap(), ~0));

	ftype = NotificationProblem;
	std::cout << "#4 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must fail." << std::endl;
	BOOST_CHECK(!(notification->GetTypeFilter() & ftype));
}

BOOST_AUTO_TEST_CASE(no_filter_problem_no_duplicate)
{
	DuplicateDueToFilterHelper helper (~0, ~0);

	helper.SendStateNotification(ServiceCritical, true);
	helper.SendStateNotification(ServiceWarning, true);
	helper.SendStateNotification(ServiceCritical, true);
}

BOOST_AUTO_TEST_CASE(filter_problem_no_duplicate)
{
	DuplicateDueToFilterHelper helper (~0, ~StateFilterWarning);

	helper.SendStateNotification(ServiceCritical, true);
	helper.SendStateNotification(ServiceWarning, false);
	helper.SendStateNotification(ServiceCritical, false);
}

BOOST_AUTO_TEST_CASE(volatile_filter_problem_duplicate)
{
	DuplicateDueToFilterHelper helper (~0, ~StateFilterWarning);

	helper.s->SetVolatile(true, true);
	helper.SendStateNotification(ServiceCritical, true);
	helper.SendStateNotification(ServiceWarning, false);
	helper.SendStateNotification(ServiceCritical, true);
}

BOOST_AUTO_TEST_CASE(no_recovery_filter_no_duplicate)
{
	DuplicateDueToFilterHelper helper (~0, ~0);

	helper.SendStateNotification(ServiceCritical, true);
	helper.SendStateNotification(ServiceOK, true);
	helper.SendStateNotification(ServiceCritical, true);
}

BOOST_AUTO_TEST_CASE(recovery_filter_duplicate)
{
	DuplicateDueToFilterHelper helper (~NotificationRecovery, ~0);

	helper.SendStateNotification(ServiceCritical, true);
	helper.SendStateNotification(ServiceOK, false);
	helper.SendStateNotification(ServiceCritical, true);
}

BOOST_AUTO_TEST_SUITE_END()