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/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>
31 int lttng_opt_quiet
= 1;
32 int lttng_opt_verbose
;
37 static void test_action_notify(void)
40 enum lttng_action_status status
;
41 struct lttng_action
*notify_action
= NULL
,
42 *notify_action_from_buffer
= NULL
;
43 struct lttng_firing_policy
*policy
= NULL
, *default_policy
;
44 struct lttng_payload payload
;
46 lttng_payload_init(&payload
);
49 policy
= lttng_firing_policy_every_n_create(100);
51 default_policy
= lttng_firing_policy_every_n_create(1);
53 assert(policy
&& default_policy
);
55 notify_action
= lttng_action_notify_create();
56 ok(notify_action
, "Create notify action");
57 ok(lttng_action_get_type(notify_action
) == LTTNG_ACTION_TYPE_NOTIFY
,
58 "Action has type LTTNG_ACTION_TYPE_NOTIFY");
60 /* Validate the default policy for a notify action. */
62 const struct lttng_firing_policy
*cur_policy
= NULL
;
63 status
= lttng_action_notify_get_firing_policy(
64 notify_action
, &cur_policy
);
65 ok(status
== LTTNG_ACTION_STATUS_OK
&&
66 lttng_firing_policy_is_equal(
69 "Default policy is every n=1");
72 /* Set a custom policy. */
73 status
= lttng_action_notify_set_firing_policy(notify_action
, policy
);
74 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set firing policy");
76 /* Validate the custom policy for a notify action. */
78 const struct lttng_firing_policy
*cur_policy
= NULL
;
79 status
= lttng_action_notify_get_firing_policy(
80 notify_action
, &cur_policy
);
81 ok(status
== LTTNG_ACTION_STATUS_OK
&&
82 lttng_firing_policy_is_equal(
85 "Notify action policy get");
88 ret
= lttng_action_serialize(notify_action
, &payload
);
89 ok(ret
== 0, "Action notify serialized");
92 struct lttng_payload_view view
=
93 lttng_payload_view_from_payload(
95 (void) lttng_action_create_from_payload(
96 &view
, ¬ify_action_from_buffer
);
98 ok(notify_action_from_buffer
,
99 "Notify action created from payload is non-null");
101 ok(lttng_action_is_equal(notify_action
, notify_action_from_buffer
),
102 "Serialized and de-serialized notify action are equal");
104 lttng_firing_policy_destroy(default_policy
);
105 lttng_firing_policy_destroy(policy
);
106 lttng_action_destroy(notify_action
);
107 lttng_action_destroy(notify_action_from_buffer
);
108 lttng_payload_reset(&payload
);
111 static void test_action_rotate_session(void)
114 enum lttng_action_status status
;
115 struct lttng_action
*rotate_session_action
= NULL
,
116 *rotate_session_action_from_buffer
= NULL
;
117 struct lttng_firing_policy
*policy
= NULL
, *default_policy
;
118 struct lttng_payload payload
;
119 const char *session_name
= "my_session_name";
120 const char *get_session_name
;
122 lttng_payload_init(&payload
);
125 policy
= lttng_firing_policy_every_n_create(100);
126 /* For comparison. */
127 default_policy
= lttng_firing_policy_every_n_create(1);
129 assert(policy
&& default_policy
);
131 rotate_session_action
= lttng_action_rotate_session_create();
132 ok(rotate_session_action
, "Create rotate_session action");
133 ok(lttng_action_get_type(rotate_session_action
) ==
134 LTTNG_ACTION_TYPE_ROTATE_SESSION
,
135 "Action has type LTTNG_ACTION_TYPE_ROTATE_SESSION");
137 /* Session name setter. */
138 status
= lttng_action_rotate_session_set_session_name(NULL
, NULL
);
139 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
140 "Set session name (NULL,NULL) expect invalid");
141 status
= lttng_action_rotate_session_set_session_name(
142 rotate_session_action
, NULL
);
143 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
144 "Set session name (object,NULL) expect invalid");
145 status
= lttng_action_rotate_session_set_session_name(
147 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
148 "Set session name (NULL,object) expect invalid");
150 /* Set the session name */
151 status
= lttng_action_rotate_session_set_session_name(
152 rotate_session_action
, session_name
);
153 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set session name");
155 status
= lttng_action_rotate_session_get_session_name(
156 rotate_session_action
, &get_session_name
);
157 ok(status
== LTTNG_ACTION_STATUS_OK
&&
158 !strcmp(session_name
, get_session_name
),
159 "Get session name, expected `%s` got `%s`",
160 session_name
, get_session_name
);
162 /* Validate the default policy for a rotate_session action. */
164 const struct lttng_firing_policy
*cur_policy
= NULL
;
165 status
= lttng_action_rotate_session_get_firing_policy(
166 rotate_session_action
, &cur_policy
);
167 ok(status
== LTTNG_ACTION_STATUS_OK
&&
168 lttng_firing_policy_is_equal(
171 "Default policy is every n=1");
174 /* Set a custom policy. */
175 status
= lttng_action_rotate_session_set_firing_policy(
176 rotate_session_action
, policy
);
177 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set firing policy");
179 /* Validate the custom policy for a rotate_session action. */
181 const struct lttng_firing_policy
*cur_policy
= NULL
;
182 status
= lttng_action_rotate_session_get_firing_policy(
183 rotate_session_action
, &cur_policy
);
184 ok(status
== LTTNG_ACTION_STATUS_OK
&&
185 lttng_firing_policy_is_equal(
188 "rotate_session action policy get");
192 ret
= lttng_action_serialize(rotate_session_action
, &payload
);
193 ok(ret
== 0, "Action rotate_session serialized");
196 struct lttng_payload_view view
=
197 lttng_payload_view_from_payload(
199 (void) lttng_action_create_from_payload(
200 &view
, &rotate_session_action_from_buffer
);
202 ok(rotate_session_action_from_buffer
,
203 "rotate_session action created from payload is non-null");
205 ok(lttng_action_is_equal(rotate_session_action
,
206 rotate_session_action_from_buffer
),
207 "Serialized and de-serialized rotate_session action are equal");
209 lttng_firing_policy_destroy(default_policy
);
210 lttng_firing_policy_destroy(policy
);
211 lttng_action_destroy(rotate_session_action
);
212 lttng_action_destroy(rotate_session_action_from_buffer
);
213 lttng_payload_reset(&payload
);
216 static void test_action_start_session(void)
219 enum lttng_action_status status
;
220 struct lttng_action
*start_session_action
= NULL
,
221 *start_session_action_from_buffer
= NULL
;
222 struct lttng_firing_policy
*policy
= NULL
, *default_policy
;
223 struct lttng_payload payload
;
224 const char *session_name
= "my_session_name";
225 const char *get_session_name
;
227 lttng_payload_init(&payload
);
230 policy
= lttng_firing_policy_every_n_create(100);
231 /* For comparison. */
232 default_policy
= lttng_firing_policy_every_n_create(1);
234 assert(policy
&& default_policy
);
236 start_session_action
= lttng_action_start_session_create();
237 ok(start_session_action
, "Create start_session action");
238 ok(lttng_action_get_type(start_session_action
) ==
239 LTTNG_ACTION_TYPE_START_SESSION
,
240 "Action has type LTTNG_ACTION_TYPE_START_SESSION");
242 /* Session name setter. */
243 status
= lttng_action_start_session_set_session_name(NULL
, NULL
);
244 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
245 "Set session name (NULL,NULL) expect invalid");
246 status
= lttng_action_start_session_set_session_name(
247 start_session_action
, NULL
);
248 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
249 "Set session name (object,NULL) expect invalid");
250 status
= lttng_action_start_session_set_session_name(
252 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
253 "Set session name (NULL,object) expect invalid");
255 /* Set the session name */
256 status
= lttng_action_start_session_set_session_name(
257 start_session_action
, session_name
);
258 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set session name");
260 status
= lttng_action_start_session_get_session_name(
261 start_session_action
, &get_session_name
);
262 ok(status
== LTTNG_ACTION_STATUS_OK
&&
263 !strcmp(session_name
, get_session_name
),
264 "Get session name, expected `%s` got `%s`",
265 session_name
, get_session_name
);
267 /* Validate the default policy for a start_session action. */
269 const struct lttng_firing_policy
*cur_policy
= NULL
;
270 status
= lttng_action_start_session_get_firing_policy(
271 start_session_action
, &cur_policy
);
272 ok(status
== LTTNG_ACTION_STATUS_OK
&&
273 lttng_firing_policy_is_equal(
276 "Default policy is every n=1");
279 /* Set a custom policy. */
280 status
= lttng_action_start_session_set_firing_policy(
281 start_session_action
, policy
);
282 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set firing policy");
284 /* Validate the custom policy for a start_session action. */
286 const struct lttng_firing_policy
*cur_policy
= NULL
;
287 status
= lttng_action_start_session_get_firing_policy(
288 start_session_action
, &cur_policy
);
289 ok(status
== LTTNG_ACTION_STATUS_OK
&&
290 lttng_firing_policy_is_equal(
293 "start_session action policy get");
297 ret
= lttng_action_serialize(start_session_action
, &payload
);
298 ok(ret
== 0, "Action start_session serialized");
301 struct lttng_payload_view view
=
302 lttng_payload_view_from_payload(
304 (void) lttng_action_create_from_payload(
305 &view
, &start_session_action_from_buffer
);
307 ok(start_session_action_from_buffer
,
308 "start_session action created from payload is non-null");
310 ok(lttng_action_is_equal(start_session_action
,
311 start_session_action_from_buffer
),
312 "Serialized and de-serialized start_session action are equal");
314 lttng_firing_policy_destroy(default_policy
);
315 lttng_firing_policy_destroy(policy
);
316 lttng_action_destroy(start_session_action
);
317 lttng_action_destroy(start_session_action_from_buffer
);
318 lttng_payload_reset(&payload
);
321 int main(int argc
, const char *argv
[])
323 plan_tests(NUM_TESTS
);
324 test_action_notify();
325 test_action_rotate_session();
326 test_action_start_session();
327 return exit_status();