Copyright ownership transfer
[lttng-tools.git] / tests / unit / test_notification.cpp
1 /*
2 * test_notification.c
3 *
4 * Unit tests for the notification API.
5 *
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
7 *
8 * SPDX-License-Identifier: MIT
9 *
10 */
11
12 #include <inttypes.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 #include <tap/tap.h>
18
19 #include <lttng/action/action.h>
20 #include <lttng/action/notify.h>
21 #include <lttng/condition/buffer-usage.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/domain.h>
24 #include <lttng/notification/notification.h>
25 #include <lttng/trigger/trigger.h>
26
27 #include <common/macros.h>
28
29 /* For error.h */
30 int lttng_opt_quiet = 1;
31 int lttng_opt_verbose;
32 int lttng_opt_mi;
33
34 #define NUM_TESTS 180
35
36 static void test_condition_buffer_usage(
37 struct lttng_condition *buffer_usage_condition)
38 {
39 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
40 const char *session_name = NULL;
41 const char *channel_name = NULL;
42 enum lttng_domain_type domain_type;
43 /* Start at a non zero value to validate initialization */
44 double threshold_ratio;
45 uint64_t threshold_bytes;
46
47 LTTNG_ASSERT(buffer_usage_condition);
48
49 diag("Validating initialization");
50 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
51 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
52
53 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
54 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold byte is unset");
55
56 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
57 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
58 ok(!session_name, "Session name is null");
59
60 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
61 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
62 ok(!session_name, "Channel name is null");
63
64 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
65 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain name is unset");
66
67 diag("Testing session name set/get");
68 status = lttng_condition_buffer_usage_set_session_name(NULL, "Test");
69 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set session name");
70 status = lttng_condition_buffer_usage_get_session_name(NULL, &session_name);
71 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get session name with null condition");
72 ok(!session_name, "Session name is null");
73 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
74 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
75 ok(!session_name, "Session name is null");
76
77 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, NULL);
78 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null session name");
79 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
80 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
81 ok(!session_name, "Session name is null");
82
83 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "");
84 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty session name");
85 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
86 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
87 ok(!session_name, "Session name is null");
88
89 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "session420");
90 ok(status == LTTNG_CONDITION_STATUS_OK, "Set session name session420");
91 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
92 ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is set");
93 ok(session_name, "Session name has a value");
94 ok(strcmp("session420", session_name) == 0, "Session name is %s", "session420");
95
96 /*
97 * Test second set on session_name. Test invalid set and validate that
98 * the value is still the previous good one.
99 */
100
101 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "");
102 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set session name to empty");
103 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
104 ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is still set");
105 ok(session_name, "Session name has a value");
106 ok(strcmp("session420", session_name) == 0, "Session is still name is %s", "session420");
107
108 diag("Testing channel name set/get");
109 status = lttng_condition_buffer_usage_set_channel_name(NULL, "Test");
110 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set channel name");
111 status = lttng_condition_buffer_usage_get_channel_name(NULL, &channel_name);
112 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get channel name with null condition");
113 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
114 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
115 ok(!channel_name, "Channel name is null");
116
117 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, NULL);
118 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null channel name");
119 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
120 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
121 ok(!channel_name, "Channel name is null");
122
123 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "");
124 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty channel name");
125 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
126 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
127 ok(!channel_name, "Channel name is null");
128
129 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "channel420");
130 ok(status == LTTNG_CONDITION_STATUS_OK, "Set channel name channel420");
131 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
132 ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is set");
133 ok(channel_name, "Channel name has a value");
134 ok(strcmp("channel420", channel_name) == 0, "Channel name is %s", "channel420");
135
136 /*
137 * Test second set on channel_name. Test invalid set and validate that
138 * the value is still the previous good one.
139 */
140
141 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "");
142 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set channel name to empty");
143 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
144 ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is still set");
145 ok(channel_name, "Channel name has a value");
146 ok(strcmp("channel420", channel_name) == 0, "Channel is still name is %s", "channel420");
147
148 diag("Testing threshold ratio set/get");
149 status = lttng_condition_buffer_usage_set_threshold_ratio(NULL, 0.420);
150 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio with null condition");
151 status = lttng_condition_buffer_usage_get_threshold_ratio(NULL, &threshold_ratio);
152 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold ratio with null condition");
153 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
154 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
155
156 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, -100.0);
157 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio < 0");
158 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
159 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
160
161 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 200.0);
162 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set Threshold ratio > 1");
163 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
164 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
165
166 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 1.0);
167 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 1.0");
168 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
169 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
170 ok(threshold_ratio == 1.0, "Threshold ratio is 1.0");
171
172 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.0);
173 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.0");
174 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
175 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
176 ok(threshold_ratio == 0.0, "Threshold ratio is 0.0");
177
178 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420);
179 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420");
180 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
181 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
182 ok(threshold_ratio == 0.420, "Threshold ratio is 0.420");
183
184 diag("Testing threshold bytes set/get");
185 status = lttng_condition_buffer_usage_set_threshold(NULL, 100000);
186 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold with null condition");
187 status = lttng_condition_buffer_usage_get_threshold(NULL, &threshold_bytes);
188 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold value with null condition ");
189 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
190 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset");
191
192 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 100000);
193 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold > 0");
194 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
195 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
196 ok(threshold_bytes == 100000, "Threshold is 100000");
197
198 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, UINT64_MAX);
199 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold UINT64_MAX");
200 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
201 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
202 ok(threshold_bytes == UINT64_MAX, "Threshold is UINT64_MAX");
203
204 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 0);
205 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold == 0");
206 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
207 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
208 ok(threshold_bytes == 0, "Threshold is %d", 0);
209
210 /*
211 * Test value of threshold ration, since we overwrote it with a byte
212 * threshold. Make sure it gets squashed.
213 */
214 diag("Testing interaction between byte and ratio thresholds");
215
216 threshold_ratio = -1.0;
217 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
218 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
219 ok(threshold_ratio == -1.0, "Threshold ratio is untouched");
220
221 /* Set a ratio to validate that the byte threshold is now unset */
222 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420);
223 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420");
224 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
225 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
226 ok(threshold_ratio == 0.420, "Threshold ratio is 0.420");
227
228 threshold_bytes = 420;
229 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
230 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset");
231 ok(threshold_bytes == 420, "Threshold is untouched");
232
233 diag("Testing domain type set/get");
234 status = lttng_condition_buffer_usage_set_domain_type(NULL, LTTNG_DOMAIN_UST);
235 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type with null condition");
236 status = lttng_condition_buffer_usage_get_domain_type(NULL, &domain_type);
237 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get domain type with null condition");
238
239 status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition, LTTNG_DOMAIN_NONE);
240 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type as LTTNG_DOMAIN_NONE");
241 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
242 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain type is unset");
243
244 status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition, LTTNG_DOMAIN_UST);
245 ok(status == LTTNG_CONDITION_STATUS_OK, "Set domain type as LTTNG_DOMAIN_UST");
246 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
247 ok(status == LTTNG_CONDITION_STATUS_OK, "Domain type is set");
248 ok(domain_type == LTTNG_DOMAIN_UST, "Domain type is LTTNG_DOMAIN_UST");
249 }
250
251 static void test_condition_buffer_usage_low(void)
252 {
253 struct lttng_condition *buffer_usage_low = NULL;
254
255 diag("Testing lttng_condition_buffer_usage_low_create");
256 buffer_usage_low = lttng_condition_buffer_usage_low_create();
257 ok(buffer_usage_low, "Condition allocated");
258
259 ok(lttng_condition_get_type(buffer_usage_low) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, "Condition is of type \"low buffer usage\"");
260
261 test_condition_buffer_usage(buffer_usage_low);
262
263 lttng_condition_destroy(buffer_usage_low);
264 }
265
266 static void test_condition_buffer_usage_high(void)
267 {
268 struct lttng_condition *buffer_usage_high = NULL;
269
270 diag("Testing lttng_condition_buffer_usage_high_create");
271 buffer_usage_high = lttng_condition_buffer_usage_high_create();
272 ok(buffer_usage_high, "High buffer usage condition allocated");
273
274 ok(lttng_condition_get_type(buffer_usage_high) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, "Condition is of type \"high buffer usage\"");
275
276 test_condition_buffer_usage(buffer_usage_high);
277
278 lttng_condition_destroy(buffer_usage_high);
279 }
280
281 static void test_trigger(void)
282 {
283 struct lttng_action *notify_action = NULL;
284 struct lttng_condition *buffer_usage_high = NULL;
285 struct lttng_trigger *trigger = NULL;
286
287 notify_action = lttng_action_notify_create();
288 buffer_usage_high = lttng_condition_buffer_usage_high_create();
289
290 trigger = lttng_trigger_create(NULL, NULL);
291 ok(!trigger, "lttng_trigger_create(NULL, NULL) returns null");
292 trigger = lttng_trigger_create(buffer_usage_high, NULL);
293 ok(!trigger, "lttng_trigger_create(NON-NULL, NULL) returns null");
294 trigger = lttng_trigger_create(NULL, notify_action);
295 ok(!trigger, "lttng_trigger_create(NULL, NON-NULL) returns null");
296
297 trigger = lttng_trigger_create(buffer_usage_high, notify_action);
298 ok(trigger, "lttng_trigger_create(NON-NULL, NON-NULL) returns an object");
299
300 lttng_action_destroy(notify_action);
301 lttng_condition_destroy(buffer_usage_high);
302 lttng_trigger_destroy(trigger);
303 }
304
305
306 int main(int argc, const char *argv[])
307 {
308 plan_tests(NUM_TESTS);
309 test_condition_buffer_usage_low();
310 test_condition_buffer_usage_high();
311 test_trigger();
312 return exit_status();
313 }
This page took 0.037856 seconds and 4 git commands to generate.