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()");
112 lttng_rotation_schedule_destroy(size_schedule
);
115 void test_add_null_schedule(void)
117 enum lttng_rotation_status status
;
119 status
= lttng_session_add_rotation_schedule(session_name
, NULL
);
120 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
121 "NULL schedule rejected by lttng_session_add_rotation_schedule()");
124 void test_add_uninitialized_schedule(void)
126 enum lttng_rotation_status status
;
127 struct lttng_rotation_schedule
*size_schedule
= NULL
,
128 *periodic_schedule
= NULL
;
130 size_schedule
= lttng_rotation_schedule_size_threshold_create();
131 ok(size_schedule
, "Created a size threshold session rotation schedule");
133 status
= lttng_session_add_rotation_schedule(session_name
,
135 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
136 "Uninitialized size schedule rejected by lttng_session_add_rotation_schedule()");
138 periodic_schedule
= lttng_rotation_schedule_periodic_create();
139 ok(periodic_schedule
, "Created a periodic session rotation schedule");
141 status
= lttng_session_add_rotation_schedule(session_name
,
143 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
144 "Uninitialized periodic schedule rejected by lttng_session_add_rotation_schedule()");
146 lttng_rotation_schedule_destroy(size_schedule
);
147 lttng_rotation_schedule_destroy(periodic_schedule
);
150 void test_remove_null_session(void)
152 enum lttng_rotation_status status
;
153 struct lttng_rotation_schedule
*size_schedule
= NULL
;
155 size_schedule
= lttng_rotation_schedule_size_threshold_create();
157 status
= lttng_session_remove_rotation_schedule(NULL
, size_schedule
);
158 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
159 "NULL session name rejected by lttng_session_remove_rotation_schedule()");
160 lttng_rotation_schedule_destroy(size_schedule
);
163 void test_remove_null_schedule(void)
165 enum lttng_rotation_status status
;
167 status
= lttng_session_remove_rotation_schedule(session_name
, NULL
);
168 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
169 "NULL schedule rejected by lttng_session_remove_rotation_schedule()");
172 void test_remove_uninitialized_schedule(void)
174 enum lttng_rotation_status status
;
175 struct lttng_rotation_schedule
*size_schedule
= NULL
,
176 *periodic_schedule
= NULL
;
178 size_schedule
= lttng_rotation_schedule_size_threshold_create();
179 status
= lttng_session_remove_rotation_schedule(session_name
,
181 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
182 "Uninitialized size schedule rejected by lttng_session_remove_rotation_schedule()");
184 periodic_schedule
= lttng_rotation_schedule_periodic_create();
185 status
= lttng_session_remove_rotation_schedule(session_name
,
187 ok(status
== LTTNG_ROTATION_STATUS_INVALID
,
188 "Uninitialized periodic schedule rejected by lttng_session_remove_rotation_schedule()");
190 lttng_rotation_schedule_destroy(size_schedule
);
191 lttng_rotation_schedule_destroy(periodic_schedule
);
194 void test_uninitialized_schedule_get(void)
197 enum lttng_rotation_status status
;
198 struct lttng_rotation_schedule
*size_schedule
= NULL
,
199 *periodic_schedule
= NULL
;
201 size_schedule
= lttng_rotation_schedule_size_threshold_create();
202 periodic_schedule
= lttng_rotation_schedule_periodic_create();
204 status
= lttng_rotation_schedule_size_threshold_get_threshold(
205 size_schedule
, &value
);
206 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
207 "Getter on size threshold rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
208 status
= lttng_rotation_schedule_periodic_get_period(periodic_schedule
,
210 ok(status
== LTTNG_ROTATION_STATUS_UNAVAILABLE
,
211 "Getter on periodic rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default");
213 lttng_rotation_schedule_destroy(size_schedule
);
214 lttng_rotation_schedule_destroy(periodic_schedule
);
218 void test_add_list_remove_schedule(
219 const struct lttng_rotation_schedule
*original_schedule
)
222 unsigned int schedules_count
= 0;
223 enum lttng_rotation_status status
;
224 const struct lttng_rotation_schedule
*list_schedule
;
225 struct lttng_rotation_schedules
*list_schedules
;
227 status
= lttng_session_add_rotation_schedule(session_name
,
229 ok(status
== LTTNG_ROTATION_STATUS_OK
,
230 "Add a rotation schedule to session \'%s\'",
233 ret
= lttng_session_list_rotation_schedules(session_name
,
235 ok(ret
== LTTNG_OK
&& list_schedules
,
236 "List rotation schedules of session \'%s\'",
239 status
= lttng_rotation_schedules_get_count(list_schedules
,
241 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 1,
242 "Listing returned 1 rotation schedule");
244 list_schedule
= lttng_rotation_schedules_get_at_index(list_schedules
,
247 "Obtain the first schedule of a schedules list");
249 ok(schedules_equal(original_schedule
, list_schedule
),
250 "Schedule returned by the listing is equal to the reference schedule that was added");
252 status
= lttng_session_remove_rotation_schedule(session_name
,
254 ok(status
== LTTNG_ROTATION_STATUS_OK
,
255 "Remove rotation schedule returned by the schedules listing");
256 lttng_rotation_schedules_destroy(list_schedules
);
258 (void) lttng_session_list_rotation_schedules(session_name
,
260 status
= lttng_rotation_schedules_get_count(list_schedules
,
262 ok(status
== LTTNG_ROTATION_STATUS_OK
&& schedules_count
== 0,
263 "Listing returned 0 rotation schedules after removal");
267 void test_add_list_remove_size_schedule(void)
269 struct lttng_rotation_schedule
*size_schedule
;
271 diag("Add, list, and remove a size threshold rotation schedule");
272 size_schedule
= lttng_rotation_schedule_size_threshold_create();
273 (void) lttng_rotation_schedule_size_threshold_set_threshold(
274 size_schedule
, SIZE_THRESHOLD_BYTES
);
275 test_add_list_remove_schedule(size_schedule
);
276 lttng_rotation_schedule_destroy(size_schedule
);
279 void test_add_list_remove_periodic_schedule(void)
281 struct lttng_rotation_schedule
*periodic_schedule
;
283 diag("Add, list, and remove a periodic rotation schedule");
284 periodic_schedule
= lttng_rotation_schedule_periodic_create();
285 (void) lttng_rotation_schedule_periodic_set_period(
286 periodic_schedule
, PERIODIC_TIME_US
);
287 test_add_list_remove_schedule(periodic_schedule
);
288 lttng_rotation_schedule_destroy(periodic_schedule
);
291 int main(int argc
, char **argv
)
293 plan_tests(NUM_TESTS
);
296 diag("Usage: schedule_api SESSION_NAME");
300 session_name
= argv
[1];
302 diag("Argument validation");
303 test_add_null_session();
304 test_add_null_schedule();
305 test_add_uninitialized_schedule();
306 test_remove_null_session();
307 test_remove_null_schedule();
308 test_remove_uninitialized_schedule();
309 test_uninitialized_schedule_get();
311 test_add_list_remove_size_schedule();
312 test_add_list_remove_periodic_schedule();
314 return exit_status();