Commit | Line | Data |
---|---|---|
9cff59ec JR |
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> | |
ce1c2b5d | 27 | #include <lttng/action/rotate-session.h> |
9cff59ec JR |
28 | |
29 | /* For error.h */ | |
30 | int lttng_opt_quiet = 1; | |
31 | int lttng_opt_verbose; | |
32 | int lttng_opt_mi; | |
33 | ||
ce1c2b5d | 34 | #define NUM_TESTS 21 |
9cff59ec JR |
35 | |
36 | static void test_action_notify(void) | |
37 | { | |
193cc911 JR |
38 | int ret; |
39 | enum lttng_action_status status; | |
40 | struct lttng_action *notify_action = NULL, | |
41 | *notify_action_from_buffer = NULL; | |
42 | struct lttng_firing_policy *policy = NULL, *default_policy; | |
43 | struct lttng_payload payload; | |
44 | ||
45 | lttng_payload_init(&payload); | |
46 | ||
47 | /* To set. */ | |
48 | policy = lttng_firing_policy_every_n_create(100); | |
49 | /* For comparison. */ | |
50 | default_policy = lttng_firing_policy_every_n_create(1); | |
51 | ||
52 | assert(policy && default_policy); | |
9cff59ec JR |
53 | |
54 | notify_action = lttng_action_notify_create(); | |
55 | ok(notify_action, "Create notify action"); | |
56 | ok(lttng_action_get_type(notify_action) == LTTNG_ACTION_TYPE_NOTIFY, | |
57 | "Action has type LTTNG_ACTION_TYPE_NOTIFY"); | |
193cc911 JR |
58 | |
59 | /* Validate the default policy for a notify action. */ | |
60 | { | |
61 | const struct lttng_firing_policy *cur_policy = NULL; | |
62 | status = lttng_action_notify_get_firing_policy( | |
63 | notify_action, &cur_policy); | |
64 | ok(status == LTTNG_ACTION_STATUS_OK && | |
65 | lttng_firing_policy_is_equal( | |
66 | default_policy, | |
67 | cur_policy), | |
68 | "Default policy is every n=1"); | |
69 | } | |
70 | ||
71 | /* Set a custom policy. */ | |
72 | status = lttng_action_notify_set_firing_policy(notify_action, policy); | |
73 | ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); | |
74 | ||
75 | /* Validate the custom policy for a notify action. */ | |
76 | { | |
77 | const struct lttng_firing_policy *cur_policy = NULL; | |
78 | status = lttng_action_notify_get_firing_policy( | |
79 | notify_action, &cur_policy); | |
80 | ok(status == LTTNG_ACTION_STATUS_OK && | |
81 | lttng_firing_policy_is_equal( | |
82 | policy, | |
83 | cur_policy), | |
84 | "Notify action policy get"); | |
85 | } | |
86 | ||
87 | ret = lttng_action_serialize(notify_action, &payload); | |
88 | ok(ret == 0, "Action notify serialized"); | |
89 | ||
90 | { | |
91 | struct lttng_payload_view view = | |
92 | lttng_payload_view_from_payload( | |
93 | &payload, 0, -1); | |
94 | (void) lttng_action_create_from_payload( | |
95 | &view, ¬ify_action_from_buffer); | |
96 | } | |
97 | ok(notify_action_from_buffer, | |
98 | "Notify action created from payload is non-null"); | |
99 | ||
100 | ok(lttng_action_is_equal(notify_action, notify_action_from_buffer), | |
101 | "Serialized and de-serialized notify action are equal"); | |
102 | ||
103 | lttng_firing_policy_destroy(default_policy); | |
104 | lttng_firing_policy_destroy(policy); | |
9cff59ec | 105 | lttng_action_destroy(notify_action); |
193cc911 JR |
106 | lttng_action_destroy(notify_action_from_buffer); |
107 | lttng_payload_reset(&payload); | |
9cff59ec JR |
108 | } |
109 | ||
ce1c2b5d JR |
110 | static void test_action_rotate_session(void) |
111 | { | |
112 | int ret; | |
113 | enum lttng_action_status status; | |
114 | struct lttng_action *rotate_session_action = NULL, | |
115 | *rotate_session_action_from_buffer = NULL; | |
116 | struct lttng_firing_policy *policy = NULL, *default_policy; | |
117 | struct lttng_payload payload; | |
118 | const char *session_name = "my_session_name"; | |
119 | const char *get_session_name; | |
120 | ||
121 | lttng_payload_init(&payload); | |
122 | ||
123 | /* To set. */ | |
124 | policy = lttng_firing_policy_every_n_create(100); | |
125 | /* For comparison. */ | |
126 | default_policy = lttng_firing_policy_every_n_create(1); | |
127 | ||
128 | assert(policy && default_policy); | |
129 | ||
130 | rotate_session_action = lttng_action_rotate_session_create(); | |
131 | ok(rotate_session_action, "Create rotate_session action"); | |
132 | ok(lttng_action_get_type(rotate_session_action) == | |
133 | LTTNG_ACTION_TYPE_ROTATE_SESSION, | |
134 | "Action has type LTTNG_ACTION_TYPE_ROTATE_SESSION"); | |
135 | ||
136 | /* Session name setter. */ | |
137 | status = lttng_action_rotate_session_set_session_name(NULL, NULL); | |
138 | ok(status == LTTNG_ACTION_STATUS_INVALID, | |
139 | "Set session name (NULL,NULL) expect invalid"); | |
140 | status = lttng_action_rotate_session_set_session_name( | |
141 | rotate_session_action, NULL); | |
142 | ok(status == LTTNG_ACTION_STATUS_INVALID, | |
143 | "Set session name (object,NULL) expect invalid"); | |
144 | status = lttng_action_rotate_session_set_session_name( | |
145 | NULL, session_name); | |
146 | ok(status == LTTNG_ACTION_STATUS_INVALID, | |
147 | "Set session name (NULL,object) expect invalid"); | |
148 | ||
149 | /* Set the session name */ | |
150 | status = lttng_action_rotate_session_set_session_name( | |
151 | rotate_session_action, session_name); | |
152 | ok(status == LTTNG_ACTION_STATUS_OK, "Set session name"); | |
153 | ||
154 | status = lttng_action_rotate_session_get_session_name( | |
155 | rotate_session_action, &get_session_name); | |
156 | ok(status == LTTNG_ACTION_STATUS_OK && | |
157 | !strcmp(session_name, get_session_name), | |
158 | "Get session name, expected `%s` got `%s`", | |
159 | session_name, get_session_name); | |
160 | ||
161 | /* Validate the default policy for a rotate_session action. */ | |
162 | { | |
163 | const struct lttng_firing_policy *cur_policy = NULL; | |
164 | status = lttng_action_rotate_session_get_firing_policy( | |
165 | rotate_session_action, &cur_policy); | |
166 | ok(status == LTTNG_ACTION_STATUS_OK && | |
167 | lttng_firing_policy_is_equal( | |
168 | default_policy, | |
169 | cur_policy), | |
170 | "Default policy is every n=1"); | |
171 | } | |
172 | ||
173 | /* Set a custom policy. */ | |
174 | status = lttng_action_rotate_session_set_firing_policy( | |
175 | rotate_session_action, policy); | |
176 | ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); | |
177 | ||
178 | /* Validate the custom policy for a rotate_session action. */ | |
179 | { | |
180 | const struct lttng_firing_policy *cur_policy = NULL; | |
181 | status = lttng_action_rotate_session_get_firing_policy( | |
182 | rotate_session_action, &cur_policy); | |
183 | ok(status == LTTNG_ACTION_STATUS_OK && | |
184 | lttng_firing_policy_is_equal( | |
185 | policy, | |
186 | cur_policy), | |
187 | "rotate_session action policy get"); | |
188 | } | |
189 | ||
190 | /* Ser/des tests. */ | |
191 | ret = lttng_action_serialize(rotate_session_action, &payload); | |
192 | ok(ret == 0, "Action rotate_session serialized"); | |
193 | ||
194 | { | |
195 | struct lttng_payload_view view = | |
196 | lttng_payload_view_from_payload( | |
197 | &payload, 0, -1); | |
198 | (void) lttng_action_create_from_payload( | |
199 | &view, &rotate_session_action_from_buffer); | |
200 | } | |
201 | ok(rotate_session_action_from_buffer, | |
202 | "rotate_session action created from payload is non-null"); | |
203 | ||
204 | ok(lttng_action_is_equal(rotate_session_action, | |
205 | rotate_session_action_from_buffer), | |
206 | "Serialized and de-serialized rotate_session action are equal"); | |
207 | ||
208 | lttng_firing_policy_destroy(default_policy); | |
209 | lttng_firing_policy_destroy(policy); | |
210 | lttng_action_destroy(rotate_session_action); | |
211 | lttng_action_destroy(rotate_session_action_from_buffer); | |
212 | lttng_payload_reset(&payload); | |
213 | } | |
214 | ||
9cff59ec JR |
215 | int main(int argc, const char *argv[]) |
216 | { | |
217 | plan_tests(NUM_TESTS); | |
218 | test_action_notify(); | |
ce1c2b5d | 219 | test_action_rotate_session(); |
9cff59ec JR |
220 | return exit_status(); |
221 | } |