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>
30 int lttng_opt_quiet
= 1;
31 int lttng_opt_verbose
;
36 static void test_action_notify(void)
39 enum lttng_action_status status
;
40 struct lttng_action
*notify_action
= NULL
,
41 *notify_action_from_buffer
= NULL
;
42 struct lttng_firing_policy
*policy
= NULL
, *default_policy
;
43 struct lttng_payload payload
;
45 lttng_payload_init(&payload
);
48 policy
= lttng_firing_policy_every_n_create(100);
50 default_policy
= lttng_firing_policy_every_n_create(1);
52 assert(policy
&& default_policy
);
54 notify_action
= lttng_action_notify_create();
55 ok(notify_action
, "Create notify action");
56 ok(lttng_action_get_type(notify_action
) == LTTNG_ACTION_TYPE_NOTIFY
,
57 "Action has type LTTNG_ACTION_TYPE_NOTIFY");
59 /* Validate the default policy for a notify action. */
61 const struct lttng_firing_policy
*cur_policy
= NULL
;
62 status
= lttng_action_notify_get_firing_policy(
63 notify_action
, &cur_policy
);
64 ok(status
== LTTNG_ACTION_STATUS_OK
&&
65 lttng_firing_policy_is_equal(
68 "Default policy is every n=1");
71 /* Set a custom policy. */
72 status
= lttng_action_notify_set_firing_policy(notify_action
, policy
);
73 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set firing policy");
75 /* Validate the custom policy for a notify action. */
77 const struct lttng_firing_policy
*cur_policy
= NULL
;
78 status
= lttng_action_notify_get_firing_policy(
79 notify_action
, &cur_policy
);
80 ok(status
== LTTNG_ACTION_STATUS_OK
&&
81 lttng_firing_policy_is_equal(
84 "Notify action policy get");
87 ret
= lttng_action_serialize(notify_action
, &payload
);
88 ok(ret
== 0, "Action notify serialized");
91 struct lttng_payload_view view
=
92 lttng_payload_view_from_payload(
94 (void) lttng_action_create_from_payload(
95 &view
, ¬ify_action_from_buffer
);
97 ok(notify_action_from_buffer
,
98 "Notify action created from payload is non-null");
100 ok(lttng_action_is_equal(notify_action
, notify_action_from_buffer
),
101 "Serialized and de-serialized notify action are equal");
103 lttng_firing_policy_destroy(default_policy
);
104 lttng_firing_policy_destroy(policy
);
105 lttng_action_destroy(notify_action
);
106 lttng_action_destroy(notify_action_from_buffer
);
107 lttng_payload_reset(&payload
);
110 static void test_action_rotate_session(void)
113 enum lttng_action_status status
;
114 struct lttng_action
*rotate_session_action
= NULL
,
115 *rotate_session_action_from_buffer
= NULL
;
116 struct lttng_firing_policy
*policy
= NULL
, *default_policy
;
117 struct lttng_payload payload
;
118 const char *session_name
= "my_session_name";
119 const char *get_session_name
;
121 lttng_payload_init(&payload
);
124 policy
= lttng_firing_policy_every_n_create(100);
125 /* For comparison. */
126 default_policy
= lttng_firing_policy_every_n_create(1);
128 assert(policy
&& default_policy
);
130 rotate_session_action
= lttng_action_rotate_session_create();
131 ok(rotate_session_action
, "Create rotate_session action");
132 ok(lttng_action_get_type(rotate_session_action
) ==
133 LTTNG_ACTION_TYPE_ROTATE_SESSION
,
134 "Action has type LTTNG_ACTION_TYPE_ROTATE_SESSION");
136 /* Session name setter. */
137 status
= lttng_action_rotate_session_set_session_name(NULL
, NULL
);
138 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
139 "Set session name (NULL,NULL) expect invalid");
140 status
= lttng_action_rotate_session_set_session_name(
141 rotate_session_action
, NULL
);
142 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
143 "Set session name (object,NULL) expect invalid");
144 status
= lttng_action_rotate_session_set_session_name(
146 ok(status
== LTTNG_ACTION_STATUS_INVALID
,
147 "Set session name (NULL,object) expect invalid");
149 /* Set the session name */
150 status
= lttng_action_rotate_session_set_session_name(
151 rotate_session_action
, session_name
);
152 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set session name");
154 status
= lttng_action_rotate_session_get_session_name(
155 rotate_session_action
, &get_session_name
);
156 ok(status
== LTTNG_ACTION_STATUS_OK
&&
157 !strcmp(session_name
, get_session_name
),
158 "Get session name, expected `%s` got `%s`",
159 session_name
, get_session_name
);
161 /* Validate the default policy for a rotate_session action. */
163 const struct lttng_firing_policy
*cur_policy
= NULL
;
164 status
= lttng_action_rotate_session_get_firing_policy(
165 rotate_session_action
, &cur_policy
);
166 ok(status
== LTTNG_ACTION_STATUS_OK
&&
167 lttng_firing_policy_is_equal(
170 "Default policy is every n=1");
173 /* Set a custom policy. */
174 status
= lttng_action_rotate_session_set_firing_policy(
175 rotate_session_action
, policy
);
176 ok(status
== LTTNG_ACTION_STATUS_OK
, "Set firing policy");
178 /* Validate the custom policy for a rotate_session action. */
180 const struct lttng_firing_policy
*cur_policy
= NULL
;
181 status
= lttng_action_rotate_session_get_firing_policy(
182 rotate_session_action
, &cur_policy
);
183 ok(status
== LTTNG_ACTION_STATUS_OK
&&
184 lttng_firing_policy_is_equal(
187 "rotate_session action policy get");
191 ret
= lttng_action_serialize(rotate_session_action
, &payload
);
192 ok(ret
== 0, "Action rotate_session serialized");
195 struct lttng_payload_view view
=
196 lttng_payload_view_from_payload(
198 (void) lttng_action_create_from_payload(
199 &view
, &rotate_session_action_from_buffer
);
201 ok(rotate_session_action_from_buffer
,
202 "rotate_session action created from payload is non-null");
204 ok(lttng_action_is_equal(rotate_session_action
,
205 rotate_session_action_from_buffer
),
206 "Serialized and de-serialized rotate_session action are equal");
208 lttng_firing_policy_destroy(default_policy
);
209 lttng_firing_policy_destroy(policy
);
210 lttng_action_destroy(rotate_session_action
);
211 lttng_action_destroy(rotate_session_action_from_buffer
);
212 lttng_payload_reset(&payload
);
215 int main(int argc
, const char *argv
[])
217 plan_tests(NUM_TESTS
);
218 test_action_notify();
219 test_action_rotate_session();
220 return exit_status();