de2197ed0d964d690c846a2b95c1cff256af832f
[lttng-tools.git] / tests / unit / test_action.cpp
1 /*
2 * test_action.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 <common/payload-view.hpp>
20 #include <common/payload.hpp>
21 #include <lttng/action/action-internal.hpp>
22 #include <lttng/action/action.h>
23 #include <lttng/action/notify.h>
24 #include <lttng/action/rate-policy-internal.hpp>
25 #include <lttng/action/rate-policy.h>
26 #include <lttng/action/rotate-session.h>
27 #include <lttng/action/snapshot-session.h>
28 #include <lttng/action/start-session.h>
29 #include <lttng/action/stop-session.h>
30
31 /* For error.h */
32 int lttng_opt_quiet = 1;
33 int lttng_opt_verbose;
34 int lttng_opt_mi;
35
36 #define NUM_TESTS 60
37
38 static void test_action_notify(void)
39 {
40 int ret;
41 enum lttng_action_status status;
42 struct lttng_action *notify_action = NULL,
43 *notify_action_from_buffer = NULL;
44 struct lttng_rate_policy *policy = NULL, *default_policy;
45 struct lttng_payload payload;
46
47 lttng_payload_init(&payload);
48
49 /* To set. */
50 policy = lttng_rate_policy_every_n_create(100);
51 /* For comparison. */
52 default_policy = lttng_rate_policy_every_n_create(1);
53
54 LTTNG_ASSERT(policy && default_policy);
55
56 notify_action = lttng_action_notify_create();
57 ok(notify_action, "Create notify action");
58 ok(lttng_action_get_type(notify_action) == LTTNG_ACTION_TYPE_NOTIFY,
59 "Action has type LTTNG_ACTION_TYPE_NOTIFY");
60
61 /* Validate the default policy for a notify action. */
62 {
63 const struct lttng_rate_policy *cur_policy = NULL;
64 status = lttng_action_notify_get_rate_policy(
65 notify_action, &cur_policy);
66 ok(status == LTTNG_ACTION_STATUS_OK &&
67 lttng_rate_policy_is_equal(
68 default_policy,
69 cur_policy),
70 "Default policy is every n=1");
71 }
72
73 /* Set a custom policy. */
74 status = lttng_action_notify_set_rate_policy(notify_action, policy);
75 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
76
77 /* Validate the custom policy for a notify action. */
78 {
79 const struct lttng_rate_policy *cur_policy = NULL;
80 status = lttng_action_notify_get_rate_policy(
81 notify_action, &cur_policy);
82 ok(status == LTTNG_ACTION_STATUS_OK &&
83 lttng_rate_policy_is_equal(
84 policy,
85 cur_policy),
86 "Notify action policy get");
87 }
88
89 ret = lttng_action_serialize(notify_action, &payload);
90 ok(ret == 0, "Action notify serialized");
91
92 {
93 struct lttng_payload_view view =
94 lttng_payload_view_from_payload(
95 &payload, 0, -1);
96 (void) lttng_action_create_from_payload(
97 &view, &notify_action_from_buffer);
98 }
99 ok(notify_action_from_buffer,
100 "Notify action created from payload is non-null");
101
102 ok(lttng_action_is_equal(notify_action, notify_action_from_buffer),
103 "Serialized and de-serialized notify action are equal");
104
105 lttng_rate_policy_destroy(default_policy);
106 lttng_rate_policy_destroy(policy);
107 lttng_action_destroy(notify_action);
108 lttng_action_destroy(notify_action_from_buffer);
109 lttng_payload_reset(&payload);
110 }
111
112 static void test_action_rotate_session(void)
113 {
114 int ret;
115 enum lttng_action_status status;
116 struct lttng_action *rotate_session_action = NULL,
117 *rotate_session_action_from_buffer = NULL;
118 struct lttng_rate_policy *policy = NULL, *default_policy;
119 struct lttng_payload payload;
120 const char *session_name = "my_session_name";
121 const char *get_session_name;
122
123 lttng_payload_init(&payload);
124
125 /* To set. */
126 policy = lttng_rate_policy_every_n_create(100);
127 /* For comparison. */
128 default_policy = lttng_rate_policy_every_n_create(1);
129
130 LTTNG_ASSERT(policy && default_policy);
131
132 rotate_session_action = lttng_action_rotate_session_create();
133 ok(rotate_session_action, "Create rotate_session action");
134 ok(lttng_action_get_type(rotate_session_action) ==
135 LTTNG_ACTION_TYPE_ROTATE_SESSION,
136 "Action has type LTTNG_ACTION_TYPE_ROTATE_SESSION");
137
138 /* Session name setter. */
139 status = lttng_action_rotate_session_set_session_name(NULL, NULL);
140 ok(status == LTTNG_ACTION_STATUS_INVALID,
141 "Set session name (NULL,NULL) expect invalid");
142 status = lttng_action_rotate_session_set_session_name(
143 rotate_session_action, NULL);
144 ok(status == LTTNG_ACTION_STATUS_INVALID,
145 "Set session name (object,NULL) expect invalid");
146 status = lttng_action_rotate_session_set_session_name(
147 NULL, session_name);
148 ok(status == LTTNG_ACTION_STATUS_INVALID,
149 "Set session name (NULL,object) expect invalid");
150
151 /* Set the session name */
152 status = lttng_action_rotate_session_set_session_name(
153 rotate_session_action, session_name);
154 ok(status == LTTNG_ACTION_STATUS_OK, "Set session name");
155
156 status = lttng_action_rotate_session_get_session_name(
157 rotate_session_action, &get_session_name);
158 ok(status == LTTNG_ACTION_STATUS_OK &&
159 !strcmp(session_name, get_session_name),
160 "Get session name, expected `%s` got `%s`",
161 session_name, get_session_name);
162
163 /* Validate the default policy for a rotate_session action. */
164 {
165 const struct lttng_rate_policy *cur_policy = NULL;
166 status = lttng_action_rotate_session_get_rate_policy(
167 rotate_session_action, &cur_policy);
168 ok(status == LTTNG_ACTION_STATUS_OK &&
169 lttng_rate_policy_is_equal(
170 default_policy,
171 cur_policy),
172 "Default policy is every n=1");
173 }
174
175 /* Set a custom policy. */
176 status = lttng_action_rotate_session_set_rate_policy(
177 rotate_session_action, policy);
178 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
179
180 /* Validate the custom policy for a rotate_session action. */
181 {
182 const struct lttng_rate_policy *cur_policy = NULL;
183 status = lttng_action_rotate_session_get_rate_policy(
184 rotate_session_action, &cur_policy);
185 ok(status == LTTNG_ACTION_STATUS_OK &&
186 lttng_rate_policy_is_equal(
187 policy,
188 cur_policy),
189 "rotate_session action policy get");
190 }
191
192 /* Ser/des tests. */
193 ret = lttng_action_serialize(rotate_session_action, &payload);
194 ok(ret == 0, "Action rotate_session serialized");
195
196 {
197 struct lttng_payload_view view =
198 lttng_payload_view_from_payload(
199 &payload, 0, -1);
200 (void) lttng_action_create_from_payload(
201 &view, &rotate_session_action_from_buffer);
202 }
203 ok(rotate_session_action_from_buffer,
204 "rotate_session action created from payload is non-null");
205
206 ok(lttng_action_is_equal(rotate_session_action,
207 rotate_session_action_from_buffer),
208 "Serialized and de-serialized rotate_session action are equal");
209
210 lttng_rate_policy_destroy(default_policy);
211 lttng_rate_policy_destroy(policy);
212 lttng_action_destroy(rotate_session_action);
213 lttng_action_destroy(rotate_session_action_from_buffer);
214 lttng_payload_reset(&payload);
215 }
216
217 static void test_action_start_session(void)
218 {
219 int ret;
220 enum lttng_action_status status;
221 struct lttng_action *start_session_action = NULL,
222 *start_session_action_from_buffer = NULL;
223 struct lttng_rate_policy *policy = NULL, *default_policy;
224 struct lttng_payload payload;
225 const char *session_name = "my_session_name";
226 const char *get_session_name;
227
228 lttng_payload_init(&payload);
229
230 /* To set. */
231 policy = lttng_rate_policy_every_n_create(100);
232 /* For comparison. */
233 default_policy = lttng_rate_policy_every_n_create(1);
234
235 LTTNG_ASSERT(policy && default_policy);
236
237 start_session_action = lttng_action_start_session_create();
238 ok(start_session_action, "Create start_session action");
239 ok(lttng_action_get_type(start_session_action) ==
240 LTTNG_ACTION_TYPE_START_SESSION,
241 "Action has type LTTNG_ACTION_TYPE_START_SESSION");
242
243 /* Session name setter. */
244 status = lttng_action_start_session_set_session_name(NULL, NULL);
245 ok(status == LTTNG_ACTION_STATUS_INVALID,
246 "Set session name (NULL,NULL) expect invalid");
247 status = lttng_action_start_session_set_session_name(
248 start_session_action, NULL);
249 ok(status == LTTNG_ACTION_STATUS_INVALID,
250 "Set session name (object,NULL) expect invalid");
251 status = lttng_action_start_session_set_session_name(
252 NULL, session_name);
253 ok(status == LTTNG_ACTION_STATUS_INVALID,
254 "Set session name (NULL,object) expect invalid");
255
256 /* Set the session name */
257 status = lttng_action_start_session_set_session_name(
258 start_session_action, session_name);
259 ok(status == LTTNG_ACTION_STATUS_OK, "Set session name");
260
261 status = lttng_action_start_session_get_session_name(
262 start_session_action, &get_session_name);
263 ok(status == LTTNG_ACTION_STATUS_OK &&
264 !strcmp(session_name, get_session_name),
265 "Get session name, expected `%s` got `%s`",
266 session_name, get_session_name);
267
268 /* Validate the default policy for a start_session action. */
269 {
270 const struct lttng_rate_policy *cur_policy = NULL;
271 status = lttng_action_start_session_get_rate_policy(
272 start_session_action, &cur_policy);
273 ok(status == LTTNG_ACTION_STATUS_OK &&
274 lttng_rate_policy_is_equal(
275 default_policy,
276 cur_policy),
277 "Default policy is every n=1");
278 }
279
280 /* Set a custom policy. */
281 status = lttng_action_start_session_set_rate_policy(
282 start_session_action, policy);
283 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
284
285 /* Validate the custom policy for a start_session action. */
286 {
287 const struct lttng_rate_policy *cur_policy = NULL;
288 status = lttng_action_start_session_get_rate_policy(
289 start_session_action, &cur_policy);
290 ok(status == LTTNG_ACTION_STATUS_OK &&
291 lttng_rate_policy_is_equal(
292 policy,
293 cur_policy),
294 "start_session action policy get");
295 }
296
297 /* Ser/des tests. */
298 ret = lttng_action_serialize(start_session_action, &payload);
299 ok(ret == 0, "Action start_session serialized");
300
301 {
302 struct lttng_payload_view view =
303 lttng_payload_view_from_payload(
304 &payload, 0, -1);
305 (void) lttng_action_create_from_payload(
306 &view, &start_session_action_from_buffer);
307 }
308 ok(start_session_action_from_buffer,
309 "start_session action created from payload is non-null");
310
311 ok(lttng_action_is_equal(start_session_action,
312 start_session_action_from_buffer),
313 "Serialized and de-serialized start_session action are equal");
314
315 lttng_rate_policy_destroy(default_policy);
316 lttng_rate_policy_destroy(policy);
317 lttng_action_destroy(start_session_action);
318 lttng_action_destroy(start_session_action_from_buffer);
319 lttng_payload_reset(&payload);
320 }
321
322 static void test_action_stop_session(void)
323 {
324 int ret;
325 enum lttng_action_status status;
326 struct lttng_action *stop_session_action = NULL,
327 *stop_session_action_from_buffer = NULL;
328 struct lttng_rate_policy *policy = NULL, *default_policy;
329 struct lttng_payload payload;
330 const char *session_name = "my_session_name";
331 const char *get_session_name;
332
333 lttng_payload_init(&payload);
334
335 /* To set. */
336 policy = lttng_rate_policy_every_n_create(100);
337 /* For comparison. */
338 default_policy = lttng_rate_policy_every_n_create(1);
339
340 LTTNG_ASSERT(policy && default_policy);
341
342 stop_session_action = lttng_action_stop_session_create();
343 ok(stop_session_action, "Create stop_session action");
344 ok(lttng_action_get_type(stop_session_action) ==
345 LTTNG_ACTION_TYPE_STOP_SESSION,
346 "Action has type LTTNG_ACTION_TYPE_STOP_SESSION");
347
348 /* Session name setter. */
349 status = lttng_action_stop_session_set_session_name(NULL, NULL);
350 ok(status == LTTNG_ACTION_STATUS_INVALID,
351 "Set session name (NULL,NULL) expect invalid");
352 status = lttng_action_stop_session_set_session_name(
353 stop_session_action, NULL);
354 ok(status == LTTNG_ACTION_STATUS_INVALID,
355 "Set session name (object,NULL) expect invalid");
356 status = lttng_action_stop_session_set_session_name(NULL, session_name);
357 ok(status == LTTNG_ACTION_STATUS_INVALID,
358 "Set session name (NULL,object) expect invalid");
359
360 /* Set the session name */
361 status = lttng_action_stop_session_set_session_name(
362 stop_session_action, session_name);
363 ok(status == LTTNG_ACTION_STATUS_OK, "Set session name");
364
365 status = lttng_action_stop_session_get_session_name(
366 stop_session_action, &get_session_name);
367 ok(status == LTTNG_ACTION_STATUS_OK &&
368 !strcmp(session_name, get_session_name),
369 "Get session name, expected `%s` got `%s`",
370 session_name, get_session_name);
371
372 /* Validate the default policy for a stop_session action. */
373 {
374 const struct lttng_rate_policy *cur_policy = NULL;
375 status = lttng_action_stop_session_get_rate_policy(
376 stop_session_action, &cur_policy);
377 ok(status == LTTNG_ACTION_STATUS_OK &&
378 lttng_rate_policy_is_equal(
379 default_policy,
380 cur_policy),
381 "Default policy is every n=1");
382 }
383
384 /* Set a custom policy. */
385 status = lttng_action_stop_session_set_rate_policy(
386 stop_session_action, policy);
387 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
388
389 /* Validate the custom policy for a stop_session action. */
390 {
391 const struct lttng_rate_policy *cur_policy = NULL;
392 status = lttng_action_stop_session_get_rate_policy(
393 stop_session_action, &cur_policy);
394 ok(status == LTTNG_ACTION_STATUS_OK &&
395 lttng_rate_policy_is_equal(
396 policy,
397 cur_policy),
398 "stop_session action policy get");
399 }
400
401 /* Ser/des tests. */
402 ret = lttng_action_serialize(stop_session_action, &payload);
403 ok(ret == 0, "Action stop_session serialized");
404
405 {
406 struct lttng_payload_view view =
407 lttng_payload_view_from_payload(
408 &payload, 0, -1);
409 (void) lttng_action_create_from_payload(
410 &view, &stop_session_action_from_buffer);
411 }
412 ok(stop_session_action_from_buffer,
413 "stop_session action created from payload is non-null");
414
415 ok(lttng_action_is_equal(stop_session_action,
416 stop_session_action_from_buffer),
417 "Serialized and de-serialized stop_session action are equal");
418
419 lttng_rate_policy_destroy(default_policy);
420 lttng_rate_policy_destroy(policy);
421 lttng_action_destroy(stop_session_action);
422 lttng_action_destroy(stop_session_action_from_buffer);
423 lttng_payload_reset(&payload);
424 }
425
426 static void test_action_snapshot_session(void)
427 {
428 int ret;
429 enum lttng_action_status status;
430 struct lttng_action *snapshot_session_action = NULL,
431 *snapshot_session_action_from_buffer = NULL;
432 struct lttng_rate_policy *policy = NULL, *default_policy;
433 struct lttng_payload payload;
434 const char *session_name = "my_session_name";
435 const char *get_session_name;
436
437 lttng_payload_init(&payload);
438
439 /* To set. */
440 policy = lttng_rate_policy_every_n_create(100);
441 /* For comparison. */
442 default_policy = lttng_rate_policy_every_n_create(1);
443
444 LTTNG_ASSERT(policy && default_policy);
445
446 snapshot_session_action = lttng_action_snapshot_session_create();
447 ok(snapshot_session_action, "Create snapshot_session action");
448 ok(lttng_action_get_type(snapshot_session_action) ==
449 LTTNG_ACTION_TYPE_SNAPSHOT_SESSION,
450 "Action has type LTTNG_ACTION_TYPE_SNAPSHOT_SESSION");
451
452 /* Session name setter. */
453 status = lttng_action_snapshot_session_set_session_name(NULL, NULL);
454 ok(status == LTTNG_ACTION_STATUS_INVALID,
455 "Set session name (NULL,NULL) expect invalid");
456 status = lttng_action_snapshot_session_set_session_name(
457 snapshot_session_action, NULL);
458 ok(status == LTTNG_ACTION_STATUS_INVALID,
459 "Set session name (object,NULL) expect invalid");
460 status = lttng_action_snapshot_session_set_session_name(
461 NULL, session_name);
462 ok(status == LTTNG_ACTION_STATUS_INVALID,
463 "Set session name (NULL,object) expect invalid");
464
465 /* Set the session name */
466 status = lttng_action_snapshot_session_set_session_name(
467 snapshot_session_action, session_name);
468 ok(status == LTTNG_ACTION_STATUS_OK, "Set session name");
469
470 status = lttng_action_snapshot_session_get_session_name(
471 snapshot_session_action, &get_session_name);
472 ok(status == LTTNG_ACTION_STATUS_OK &&
473 !strcmp(session_name, get_session_name),
474 "Get session name, expected `%s` got `%s`",
475 session_name, get_session_name);
476
477 /* Validate the default policy for a snapshot_session action. */
478 {
479 const struct lttng_rate_policy *cur_policy = NULL;
480 status = lttng_action_snapshot_session_get_rate_policy(
481 snapshot_session_action, &cur_policy);
482 ok(status == LTTNG_ACTION_STATUS_OK &&
483 lttng_rate_policy_is_equal(
484 default_policy,
485 cur_policy),
486 "Default policy is every n=1");
487 }
488
489 /* Set a custom policy. */
490 status = lttng_action_snapshot_session_set_rate_policy(
491 snapshot_session_action, policy);
492 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
493
494 /* Validate the custom policy for a snapshot_session action. */
495 {
496 const struct lttng_rate_policy *cur_policy = NULL;
497 status = lttng_action_snapshot_session_get_rate_policy(
498 snapshot_session_action, &cur_policy);
499 ok(status == LTTNG_ACTION_STATUS_OK &&
500 lttng_rate_policy_is_equal(
501 policy,
502 cur_policy),
503 "snapshot_session action policy get");
504 }
505
506 /* Ser/des tests. */
507 ret = lttng_action_serialize(snapshot_session_action, &payload);
508 ok(ret == 0, "Action snapshot_session serialized");
509
510 {
511 struct lttng_payload_view view =
512 lttng_payload_view_from_payload(
513 &payload, 0, -1);
514 (void) lttng_action_create_from_payload(
515 &view, &snapshot_session_action_from_buffer);
516 }
517 ok(snapshot_session_action_from_buffer,
518 "snapshot_session action created from payload is non-null");
519
520 ok(lttng_action_is_equal(snapshot_session_action,
521 snapshot_session_action_from_buffer),
522 "Serialized and de-serialized snapshot_session action are equal");
523
524 lttng_rate_policy_destroy(default_policy);
525 lttng_rate_policy_destroy(policy);
526 lttng_action_destroy(snapshot_session_action);
527 lttng_action_destroy(snapshot_session_action_from_buffer);
528 lttng_payload_reset(&payload);
529 }
530
531 int main(void)
532 {
533 plan_tests(NUM_TESTS);
534 test_action_notify();
535 test_action_rotate_session();
536 test_action_start_session();
537 test_action_stop_session();
538 test_action_snapshot_session();
539 return exit_status();
540 }
This page took 0.040064 seconds and 4 git commands to generate.