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
12 #include <lttng/lttng.h>
20 #define SIZE_THRESHOLD_BYTES 1024
21 #define PERIODIC_TIME_US 1000000
23 const char *session_name
;
25 static 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)", a_type
, b_type
);
41 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
43 status
= lttng_rotation_schedule_size_threshold_get_threshold(a
, &a_value
);
44 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
45 diag("Failed to retrieve size threshold of schedule 'a'");
48 status
= lttng_rotation_schedule_size_threshold_get_threshold(b
, &b_value
);
49 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
50 diag("Failed to retrieve size threshold of schedule 'b'");
55 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
57 status
= lttng_rotation_schedule_periodic_get_period(a
, &a_value
);
58 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
59 diag("Failed to retrieve period of schedule 'a'");
62 status
= lttng_rotation_schedule_periodic_get_period(b
, &b_value
);
63 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
64 diag("Failed to retrieve period of schedule 'b'");
70 diag("Unexpected schedule type: %i", a_type
);
74 equal
= a_value
== b_value
;
76 diag("Schedules have different values");
82 static void test_add_null_session(void)
84 enum lttng_rotation_status status
;
85 struct lttng_rotation_schedule
*size_schedule
= NULL
;
87 size_schedule
= lttng_rotation_schedule_size_threshold_create();
89 status
= lttng_session_add_rotation_schedule(NULL
, size_schedule
);
90 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
91 "NULL session name rejected by lttng_session_add_rotation_schedule()");
92 lttng_rotation_schedule_destroy(size_schedule
);
95 static void test_add_null_schedule(void)
97 enum lttng_rotation_status status
;
99 status
= lttng_session_add_rotation_schedule(session_name
, NULL
);
100 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
101 "NULL schedule rejected by lttng_session_add_rotation_schedule()");
104 static void test_add_uninitialized_schedule(void)
106 enum lttng_rotation_status status
;
107 struct lttng_rotation_schedule
*size_schedule
= NULL
, *periodic_schedule
= NULL
;
109 size_schedule
= lttng_rotation_schedule_size_threshold_create();
110 ok(size_schedule
, "Created a size threshold session rotation schedule");
112 status
= lttng_session_add_rotation_schedule(session_name
, size_schedule
);
113 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
114 "Uninitialized size schedule rejected by lttng_session_add_rotation_schedule()");
116 periodic_schedule
= lttng_rotation_schedule_periodic_create();
117 ok(periodic_schedule
, "Created a periodic session rotation schedule");
119 status
= lttng_session_add_rotation_schedule(session_name
, periodic_schedule
);
120 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
121 "Uninitialized periodic schedule rejected by lttng_session_add_rotation_schedule()");
123 lttng_rotation_schedule_destroy(size_schedule
);
124 lttng_rotation_schedule_destroy(periodic_schedule
);
127 static void test_remove_null_session(void)
129 enum lttng_rotation_status status
;
130 struct lttng_rotation_schedule
*size_schedule
= NULL
;
132 size_schedule
= lttng_rotation_schedule_size_threshold_create();
134 status
= lttng_session_remove_rotation_schedule(NULL
, size_schedule
);
135 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
136 "NULL session name rejected by lttng_session_remove_rotation_schedule()");
137 lttng_rotation_schedule_destroy(size_schedule
);
140 static void test_remove_null_schedule(void)
142 enum lttng_rotation_status status
;
144 status
= lttng_session_remove_rotation_schedule(session_name
, NULL
);
145 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
146 "NULL schedule rejected by lttng_session_remove_rotation_schedule()");
149 static void test_remove_uninitialized_schedule(void)
151 enum lttng_rotation_status status
;
152 struct lttng_rotation_schedule
*size_schedule
= NULL
, *periodic_schedule
= NULL
;
154 size_schedule
= lttng_rotation_schedule_size_threshold_create();
155 status
= lttng_session_remove_rotation_schedule(session_name
, size_schedule
);
156 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
157 "Uninitialized size schedule rejected by lttng_session_remove_rotation_schedule()");
159 periodic_schedule
= lttng_rotation_schedule_periodic_create();
160 status
= lttng_session_remove_rotation_schedule(session_name
, periodic_schedule
);
161 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
162 "Uninitialized periodic schedule rejected by lttng_session_remove_rotation_schedule()");
164 lttng_rotation_schedule_destroy(size_schedule
);
165 lttng_rotation_schedule_destroy(periodic_schedule
);
168 static void test_uninitialized_schedule_get(void)
171 enum lttng_rotation_status status
;
172 struct lttng_rotation_schedule
*size_schedule
= NULL
, *periodic_schedule
= NULL
;
174 size_schedule
= lttng_rotation_schedule_size_threshold_create();
175 periodic_schedule
= lttng_rotation_schedule_periodic_create();
177 status
= lttng_rotation_schedule_size_threshold_get_threshold(size_schedule
, &value
);
178 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
179 "Getter on size threshold rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
180 status
= lttng_rotation_schedule_periodic_get_period(periodic_schedule
, &value
);
181 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
182 "Getter on periodic rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
184 lttng_rotation_schedule_destroy(size_schedule
);
185 lttng_rotation_schedule_destroy(periodic_schedule
);
188 static void test_add_list_remove_schedule(const struct lttng_rotation_schedule
*original_schedule
)
191 unsigned int schedules_count
= 0;
192 enum lttng_rotation_status status
;
193 const struct lttng_rotation_schedule
*list_schedule
;
194 struct lttng_rotation_schedules
*list_schedules
;
196 status
= lttng_session_add_rotation_schedule(session_name
, original_schedule
);
197 ok(status
== LTTNG_ROTATION_STATUS_OK
,
198 "Add a rotation schedule to session \'%s\'",
201 ret
= lttng_session_list_rotation_schedules(session_name
, &list_schedules
);
202 ok(ret
== LTTNG_OK
&& list_schedules
,
203 "List rotation schedules of session \'%s\'",
206 status
= lttng_rotation_schedules_get_count(list_schedules
, &schedules_count
);
207 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 1,
208 "Listing returned 1 rotation schedule");
210 list_schedule
= lttng_rotation_schedules_get_at_index(list_schedules
, 0);
211 ok(list_schedule
, "Obtain the first schedule of a schedules list");
213 ok(schedules_equal(original_schedule
, list_schedule
),
214 "Schedule returned by the listing is equal to the reference schedule that was added");
216 status
= lttng_session_remove_rotation_schedule(session_name
, list_schedule
);
217 ok(status
== LTTNG_ROTATION_STATUS_OK
,
218 "Remove rotation schedule returned by the schedules listing");
219 lttng_rotation_schedules_destroy(list_schedules
);
221 (void) lttng_session_list_rotation_schedules(session_name
, &list_schedules
);
222 status
= lttng_rotation_schedules_get_count(list_schedules
, &schedules_count
);
223 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 0,
224 "Listing returned 0 rotation schedules after removal");
225 lttng_rotation_schedules_destroy(list_schedules
);
228 static void test_add_list_remove_size_schedule(void)
230 struct lttng_rotation_schedule
*size_schedule
;
232 diag("Add, list, and remove a size threshold rotation schedule");
233 size_schedule
= lttng_rotation_schedule_size_threshold_create();
234 (void) lttng_rotation_schedule_size_threshold_set_threshold(size_schedule
,
235 SIZE_THRESHOLD_BYTES
);
236 test_add_list_remove_schedule(size_schedule
);
237 lttng_rotation_schedule_destroy(size_schedule
);
240 static void test_add_list_remove_periodic_schedule(void)
242 struct lttng_rotation_schedule
*periodic_schedule
;
244 diag("Add, list, and remove a periodic rotation schedule");
245 periodic_schedule
= lttng_rotation_schedule_periodic_create();
246 (void) lttng_rotation_schedule_periodic_set_period(periodic_schedule
, PERIODIC_TIME_US
);
247 test_add_list_remove_schedule(periodic_schedule
);
248 lttng_rotation_schedule_destroy(periodic_schedule
);
251 int main(int argc
, char **argv
)
253 plan_tests(NUM_TESTS
);
256 diag("Usage: schedule_api SESSION_NAME");
260 session_name
= argv
[1];
262 diag("Argument validation");
263 test_add_null_session();
264 test_add_null_schedule();
265 test_add_uninitialized_schedule();
266 test_remove_null_session();
267 test_remove_null_schedule();
268 test_remove_uninitialized_schedule();
269 test_uninitialized_schedule_get();
271 test_add_list_remove_size_schedule();
272 test_add_list_remove_periodic_schedule();
274 return exit_status();