| 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 | bool schedules_equal(const struct lttng_rotation_schedule *a, |
| 26 | const struct lttng_rotation_schedule *b) |
| 27 | { |
| 28 | bool equal = false; |
| 29 | enum lttng_rotation_schedule_type a_type, b_type; |
| 30 | uint64_t a_value, b_value; |
| 31 | enum lttng_rotation_status status; |
| 32 | |
| 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)", |
| 37 | a_type, b_type); |
| 38 | goto end; |
| 39 | } |
| 40 | |
| 41 | switch (a_type) { |
| 42 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 43 | { |
| 44 | status = lttng_rotation_schedule_size_threshold_get_threshold(a, |
| 45 | &a_value); |
| 46 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 47 | diag("Failed to retrieve size threshold of schedule 'a'"); |
| 48 | goto end; |
| 49 | } |
| 50 | status = lttng_rotation_schedule_size_threshold_get_threshold(b, |
| 51 | &b_value); |
| 52 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 53 | diag("Failed to retrieve size threshold of schedule 'b'"); |
| 54 | goto end; |
| 55 | } |
| 56 | break; |
| 57 | } |
| 58 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 59 | { |
| 60 | status = lttng_rotation_schedule_periodic_get_period(a, |
| 61 | &a_value); |
| 62 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 63 | diag("Failed to retrieve period of schedule 'a'"); |
| 64 | goto end; |
| 65 | } |
| 66 | status = lttng_rotation_schedule_periodic_get_period(b, |
| 67 | &b_value); |
| 68 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 69 | diag("Failed to retrieve period of schedule 'b'"); |
| 70 | goto end; |
| 71 | } |
| 72 | break; |
| 73 | } |
| 74 | default: |
| 75 | diag("Unexpected schedule type: %i", a_type); |
| 76 | goto end; |
| 77 | } |
| 78 | |
| 79 | equal = a_value == b_value; |
| 80 | if (!equal) { |
| 81 | diag("Schedules have different values"); |
| 82 | } |
| 83 | end: |
| 84 | return equal; |
| 85 | } |
| 86 | |
| 87 | void test_add_null_session(void) |
| 88 | { |
| 89 | enum lttng_rotation_status status; |
| 90 | struct lttng_rotation_schedule *size_schedule = NULL; |
| 91 | |
| 92 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 93 | |
| 94 | status = lttng_session_add_rotation_schedule(NULL, size_schedule); |
| 95 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 96 | "NULL session name rejected by lttng_session_add_rotation_schedule()"); |
| 97 | lttng_rotation_schedule_destroy(size_schedule); |
| 98 | } |
| 99 | |
| 100 | void test_add_null_schedule(void) |
| 101 | { |
| 102 | enum lttng_rotation_status status; |
| 103 | |
| 104 | status = lttng_session_add_rotation_schedule(session_name, NULL); |
| 105 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 106 | "NULL schedule rejected by lttng_session_add_rotation_schedule()"); |
| 107 | } |
| 108 | |
| 109 | void test_add_uninitialized_schedule(void) |
| 110 | { |
| 111 | enum lttng_rotation_status status; |
| 112 | struct lttng_rotation_schedule *size_schedule = NULL, |
| 113 | *periodic_schedule = NULL; |
| 114 | |
| 115 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 116 | ok(size_schedule, "Created a size threshold session rotation schedule"); |
| 117 | |
| 118 | status = lttng_session_add_rotation_schedule(session_name, |
| 119 | size_schedule); |
| 120 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 121 | "Uninitialized size schedule rejected by lttng_session_add_rotation_schedule()"); |
| 122 | |
| 123 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 124 | ok(periodic_schedule, "Created a periodic session rotation schedule"); |
| 125 | |
| 126 | status = lttng_session_add_rotation_schedule(session_name, |
| 127 | periodic_schedule); |
| 128 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 129 | "Uninitialized periodic schedule rejected by lttng_session_add_rotation_schedule()"); |
| 130 | |
| 131 | lttng_rotation_schedule_destroy(size_schedule); |
| 132 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 133 | } |
| 134 | |
| 135 | void test_remove_null_session(void) |
| 136 | { |
| 137 | enum lttng_rotation_status status; |
| 138 | struct lttng_rotation_schedule *size_schedule = NULL; |
| 139 | |
| 140 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 141 | |
| 142 | status = lttng_session_remove_rotation_schedule(NULL, size_schedule); |
| 143 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 144 | "NULL session name rejected by lttng_session_remove_rotation_schedule()"); |
| 145 | lttng_rotation_schedule_destroy(size_schedule); |
| 146 | } |
| 147 | |
| 148 | void test_remove_null_schedule(void) |
| 149 | { |
| 150 | enum lttng_rotation_status status; |
| 151 | |
| 152 | status = lttng_session_remove_rotation_schedule(session_name, NULL); |
| 153 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 154 | "NULL schedule rejected by lttng_session_remove_rotation_schedule()"); |
| 155 | } |
| 156 | |
| 157 | void test_remove_uninitialized_schedule(void) |
| 158 | { |
| 159 | enum lttng_rotation_status status; |
| 160 | struct lttng_rotation_schedule *size_schedule = NULL, |
| 161 | *periodic_schedule = NULL; |
| 162 | |
| 163 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 164 | status = lttng_session_remove_rotation_schedule(session_name, |
| 165 | size_schedule); |
| 166 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 167 | "Uninitialized size schedule rejected by lttng_session_remove_rotation_schedule()"); |
| 168 | |
| 169 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 170 | status = lttng_session_remove_rotation_schedule(session_name, |
| 171 | periodic_schedule); |
| 172 | ok(status == LTTNG_ROTATION_STATUS_INVALID, |
| 173 | "Uninitialized periodic schedule rejected by lttng_session_remove_rotation_schedule()"); |
| 174 | |
| 175 | lttng_rotation_schedule_destroy(size_schedule); |
| 176 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 177 | } |
| 178 | |
| 179 | void test_uninitialized_schedule_get(void) |
| 180 | { |
| 181 | uint64_t value; |
| 182 | enum lttng_rotation_status status; |
| 183 | struct lttng_rotation_schedule *size_schedule = NULL, |
| 184 | *periodic_schedule = NULL; |
| 185 | |
| 186 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 187 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 188 | |
| 189 | status = lttng_rotation_schedule_size_threshold_get_threshold( |
| 190 | size_schedule, &value); |
| 191 | ok(status == LTTNG_ROTATION_STATUS_UNAVAILABLE, |
| 192 | "Getter on size threshold rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default"); |
| 193 | status = lttng_rotation_schedule_periodic_get_period(periodic_schedule, |
| 194 | &value); |
| 195 | ok(status == LTTNG_ROTATION_STATUS_UNAVAILABLE, |
| 196 | "Getter on periodic rotation schedule returns LTTNG_ROTATION_STATUS_UNAVAILABLE by default"); |
| 197 | |
| 198 | lttng_rotation_schedule_destroy(size_schedule); |
| 199 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | void test_add_list_remove_schedule( |
| 204 | const struct lttng_rotation_schedule *original_schedule) |
| 205 | { |
| 206 | int ret; |
| 207 | unsigned int schedules_count = 0; |
| 208 | enum lttng_rotation_status status; |
| 209 | const struct lttng_rotation_schedule *list_schedule; |
| 210 | struct lttng_rotation_schedules *list_schedules; |
| 211 | |
| 212 | status = lttng_session_add_rotation_schedule(session_name, |
| 213 | original_schedule); |
| 214 | ok(status == LTTNG_ROTATION_STATUS_OK, |
| 215 | "Add a rotation schedule to session \'%s\'", |
| 216 | session_name); |
| 217 | |
| 218 | ret = lttng_session_list_rotation_schedules(session_name, |
| 219 | &list_schedules); |
| 220 | ok(ret == LTTNG_OK && list_schedules, |
| 221 | "List rotation schedules of session \'%s\'", |
| 222 | session_name); |
| 223 | |
| 224 | status = lttng_rotation_schedules_get_count(list_schedules, |
| 225 | &schedules_count); |
| 226 | ok(status == LTTNG_ROTATION_STATUS_OK && schedules_count == 1, |
| 227 | "Listing returned 1 rotation schedule"); |
| 228 | |
| 229 | list_schedule = lttng_rotation_schedules_get_at_index(list_schedules, |
| 230 | 0); |
| 231 | ok(list_schedule, |
| 232 | "Obtain the first schedule of a schedules list"); |
| 233 | |
| 234 | ok(schedules_equal(original_schedule, list_schedule), |
| 235 | "Schedule returned by the listing is equal to the reference schedule that was added"); |
| 236 | |
| 237 | status = lttng_session_remove_rotation_schedule(session_name, |
| 238 | list_schedule); |
| 239 | ok(status == LTTNG_ROTATION_STATUS_OK, |
| 240 | "Remove rotation schedule returned by the schedules listing"); |
| 241 | lttng_rotation_schedules_destroy(list_schedules); |
| 242 | |
| 243 | (void) lttng_session_list_rotation_schedules(session_name, |
| 244 | &list_schedules); |
| 245 | status = lttng_rotation_schedules_get_count(list_schedules, |
| 246 | &schedules_count); |
| 247 | ok(status == LTTNG_ROTATION_STATUS_OK && schedules_count == 0, |
| 248 | "Listing returned 0 rotation schedules after removal"); |
| 249 | |
| 250 | } |
| 251 | |
| 252 | void test_add_list_remove_size_schedule(void) |
| 253 | { |
| 254 | struct lttng_rotation_schedule *size_schedule; |
| 255 | |
| 256 | diag("Add, list, and remove a size threshold rotation schedule"); |
| 257 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 258 | (void) lttng_rotation_schedule_size_threshold_set_threshold( |
| 259 | size_schedule, SIZE_THRESHOLD_BYTES); |
| 260 | test_add_list_remove_schedule(size_schedule); |
| 261 | lttng_rotation_schedule_destroy(size_schedule); |
| 262 | } |
| 263 | |
| 264 | void test_add_list_remove_periodic_schedule(void) |
| 265 | { |
| 266 | struct lttng_rotation_schedule *periodic_schedule; |
| 267 | |
| 268 | diag("Add, list, and remove a periodic rotation schedule"); |
| 269 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 270 | (void) lttng_rotation_schedule_periodic_set_period( |
| 271 | periodic_schedule, PERIODIC_TIME_US); |
| 272 | test_add_list_remove_schedule(periodic_schedule); |
| 273 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 274 | } |
| 275 | |
| 276 | int main(int argc, char **argv) |
| 277 | { |
| 278 | plan_tests(NUM_TESTS); |
| 279 | |
| 280 | if (argc < 2) { |
| 281 | diag("Usage: schedule_api SESSION_NAME"); |
| 282 | goto end; |
| 283 | } |
| 284 | |
| 285 | session_name = argv[1]; |
| 286 | |
| 287 | diag("Argument validation"); |
| 288 | test_add_null_session(); |
| 289 | test_add_null_schedule(); |
| 290 | test_add_uninitialized_schedule(); |
| 291 | test_remove_null_session(); |
| 292 | test_remove_null_schedule(); |
| 293 | test_remove_uninitialized_schedule(); |
| 294 | test_uninitialized_schedule_get(); |
| 295 | |
| 296 | test_add_list_remove_size_schedule(); |
| 297 | test_add_list_remove_periodic_schedule(); |
| 298 | end: |
| 299 | return exit_status(); |
| 300 | } |