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