4 * Unit tests for the notification API.
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
8 * SPDX-License-Identifier: MIT
20 #include <lttng/action/action.h>
21 #include <lttng/action/notify.h>
22 #include <lttng/condition/buffer-usage.h>
23 #include <lttng/condition/condition.h>
24 #include <lttng/domain.h>
25 #include <lttng/notification/notification.h>
26 #include <lttng/trigger/trigger.h>
29 int lttng_opt_quiet
= 1;
30 int lttng_opt_verbose
;
35 static void test_condition_buffer_usage(
36 struct lttng_condition
*buffer_usage_condition
)
38 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
39 const char *session_name
= NULL
;
40 const char *channel_name
= NULL
;
41 enum lttng_domain_type domain_type
;
42 /* Start at a non zero value to validate initialization */
43 double threshold_ratio
;
44 uint64_t threshold_bytes
;
46 assert(buffer_usage_condition
);
48 diag("Validating initialization");
49 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
50 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
52 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
53 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold byte is unset");
55 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
56 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
57 ok(!session_name
, "Session name is null");
59 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
60 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
61 ok(!session_name
, "Channel name is null");
63 status
= lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition
, &domain_type
);
64 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Domain name is unset");
66 diag("Testing session name set/get");
67 status
= lttng_condition_buffer_usage_set_session_name(NULL
, "Test");
68 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null condition on set session name");
69 status
= lttng_condition_buffer_usage_get_session_name(NULL
, &session_name
);
70 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get session name with null condition");
71 ok(!session_name
, "Session name is null");
72 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
73 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
74 ok(!session_name
, "Session name is null");
76 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, NULL
);
77 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null session name");
78 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
79 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
80 ok(!session_name
, "Session name is null");
82 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "");
83 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set empty session name");
84 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
85 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Session name is unset");
86 ok(!session_name
, "Session name is null");
88 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "session420");
89 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set session name session420");
90 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
91 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Session name is set");
92 ok(session_name
, "Session name has a value");
93 ok(strcmp("session420", session_name
) == 0, "Session name is %s", "session420");
96 * Test second set on session_name. Test invalid set and validate that
97 * the value is still the previous good one.
100 status
= lttng_condition_buffer_usage_set_session_name(buffer_usage_condition
, "");
101 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set session name to empty");
102 status
= lttng_condition_buffer_usage_get_session_name(buffer_usage_condition
, &session_name
);
103 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Session name is still set");
104 ok(session_name
, "Session name has a value");
105 ok(strcmp("session420", session_name
) == 0, "Session is still name is %s", "session420");
107 diag("Testing channel name set/get");
108 status
= lttng_condition_buffer_usage_set_channel_name(NULL
, "Test");
109 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null condition on set channel name");
110 status
= lttng_condition_buffer_usage_get_channel_name(NULL
, &channel_name
);
111 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get channel name with null condition");
112 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
113 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
114 ok(!channel_name
, "Channel name is null");
116 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, NULL
);
117 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set null channel name");
118 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
119 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
120 ok(!channel_name
, "Channel name is null");
122 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "");
123 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set empty channel name");
124 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
125 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Channel name is unset");
126 ok(!channel_name
, "Channel name is null");
128 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "channel420");
129 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set channel name channel420");
130 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
131 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Channel name is set");
132 ok(channel_name
, "Channel name has a value");
133 ok(strcmp("channel420", channel_name
) == 0, "Channel name is %s", "channel420");
136 * Test second set on channel_name. Test invalid set and validate that
137 * the value is still the previous good one.
140 status
= lttng_condition_buffer_usage_set_channel_name(buffer_usage_condition
, "");
141 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set channel name to empty");
142 status
= lttng_condition_buffer_usage_get_channel_name(buffer_usage_condition
, &channel_name
);
143 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Channel name is still set");
144 ok(channel_name
, "Channel name has a value");
145 ok(strcmp("channel420", channel_name
) == 0, "Channel is still name is %s", "channel420");
147 diag("Testing threshold ratio set/get");
148 status
= lttng_condition_buffer_usage_set_threshold_ratio(NULL
, 0.420);
149 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set threshold ratio with null condition");
150 status
= lttng_condition_buffer_usage_get_threshold_ratio(NULL
, &threshold_ratio
);
151 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get threshold ratio with null condition");
152 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
153 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
155 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, -100.0);
156 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set threshold ratio < 0");
157 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
158 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
160 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 200.0);
161 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set Threshold ratio > 1");
162 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
163 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
165 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 1.0);
166 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold ratio == 1.0");
167 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
168 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
169 ok(threshold_ratio
== 1.0, "Threshold ratio is 1.0");
171 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 0.0);
172 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold ratio == 0.0");
173 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
174 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
175 ok(threshold_ratio
== 0.0, "Threshold ratio is 0.0");
177 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 0.420);
178 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold ratio == 0.420");
179 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
180 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
181 ok(threshold_ratio
== 0.420, "Threshold ratio is 0.420");
183 diag("Testing threshold bytes set/get");
184 status
= lttng_condition_buffer_usage_set_threshold(NULL
, 100000);
185 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set threshold with null condition");
186 status
= lttng_condition_buffer_usage_get_threshold(NULL
, &threshold_bytes
);
187 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get threshold value with null condition ");
188 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
189 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold is unset");
191 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, 100000);
192 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold > 0");
193 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
194 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
195 ok(threshold_bytes
== 100000, "Threshold is %" PRIu64
, 100000);
197 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, UINT64_MAX
);
198 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold UINT64_MAX");
199 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
200 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
201 ok(threshold_bytes
== UINT64_MAX
, "Threshold is UINT64_MAX");
203 status
= lttng_condition_buffer_usage_set_threshold(buffer_usage_condition
, 0);
204 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold == 0");
205 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
206 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold is set");
207 ok(threshold_bytes
== 0, "Threshold is %d", 0);
210 * Test value of threshold ration, since we overwrote it with a byte
211 * threshold. Make sure it gets squashed.
213 diag("Testing interaction between byte and ratio thresholds");
215 threshold_ratio
= -1.0;
216 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
217 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold ratio is unset");
218 ok(threshold_ratio
== -1.0, "Threshold ratio is untouched");
220 /* Set a ratio to validate that the byte threshold is now unset */
221 status
= lttng_condition_buffer_usage_set_threshold_ratio(buffer_usage_condition
, 0.420);
222 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set threshold ratio == 0.420");
223 status
= lttng_condition_buffer_usage_get_threshold_ratio(buffer_usage_condition
, &threshold_ratio
);
224 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Threshold ratio is set");
225 ok(threshold_ratio
== 0.420, "Threshold ratio is 0.420");
227 threshold_bytes
= 420;
228 status
= lttng_condition_buffer_usage_get_threshold(buffer_usage_condition
, &threshold_bytes
);
229 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Threshold is unset");
230 ok(threshold_bytes
== 420, "Threshold is untouched");
232 diag("Testing domain type set/get");
233 status
= lttng_condition_buffer_usage_set_domain_type(NULL
, LTTNG_DOMAIN_UST
);
234 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set domain type with null condition");
235 status
= lttng_condition_buffer_usage_get_domain_type(NULL
, &domain_type
);
236 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Get domain type with null condition");
238 status
= lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition
, LTTNG_DOMAIN_NONE
);
239 ok(status
== LTTNG_CONDITION_STATUS_INVALID
, "Set domain type as LTTNG_DOMAIN_NONE");
240 status
= lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition
, &domain_type
);
241 ok(status
== LTTNG_CONDITION_STATUS_UNSET
, "Domain type is unset");
243 status
= lttng_condition_buffer_usage_set_domain_type(buffer_usage_condition
, LTTNG_DOMAIN_UST
);
244 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Set domain type as LTTNG_DOMAIN_UST");
245 status
= lttng_condition_buffer_usage_get_domain_type(buffer_usage_condition
, &domain_type
);
246 ok(status
== LTTNG_CONDITION_STATUS_OK
, "Domain type is set");
247 ok(domain_type
== LTTNG_DOMAIN_UST
, "Domain type is LTTNG_DOMAIN_UST");
250 static void test_condition_buffer_usage_low(void)
252 struct lttng_condition
*buffer_usage_low
= NULL
;
254 diag("Testing lttng_condition_buffer_usage_low_create");
255 buffer_usage_low
= lttng_condition_buffer_usage_low_create();
256 ok(buffer_usage_low
, "Condition allocated");
258 ok(lttng_condition_get_type(buffer_usage_low
) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
, "Condition is of type \"low buffer usage\"");
260 test_condition_buffer_usage(buffer_usage_low
);
262 lttng_condition_destroy(buffer_usage_low
);
265 static void test_condition_buffer_usage_high(void)
267 struct lttng_condition
*buffer_usage_high
= NULL
;
269 diag("Testing lttng_condition_buffer_usage_high_create");
270 buffer_usage_high
= lttng_condition_buffer_usage_high_create();
271 ok(buffer_usage_high
, "High buffer usage condition allocated");
273 ok(lttng_condition_get_type(buffer_usage_high
) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
, "Condition is of type \"high buffer usage\"");
275 test_condition_buffer_usage(buffer_usage_high
);
277 lttng_condition_destroy(buffer_usage_high
);
280 static void test_action(void)
282 struct lttng_action
*notify_action
= NULL
;
284 notify_action
= lttng_action_notify_create();
285 ok(notify_action
, "Create notify action");
286 ok(lttng_action_get_type(notify_action
) == LTTNG_ACTION_TYPE_NOTIFY
, "Action has type LTTNG_ACTION_TYPE_NOTIFY");
287 lttng_action_destroy(notify_action
);
290 static void test_trigger(void)
292 struct lttng_action
*notify_action
= NULL
;
293 struct lttng_condition
*buffer_usage_high
= NULL
;
294 struct lttng_trigger
*trigger
= NULL
;
296 notify_action
= lttng_action_notify_create();
297 buffer_usage_high
= lttng_condition_buffer_usage_high_create();
299 trigger
= lttng_trigger_create(NULL
, NULL
);
300 ok(!trigger
, "lttng_trigger_create(NULL, NULL) returns null");
301 trigger
= lttng_trigger_create(buffer_usage_high
, NULL
);
302 ok(!trigger
, "lttng_trigger_create(NON-NULL, NULL) returns null");
303 trigger
= lttng_trigger_create(NULL
, notify_action
);
304 ok(!trigger
, "lttng_trigger_create(NULL, NON-NULL) returns null");
306 trigger
= lttng_trigger_create(buffer_usage_high
, notify_action
);
307 ok(trigger
, "lttng_trigger_create(NON-NULL, NON-NULL) returns an object");
309 lttng_action_destroy(notify_action
);
310 lttng_condition_destroy(buffer_usage_high
);
311 lttng_trigger_destroy(trigger
);
315 int main(int argc
, const char *argv
[])
317 plan_tests(NUM_TESTS
);
318 test_condition_buffer_usage_low();
319 test_condition_buffer_usage_high();
322 return exit_status();