| 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 <lttng/lttng.h> |
| 13 | |
| 14 | #include <stdbool.h> |
| 15 | #include <stddef.h> |
| 16 | #include <tap/tap.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 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)", a_type, b_type); |
| 37 | goto end; |
| 38 | } |
| 39 | |
| 40 | switch (a_type) { |
| 41 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 42 | { |
| 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'"); |
| 46 | goto end; |
| 47 | } |
| 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'"); |
| 51 | goto end; |
| 52 | } |
| 53 | break; |
| 54 | } |
| 55 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 56 | { |
| 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'"); |
| 60 | goto end; |
| 61 | } |
| 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'"); |
| 65 | goto end; |
| 66 | } |
| 67 | break; |
| 68 | } |
| 69 | default: |
| 70 | diag("Unexpected schedule type: %i", a_type); |
| 71 | goto end; |
| 72 | } |
| 73 | |
| 74 | equal = a_value == b_value; |
| 75 | if (!equal) { |
| 76 | diag("Schedules have different values"); |
| 77 | } |
| 78 | end: |
| 79 | return equal; |
| 80 | } |
| 81 | |
| 82 | static void test_add_null_session(void) |
| 83 | { |
| 84 | enum lttng_rotation_status status; |
| 85 | struct lttng_rotation_schedule *size_schedule = NULL; |
| 86 | |
| 87 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 88 | |
| 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); |
| 93 | } |
| 94 | |
| 95 | static void test_add_null_schedule(void) |
| 96 | { |
| 97 | enum lttng_rotation_status status; |
| 98 | |
| 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()"); |
| 102 | } |
| 103 | |
| 104 | static void test_add_uninitialized_schedule(void) |
| 105 | { |
| 106 | enum lttng_rotation_status status; |
| 107 | struct lttng_rotation_schedule *size_schedule = NULL, *periodic_schedule = NULL; |
| 108 | |
| 109 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 110 | ok(size_schedule, "Created a size threshold session rotation schedule"); |
| 111 | |
| 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()"); |
| 115 | |
| 116 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 117 | ok(periodic_schedule, "Created a periodic session rotation schedule"); |
| 118 | |
| 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()"); |
| 122 | |
| 123 | lttng_rotation_schedule_destroy(size_schedule); |
| 124 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 125 | } |
| 126 | |
| 127 | static void test_remove_null_session(void) |
| 128 | { |
| 129 | enum lttng_rotation_status status; |
| 130 | struct lttng_rotation_schedule *size_schedule = NULL; |
| 131 | |
| 132 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 133 | |
| 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); |
| 138 | } |
| 139 | |
| 140 | static void test_remove_null_schedule(void) |
| 141 | { |
| 142 | enum lttng_rotation_status status; |
| 143 | |
| 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()"); |
| 147 | } |
| 148 | |
| 149 | static void test_remove_uninitialized_schedule(void) |
| 150 | { |
| 151 | enum lttng_rotation_status status; |
| 152 | struct lttng_rotation_schedule *size_schedule = NULL, *periodic_schedule = NULL; |
| 153 | |
| 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()"); |
| 158 | |
| 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()"); |
| 163 | |
| 164 | lttng_rotation_schedule_destroy(size_schedule); |
| 165 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 166 | } |
| 167 | |
| 168 | static void test_uninitialized_schedule_get(void) |
| 169 | { |
| 170 | uint64_t value; |
| 171 | enum lttng_rotation_status status; |
| 172 | struct lttng_rotation_schedule *size_schedule = NULL, *periodic_schedule = NULL; |
| 173 | |
| 174 | size_schedule = lttng_rotation_schedule_size_threshold_create(); |
| 175 | periodic_schedule = lttng_rotation_schedule_periodic_create(); |
| 176 | |
| 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"); |
| 183 | |
| 184 | lttng_rotation_schedule_destroy(size_schedule); |
| 185 | lttng_rotation_schedule_destroy(periodic_schedule); |
| 186 | } |
| 187 | |
| 188 | static void test_add_list_remove_schedule(const struct lttng_rotation_schedule *original_schedule) |
| 189 | { |
| 190 | int ret; |
| 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; |
| 195 | |
| 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\'", |
| 199 | session_name); |
| 200 | |
| 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\'", |
| 204 | session_name); |
| 205 | |
| 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"); |
| 209 | |
| 210 | list_schedule = lttng_rotation_schedules_get_at_index(list_schedules, 0); |
| 211 | ok(list_schedule, "Obtain the first schedule of a schedules list"); |
| 212 | |
| 213 | ok(schedules_equal(original_schedule, list_schedule), |
| 214 | "Schedule returned by the listing is equal to the reference schedule that was added"); |
| 215 | |
| 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); |
| 220 | |
| 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); |
| 226 | } |
| 227 | |
| 228 | static void test_add_list_remove_size_schedule(void) |
| 229 | { |
| 230 | struct lttng_rotation_schedule *size_schedule; |
| 231 | |
| 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); |
| 238 | } |
| 239 | |
| 240 | static void test_add_list_remove_periodic_schedule(void) |
| 241 | { |
| 242 | struct lttng_rotation_schedule *periodic_schedule; |
| 243 | |
| 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); |
| 249 | } |
| 250 | |
| 251 | int main(int argc, char **argv) |
| 252 | { |
| 253 | plan_tests(NUM_TESTS); |
| 254 | |
| 255 | if (argc < 2) { |
| 256 | diag("Usage: schedule_api SESSION_NAME"); |
| 257 | goto end; |
| 258 | } |
| 259 | |
| 260 | session_name = argv[1]; |
| 261 | |
| 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(); |
| 270 | |
| 271 | test_add_list_remove_size_schedule(); |
| 272 | test_add_list_remove_periodic_schedule(); |
| 273 | end: |
| 274 | return exit_status(); |
| 275 | } |