tests: make functions static in test_notification.c
[lttng-tools.git] / tests / unit / test_notification.c
CommitLineData
572cb877
JR
1/*
2 * test_notification.c
3 *
4 * Unit tests for the notification API.
5 *
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@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 <assert.h>
28#include <inttypes.h>
29#include <stdio.h>
30#include <string.h>
31#include <unistd.h>
32
33#include <tap/tap.h>
34
35#include <lttng/action/action.h>
36#include <lttng/action/notify.h>
37#include <lttng/condition/buffer-usage.h>
38#include <lttng/condition/condition.h>
39#include <lttng/domain.h>
40#include <lttng/notification/notification.h>
41#include <lttng/trigger/trigger.h>
42
43/* For error.h */
44int lttng_opt_quiet = 1;
45int lttng_opt_verbose;
46int lttng_opt_mi;
47
48#define NUM_TESTS 182
49
f059a4e0
SM
50static void test_condition_buffer_usage(
51 struct lttng_condition *buffer_usage_condition)
572cb877
JR
52{
53 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
54 const char *session_name = NULL;
55 const char *channel_name = NULL;
56 enum lttng_domain_type domain_type;
57 /* Start at a non zero value to validate initialization */
58 double threshold_ratio;
59 uint64_t threshold_bytes;
60
61 assert(buffer_usage_condition);
62
63 diag("Validating initialization");
64 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
65 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
66
67 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
68 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold byte is unset");
69
70 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
71 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
72 ok(!session_name, "Session name is null");
73
74 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
75 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
76 ok(!session_name, "Channel name is null");
77
78 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
79 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain name is unset");
80
81 diag("Testing session name set/get");
82 status = lttng_condition_buffer_usage_set_session_name(NULL, "Test");
83 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set session name");
84 status = lttng_condition_buffer_usage_get_session_name(NULL, &session_name);
85 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get session name with null condition");
86 ok(!session_name, "Session name is null");
87 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
88 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
89 ok(!session_name, "Session name is null");
90
91 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, NULL);
92 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null session name");
93 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
94 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
95 ok(!session_name, "Session name is null");
96
97 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "");
98 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty session name");
99 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
100 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Session name is unset");
101 ok(!session_name, "Session name is null");
102
103 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "session420");
104 ok(status == LTTNG_CONDITION_STATUS_OK, "Set session name session420");
105 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
106 ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is set");
107 ok(session_name, "Session name has a value");
108 ok(strcmp("session420", session_name) == 0, "Session name is %s", "session420");
109
110 /*
111 * Test second set on session_name. Test invalid set and validate that
112 * the value is still the previous good one.
113 */
114
115 status = lttng_condition_buffer_usage_set_session_name(buffer_usage_condition, "");
116 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set session name to empty");
117 status = lttng_condition_buffer_usage_get_session_name(buffer_usage_condition, &session_name);
118 ok(status == LTTNG_CONDITION_STATUS_OK, "Session name is still set");
119 ok(session_name, "Session name has a value");
120 ok(strcmp("session420", session_name) == 0, "Session is still name is %s", "session420");
121
122 diag("Testing channel name set/get");
123 status = lttng_condition_buffer_usage_set_channel_name(NULL, "Test");
124 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null condition on set channel name");
125 status = lttng_condition_buffer_usage_get_channel_name(NULL, &channel_name);
126 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get channel name with null condition");
127 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
128 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
129 ok(!channel_name, "Channel name is null");
130
131 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, NULL);
132 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set null channel name");
133 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
134 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
135 ok(!channel_name, "Channel name is null");
136
137 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "");
138 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set empty channel name");
139 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
140 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Channel name is unset");
141 ok(!channel_name, "Channel name is null");
142
143 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "channel420");
144 ok(status == LTTNG_CONDITION_STATUS_OK, "Set channel name channel420");
145 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
146 ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is set");
147 ok(channel_name, "Channel name has a value");
148 ok(strcmp("channel420", channel_name) == 0, "Channel name is %s", "channel420");
149
150 /*
151 * Test second set on channel_name. Test invalid set and validate that
152 * the value is still the previous good one.
153 */
154
155 status = lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition, "");
156 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set channel name to empty");
157 status = lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition, &channel_name);
158 ok(status == LTTNG_CONDITION_STATUS_OK, "Channel name is still set");
159 ok(channel_name, "Channel name has a value");
160 ok(strcmp("channel420", channel_name) == 0, "Channel is still name is %s", "channel420");
161
162 diag("Testing threshold ratio set/get");
163 status = lttng_condition_buffer_usage_set_threshold_ratio(NULL, 0.420);
164 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio with null condition");
165 status = lttng_condition_buffer_usage_get_threshold_ratio(NULL, &threshold_ratio);
166 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold ratio with null condition");
167 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
168 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
169
170 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, -100.0);
171 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold ratio < 0");
172 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
173 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
174
175 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 200.0);
176 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set Threshold ratio > 1");
177 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
178 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
179
180 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 1.0);
181 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 1.0");
182 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
183 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
184 ok(threshold_ratio == 1.0, "Threshold ratio is 1.0");
185
186 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.0);
187 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.0");
188 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
189 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
190 ok(threshold_ratio == 0.0, "Threshold ratio is 0.0");
191
192 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420);
193 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420");
194 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
195 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
196 ok(threshold_ratio == 0.420, "Threshold ratio is 0.420");
197
198 diag("Testing threshold bytes set/get");
199 status = lttng_condition_buffer_usage_set_threshold(NULL, 100000);
200 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set threshold with null condition");
201 status = lttng_condition_buffer_usage_get_threshold(NULL, &threshold_bytes);
202 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get threshold value with null condition ");
203 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
204 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset");
205
206 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 100000);
207 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold > 0");
208 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
209 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
210 ok(threshold_bytes == 100000, "Threshold is %" PRIu64 , 100000);
211
212 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, UINT64_MAX);
213 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold UINT64_MAX");
214 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
215 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
216 ok(threshold_bytes == UINT64_MAX, "Threshold is UINT64_MAX");
217
218 status = lttng_condition_buffer_usage_set_threshold(buffer_usage_condition, 0);
219 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold == 0");
220 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
221 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold is set");
222 ok(threshold_bytes == 0, "Threshold is %d", 0);
223
224 /*
225 * Test value of threshold ration, since we overwrote it with a byte
226 * threshold. Make sure it gets squashed.
227 */
228 diag("Testing interaction between byte and ratio thresholds");
229
230 threshold_ratio = -1.0;
231 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
232 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold ratio is unset");
233 ok(threshold_ratio == -1.0, "Threshold ratio is untouched");
234
235 /* Set a ratio to validate that the byte threshold is now unset */
236 status = lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition, 0.420);
237 ok(status == LTTNG_CONDITION_STATUS_OK, "Set threshold ratio == 0.420");
238 status = lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition, &threshold_ratio);
239 ok(status == LTTNG_CONDITION_STATUS_OK, "Threshold ratio is set");
240 ok(threshold_ratio == 0.420, "Threshold ratio is 0.420");
241
242 threshold_bytes = 420;
243 status = lttng_condition_buffer_usage_get_threshold(buffer_usage_condition, &threshold_bytes);
244 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Threshold is unset");
245 ok(threshold_bytes == 420, "Threshold is untouched");
246
247 diag("Testing domain type set/get");
248 status = lttng_condition_buffer_usage_set_domain_type(NULL, LTTNG_DOMAIN_UST);
249 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type with null condition");
250 status = lttng_condition_buffer_usage_get_domain_type(NULL, &domain_type);
251 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Get domain type with null condition");
252
253 status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition, LTTNG_DOMAIN_NONE);
254 ok(status == LTTNG_CONDITION_STATUS_INVALID, "Set domain type as LTTNG_DOMAIN_NONE");
255 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
256 ok(status == LTTNG_CONDITION_STATUS_UNSET, "Domain type is unset");
257
258 status = lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition, LTTNG_DOMAIN_UST);
259 ok(status == LTTNG_CONDITION_STATUS_OK, "Set domain type as LTTNG_DOMAIN_UST");
260 status = lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition, &domain_type);
261 ok(status == LTTNG_CONDITION_STATUS_OK, "Domain type is set");
262 ok(domain_type == LTTNG_DOMAIN_UST, "Domain type is LTTNG_DOMAIN_UST");
263}
264
f059a4e0 265static void test_condition_buffer_usage_low(void)
572cb877
JR
266{
267 struct lttng_condition *buffer_usage_low = NULL;
268
269 diag("Testing lttng_condition_buffer_usage_low_create");
270 buffer_usage_low = lttng_condition_buffer_usage_low_create();
271 ok(buffer_usage_low, "Condition allocated");
272
273 ok(lttng_condition_get_type(buffer_usage_low) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, "Condition is of type \"low buffer usage\"");
274
275 test_condition_buffer_usage(buffer_usage_low);
276
277 lttng_condition_destroy(buffer_usage_low);
278}
279
f059a4e0 280static void test_condition_buffer_usage_high(void)
572cb877
JR
281{
282 struct lttng_condition *buffer_usage_high = NULL;
283
284 diag("Testing lttng_condition_buffer_usage_high_create");
285 buffer_usage_high = lttng_condition_buffer_usage_high_create();
286 ok(buffer_usage_high, "High buffer usage condition allocated");
287
288 ok(lttng_condition_get_type(buffer_usage_high) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, "Condition is of type \"high buffer usage\"");
289
290 test_condition_buffer_usage(buffer_usage_high);
291
292 lttng_condition_destroy(buffer_usage_high);
293}
294
f059a4e0 295static void test_action(void)
572cb877
JR
296{
297 struct lttng_action *notify_action = NULL;
298
299 notify_action = lttng_action_notify_create();
300 ok(notify_action, "Create notify action");
301 ok(lttng_action_get_type(notify_action) == LTTNG_ACTION_TYPE_NOTIFY, "Action has type LTTNG_ACTION_TYPE_NOTIFY");
302 lttng_action_destroy(notify_action);
303}
304
f059a4e0 305static void test_trigger(void)
572cb877
JR
306{
307 struct lttng_action *notify_action = NULL;
308 struct lttng_condition *buffer_usage_high = NULL;
309 struct lttng_trigger *trigger = NULL;
310
311 notify_action = lttng_action_notify_create();
312 buffer_usage_high = lttng_condition_buffer_usage_high_create();
313
314 trigger = lttng_trigger_create(NULL, NULL);
315 ok(!trigger, "lttng_trigger_create(NULL, NULL) returns null");
316 trigger = lttng_trigger_create(buffer_usage_high, NULL);
317 ok(!trigger, "lttng_trigger_create(NON-NULL, NULL) returns null");
318 trigger = lttng_trigger_create(NULL, notify_action);
319 ok(!trigger, "lttng_trigger_create(NULL, NON-NULL) returns null");
320
321 trigger = lttng_trigger_create(buffer_usage_high, notify_action);
322 ok(trigger, "lttng_trigger_create(NON-NULL, NON-NULL) returns an object");
323
324 lttng_action_destroy(notify_action);
325 lttng_condition_destroy(buffer_usage_high);
326 lttng_trigger_destroy(trigger);
327}
328
329
330int main(int argc, const char *argv[])
331{
332 plan_tests(NUM_TESTS);
333 test_condition_buffer_usage_low();
334 test_condition_buffer_usage_high();
335 test_action();
336 test_trigger();
337 return exit_status();
338}
This page took 0.048349 seconds and 5 git commands to generate.