tests: units: action: stop session
[lttng-tools.git] / tests / unit / test_action.c
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 <assert.h>
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include <tap/tap.h>
19
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/firing-policy-internal.h>
25 #include <lttng/action/firing-policy.h>
26 #include <lttng/action/notify.h>
27 #include <lttng/action/rotate-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 47
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_firing_policy *policy = NULL, *default_policy;
45 struct lttng_payload payload;
46
47 lttng_payload_init(&payload);
48
49 /* To set. */
50 policy = lttng_firing_policy_every_n_create(100);
51 /* For comparison. */
52 default_policy = lttng_firing_policy_every_n_create(1);
53
54 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_firing_policy *cur_policy = NULL;
64 status = lttng_action_notify_get_firing_policy(
65 notify_action, &cur_policy);
66 ok(status == LTTNG_ACTION_STATUS_OK &&
67 lttng_firing_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_firing_policy(notify_action, policy);
75 ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy");
76
77 /* Validate the custom policy for a notify action. */
78 {
79 const struct lttng_firing_policy *cur_policy = NULL;
80 status = lttng_action_notify_get_firing_policy(
81 notify_action, &cur_policy);
82 ok(status == LTTNG_ACTION_STATUS_OK &&
83 lttng_firing_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_firing_policy_destroy(default_policy);
106 lttng_firing_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_firing_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_firing_policy_every_n_create(100);
127 /* For comparison. */
128 default_policy = lttng_firing_policy_every_n_create(1);
129
130 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_firing_policy *cur_policy = NULL;
166 status = lttng_action_rotate_session_get_firing_policy(
167 rotate_session_action, &cur_policy);
168 ok(status == LTTNG_ACTION_STATUS_OK &&
169 lttng_firing_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_firing_policy(
177 rotate_session_action, policy);
178 ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy");
179
180 /* Validate the custom policy for a rotate_session action. */
181 {
182 const struct lttng_firing_policy *cur_policy = NULL;
183 status = lttng_action_rotate_session_get_firing_policy(
184 rotate_session_action, &cur_policy);
185 ok(status == LTTNG_ACTION_STATUS_OK &&
186 lttng_firing_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_firing_policy_destroy(default_policy);
211 lttng_firing_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_firing_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_firing_policy_every_n_create(100);
232 /* For comparison. */
233 default_policy = lttng_firing_policy_every_n_create(1);
234
235 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_firing_policy *cur_policy = NULL;
271 status = lttng_action_start_session_get_firing_policy(
272 start_session_action, &cur_policy);
273 ok(status == LTTNG_ACTION_STATUS_OK &&
274 lttng_firing_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_firing_policy(
282 start_session_action, policy);
283 ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy");
284
285 /* Validate the custom policy for a start_session action. */
286 {
287 const struct lttng_firing_policy *cur_policy = NULL;
288 status = lttng_action_start_session_get_firing_policy(
289 start_session_action, &cur_policy);
290 ok(status == LTTNG_ACTION_STATUS_OK &&
291 lttng_firing_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_firing_policy_destroy(default_policy);
316 lttng_firing_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_firing_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_firing_policy_every_n_create(100);
337 /* For comparison. */
338 default_policy = lttng_firing_policy_every_n_create(1);
339
340 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_firing_policy *cur_policy = NULL;
375 status = lttng_action_stop_session_get_firing_policy(
376 stop_session_action, &cur_policy);
377 ok(status == LTTNG_ACTION_STATUS_OK &&
378 lttng_firing_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_firing_policy(
386 stop_session_action, policy);
387 ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy");
388
389 /* Validate the custom policy for a stop_session action. */
390 {
391 const struct lttng_firing_policy *cur_policy = NULL;
392 status = lttng_action_stop_session_get_firing_policy(
393 stop_session_action, &cur_policy);
394 ok(status == LTTNG_ACTION_STATUS_OK &&
395 lttng_firing_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_firing_policy_destroy(default_policy);
420 lttng_firing_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 int main(int argc, const char *argv[])
427 {
428 plan_tests(NUM_TESTS);
429 test_action_notify();
430 test_action_rotate_session();
431 test_action_start_session();
432 test_action_stop_session();
433 return exit_status();
434 }
This page took 0.06137 seconds and 4 git commands to generate.