4 * Unit tests for the session rotation schedule API
6 * Copyright (C) 2018 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
31 #include <lttng/lttng.h>
35 #define SIZE_THRESHOLD_BYTES 1024
36 #define PERIODIC_TIME_US 1000000
38 const char *session_name
;
40 bool schedules_equal(const struct lttng_rotation_schedule
*a
,
41 const struct lttng_rotation_schedule
*b
)
44 enum lttng_rotation_schedule_type a_type
, b_type
;
45 uint64_t a_value
, b_value
;
46 enum lttng_rotation_status status
;
48 a_type
= lttng_rotation_schedule_get_type(a
);
49 b_type
= lttng_rotation_schedule_get_type(b
);
50 if (a_type
!= b_type
) {
51 diag("Schedules are not of the same type (%i != %i)",
57 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
59 status
= lttng_rotation_schedule_size_threshold_get_threshold(a
,
61 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
62 diag("Failed to retrieve size threshold of schedule 'a'");
65 status
= lttng_rotation_schedule_size_threshold_get_threshold(b
,
67 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
68 diag("Failed to retrieve size threshold of schedule 'b'");
73 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
75 status
= lttng_rotation_schedule_periodic_get_period(a
,
77 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
78 diag("Failed to retrieve period of schedule 'a'");
81 status
= lttng_rotation_schedule_periodic_get_period(b
,
83 if (status
!= LTTNG_ROTATION_STATUS_OK
) {
84 diag("Failed to retrieve period of schedule 'b'");
90 diag("Unexpected schedule type: %i", a_type
);
94 equal
= a_value
== b_value
;
96 diag("Schedules have different values");
102 void test_add_null_session(void)
104 enum lttng_rotation_status status
;
105 struct lttng_rotation_schedule
*size_schedule
= NULL
;
107 size_schedule
= lttng_rotation_schedule_size_threshold_create();
109 status
= lttng_session_add_rotation_schedule(NULL
, size_schedule
);
110 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
111 "NULL session name rejected by lttng_session_add_rotation_schedule()");
114 void test_add_null_schedule(void)
116 enum lttng_rotation_status status
;
118 status
= lttng_session_add_rotation_schedule(session_name
, NULL
);
119 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
120 "NULL schedule rejected by lttng_session_add_rotation_schedule()");
123 void test_add_uninitialized_schedule(void)
125 enum lttng_rotation_status status
;
126 struct lttng_rotation_schedule
*size_schedule
= NULL
,
127 *periodic_schedule
= NULL
;
129 size_schedule
= lttng_rotation_schedule_size_threshold_create();
130 ok(size_schedule
, "Created a size threshold session rotation schedule");
132 status
= lttng_session_add_rotation_schedule(session_name
,
134 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
135 "Uninitialized size schedule rejected by lttng_session_add_rotation_schedule()");
137 periodic_schedule
= lttng_rotation_schedule_periodic_create();
138 ok(periodic_schedule
, "Created a periodic session rotation schedule");
140 status
= lttng_session_add_rotation_schedule(session_name
,
142 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
143 "Uninitialized periodic schedule rejected by lttng_session_add_rotation_schedule()");
145 lttng_rotation_schedule_destroy(size_schedule
);
146 lttng_rotation_schedule_destroy(periodic_schedule
);
149 void test_remove_null_session(void)
151 enum lttng_rotation_status status
;
152 struct lttng_rotation_schedule
*size_schedule
= NULL
;
154 size_schedule
= lttng_rotation_schedule_size_threshold_create();
156 status
= lttng_session_remove_rotation_schedule(NULL
, size_schedule
);
157 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
158 "NULL session name rejected by lttng_session_remove_rotation_schedule()");
161 void test_remove_null_schedule(void)
163 enum lttng_rotation_status status
;
165 status
= lttng_session_remove_rotation_schedule(session_name
, NULL
);
166 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
167 "NULL schedule rejected by lttng_session_remove_rotation_schedule()");
170 void test_remove_uninitialized_schedule(void)
172 enum lttng_rotation_status status
;
173 struct lttng_rotation_schedule
*size_schedule
= NULL
,
174 *periodic_schedule
= NULL
;
176 size_schedule
= lttng_rotation_schedule_size_threshold_create();
177 status
= lttng_session_remove_rotation_schedule(session_name
,
179 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
180 "Uninitialized size schedule rejected by lttng_session_remove_rotation_schedule()");
182 periodic_schedule
= lttng_rotation_schedule_periodic_create();
183 status
= lttng_session_remove_rotation_schedule(session_name
,
185 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
186 "Uninitialized periodic schedule rejected by lttng_session_remove_rotation_schedule()");
188 lttng_rotation_schedule_destroy(size_schedule
);
189 lttng_rotation_schedule_destroy(periodic_schedule
);
192 void test_uninitialized_schedule_get(void)
195 enum lttng_rotation_status status
;
196 struct lttng_rotation_schedule
*size_schedule
= NULL
,
197 *periodic_schedule
= NULL
;
199 size_schedule
= lttng_rotation_schedule_size_threshold_create();
200 periodic_schedule
= lttng_rotation_schedule_periodic_create();
202 status
= lttng_rotation_schedule_size_threshold_get_threshold(
203 size_schedule
, &value
);
204 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
205 "Getter on size threshold rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
206 status
= lttng_rotation_schedule_periodic_get_period(periodic_schedule
,
208 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
209 "Getter on periodic rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
211 lttng_rotation_schedule_destroy(size_schedule
);
212 lttng_rotation_schedule_destroy(periodic_schedule
);
216 void test_add_list_remove_schedule(
217 const struct lttng_rotation_schedule
*original_schedule
)
220 unsigned int schedules_count
= 0;
221 enum lttng_rotation_status status
;
222 const struct lttng_rotation_schedule
*list_schedule
;
223 struct lttng_rotation_schedules
*list_schedules
;
225 status
= lttng_session_add_rotation_schedule(session_name
,
227 ok(status
== LTTNG_ROTATION_STATUS_OK
,
228 "Add a rotation schedule to session \'%s\'",
231 ret
= lttng_session_list_rotation_schedules(session_name
,
233 ok(ret
== LTTNG_OK
&& list_schedules
,
234 "List rotation schedules of session \'%s\'",
237 status
= lttng_rotation_schedules_get_count(list_schedules
,
239 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 1,
240 "Listing returned 1 rotation schedule");
242 list_schedule
= lttng_rotation_schedules_get_at_index(list_schedules
,
245 "Obtain the first schedule of a schedules list");
247 ok(schedules_equal(original_schedule
, list_schedule
),
248 "Schedule returned by the listing is equal to the reference schedule that was added");
250 status
= lttng_session_remove_rotation_schedule(session_name
,
252 ok(status
== LTTNG_ROTATION_STATUS_OK
,
253 "Remove rotation schedule returned by the schedules listing");
254 lttng_rotation_schedules_destroy(list_schedules
);
256 (void) lttng_session_list_rotation_schedules(session_name
,
258 status
= lttng_rotation_schedules_get_count(list_schedules
,
260 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 0,
261 "Listing returned 0 rotation schedules after removal");
265 void test_add_list_remove_size_schedule(void)
267 struct lttng_rotation_schedule
*size_schedule
;
269 diag("Add, list, and remove a size threshold rotation schedule");
270 size_schedule
= lttng_rotation_schedule_size_threshold_create();
271 (void) lttng_rotation_schedule_size_threshold_set_threshold(
272 size_schedule
, SIZE_THRESHOLD_BYTES
);
273 test_add_list_remove_schedule(size_schedule
);
274 lttng_rotation_schedule_destroy(size_schedule
);
277 void test_add_list_remove_periodic_schedule(void)
279 struct lttng_rotation_schedule
*periodic_schedule
;
281 diag("Add, list, and remove a periodic rotation schedule");
282 periodic_schedule
= lttng_rotation_schedule_periodic_create();
283 (void) lttng_rotation_schedule_periodic_set_period(
284 periodic_schedule
, PERIODIC_TIME_US
);
285 test_add_list_remove_schedule(periodic_schedule
);
286 lttng_rotation_schedule_destroy(periodic_schedule
);
289 int main(int argc
, char **argv
)
291 plan_tests(NUM_TESTS
);
294 diag("Usage: schedule_api SESSION_NAME");
298 session_name
= argv
[1];
300 diag("Argument validation");
301 test_add_null_session();
302 test_add_null_schedule();
303 test_add_uninitialized_schedule();
304 test_remove_null_session();
305 test_remove_null_schedule();
306 test_remove_uninitialized_schedule();
307 test_uninitialized_schedule_get();
309 test_add_list_remove_size_schedule();
310 test_add_list_remove_periodic_schedule();
312 return exit_status();