| 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 | |
| 28 | /* For error.h */ |
| 29 | int lttng_opt_quiet = 1; |
| 30 | int lttng_opt_verbose; |
| 31 | int lttng_opt_mi; |
| 32 | |
| 33 | #define NUM_TESTS 8 |
| 34 | |
| 35 | static void test_action_notify(void) |
| 36 | { |
| 37 | int ret; |
| 38 | enum lttng_action_status status; |
| 39 | struct lttng_action *notify_action = NULL, |
| 40 | *notify_action_from_buffer = NULL; |
| 41 | struct lttng_firing_policy *policy = NULL, *default_policy; |
| 42 | struct lttng_payload payload; |
| 43 | |
| 44 | lttng_payload_init(&payload); |
| 45 | |
| 46 | /* To set. */ |
| 47 | policy = lttng_firing_policy_every_n_create(100); |
| 48 | /* For comparison. */ |
| 49 | default_policy = lttng_firing_policy_every_n_create(1); |
| 50 | |
| 51 | assert(policy && default_policy); |
| 52 | |
| 53 | notify_action = lttng_action_notify_create(); |
| 54 | ok(notify_action, "Create notify action"); |
| 55 | ok(lttng_action_get_type(notify_action) == LTTNG_ACTION_TYPE_NOTIFY, |
| 56 | "Action has type LTTNG_ACTION_TYPE_NOTIFY"); |
| 57 | |
| 58 | /* Validate the default policy for a notify action. */ |
| 59 | { |
| 60 | const struct lttng_firing_policy *cur_policy = NULL; |
| 61 | status = lttng_action_notify_get_firing_policy( |
| 62 | notify_action, &cur_policy); |
| 63 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 64 | lttng_firing_policy_is_equal( |
| 65 | default_policy, |
| 66 | cur_policy), |
| 67 | "Default policy is every n=1"); |
| 68 | } |
| 69 | |
| 70 | /* Set a custom policy. */ |
| 71 | status = lttng_action_notify_set_firing_policy(notify_action, policy); |
| 72 | ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); |
| 73 | |
| 74 | /* Validate the custom policy for a notify action. */ |
| 75 | { |
| 76 | const struct lttng_firing_policy *cur_policy = NULL; |
| 77 | status = lttng_action_notify_get_firing_policy( |
| 78 | notify_action, &cur_policy); |
| 79 | ok(status == LTTNG_ACTION_STATUS_OK && |
| 80 | lttng_firing_policy_is_equal( |
| 81 | policy, |
| 82 | cur_policy), |
| 83 | "Notify action policy get"); |
| 84 | } |
| 85 | |
| 86 | ret = lttng_action_serialize(notify_action, &payload); |
| 87 | ok(ret == 0, "Action notify serialized"); |
| 88 | |
| 89 | { |
| 90 | struct lttng_payload_view view = |
| 91 | lttng_payload_view_from_payload( |
| 92 | &payload, 0, -1); |
| 93 | (void) lttng_action_create_from_payload( |
| 94 | &view, ¬ify_action_from_buffer); |
| 95 | } |
| 96 | ok(notify_action_from_buffer, |
| 97 | "Notify action created from payload is non-null"); |
| 98 | |
| 99 | ok(lttng_action_is_equal(notify_action, notify_action_from_buffer), |
| 100 | "Serialized and de-serialized notify action are equal"); |
| 101 | |
| 102 | lttng_firing_policy_destroy(default_policy); |
| 103 | lttng_firing_policy_destroy(policy); |
| 104 | lttng_action_destroy(notify_action); |
| 105 | lttng_action_destroy(notify_action_from_buffer); |
| 106 | lttng_payload_reset(&payload); |
| 107 | } |
| 108 | |
| 109 | int main(int argc, const char *argv[]) |
| 110 | { |
| 111 | plan_tests(NUM_TESTS); |
| 112 | test_action_notify(); |
| 113 | return exit_status(); |
| 114 | } |