4 * Unit tests for the condition API.
6 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
8 * SPDX-License-Identifier: GPL-2.0-only
20 #include <lttng/event.h>
21 #include <lttng/event-rule/tracepoint.h>
22 #include <lttng/condition/condition-internal.h>
23 #include <lttng/condition/on-event.h>
24 #include <lttng/condition/on-event-internal.h>
25 #include <lttng/domain.h>
26 #include <lttng/log-level-rule.h>
27 #include <common/dynamic-buffer.h>
28 #include <common/buffer-view.h>
31 int lttng_opt_quiet
= 1;
32 int lttng_opt_verbose
;
38 void test_condition_event_rule(void)
41 struct lttng_event_rule
*tracepoint
= NULL
;
42 const struct lttng_event_rule
*tracepoint_tmp
= NULL
;
43 enum lttng_event_rule_status status
;
44 struct lttng_condition
*condition
= NULL
;
45 struct lttng_condition
*condition_from_buffer
= NULL
;
46 enum lttng_condition_status condition_status
;
47 const char *pattern
="my_event_*";
48 const char *filter
="msg_id == 23 && size >= 2048";
49 const char *exclusions
[] = { "my_event_test1", "my_event_test2", "my_event_test3" };
50 uint64_t _error_count
= 420, _error_counter_index
= 9999, error_count
, error_counter_index
;
51 struct lttng_log_level_rule
*log_level_rule_at_least_as_severe
= NULL
;
52 struct lttng_payload buffer
;
54 lttng_payload_init(&buffer
);
56 /* Create log level rule */
57 log_level_rule_at_least_as_severe
= lttng_log_level_rule_at_least_as_severe_as_create(LTTNG_LOGLEVEL_WARNING
);
58 assert(log_level_rule_at_least_as_severe
);
60 tracepoint
= lttng_event_rule_tracepoint_create(LTTNG_DOMAIN_UST
);
61 ok(tracepoint
, "tracepoint UST_DOMAIN");
63 status
= lttng_event_rule_tracepoint_set_pattern(tracepoint
, pattern
);
64 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting pattern");
66 status
= lttng_event_rule_tracepoint_set_filter(tracepoint
, filter
);
67 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting filter");
69 status
= lttng_event_rule_tracepoint_set_log_level_rule(tracepoint
, log_level_rule_at_least_as_severe
);
70 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting log level range");
72 for (i
= 0; i
< 3; i
++) {
73 status
= lttng_event_rule_tracepoint_add_exclusion(
74 tracepoint
, exclusions
[i
]);
75 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
76 "Setting exclusion pattern");
79 condition
= lttng_condition_on_event_create(tracepoint
);
80 ok(condition
, "Created condition");
82 /* Set the error count information */
83 lttng_condition_on_event_set_error_count(condition
, _error_count
);
84 lttng_condition_on_event_set_error_counter_index(condition
, _error_counter_index
);
86 condition_status
= lttng_condition_on_event_get_rule(
87 condition
, &tracepoint_tmp
);
88 ok(condition_status
== LTTNG_CONDITION_STATUS_OK
,
89 "Getting event rule from event rule condition");
90 ok(tracepoint
== tracepoint_tmp
, "lttng_condition_event_rule_get_rule provides a reference to the original rule");
92 ret
= lttng_condition_serialize(condition
, &buffer
);
93 ok(ret
== 0, "Condition serialized");
96 struct lttng_payload_view view
=
97 lttng_payload_view_from_payload(&buffer
, 0, -1);
99 (void) lttng_condition_create_from_payload(
100 &view
, &condition_from_buffer
);
103 ok(condition_from_buffer
, "Condition created from payload is non-null");
105 ok(lttng_condition_is_equal(condition
, condition_from_buffer
),
106 "Serialized and de-serialized conditions are equal");
108 /* Error count info is not considered in is_equal so test it separately */
109 error_count
= lttng_condition_on_event_get_error_count(condition_from_buffer
);
110 error_counter_index
= lttng_condition_on_event_get_error_counter_index(condition_from_buffer
);
111 ok(error_count
== _error_count
, "Error count is the same. Got %" PRIu64
" Expected %" PRIu64
, error_count
, _error_count
);
112 ok(error_counter_index
== _error_counter_index
, "Error count index is the same. Got %" PRIu64
" Expected %" PRIu64
, error_count
, _error_count
);
114 lttng_payload_reset(&buffer
);
115 lttng_event_rule_destroy(tracepoint
);
116 lttng_condition_destroy(condition
);
117 lttng_condition_destroy(condition_from_buffer
);
118 lttng_log_level_rule_destroy(log_level_rule_at_least_as_severe
);
121 int main(int argc
, const char *argv
[])
123 plan_tests(NUM_TESTS
);
124 test_condition_event_rule();
125 return exit_status();