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
;
26 bool schedules_equal(const struct lttng_rotation_schedule
*a
,
27 const struct lttng_rotation_schedule
*b
)
30 enum lttng_rotation_schedule_type a_type
, b_type
;
31 uint64_t a_value
, b_value
;
32 enum lttng_rotation_status status
;
34 a_type
= lttng_rotation_schedule_get_type(a
);
35 b_type
= lttng_rotation_schedule_get_type(b
);
36 if (a_type
!= b_type
) {
37 diag("Schedules are not of the same type (%i != %i)",
43 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
45 status
= lttng_rotation_schedule_size_threshold_get_threshold(a
,
47 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
48 diag("Failed to retrieve size threshold of schedule 'a'");
51 status
= lttng_rotation_schedule_size_threshold_get_threshold(b
,
53 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
54 diag("Failed to retrieve size threshold of schedule 'b'");
59 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
61 status
= lttng_rotation_schedule_periodic_get_period(a
,
63 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
64 diag("Failed to retrieve period of schedule 'a'");
67 status
= lttng_rotation_schedule_periodic_get_period(b
,
69 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
70 diag("Failed to retrieve period of schedule 'b'");
76 diag("Unexpected schedule type: %i", a_type
);
80 equal
= a_value
== b_value
;
82 diag("Schedules have different values");
89 void test_add_null_session(void)
91 enum lttng_rotation_status status
;
92 struct lttng_rotation_schedule
*size_schedule
= NULL
;
94 size_schedule
= lttng_rotation_schedule_size_threshold_create();
96 status
= lttng_session_add_rotation_schedule(NULL
, size_schedule
);
97 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
98 "NULL session name rejected by lttng_session_add_rotation_schedule()");
99 lttng_rotation_schedule_destroy(size_schedule
);
103 void test_add_null_schedule(void)
105 enum lttng_rotation_status status
;
107 status
= lttng_session_add_rotation_schedule(session_name
, NULL
);
108 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
109 "NULL schedule rejected by lttng_session_add_rotation_schedule()");
113 void test_add_uninitialized_schedule(void)
115 enum lttng_rotation_status status
;
116 struct lttng_rotation_schedule
*size_schedule
= NULL
,
117 *periodic_schedule
= NULL
;
119 size_schedule
= lttng_rotation_schedule_size_threshold_create();
120 ok(size_schedule
, "Created a size threshold session rotation schedule");
122 status
= lttng_session_add_rotation_schedule(session_name
,
124 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
125 "Uninitialized size schedule rejected by lttng_session_add_rotation_schedule()");
127 periodic_schedule
= lttng_rotation_schedule_periodic_create();
128 ok(periodic_schedule
, "Created a periodic session rotation schedule");
130 status
= lttng_session_add_rotation_schedule(session_name
,
132 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
133 "Uninitialized periodic schedule rejected by lttng_session_add_rotation_schedule()");
135 lttng_rotation_schedule_destroy(size_schedule
);
136 lttng_rotation_schedule_destroy(periodic_schedule
);
140 void test_remove_null_session(void)
142 enum lttng_rotation_status status
;
143 struct lttng_rotation_schedule
*size_schedule
= NULL
;
145 size_schedule
= lttng_rotation_schedule_size_threshold_create();
147 status
= lttng_session_remove_rotation_schedule(NULL
, size_schedule
);
148 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
149 "NULL session name rejected by lttng_session_remove_rotation_schedule()");
150 lttng_rotation_schedule_destroy(size_schedule
);
154 void test_remove_null_schedule(void)
156 enum lttng_rotation_status status
;
158 status
= lttng_session_remove_rotation_schedule(session_name
, NULL
);
159 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
160 "NULL schedule rejected by lttng_session_remove_rotation_schedule()");
164 void test_remove_uninitialized_schedule(void)
166 enum lttng_rotation_status status
;
167 struct lttng_rotation_schedule
*size_schedule
= NULL
,
168 *periodic_schedule
= NULL
;
170 size_schedule
= lttng_rotation_schedule_size_threshold_create();
171 status
= lttng_session_remove_rotation_schedule(session_name
,
173 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
174 "Uninitialized size schedule rejected by lttng_session_remove_rotation_schedule()");
176 periodic_schedule
= lttng_rotation_schedule_periodic_create();
177 status
= lttng_session_remove_rotation_schedule(session_name
,
179 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
180 "Uninitialized periodic schedule rejected by lttng_session_remove_rotation_schedule()");
182 lttng_rotation_schedule_destroy(size_schedule
);
183 lttng_rotation_schedule_destroy(periodic_schedule
);
187 void test_uninitialized_schedule_get(void)
190 enum lttng_rotation_status status
;
191 struct lttng_rotation_schedule
*size_schedule
= NULL
,
192 *periodic_schedule
= NULL
;
194 size_schedule
= lttng_rotation_schedule_size_threshold_create();
195 periodic_schedule
= lttng_rotation_schedule_periodic_create();
197 status
= lttng_rotation_schedule_size_threshold_get_threshold(
198 size_schedule
, &value
);
199 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
200 "Getter on size threshold rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
201 status
= lttng_rotation_schedule_periodic_get_period(periodic_schedule
,
203 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
204 "Getter on periodic rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
206 lttng_rotation_schedule_destroy(size_schedule
);
207 lttng_rotation_schedule_destroy(periodic_schedule
);
212 void test_add_list_remove_schedule(
213 const struct lttng_rotation_schedule
*original_schedule
)
216 unsigned int schedules_count
= 0;
217 enum lttng_rotation_status status
;
218 const struct lttng_rotation_schedule
*list_schedule
;
219 struct lttng_rotation_schedules
*list_schedules
;
221 status
= lttng_session_add_rotation_schedule(session_name
,
223 ok(status
== LTTNG_ROTATION_STATUS_OK
,
224 "Add a rotation schedule to session \'%s\'",
227 ret
= lttng_session_list_rotation_schedules(session_name
,
229 ok(ret
== LTTNG_OK
&& list_schedules
,
230 "List rotation schedules of session \'%s\'",
233 status
= lttng_rotation_schedules_get_count(list_schedules
,
235 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 1,
236 "Listing returned 1 rotation schedule");
238 list_schedule
= lttng_rotation_schedules_get_at_index(list_schedules
,
241 "Obtain the first schedule of a schedules list");
243 ok(schedules_equal(original_schedule
, list_schedule
),
244 "Schedule returned by the listing is equal to the reference schedule that was added");
246 status
= lttng_session_remove_rotation_schedule(session_name
,
248 ok(status
== LTTNG_ROTATION_STATUS_OK
,
249 "Remove rotation schedule returned by the schedules listing");
250 lttng_rotation_schedules_destroy(list_schedules
);
252 (void) lttng_session_list_rotation_schedules(session_name
,
254 status
= lttng_rotation_schedules_get_count(list_schedules
,
256 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 0,
257 "Listing returned 0 rotation schedules after removal");
262 void test_add_list_remove_size_schedule(void)
264 struct lttng_rotation_schedule
*size_schedule
;
266 diag("Add, list, and remove a size threshold rotation schedule");
267 size_schedule
= lttng_rotation_schedule_size_threshold_create();
268 (void) lttng_rotation_schedule_size_threshold_set_threshold(
269 size_schedule
, SIZE_THRESHOLD_BYTES
);
270 test_add_list_remove_schedule(size_schedule
);
271 lttng_rotation_schedule_destroy(size_schedule
);
275 void test_add_list_remove_periodic_schedule(void)
277 struct lttng_rotation_schedule
*periodic_schedule
;
279 diag("Add, list, and remove a periodic rotation schedule");
280 periodic_schedule
= lttng_rotation_schedule_periodic_create();
281 (void) lttng_rotation_schedule_periodic_set_period(
282 periodic_schedule
, PERIODIC_TIME_US
);
283 test_add_list_remove_schedule(periodic_schedule
);
284 lttng_rotation_schedule_destroy(periodic_schedule
);
287 int main(int argc
, char **argv
)
289 plan_tests(NUM_TESTS
);
292 diag("Usage: schedule_api SESSION_NAME");
296 session_name
= argv
[1];
298 diag("Argument validation");
299 test_add_null_session();
300 test_add_null_schedule();
301 test_add_uninitialized_schedule();
302 test_remove_null_session();
303 test_remove_null_schedule();
304 test_remove_uninitialized_schedule();
305 test_uninitialized_schedule_get();
307 test_add_list_remove_size_schedule();
308 test_add_list_remove_periodic_schedule();
310 return exit_status();