| 1 | /* |
| 2 | * test_action.c |
| 3 | * |
| 4 | * Unit tests for the notification API. |
| 5 | * |
| 6 | * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 7 | * |
| 8 | * SPDX-License-Identifier: MIT |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <assert.h> |
| 13 | #include <inttypes.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | #include <tap/tap.h> |
| 19 | |
| 20 | #include <common/payload-view.h> |
| 21 | #include <common/payload.h> |
| 22 | #include <lttng/action/action-internal.h> |
| 23 | #include <lttng/action/action.h> |
| 24 | #include <lttng/action/firing-policy-internal.h> |
| 25 | #include <lttng/action/firing-policy.h> |
| 26 | #include <lttng/action/notify.h> |
| 27 | #include <lttng/action/rotate-session.h> |
| 28 | #include <lttng/action/start-session.h> |
| 29 | |
| 30 | /* For error.h */ |
| 31 | int lttng_opt_quiet = 1; |
| 32 | int lttng_opt_verbose; |
| 33 | int lttng_opt_mi; |
| 34 | |
| 35 | #define NUM_TESTS 34 |
| 36 | |
| 37 | static void test_action_notify(void) |
| 38 | { |
| 39 | int ret; |
| 40 | enum lttng_action_status status; |
| 41 | struct lttng_action *notify_action = NULL, |
| 42 | *notify_action_from_buffer = NULL; |
| 43 | struct lttng_firing_policy *policy = NULL, *default_policy; |
| 44 | struct lttng_payload payload; |
| 45 | |
| 46 | lttng_payload_init(&payload); |
| 47 | |
| 48 | /* To set. */ |
| 49 | policy = lttng_firing_policy_every_n_create(100); |
| 50 | /* For comparison. */ |
| 51 | default_policy = lttng_firing_policy_every_n_create(1); |
| 52 | |
| 53 | assert(policy && default_policy); |
| 54 | |
| 55 | notify_action = lttng_action_notify_create(); |
| 56 | ok(notify_action, "Create notify action"); |
| 57 | ok(lttng_action_get_type(notify_action) == LTTNG_ACTION_TYPE_NOTIFY, |
| 58 | "Action has type LTTNG_ACTION_TYPE_NOTIFY"); |
| 59 | |
| 60 | /* Validate the default policy for a notify action. */ |
| 61 | { |
| 62 | const struct lttng_firing_policy *cur_policy = NULL; |
| 63 | status = lttng_action_notify_get_firing_policy( |
| 64 | notify_action, &cur_policy); |
| 65 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 66 | lttng_firing_policy_is_equal( |
| 67 | default_policy, |
| 68 | cur_policy), |
| 69 | "Default policy is every n=1"); |
| 70 | } |
| 71 | |
| 72 | /* Set a custom policy. */ |
| 73 | status = lttng_action_notify_set_firing_policy(notify_action, policy); |
| 74 | ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); |
| 75 | |
| 76 | /* Validate the custom policy for a notify action. */ |
| 77 | { |
| 78 | const struct lttng_firing_policy *cur_policy = NULL; |
| 79 | status = lttng_action_notify_get_firing_policy( |
| 80 | notify_action, &cur_policy); |
| 81 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 82 | lttng_firing_policy_is_equal( |
| 83 | policy, |
| 84 | cur_policy), |
| 85 | "Notify action policy get"); |
| 86 | } |
| 87 | |
| 88 | ret = lttng_action_serialize(notify_action, &payload); |
| 89 | ok(ret == 0, "Action notify serialized"); |
| 90 | |
| 91 | { |
| 92 | struct lttng_payload_view view = |
| 93 | lttng_payload_view_from_payload( |
| 94 | &payload, 0, -1); |
| 95 | (void) lttng_action_create_from_payload( |
| 96 | &view, ¬ify_action_from_buffer); |
| 97 | } |
| 98 | ok(notify_action_from_buffer, |
| 99 | "Notify action created from payload is non-null"); |
| 100 | |
| 101 | ok(lttng_action_is_equal(notify_action, notify_action_from_buffer), |
| 102 | "Serialized and de-serialized notify action are equal"); |
| 103 | |
| 104 | lttng_firing_policy_destroy(default_policy); |
| 105 | lttng_firing_policy_destroy(policy); |
| 106 | lttng_action_destroy(notify_action); |
| 107 | lttng_action_destroy(notify_action_from_buffer); |
| 108 | lttng_payload_reset(&payload); |
| 109 | } |
| 110 | |
| 111 | static void test_action_rotate_session(void) |
| 112 | { |
| 113 | int ret; |
| 114 | enum lttng_action_status status; |
| 115 | struct lttng_action *rotate_session_action = NULL, |
| 116 | *rotate_session_action_from_buffer = NULL; |
| 117 | struct lttng_firing_policy *policy = NULL, *default_policy; |
| 118 | struct lttng_payload payload; |
| 119 | const char *session_name = "my_session_name"; |
| 120 | const char *get_session_name; |
| 121 | |
| 122 | lttng_payload_init(&payload); |
| 123 | |
| 124 | /* To set. */ |
| 125 | policy = lttng_firing_policy_every_n_create(100); |
| 126 | /* For comparison. */ |
| 127 | default_policy = lttng_firing_policy_every_n_create(1); |
| 128 | |
| 129 | assert(policy && default_policy); |
| 130 | |
| 131 | rotate_session_action = lttng_action_rotate_session_create(); |
| 132 | ok(rotate_session_action, "Create rotate_session action"); |
| 133 | ok(lttng_action_get_type(rotate_session_action) == |
| 134 | LTTNG_ACTION_TYPE_ROTATE_SESSION, |
| 135 | "Action has type LTTNG_ACTION_TYPE_ROTATE_SESSION"); |
| 136 | |
| 137 | /* Session name setter. */ |
| 138 | status = lttng_action_rotate_session_set_session_name(NULL, NULL); |
| 139 | ok(status == LTTNG_ACTION_STATUS_INVALID, |
| 140 | "Set session name (NULL,NULL) expect invalid"); |
| 141 | status = lttng_action_rotate_session_set_session_name( |
| 142 | rotate_session_action, NULL); |
| 143 | ok(status == LTTNG_ACTION_STATUS_INVALID, |
| 144 | "Set session name (object,NULL) expect invalid"); |
| 145 | status = lttng_action_rotate_session_set_session_name( |
| 146 | NULL, session_name); |
| 147 | ok(status == LTTNG_ACTION_STATUS_INVALID, |
| 148 | "Set session name (NULL,object) expect invalid"); |
| 149 | |
| 150 | /* Set the session name */ |
| 151 | status = lttng_action_rotate_session_set_session_name( |
| 152 | rotate_session_action, session_name); |
| 153 | ok(status == LTTNG_ACTION_STATUS_OK, "Set session name"); |
| 154 | |
| 155 | status = lttng_action_rotate_session_get_session_name( |
| 156 | rotate_session_action, &get_session_name); |
| 157 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 158 | !strcmp(session_name, get_session_name), |
| 159 | "Get session name, expected `%s` got `%s`", |
| 160 | session_name, get_session_name); |
| 161 | |
| 162 | /* Validate the default policy for a rotate_session action. */ |
| 163 | { |
| 164 | const struct lttng_firing_policy *cur_policy = NULL; |
| 165 | status = lttng_action_rotate_session_get_firing_policy( |
| 166 | rotate_session_action, &cur_policy); |
| 167 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 168 | lttng_firing_policy_is_equal( |
| 169 | default_policy, |
| 170 | cur_policy), |
| 171 | "Default policy is every n=1"); |
| 172 | } |
| 173 | |
| 174 | /* Set a custom policy. */ |
| 175 | status = lttng_action_rotate_session_set_firing_policy( |
| 176 | rotate_session_action, policy); |
| 177 | ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); |
| 178 | |
| 179 | /* Validate the custom policy for a rotate_session action. */ |
| 180 | { |
| 181 | const struct lttng_firing_policy *cur_policy = NULL; |
| 182 | status = lttng_action_rotate_session_get_firing_policy( |
| 183 | rotate_session_action, &cur_policy); |
| 184 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 185 | lttng_firing_policy_is_equal( |
| 186 | policy, |
| 187 | cur_policy), |
| 188 | "rotate_session action policy get"); |
| 189 | } |
| 190 | |
| 191 | /* Ser/des tests. */ |
| 192 | ret = lttng_action_serialize(rotate_session_action, &payload); |
| 193 | ok(ret == 0, "Action rotate_session serialized"); |
| 194 | |
| 195 | { |
| 196 | struct lttng_payload_view view = |
| 197 | lttng_payload_view_from_payload( |
| 198 | &payload, 0, -1); |
| 199 | (void) lttng_action_create_from_payload( |
| 200 | &view, &rotate_session_action_from_buffer); |
| 201 | } |
| 202 | ok(rotate_session_action_from_buffer, |
| 203 | "rotate_session action created from payload is non-null"); |
| 204 | |
| 205 | ok(lttng_action_is_equal(rotate_session_action, |
| 206 | rotate_session_action_from_buffer), |
| 207 | "Serialized and de-serialized rotate_session action are equal"); |
| 208 | |
| 209 | lttng_firing_policy_destroy(default_policy); |
| 210 | lttng_firing_policy_destroy(policy); |
| 211 | lttng_action_destroy(rotate_session_action); |
| 212 | lttng_action_destroy(rotate_session_action_from_buffer); |
| 213 | lttng_payload_reset(&payload); |
| 214 | } |
| 215 | |
| 216 | static void test_action_start_session(void) |
| 217 | { |
| 218 | int ret; |
| 219 | enum lttng_action_status status; |
| 220 | struct lttng_action *start_session_action = NULL, |
| 221 | *start_session_action_from_buffer = NULL; |
| 222 | struct lttng_firing_policy *policy = NULL, *default_policy; |
| 223 | struct lttng_payload payload; |
| 224 | const char *session_name = "my_session_name"; |
| 225 | const char *get_session_name; |
| 226 | |
| 227 | lttng_payload_init(&payload); |
| 228 | |
| 229 | /* To set. */ |
| 230 | policy = lttng_firing_policy_every_n_create(100); |
| 231 | /* For comparison. */ |
| 232 | default_policy = lttng_firing_policy_every_n_create(1); |
| 233 | |
| 234 | assert(policy && default_policy); |
| 235 | |
| 236 | start_session_action = lttng_action_start_session_create(); |
| 237 | ok(start_session_action, "Create start_session action"); |
| 238 | ok(lttng_action_get_type(start_session_action) == |
| 239 | LTTNG_ACTION_TYPE_START_SESSION, |
| 240 | "Action has type LTTNG_ACTION_TYPE_START_SESSION"); |
| 241 | |
| 242 | /* Session name setter. */ |
| 243 | status = lttng_action_start_session_set_session_name(NULL, NULL); |
| 244 | ok(status == LTTNG_ACTION_STATUS_INVALID, |
| 245 | "Set session name (NULL,NULL) expect invalid"); |
| 246 | status = lttng_action_start_session_set_session_name( |
| 247 | start_session_action, NULL); |
| 248 | ok(status == LTTNG_ACTION_STATUS_INVALID, |
| 249 | "Set session name (object,NULL) expect invalid"); |
| 250 | status = lttng_action_start_session_set_session_name( |
| 251 | NULL, session_name); |
| 252 | ok(status == LTTNG_ACTION_STATUS_INVALID, |
| 253 | "Set session name (NULL,object) expect invalid"); |
| 254 | |
| 255 | /* Set the session name */ |
| 256 | status = lttng_action_start_session_set_session_name( |
| 257 | start_session_action, session_name); |
| 258 | ok(status == LTTNG_ACTION_STATUS_OK, "Set session name"); |
| 259 | |
| 260 | status = lttng_action_start_session_get_session_name( |
| 261 | start_session_action, &get_session_name); |
| 262 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 263 | !strcmp(session_name, get_session_name), |
| 264 | "Get session name, expected `%s` got `%s`", |
| 265 | session_name, get_session_name); |
| 266 | |
| 267 | /* Validate the default policy for a start_session action. */ |
| 268 | { |
| 269 | const struct lttng_firing_policy *cur_policy = NULL; |
| 270 | status = lttng_action_start_session_get_firing_policy( |
| 271 | start_session_action, &cur_policy); |
| 272 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 273 | lttng_firing_policy_is_equal( |
| 274 | default_policy, |
| 275 | cur_policy), |
| 276 | "Default policy is every n=1"); |
| 277 | } |
| 278 | |
| 279 | /* Set a custom policy. */ |
| 280 | status = lttng_action_start_session_set_firing_policy( |
| 281 | start_session_action, policy); |
| 282 | ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); |
| 283 | |
| 284 | /* Validate the custom policy for a start_session action. */ |
| 285 | { |
| 286 | const struct lttng_firing_policy *cur_policy = NULL; |
| 287 | status = lttng_action_start_session_get_firing_policy( |
| 288 | start_session_action, &cur_policy); |
| 289 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 290 | lttng_firing_policy_is_equal( |
| 291 | policy, |
| 292 | cur_policy), |
| 293 | "start_session action policy get"); |
| 294 | } |
| 295 | |
| 296 | /* Ser/des tests. */ |
| 297 | ret = lttng_action_serialize(start_session_action, &payload); |
| 298 | ok(ret == 0, "Action start_session serialized"); |
| 299 | |
| 300 | { |
| 301 | struct lttng_payload_view view = |
| 302 | lttng_payload_view_from_payload( |
| 303 | &payload, 0, -1); |
| 304 | (void) lttng_action_create_from_payload( |
| 305 | &view, &start_session_action_from_buffer); |
| 306 | } |
| 307 | ok(start_session_action_from_buffer, |
| 308 | "start_session action created from payload is non-null"); |
| 309 | |
| 310 | ok(lttng_action_is_equal(start_session_action, |
| 311 | start_session_action_from_buffer), |
| 312 | "Serialized and de-serialized start_session action are equal"); |
| 313 | |
| 314 | lttng_firing_policy_destroy(default_policy); |
| 315 | lttng_firing_policy_destroy(policy); |
| 316 | lttng_action_destroy(start_session_action); |
| 317 | lttng_action_destroy(start_session_action_from_buffer); |
| 318 | lttng_payload_reset(&payload); |
| 319 | } |
| 320 | |
| 321 | int main(int argc, const char *argv[]) |
| 322 | { |
| 323 | plan_tests(NUM_TESTS); |
| 324 | test_action_notify(); |
| 325 | test_action_rotate_session(); |
| 326 | test_action_start_session(); |
| 327 | return exit_status(); |
| 328 | } |