4 * Unit tests for the notification API.
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
8 * SPDX-License-Identifier: MIT
20 #include <common/payload-view.h>
21 #include <common/payload.h>
22 #include <lttng/action/action-internal.h>
23 #include <lttng/action/action.h>
24 #include <lttng/action/notify.h>
25 #include <lttng/action/rate-policy-internal.h>
26 #include <lttng/action/rate-policy.h>
27 #include <lttng/action/rotate-session.h>
28 #include <lttng/action/snapshot-session.h>
29 #include <lttng/action/start-session.h>
30 #include <lttng/action/stop-session.h>
33 int lttng_opt_quiet
= 1;
34 int lttng_opt_verbose
;
39 static void test_action_notify(void)
42 enum lttng_action_status status
;
43 struct lttng_action
*notify_action
= NULL
,
44 *notify_action_from_buffer
= NULL
;
45 struct lttng_rate_policy
*policy
= NULL
, *default_policy
;
46 struct lttng_payload payload
;
48 lttng_payload_init(&payload
);
51 policy
= lttng_rate_policy_every_n_create(100);
53 default_policy
= lttng_rate_policy_every_n_create(1);
55 assert(policy
&& default_policy
);
57 notify_action
= lttng_action_notify_create();
58 ok(notify_action
, "Create notify action");
59 ok(lttng_action_get_type(notify_action
) == LTTNG_ACTION_TYPE_NOTIFY
,
60 "Action has type LTTNG_ACTION_TYPE_NOTIFY");
62 /* Validate the default policy for a notify action. */
64 const struct lttng_rate_policy
*cur_policy
= NULL
;
65 status
= lttng_action_notify_get_rate_policy(
66 notify_action
, &cur_policy
);
67 ok(status
== LTTNG_ACTION_STATUS_OK
&&
68 lttng_rate_policy_is_equal(
71 "Default policy is every n=1");
74 /* Set a custom policy. */
75 status
= lttng_action_notify_set_rate_policy(notify_action
, policy
);
76 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set rate policy");
78 /* Validate the custom policy for a notify action. */
80 const struct lttng_rate_policy
*cur_policy
= NULL
;
81 status
= lttng_action_notify_get_rate_policy(
82 notify_action
, &cur_policy
);
83 ok(status
== LTTNG_ACTION_STATUS_OK
&&
84 lttng_rate_policy_is_equal(
87 "Notify action policy get");
90 ret
= lttng_action_serialize(notify_action
, &payload
);
91 ok(ret
== 0, "Action notify serialized");
94 struct lttng_payload_view view
=
95 lttng_payload_view_from_payload(
97 (void) lttng_action_create_from_payload(
98 &view
, ¬ify_action_from_buffer
);
100 ok(notify_action_from_buffer
,
101 "Notify action created from payload is non-null");
103 ok(lttng_action_is_equal(notify_action
, notify_action_from_buffer
),
104 "Serialized and de-serialized notify action are equal");
106 lttng_rate_policy_destroy(default_policy
);
107 lttng_rate_policy_destroy(policy
);
108 lttng_action_destroy(notify_action
);
109 lttng_action_destroy(notify_action_from_buffer
);
110 lttng_payload_reset(&payload
);
113 static void test_action_rotate_session(void)
116 enum lttng_action_status status
;
117 struct lttng_action
*rotate_session_action
= NULL
,
118 *rotate_session_action_from_buffer
= NULL
;
119 struct lttng_rate_policy
*policy
= NULL
, *default_policy
;
120 struct lttng_payload payload
;
121 const char *session_name
= "my_session_name";
122 const char *get_session_name
;
124 lttng_payload_init(&payload
);
127 policy
= lttng_rate_policy_every_n_create(100);
128 /* For comparison. */
129 default_policy
= lttng_rate_policy_every_n_create(1);
131 assert(policy
&& default_policy
);
133 rotate_session_action
= lttng_action_rotate_session_create();
134 ok(rotate_session_action
, "Create rotate_session action");
135 ok(lttng_action_get_type(rotate_session_action
) ==
136 LTTNG_ACTION_TYPE_ROTATE_SESSION
,
137 "Action has type LTTNG_ACTION_TYPE_ROTATE_SESSION");
139 /* Session name setter. */
140 status
= lttng_action_rotate_session_set_session_name(NULL
, NULL
);
141 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
142 "Set session name (NULL,NULL) expect invalid");
143 status
= lttng_action_rotate_session_set_session_name(
144 rotate_session_action
, NULL
);
145 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
146 "Set session name (object,NULL) expect invalid");
147 status
= lttng_action_rotate_session_set_session_name(
149 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
150 "Set session name (NULL,object) expect invalid");
152 /* Set the session name */
153 status
= lttng_action_rotate_session_set_session_name(
154 rotate_session_action
, session_name
);
155 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set session name");
157 status
= lttng_action_rotate_session_get_session_name(
158 rotate_session_action
, &get_session_name
);
159 ok(status
== LTTNG_ACTION_STATUS_OK
&&
160 !strcmp(session_name
, get_session_name
),
161 "Get session name, expected `%s` got `%s`",
162 session_name
, get_session_name
);
164 /* Validate the default policy for a rotate_session action. */
166 const struct lttng_rate_policy
*cur_policy
= NULL
;
167 status
= lttng_action_rotate_session_get_rate_policy(
168 rotate_session_action
, &cur_policy
);
169 ok(status
== LTTNG_ACTION_STATUS_OK
&&
170 lttng_rate_policy_is_equal(
173 "Default policy is every n=1");
176 /* Set a custom policy. */
177 status
= lttng_action_rotate_session_set_rate_policy(
178 rotate_session_action
, policy
);
179 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set rate policy");
181 /* Validate the custom policy for a rotate_session action. */
183 const struct lttng_rate_policy
*cur_policy
= NULL
;
184 status
= lttng_action_rotate_session_get_rate_policy(
185 rotate_session_action
, &cur_policy
);
186 ok(status
== LTTNG_ACTION_STATUS_OK
&&
187 lttng_rate_policy_is_equal(
190 "rotate_session action policy get");
194 ret
= lttng_action_serialize(rotate_session_action
, &payload
);
195 ok(ret
== 0, "Action rotate_session serialized");
198 struct lttng_payload_view view
=
199 lttng_payload_view_from_payload(
201 (void) lttng_action_create_from_payload(
202 &view
, &rotate_session_action_from_buffer
);
204 ok(rotate_session_action_from_buffer
,
205 "rotate_session action created from payload is non-null");
207 ok(lttng_action_is_equal(rotate_session_action
,
208 rotate_session_action_from_buffer
),
209 "Serialized and de-serialized rotate_session action are equal");
211 lttng_rate_policy_destroy(default_policy
);
212 lttng_rate_policy_destroy(policy
);
213 lttng_action_destroy(rotate_session_action
);
214 lttng_action_destroy(rotate_session_action_from_buffer
);
215 lttng_payload_reset(&payload
);
218 static void test_action_start_session(void)
221 enum lttng_action_status status
;
222 struct lttng_action
*start_session_action
= NULL
,
223 *start_session_action_from_buffer
= NULL
;
224 struct lttng_rate_policy
*policy
= NULL
, *default_policy
;
225 struct lttng_payload payload
;
226 const char *session_name
= "my_session_name";
227 const char *get_session_name
;
229 lttng_payload_init(&payload
);
232 policy
= lttng_rate_policy_every_n_create(100);
233 /* For comparison. */
234 default_policy
= lttng_rate_policy_every_n_create(1);
236 assert(policy
&& default_policy
);
238 start_session_action
= lttng_action_start_session_create();
239 ok(start_session_action
, "Create start_session action");
240 ok(lttng_action_get_type(start_session_action
) ==
241 LTTNG_ACTION_TYPE_START_SESSION
,
242 "Action has type LTTNG_ACTION_TYPE_START_SESSION");
244 /* Session name setter. */
245 status
= lttng_action_start_session_set_session_name(NULL
, NULL
);
246 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
247 "Set session name (NULL,NULL) expect invalid");
248 status
= lttng_action_start_session_set_session_name(
249 start_session_action
, NULL
);
250 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
251 "Set session name (object,NULL) expect invalid");
252 status
= lttng_action_start_session_set_session_name(
254 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
255 "Set session name (NULL,object) expect invalid");
257 /* Set the session name */
258 status
= lttng_action_start_session_set_session_name(
259 start_session_action
, session_name
);
260 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set session name");
262 status
= lttng_action_start_session_get_session_name(
263 start_session_action
, &get_session_name
);
264 ok(status
== LTTNG_ACTION_STATUS_OK
&&
265 !strcmp(session_name
, get_session_name
),
266 "Get session name, expected `%s` got `%s`",
267 session_name
, get_session_name
);
269 /* Validate the default policy for a start_session action. */
271 const struct lttng_rate_policy
*cur_policy
= NULL
;
272 status
= lttng_action_start_session_get_rate_policy(
273 start_session_action
, &cur_policy
);
274 ok(status
== LTTNG_ACTION_STATUS_OK
&&
275 lttng_rate_policy_is_equal(
278 "Default policy is every n=1");
281 /* Set a custom policy. */
282 status
= lttng_action_start_session_set_rate_policy(
283 start_session_action
, policy
);
284 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set rate policy");
286 /* Validate the custom policy for a start_session action. */
288 const struct lttng_rate_policy
*cur_policy
= NULL
;
289 status
= lttng_action_start_session_get_rate_policy(
290 start_session_action
, &cur_policy
);
291 ok(status
== LTTNG_ACTION_STATUS_OK
&&
292 lttng_rate_policy_is_equal(
295 "start_session action policy get");
299 ret
= lttng_action_serialize(start_session_action
, &payload
);
300 ok(ret
== 0, "Action start_session serialized");
303 struct lttng_payload_view view
=
304 lttng_payload_view_from_payload(
306 (void) lttng_action_create_from_payload(
307 &view
, &start_session_action_from_buffer
);
309 ok(start_session_action_from_buffer
,
310 "start_session action created from payload is non-null");
312 ok(lttng_action_is_equal(start_session_action
,
313 start_session_action_from_buffer
),
314 "Serialized and de-serialized start_session action are equal");
316 lttng_rate_policy_destroy(default_policy
);
317 lttng_rate_policy_destroy(policy
);
318 lttng_action_destroy(start_session_action
);
319 lttng_action_destroy(start_session_action_from_buffer
);
320 lttng_payload_reset(&payload
);
323 static void test_action_stop_session(void)
326 enum lttng_action_status status
;
327 struct lttng_action
*stop_session_action
= NULL
,
328 *stop_session_action_from_buffer
= NULL
;
329 struct lttng_rate_policy
*policy
= NULL
, *default_policy
;
330 struct lttng_payload payload
;
331 const char *session_name
= "my_session_name";
332 const char *get_session_name
;
334 lttng_payload_init(&payload
);
337 policy
= lttng_rate_policy_every_n_create(100);
338 /* For comparison. */
339 default_policy
= lttng_rate_policy_every_n_create(1);
341 assert(policy
&& default_policy
);
343 stop_session_action
= lttng_action_stop_session_create();
344 ok(stop_session_action
, "Create stop_session action");
345 ok(lttng_action_get_type(stop_session_action
) ==
346 LTTNG_ACTION_TYPE_STOP_SESSION
,
347 "Action has type LTTNG_ACTION_TYPE_STOP_SESSION");
349 /* Session name setter. */
350 status
= lttng_action_stop_session_set_session_name(NULL
, NULL
);
351 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
352 "Set session name (NULL,NULL) expect invalid");
353 status
= lttng_action_stop_session_set_session_name(
354 stop_session_action
, NULL
);
355 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
356 "Set session name (object,NULL) expect invalid");
357 status
= lttng_action_stop_session_set_session_name(NULL
, session_name
);
358 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
359 "Set session name (NULL,object) expect invalid");
361 /* Set the session name */
362 status
= lttng_action_stop_session_set_session_name(
363 stop_session_action
, session_name
);
364 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set session name");
366 status
= lttng_action_stop_session_get_session_name(
367 stop_session_action
, &get_session_name
);
368 ok(status
== LTTNG_ACTION_STATUS_OK
&&
369 !strcmp(session_name
, get_session_name
),
370 "Get session name, expected `%s` got `%s`",
371 session_name
, get_session_name
);
373 /* Validate the default policy for a stop_session action. */
375 const struct lttng_rate_policy
*cur_policy
= NULL
;
376 status
= lttng_action_stop_session_get_rate_policy(
377 stop_session_action
, &cur_policy
);
378 ok(status
== LTTNG_ACTION_STATUS_OK
&&
379 lttng_rate_policy_is_equal(
382 "Default policy is every n=1");
385 /* Set a custom policy. */
386 status
= lttng_action_stop_session_set_rate_policy(
387 stop_session_action
, policy
);
388 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set rate policy");
390 /* Validate the custom policy for a stop_session action. */
392 const struct lttng_rate_policy
*cur_policy
= NULL
;
393 status
= lttng_action_stop_session_get_rate_policy(
394 stop_session_action
, &cur_policy
);
395 ok(status
== LTTNG_ACTION_STATUS_OK
&&
396 lttng_rate_policy_is_equal(
399 "stop_session action policy get");
403 ret
= lttng_action_serialize(stop_session_action
, &payload
);
404 ok(ret
== 0, "Action stop_session serialized");
407 struct lttng_payload_view view
=
408 lttng_payload_view_from_payload(
410 (void) lttng_action_create_from_payload(
411 &view
, &stop_session_action_from_buffer
);
413 ok(stop_session_action_from_buffer
,
414 "stop_session action created from payload is non-null");
416 ok(lttng_action_is_equal(stop_session_action
,
417 stop_session_action_from_buffer
),
418 "Serialized and de-serialized stop_session action are equal");
420 lttng_rate_policy_destroy(default_policy
);
421 lttng_rate_policy_destroy(policy
);
422 lttng_action_destroy(stop_session_action
);
423 lttng_action_destroy(stop_session_action_from_buffer
);
424 lttng_payload_reset(&payload
);
427 static void test_action_snapshot_session(void)
430 enum lttng_action_status status
;
431 struct lttng_action
*snapshot_session_action
= NULL
,
432 *snapshot_session_action_from_buffer
= NULL
;
433 struct lttng_rate_policy
*policy
= NULL
, *default_policy
;
434 struct lttng_payload payload
;
435 const char *session_name
= "my_session_name";
436 const char *get_session_name
;
438 lttng_payload_init(&payload
);
441 policy
= lttng_rate_policy_every_n_create(100);
442 /* For comparison. */
443 default_policy
= lttng_rate_policy_every_n_create(1);
445 assert(policy
&& default_policy
);
447 snapshot_session_action
= lttng_action_snapshot_session_create();
448 ok(snapshot_session_action
, "Create snapshot_session action");
449 ok(lttng_action_get_type(snapshot_session_action
) ==
450 LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
,
451 "Action has type LTTNG_ACTION_TYPE_SNAPSHOT_SESSION");
453 /* Session name setter. */
454 status
= lttng_action_snapshot_session_set_session_name(NULL
, NULL
);
455 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
456 "Set session name (NULL,NULL) expect invalid");
457 status
= lttng_action_snapshot_session_set_session_name(
458 snapshot_session_action
, NULL
);
459 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
460 "Set session name (object,NULL) expect invalid");
461 status
= lttng_action_snapshot_session_set_session_name(
463 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
464 "Set session name (NULL,object) expect invalid");
466 /* Set the session name */
467 status
= lttng_action_snapshot_session_set_session_name(
468 snapshot_session_action
, session_name
);
469 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set session name");
471 status
= lttng_action_snapshot_session_get_session_name(
472 snapshot_session_action
, &get_session_name
);
473 ok(status
== LTTNG_ACTION_STATUS_OK
&&
474 !strcmp(session_name
, get_session_name
),
475 "Get session name, expected `%s` got `%s`",
476 session_name
, get_session_name
);
478 /* Validate the default policy for a snapshot_session action. */
480 const struct lttng_rate_policy
*cur_policy
= NULL
;
481 status
= lttng_action_snapshot_session_get_rate_policy(
482 snapshot_session_action
, &cur_policy
);
483 ok(status
== LTTNG_ACTION_STATUS_OK
&&
484 lttng_rate_policy_is_equal(
487 "Default policy is every n=1");
490 /* Set a custom policy. */
491 status
= lttng_action_snapshot_session_set_rate_policy(
492 snapshot_session_action
, policy
);
493 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set rate policy");
495 /* Validate the custom policy for a snapshot_session action. */
497 const struct lttng_rate_policy
*cur_policy
= NULL
;
498 status
= lttng_action_snapshot_session_get_rate_policy(
499 snapshot_session_action
, &cur_policy
);
500 ok(status
== LTTNG_ACTION_STATUS_OK
&&
501 lttng_rate_policy_is_equal(
504 "snapshot_session action policy get");
508 ret
= lttng_action_serialize(snapshot_session_action
, &payload
);
509 ok(ret
== 0, "Action snapshot_session serialized");
512 struct lttng_payload_view view
=
513 lttng_payload_view_from_payload(
515 (void) lttng_action_create_from_payload(
516 &view
, &snapshot_session_action_from_buffer
);
518 ok(snapshot_session_action_from_buffer
,
519 "snapshot_session action created from payload is non-null");
521 ok(lttng_action_is_equal(snapshot_session_action
,
522 snapshot_session_action_from_buffer
),
523 "Serialized and de-serialized snapshot_session action are equal");
525 lttng_rate_policy_destroy(default_policy
);
526 lttng_rate_policy_destroy(policy
);
527 lttng_action_destroy(snapshot_session_action
);
528 lttng_action_destroy(snapshot_session_action_from_buffer
);
529 lttng_payload_reset(&payload
);
532 int main(int argc
, const char *argv
[])
534 plan_tests(NUM_TESTS
);
535 test_action_notify();
536 test_action_rotate_session();
537 test_action_start_session();
538 test_action_stop_session();
539 test_action_snapshot_session();
540 return exit_status();