| 1 | /* |
| 2 | * schedule_api.c |
| 3 | * |
| 4 | * Unit tests for the session rotation schedule API |
| 5 | * |
| 6 | * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * SPDX-License-Identifier: MIT |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <stddef.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <tap/tap.h> |
| 15 | |
| 16 | #include <lttng/lttng.h> |
| 17 | |
| 18 | #define NUM_TESTS 26 |
| 19 | |
| 20 | #define SIZE_THRESHOLD_BYTES 1024 |
| 21 | #define PERIODIC_TIME_US 1000000 |
| 22 | |
| 23 | const char *session_name; |
| 24 | |
| 25 | static |
| 26 | bool schedules_equal(const struct lttng_rotation_schedule *a, |
| 27 | const struct lttng_rotation_schedule *b) |
| 28 | { |
| 29 | bool equal = false; |
| 30 | enum lttng_rotation_schedule_type a_type, b_type; |
| 31 | uint64_t a_value, b_value; |
| 32 | enum lttng_rotation_status status; |
| 33 | |
| 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)", |
| 38 | a_type, b_type); |
| 39 | goto end; |
| 40 | } |
| 41 | |
| 42 | switch (a_type) { |
| 43 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 44 | { |
| 45 | status = lttng_rotation_schedule_size_threshold_get_threshold(a, |
| 46 | &a_value); |
| 47 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 48 | diag("Failed to retrieve size threshold of schedule 'a'"); |
| 49 | goto end; |
| 50 | } |
| 51 | status = lttng_rotation_schedule_size_threshold_get_threshold(b, |
| 52 | &b_value); |
| 53 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 54 | diag("Failed to retrieve size threshold of schedule 'b'"); |
| 55 | goto end; |
| 56 | } |
| 57 | break; |
| 58 | } |
| 59 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 60 | { |
| 61 | status = lttng_rotation_schedule_periodic_get_period(a, |
| 62 | &a_value); |
| 63 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 64 | diag("Failed to retrieve period of schedule 'a'"); |
| 65 | goto end; |
| 66 | } |
| 67 | status = lttng_rotation_schedule_periodic_get_period(b, |
| 68 | &b_value); |
| 69 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 70 | diag("Failed to retrieve period of schedule 'b'"); |
| 71 | goto end; |
| 72 | } |
| 73 | break; |
| 74 | } |
| 75 | default: |
| 76 | diag("Unexpected schedule type: %i", a_type); |
| 77 | goto end; |
| 78 | } |
| 79 | |
| 80 | equal = a_value == b_value; |
| 81 | if (!equal) { |
| 82 | diag("Schedules have different values"); |
| 83 | } |
| 84 | end: |
| 85 | return equal; |
| 86 | } |
| 87 | |
| 88 | static |
| 89 | void test_add_null_session(void) |
| 90 | { |
| 91 | enum lttng_rotation_status status; |
| 92 | struct lttng_rotation_schedule *size_schedule = NULL; |
| 93 | |
| 94 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 95 | |
| 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); |
| 100 | } |
| 101 | |
| 102 | static |
| 103 | void test_add_null_schedule(void) |
| 104 | { |
| 105 | enum lttng_rotation_status status; |
| 106 | |
| 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()"); |
| 110 | } |
| 111 | |
| 112 | static |
| 113 | void test_add_uninitialized_schedule(void) |
| 114 | { |
| 115 | enum lttng_rotation_status status; |
| 116 | struct lttng_rotation_schedule *size_schedule = NULL, |
| 117 | *periodic_schedule = NULL; |
| 118 | |
| 119 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 120 | ok(size_schedule, "Created a size threshold session rotation schedule"); |
| 121 | |
| 122 | status = lttng_session_add_rotation_schedule(session_name, |
| 123 | size_schedule); |
| 124 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 125 | "Uninitialized size schedule rejected by lttng_session_add_rotation_schedule()"); |
| 126 | |
| 127 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 128 | ok(periodic_schedule, "Created a periodic session rotation schedule"); |
| 129 | |
| 130 | status = lttng_session_add_rotation_schedule(session_name, |
| 131 | periodic_schedule); |
| 132 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 133 | "Uninitialized periodic schedule rejected by lttng_session_add_rotation_schedule()"); |
| 134 | |
| 135 | lttng_rotation_schedule_destroy(size_schedule); |
| 136 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 137 | } |
| 138 | |
| 139 | static |
| 140 | void test_remove_null_session(void) |
| 141 | { |
| 142 | enum lttng_rotation_status status; |
| 143 | struct lttng_rotation_schedule *size_schedule = NULL; |
| 144 | |
| 145 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 146 | |
| 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); |
| 151 | } |
| 152 | |
| 153 | static |
| 154 | void test_remove_null_schedule(void) |
| 155 | { |
| 156 | enum lttng_rotation_status status; |
| 157 | |
| 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()"); |
| 161 | } |
| 162 | |
| 163 | static |
| 164 | void test_remove_uninitialized_schedule(void) |
| 165 | { |
| 166 | enum lttng_rotation_status status; |
| 167 | struct lttng_rotation_schedule *size_schedule = NULL, |
| 168 | *periodic_schedule = NULL; |
| 169 | |
| 170 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 171 | status = lttng_session_remove_rotation_schedule(session_name, |
| 172 | size_schedule); |
| 173 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 174 | "Uninitialized size schedule rejected by lttng_session_remove_rotation_schedule()"); |
| 175 | |
| 176 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 177 | status = lttng_session_remove_rotation_schedule(session_name, |
| 178 | periodic_schedule); |
| 179 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 180 | "Uninitialized periodic schedule rejected by lttng_session_remove_rotation_schedule()"); |
| 181 | |
| 182 | lttng_rotation_schedule_destroy(size_schedule); |
| 183 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 184 | } |
| 185 | |
| 186 | static |
| 187 | void test_uninitialized_schedule_get(void) |
| 188 | { |
| 189 | uint64_t value; |
| 190 | enum lttng_rotation_status status; |
| 191 | struct lttng_rotation_schedule *size_schedule = NULL, |
| 192 | *periodic_schedule = NULL; |
| 193 | |
| 194 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 195 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 196 | |
| 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, |
| 202 | &value); |
| 203 | ok(status == LTTNG_ROTATION_STATUS_UNAVAILABLE, |
| 204 | "Getter on periodic rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default"); |
| 205 | |
| 206 | lttng_rotation_schedule_destroy(size_schedule); |
| 207 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 208 | |
| 209 | } |
| 210 | |
| 211 | static |
| 212 | void test_add_list_remove_schedule( |
| 213 | const struct lttng_rotation_schedule *original_schedule) |
| 214 | { |
| 215 | int ret; |
| 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; |
| 220 | |
| 221 | status = lttng_session_add_rotation_schedule(session_name, |
| 222 | original_schedule); |
| 223 | ok(status == LTTNG_ROTATION_STATUS_OK, |
| 224 | "Add a rotation schedule to session \'%s\'", |
| 225 | session_name); |
| 226 | |
| 227 | ret = lttng_session_list_rotation_schedules(session_name, |
| 228 | &list_schedules); |
| 229 | ok(ret == LTTNG_OK && list_schedules, |
| 230 | "List rotation schedules of session \'%s\'", |
| 231 | session_name); |
| 232 | |
| 233 | status = lttng_rotation_schedules_get_count(list_schedules, |
| 234 | &schedules_count); |
| 235 | ok(status == LTTNG_ROTATION_STATUS_OK && schedules_count == 1, |
| 236 | "Listing returned 1 rotation schedule"); |
| 237 | |
| 238 | list_schedule = lttng_rotation_schedules_get_at_index(list_schedules, |
| 239 | 0); |
| 240 | ok(list_schedule, |
| 241 | "Obtain the first schedule of a schedules list"); |
| 242 | |
| 243 | ok(schedules_equal(original_schedule, list_schedule), |
| 244 | "Schedule returned by the listing is equal to the reference schedule that was added"); |
| 245 | |
| 246 | status = lttng_session_remove_rotation_schedule(session_name, |
| 247 | list_schedule); |
| 248 | ok(status == LTTNG_ROTATION_STATUS_OK, |
| 249 | "Remove rotation schedule returned by the schedules listing"); |
| 250 | lttng_rotation_schedules_destroy(list_schedules); |
| 251 | |
| 252 | (void) lttng_session_list_rotation_schedules(session_name, |
| 253 | &list_schedules); |
| 254 | status = lttng_rotation_schedules_get_count(list_schedules, |
| 255 | &schedules_count); |
| 256 | ok(status == LTTNG_ROTATION_STATUS_OK && schedules_count == 0, |
| 257 | "Listing returned 0 rotation schedules after removal"); |
| 258 | |
| 259 | } |
| 260 | |
| 261 | static |
| 262 | void test_add_list_remove_size_schedule(void) |
| 263 | { |
| 264 | struct lttng_rotation_schedule *size_schedule; |
| 265 | |
| 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); |
| 272 | } |
| 273 | |
| 274 | static |
| 275 | void test_add_list_remove_periodic_schedule(void) |
| 276 | { |
| 277 | struct lttng_rotation_schedule *periodic_schedule; |
| 278 | |
| 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); |
| 285 | } |
| 286 | |
| 287 | int main(int argc, char **argv) |
| 288 | { |
| 289 | plan_tests(NUM_TESTS); |
| 290 | |
| 291 | if (argc < 2) { |
| 292 | diag("Usage: schedule_api SESSION_NAME"); |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | session_name = argv[1]; |
| 297 | |
| 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(); |
| 306 | |
| 307 | test_add_list_remove_size_schedule(); |
| 308 | test_add_list_remove_periodic_schedule(); |
| 309 | end: |
| 310 | return exit_status(); |
| 311 | } |