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/user-tracepoint.h>
22 #include <lttng/condition/condition-internal.h>
23 #include <lttng/condition/event-rule-matches.h>
24 #include <lttng/condition/event-rule-matches-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 struct lttng_log_level_rule
*log_level_rule_at_least_as_severe
= NULL
;
51 struct lttng_payload buffer
;
53 lttng_payload_init(&buffer
);
55 /* Create log level rule. */
56 log_level_rule_at_least_as_severe
=
57 lttng_log_level_rule_at_least_as_severe_as_create(
58 LTTNG_LOGLEVEL_WARNING
);
59 assert(log_level_rule_at_least_as_severe
);
61 tracepoint
= lttng_event_rule_user_tracepoint_create();
62 ok(tracepoint
, "user tracepoint");
64 status
= lttng_event_rule_user_tracepoint_set_name_pattern(tracepoint
, pattern
);
65 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting pattern");
67 status
= lttng_event_rule_user_tracepoint_set_filter(tracepoint
, filter
);
68 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting filter");
70 status
= lttng_event_rule_user_tracepoint_set_log_level_rule(
71 tracepoint
, log_level_rule_at_least_as_severe
);
72 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting log level range");
74 for (i
= 0; i
< 3; i
++) {
75 status
= lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
76 tracepoint
, exclusions
[i
]);
77 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
,
78 "Setting exclusion pattern");
81 condition
= lttng_condition_event_rule_matches_create(tracepoint
);
82 ok(condition
, "Created condition");
84 condition_status
= lttng_condition_event_rule_matches_get_rule(
85 condition
, &tracepoint_tmp
);
86 ok(condition_status
== LTTNG_CONDITION_STATUS_OK
,
87 "Getting event rule from event rule condition");
88 ok(tracepoint
== tracepoint_tmp
, "lttng_condition_event_rule_get_rule provides a reference to the original rule");
90 ret
= lttng_condition_serialize(condition
, &buffer
);
91 ok(ret
== 0, "Condition serialized");
94 struct lttng_payload_view view
=
95 lttng_payload_view_from_payload(&buffer
, 0, -1);
97 (void) lttng_condition_create_from_payload(
98 &view
, &condition_from_buffer
);
101 ok(condition_from_buffer
, "Condition created from payload is non-null");
103 ok(lttng_condition_is_equal(condition
, condition_from_buffer
),
104 "Serialized and de-serialized conditions are equal");
106 lttng_payload_reset(&buffer
);
107 lttng_event_rule_destroy(tracepoint
);
108 lttng_condition_destroy(condition
);
109 lttng_condition_destroy(condition_from_buffer
);
110 lttng_log_level_rule_destroy(log_level_rule_at_least_as_severe
);
113 int main(int argc
, const char *argv
[])
115 plan_tests(NUM_TESTS
);
116 test_condition_event_rule();
117 return exit_status();