4 * Tests suite for LTTng notification API (rotation notifications)
6 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include <lttng/rotation.h>
33 #include <lttng/notification/channel.h>
34 #include <lttng/notification/notification.h>
35 #include <lttng/condition/evaluation.h>
36 #include <lttng/condition/condition.h>
37 #include <lttng/endpoint.h>
38 #include <lttng/action/notify.h>
39 #include <lttng/action/action.h>
40 #include <lttng/trigger/trigger.h>
41 #include <lttng/condition/session-rotation.h>
48 const char *output_path
;
51 uint64_t expected_rotation_id
= UINT64_MAX
;
54 int test_condition(struct lttng_condition
*condition
, const char *type_name
)
57 const char *out_session_name
;
58 const char * const session_name
= "test session name";
59 enum lttng_condition_status status
;
61 status
= lttng_condition_session_rotation_get_session_name(condition
,
63 ok(status
== LTTNG_CONDITION_STATUS_UNSET
,
64 "Getting unset name of %s condition fails with LTTNG_CONDITION_STATUS_UNSET",
67 status
= lttng_condition_session_rotation_set_session_name(condition
,
69 ok(status
== LTTNG_CONDITION_STATUS_OK
,
70 "Setting session name \"%s\" of %s condition succeeds",
71 session_name
, type_name
);
73 status
= lttng_condition_session_rotation_get_session_name(condition
,
75 ok(status
== LTTNG_CONDITION_STATUS_OK
,
76 "Getting name of %s condition succeeds",
79 ok(out_session_name
&& !strcmp(session_name
, out_session_name
),
80 "Session name returned by %s condition matches the expected name",
86 int setup_rotation_trigger(const struct session
*session
,
87 struct lttng_notification_channel
*notification_channel
)
90 struct lttng_condition
*rotation_ongoing_condition
= NULL
;
91 struct lttng_condition
*rotation_completed_condition
= NULL
;
92 struct lttng_action
*notify
= NULL
;
93 struct lttng_trigger
*rotation_ongoing_trigger
= NULL
;
94 struct lttng_trigger
*rotation_completed_trigger
= NULL
;
95 enum lttng_condition_status condition_status
;
96 enum lttng_notification_channel_status notification_channel_status
;
98 notify
= lttng_action_notify_create();
104 /* Create rotation ongoing and completed conditions. */
105 rotation_ongoing_condition
=
106 lttng_condition_session_rotation_ongoing_create();
107 ok(rotation_ongoing_condition
, "Create session rotation ongoing condition");
108 if (!rotation_ongoing_condition
) {
112 ret
= test_condition(rotation_ongoing_condition
, "rotation ongoing");
116 condition_status
= lttng_condition_session_rotation_set_session_name(
117 rotation_ongoing_condition
, session
->name
);
118 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
120 diag("Failed to set session name on session rotation ongoing condition");
124 rotation_completed_condition
=
125 lttng_condition_session_rotation_completed_create();
126 ok(rotation_completed_condition
, "Create session rotation completed condition");
127 if (!rotation_completed_condition
) {
131 ret
= test_condition(rotation_completed_condition
, "rotation completed");
133 diag("Failed to set session name on session rotation completed condition");
136 condition_status
= lttng_condition_session_rotation_set_session_name(
137 rotation_completed_condition
, session
->name
);
138 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
143 notification_channel_status
= lttng_notification_channel_subscribe(
144 notification_channel
, rotation_ongoing_condition
);
145 ok(notification_channel_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
,
146 "Subscribe to session rotation ongoing notifications");
147 if (notification_channel_status
!=
148 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
152 notification_channel_status
= lttng_notification_channel_subscribe(
153 notification_channel
, rotation_completed_condition
);
154 ok(notification_channel_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
,
155 "Subscribe to session rotation completed notifications");
156 if (notification_channel_status
!=
157 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
162 /* Create rotation ongoing and completed triggers. */
163 rotation_ongoing_trigger
= lttng_trigger_create(
164 rotation_ongoing_condition
, notify
);
165 ok(rotation_ongoing_trigger
, "Create a rotation ongoing notification trigger");
166 if (!rotation_ongoing_trigger
) {
171 rotation_completed_trigger
= lttng_trigger_create(
172 rotation_completed_condition
, notify
);
173 ok(rotation_completed_trigger
, "Create a rotation completed notification trigger");
174 if (!rotation_completed_trigger
) {
179 /* Register rotation ongoing and completed triggers. */
180 ret
= lttng_register_trigger(rotation_ongoing_trigger
);
181 ok(ret
== 0, "Registered session rotation ongoing trigger");
186 ret
= lttng_register_trigger(rotation_completed_trigger
);
187 ok(ret
== 0, "Registered session rotation completed trigger");
192 lttng_trigger_destroy(rotation_ongoing_trigger
);
193 lttng_trigger_destroy(rotation_completed_trigger
);
194 lttng_condition_destroy(rotation_ongoing_condition
);
195 lttng_condition_destroy(rotation_completed_condition
);
196 lttng_action_destroy(notify
);
201 int test_notification(
202 struct lttng_notification_channel
*notification_channel
,
203 const struct session
*session
,
204 const char *expected_notification_type_name
,
205 enum lttng_condition_type expected_condition_type
)
208 bool notification_pending
;
209 enum lttng_notification_channel_status notification_channel_status
;
210 enum lttng_condition_status condition_status
;
211 enum lttng_evaluation_status evaluation_status
;
212 enum lttng_trace_archive_location_status location_status
;
213 enum lttng_condition_type condition_type
;
214 struct lttng_notification
*notification
= NULL
;
215 const struct lttng_condition
*condition
;
216 const struct lttng_evaluation
*evaluation
;
217 const char *session_name
= NULL
;
218 const struct lttng_trace_archive_location
*location
= NULL
;
219 uint64_t rotation_id
= UINT64_MAX
;
220 const char *chunk_path
= NULL
;
222 notification_channel_status
= lttng_notification_channel_has_pending_notification(
223 notification_channel
, ¬ification_pending
);
224 ok(notification_channel_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
,
225 "Check for %s notification pending on notification channel",
226 expected_notification_type_name
);
227 if (notification_channel_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
232 ok(notification_pending
,
233 "Session %s notification is pending on notification channel",
234 expected_notification_type_name
);
235 if (!notification_pending
) {
240 notification_channel_status
= lttng_notification_channel_get_next_notification(
241 notification_channel
, ¬ification
);
242 ok(notification_channel_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
,
243 "Get %s notification from notification channel",
244 expected_notification_type_name
);
245 if (notification_channel_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
|| !notification
) {
250 condition
= lttng_notification_get_condition(notification
);
252 diag("Failed to get notification condition");
257 condition_type
= lttng_condition_get_type(condition
);
258 ok(condition_type
== expected_condition_type
,
259 "Notification condition obtained from notification channel is of type \"%s\"",
260 expected_notification_type_name
);
261 if (condition_type
!= expected_condition_type
) {
266 condition_status
= lttng_condition_session_rotation_get_session_name(
267 condition
, &session_name
);
268 ok(condition_status
== LTTNG_CONDITION_STATUS_OK
&& session_name
&&
269 !strcmp(session_name
, session
->name
),
270 "Condition obtained from notification has the correct session name assigned");
271 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
|| !session_name
) {
276 evaluation
= lttng_notification_get_evaluation(notification
);
278 diag("Failed to get notification evaluation");
282 condition_type
= lttng_evaluation_get_type(evaluation
);
283 ok(condition_type
== expected_condition_type
,
284 "Condition evaluation obtained from notification channel is of type \"%s\"",
285 expected_notification_type_name
);
286 if (condition_type
!= expected_condition_type
) {
291 evaluation_status
= lttng_evaluation_session_rotation_get_id(evaluation
,
293 ok(evaluation_status
== LTTNG_EVALUATION_STATUS_OK
,
294 "Get %s id from notification evaluation",
295 expected_notification_type_name
);
296 if (evaluation_status
!= LTTNG_EVALUATION_STATUS_OK
) {
301 if (expected_condition_type
!= LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
) {
303 * Remaining tests only apply to "session rotation completed"
309 evaluation_status
= lttng_evaluation_session_rotation_completed_get_location(
310 evaluation
, &location
);
311 ok(evaluation_status
== LTTNG_EVALUATION_STATUS_OK
&& location
,
312 "Get session %s chunk location from evaluation",
313 expected_notification_type_name
);
314 if (evaluation_status
!= LTTNG_EVALUATION_STATUS_OK
|| !location
) {
319 ok(lttng_trace_archive_location_get_type(location
) == LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
,
320 "Location returned from the session rotation completed notification is of type 'local'");
322 location_status
= lttng_trace_archive_location_local_get_absolute_path(
323 location
, &chunk_path
);
324 ok(location_status
== LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
&& chunk_path
,
325 "Retrieved path from location returned by the session rotation completed notification");
326 diag("Chunk available at %s", chunk_path
? chunk_path
: "NULL");
328 ok(chunk_path
&& !strncmp(session
->output_path
, chunk_path
, strlen(session
->output_path
)),
329 "Returned path from location starts with the output path");
332 lttng_notification_destroy(notification
);
337 int test_rotation_ongoing_notification(
338 struct lttng_notification_channel
*notification_channel
,
339 struct session
*session
)
341 return test_notification(notification_channel
, session
,
343 LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
);
347 int test_rotation_completed_notification(
348 struct lttng_notification_channel
*notification_channel
,
349 struct session
*session
)
351 return test_notification(notification_channel
, session
,
352 "rotation completed",
353 LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
);
356 int main(int argc
, const char *argv
[])
359 struct session session
= { 0 };
360 struct lttng_notification_channel
*notification_channel
= NULL
;
361 struct lttng_rotation_handle
*rotation_handle
= NULL
;
362 enum lttng_rotation_status rotation_status
;
363 enum lttng_rotation_state rotation_state
=
364 LTTNG_ROTATION_STATE_NO_ROTATION
;
367 puts("Usage: rotation SESSION_NAME SESSION_OUTPUT_PATH");
372 session
.name
= argv
[1];
373 session
.output_path
= argv
[2];
375 plan_tests(TEST_COUNT
);
377 notification_channel
= lttng_notification_channel_create(
378 lttng_session_daemon_notification_endpoint
);
379 if (!notification_channel
) {
380 diag("Failed to create notification channel");
385 ret
= setup_rotation_trigger(&session
, notification_channel
);
390 /* Start rotation and wait for its completion. */
391 ret
= lttng_rotate_session(session
.name
, NULL
, &rotation_handle
);
392 ok(ret
>= 0 && rotation_handle
, "Start rotation of session \"%s\"",
394 if (ret
< 0 || !rotation_handle
) {
399 rotation_status
= lttng_rotation_handle_get_state(
400 rotation_handle
, &rotation_state
);
401 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
402 rotation_status
== LTTNG_ROTATION_STATUS_OK
);
403 ok(rotation_status
== LTTNG_ROTATION_STATUS_OK
&&
404 rotation_state
== LTTNG_ROTATION_STATE_COMPLETED
,
405 "Complete rotation of session \"%s\"", session
.name
);
408 * After a rotation has completed, we can expect two notifications to
410 * - Session rotation ongoing
411 * - Session rotation completed
413 ret
= test_rotation_ongoing_notification(notification_channel
,
419 ret
= test_rotation_completed_notification(notification_channel
,
425 lttng_notification_channel_destroy(notification_channel
);
426 lttng_rotation_handle_destroy(rotation_handle
);