4 * Unit tests for the session rotation schedule API
6 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * SPDX-License-Identifier: MIT
16 #include <lttng/lttng.h>
20 #define SIZE_THRESHOLD_BYTES 1024
21 #define PERIODIC_TIME_US 1000000
23 const char *session_name
;
25 bool schedules_equal(const struct lttng_rotation_schedule
*a
,
26 const struct lttng_rotation_schedule
*b
)
29 enum lttng_rotation_schedule_type a_type
, b_type
;
30 uint64_t a_value
, b_value
;
31 enum lttng_rotation_status status
;
33 a_type
= lttng_rotation_schedule_get_type(a
);
34 b_type
= lttng_rotation_schedule_get_type(b
);
35 if (a_type
!= b_type
) {
36 diag("Schedules are not of the same type (%i != %i)",
42 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
44 status
= lttng_rotation_schedule_size_threshold_get_threshold(a
,
46 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
47 diag("Failed to retrieve size threshold of schedule 'a'");
50 status
= lttng_rotation_schedule_size_threshold_get_threshold(b
,
52 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
53 diag("Failed to retrieve size threshold of schedule 'b'");
58 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
60 status
= lttng_rotation_schedule_periodic_get_period(a
,
62 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
63 diag("Failed to retrieve period of schedule 'a'");
66 status
= lttng_rotation_schedule_periodic_get_period(b
,
68 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
69 diag("Failed to retrieve period of schedule 'b'");
75 diag("Unexpected schedule type: %i", a_type
);
79 equal
= a_value
== b_value
;
81 diag("Schedules have different values");
87 void test_add_null_session(void)
89 enum lttng_rotation_status status
;
90 struct lttng_rotation_schedule
*size_schedule
= NULL
;
92 size_schedule
= lttng_rotation_schedule_size_threshold_create();
94 status
= lttng_session_add_rotation_schedule(NULL
, size_schedule
);
95 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
96 "NULL session name rejected by lttng_session_add_rotation_schedule()");
97 lttng_rotation_schedule_destroy(size_schedule
);
100 void test_add_null_schedule(void)
102 enum lttng_rotation_status status
;
104 status
= lttng_session_add_rotation_schedule(session_name
, NULL
);
105 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
106 "NULL schedule rejected by lttng_session_add_rotation_schedule()");
109 void test_add_uninitialized_schedule(void)
111 enum lttng_rotation_status status
;
112 struct lttng_rotation_schedule
*size_schedule
= NULL
,
113 *periodic_schedule
= NULL
;
115 size_schedule
= lttng_rotation_schedule_size_threshold_create();
116 ok(size_schedule
, "Created a size threshold session rotation schedule");
118 status
= lttng_session_add_rotation_schedule(session_name
,
120 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
121 "Uninitialized size schedule rejected by lttng_session_add_rotation_schedule()");
123 periodic_schedule
= lttng_rotation_schedule_periodic_create();
124 ok(periodic_schedule
, "Created a periodic session rotation schedule");
126 status
= lttng_session_add_rotation_schedule(session_name
,
128 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
129 "Uninitialized periodic schedule rejected by lttng_session_add_rotation_schedule()");
131 lttng_rotation_schedule_destroy(size_schedule
);
132 lttng_rotation_schedule_destroy(periodic_schedule
);
135 void test_remove_null_session(void)
137 enum lttng_rotation_status status
;
138 struct lttng_rotation_schedule
*size_schedule
= NULL
;
140 size_schedule
= lttng_rotation_schedule_size_threshold_create();
142 status
= lttng_session_remove_rotation_schedule(NULL
, size_schedule
);
143 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
144 "NULL session name rejected by lttng_session_remove_rotation_schedule()");
145 lttng_rotation_schedule_destroy(size_schedule
);
148 void test_remove_null_schedule(void)
150 enum lttng_rotation_status status
;
152 status
= lttng_session_remove_rotation_schedule(session_name
, NULL
);
153 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
154 "NULL schedule rejected by lttng_session_remove_rotation_schedule()");
157 void test_remove_uninitialized_schedule(void)
159 enum lttng_rotation_status status
;
160 struct lttng_rotation_schedule
*size_schedule
= NULL
,
161 *periodic_schedule
= NULL
;
163 size_schedule
= lttng_rotation_schedule_size_threshold_create();
164 status
= lttng_session_remove_rotation_schedule(session_name
,
166 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
167 "Uninitialized size schedule rejected by lttng_session_remove_rotation_schedule()");
169 periodic_schedule
= lttng_rotation_schedule_periodic_create();
170 status
= lttng_session_remove_rotation_schedule(session_name
,
172 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
173 "Uninitialized periodic schedule rejected by lttng_session_remove_rotation_schedule()");
175 lttng_rotation_schedule_destroy(size_schedule
);
176 lttng_rotation_schedule_destroy(periodic_schedule
);
179 void test_uninitialized_schedule_get(void)
182 enum lttng_rotation_status status
;
183 struct lttng_rotation_schedule
*size_schedule
= NULL
,
184 *periodic_schedule
= NULL
;
186 size_schedule
= lttng_rotation_schedule_size_threshold_create();
187 periodic_schedule
= lttng_rotation_schedule_periodic_create();
189 status
= lttng_rotation_schedule_size_threshold_get_threshold(
190 size_schedule
, &value
);
191 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
192 "Getter on size threshold rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
193 status
= lttng_rotation_schedule_periodic_get_period(periodic_schedule
,
195 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
196 "Getter on periodic rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
198 lttng_rotation_schedule_destroy(size_schedule
);
199 lttng_rotation_schedule_destroy(periodic_schedule
);
203 void test_add_list_remove_schedule(
204 const struct lttng_rotation_schedule
*original_schedule
)
207 unsigned int schedules_count
= 0;
208 enum lttng_rotation_status status
;
209 const struct lttng_rotation_schedule
*list_schedule
;
210 struct lttng_rotation_schedules
*list_schedules
;
212 status
= lttng_session_add_rotation_schedule(session_name
,
214 ok(status
== LTTNG_ROTATION_STATUS_OK
,
215 "Add a rotation schedule to session \'%s\'",
218 ret
= lttng_session_list_rotation_schedules(session_name
,
220 ok(ret
== LTTNG_OK
&& list_schedules
,
221 "List rotation schedules of session \'%s\'",
224 status
= lttng_rotation_schedules_get_count(list_schedules
,
226 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 1,
227 "Listing returned 1 rotation schedule");
229 list_schedule
= lttng_rotation_schedules_get_at_index(list_schedules
,
232 "Obtain the first schedule of a schedules list");
234 ok(schedules_equal(original_schedule
, list_schedule
),
235 "Schedule returned by the listing is equal to the reference schedule that was added");
237 status
= lttng_session_remove_rotation_schedule(session_name
,
239 ok(status
== LTTNG_ROTATION_STATUS_OK
,
240 "Remove rotation schedule returned by the schedules listing");
241 lttng_rotation_schedules_destroy(list_schedules
);
243 (void) lttng_session_list_rotation_schedules(session_name
,
245 status
= lttng_rotation_schedules_get_count(list_schedules
,
247 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 0,
248 "Listing returned 0 rotation schedules after removal");
252 void test_add_list_remove_size_schedule(void)
254 struct lttng_rotation_schedule
*size_schedule
;
256 diag("Add, list, and remove a size threshold rotation schedule");
257 size_schedule
= lttng_rotation_schedule_size_threshold_create();
258 (void) lttng_rotation_schedule_size_threshold_set_threshold(
259 size_schedule
, SIZE_THRESHOLD_BYTES
);
260 test_add_list_remove_schedule(size_schedule
);
261 lttng_rotation_schedule_destroy(size_schedule
);
264 void test_add_list_remove_periodic_schedule(void)
266 struct lttng_rotation_schedule
*periodic_schedule
;
268 diag("Add, list, and remove a periodic rotation schedule");
269 periodic_schedule
= lttng_rotation_schedule_periodic_create();
270 (void) lttng_rotation_schedule_periodic_set_period(
271 periodic_schedule
, PERIODIC_TIME_US
);
272 test_add_list_remove_schedule(periodic_schedule
);
273 lttng_rotation_schedule_destroy(periodic_schedule
);
276 int main(int argc
, char **argv
)
278 plan_tests(NUM_TESTS
);
281 diag("Usage: schedule_api SESSION_NAME");
285 session_name
= argv
[1];
287 diag("Argument validation");
288 test_add_null_session();
289 test_add_null_schedule();
290 test_add_uninitialized_schedule();
291 test_remove_null_session();
292 test_remove_null_schedule();
293 test_remove_uninitialized_schedule();
294 test_uninitialized_schedule_get();
296 test_add_list_remove_size_schedule();
297 test_add_list_remove_periodic_schedule();
299 return exit_status();