4 * Unit tests for the notification API.
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
8 * SPDX-License-Identifier: MIT
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>
32 int lttng_opt_quiet
= 1;
33 int lttng_opt_verbose
;
38 static void test_action_notify(void)
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
;
47 lttng_payload_init(&payload
);
50 policy
= lttng_rate_policy_every_n_create(100);
52 default_policy
= lttng_rate_policy_every_n_create(1);
54 LTTNG_ASSERT(policy
&& default_policy
);
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");
61 /* Validate the default policy for a notify action. */
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(
70 "Default policy is every n=1");
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");
77 /* Validate the custom policy for a notify action. */
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(
86 "Notify action policy get");
89 ret
= lttng_action_serialize(notify_action
, &payload
);
90 ok(ret
== 0, "Action notify serialized");
93 struct lttng_payload_view view
=
94 lttng_payload_view_from_payload(
96 (void) lttng_action_create_from_payload(
97 &view
, ¬ify_action_from_buffer
);
99 ok(notify_action_from_buffer
,
100 "Notify action created from payload is non-null");
102 ok(lttng_action_is_equal(notify_action
, notify_action_from_buffer
),
103 "Serialized and de-serialized notify action are equal");
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
);
112 static void test_action_rotate_session(void)
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
;
123 lttng_payload_init(&payload
);
126 policy
= lttng_rate_policy_every_n_create(100);
127 /* For comparison. */
128 default_policy
= lttng_rate_policy_every_n_create(1);
130 LTTNG_ASSERT(policy
&& default_policy
);
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");
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(
148 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
149 "Set session name (NULL,object) expect invalid");
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");
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
);
163 /* Validate the default policy for a rotate_session action. */
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(
172 "Default policy is every n=1");
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");
180 /* Validate the custom policy for a rotate_session action. */
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(
189 "rotate_session action policy get");
193 ret
= lttng_action_serialize(rotate_session_action
, &payload
);
194 ok(ret
== 0, "Action rotate_session serialized");
197 struct lttng_payload_view view
=
198 lttng_payload_view_from_payload(
200 (void) lttng_action_create_from_payload(
201 &view
, &rotate_session_action_from_buffer
);
203 ok(rotate_session_action_from_buffer
,
204 "rotate_session action created from payload is non-null");
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");
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
);
217 static void test_action_start_session(void)
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
;
228 lttng_payload_init(&payload
);
231 policy
= lttng_rate_policy_every_n_create(100);
232 /* For comparison. */
233 default_policy
= lttng_rate_policy_every_n_create(1);
235 LTTNG_ASSERT(policy
&& default_policy
);
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");
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(
253 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
254 "Set session name (NULL,object) expect invalid");
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");
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
);
268 /* Validate the default policy for a start_session action. */
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(
277 "Default policy is every n=1");
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");
285 /* Validate the custom policy for a start_session action. */
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(
294 "start_session action policy get");
298 ret
= lttng_action_serialize(start_session_action
, &payload
);
299 ok(ret
== 0, "Action start_session serialized");
302 struct lttng_payload_view view
=
303 lttng_payload_view_from_payload(
305 (void) lttng_action_create_from_payload(
306 &view
, &start_session_action_from_buffer
);
308 ok(start_session_action_from_buffer
,
309 "start_session action created from payload is non-null");
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");
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
);
322 static void test_action_stop_session(void)
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
;
333 lttng_payload_init(&payload
);
336 policy
= lttng_rate_policy_every_n_create(100);
337 /* For comparison. */
338 default_policy
= lttng_rate_policy_every_n_create(1);
340 LTTNG_ASSERT(policy
&& default_policy
);
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");
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");
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");
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
);
372 /* Validate the default policy for a stop_session action. */
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(
381 "Default policy is every n=1");
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");
389 /* Validate the custom policy for a stop_session action. */
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(
398 "stop_session action policy get");
402 ret
= lttng_action_serialize(stop_session_action
, &payload
);
403 ok(ret
== 0, "Action stop_session serialized");
406 struct lttng_payload_view view
=
407 lttng_payload_view_from_payload(
409 (void) lttng_action_create_from_payload(
410 &view
, &stop_session_action_from_buffer
);
412 ok(stop_session_action_from_buffer
,
413 "stop_session action created from payload is non-null");
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");
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
);
426 static void test_action_snapshot_session(void)
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
;
437 lttng_payload_init(&payload
);
440 policy
= lttng_rate_policy_every_n_create(100);
441 /* For comparison. */
442 default_policy
= lttng_rate_policy_every_n_create(1);
444 LTTNG_ASSERT(policy
&& default_policy
);
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");
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(
462 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
463 "Set session name (NULL,object) expect invalid");
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");
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
);
477 /* Validate the default policy for a snapshot_session action. */
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(
486 "Default policy is every n=1");
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");
494 /* Validate the custom policy for a snapshot_session action. */
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(
503 "snapshot_session action policy get");
507 ret
= lttng_action_serialize(snapshot_session_action
, &payload
);
508 ok(ret
== 0, "Action snapshot_session serialized");
511 struct lttng_payload_view view
=
512 lttng_payload_view_from_payload(
514 (void) lttng_action_create_from_payload(
515 &view
, &snapshot_session_action_from_buffer
);
517 ok(snapshot_session_action_from_buffer
,
518 "snapshot_session action created from payload is non-null");
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");
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
);
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();