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
12 #include <common/buffer-view.hpp>
13 #include <common/dynamic-buffer.hpp>
15 #include <lttng/condition/condition-internal.hpp>
16 #include <lttng/condition/event-rule-matches-internal.hpp>
17 #include <lttng/condition/event-rule-matches.h>
18 #include <lttng/domain.h>
19 #include <lttng/event-rule/user-tracepoint.h>
20 #include <lttng/event.h>
21 #include <lttng/log-level-rule.h>
30 int lttng_opt_quiet
= 1;
31 int lttng_opt_verbose
;
36 static void test_condition_event_rule()
39 struct lttng_event_rule
*tracepoint
= nullptr;
40 const struct lttng_event_rule
*tracepoint_tmp
= nullptr;
41 enum lttng_event_rule_status status
;
42 struct lttng_condition
*condition
= nullptr;
43 struct lttng_condition
*condition_from_buffer
= nullptr;
44 enum lttng_condition_status condition_status
;
45 const char *pattern
= "my_event_*";
46 const char *filter
= "msg_id == 23 && size >= 2048";
47 const char *exclusions
[] = { "my_event_test1", "my_event_test2", "my_event_test3" };
48 struct lttng_log_level_rule
*log_level_rule_at_least_as_severe
= nullptr;
49 struct lttng_payload buffer
;
51 lttng_payload_init(&buffer
);
53 /* Create log level rule. */
54 log_level_rule_at_least_as_severe
=
55 lttng_log_level_rule_at_least_as_severe_as_create(LTTNG_LOGLEVEL_WARNING
);
56 LTTNG_ASSERT(log_level_rule_at_least_as_severe
);
58 tracepoint
= lttng_event_rule_user_tracepoint_create();
59 ok(tracepoint
, "user tracepoint");
61 status
= lttng_event_rule_user_tracepoint_set_name_pattern(tracepoint
, pattern
);
62 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting pattern");
64 status
= lttng_event_rule_user_tracepoint_set_filter(tracepoint
, filter
);
65 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting filter");
67 status
= lttng_event_rule_user_tracepoint_set_log_level_rule(
68 tracepoint
, log_level_rule_at_least_as_severe
);
69 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting log level range");
71 for (i
= 0; i
< 3; i
++) {
72 status
= lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(tracepoint
,
74 ok(status
== LTTNG_EVENT_RULE_STATUS_OK
, "Setting exclusion pattern");
77 condition
= lttng_condition_event_rule_matches_create(tracepoint
);
78 ok(condition
, "Created condition");
80 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &tracepoint_tmp
);
81 ok(condition_status
== LTTNG_CONDITION_STATUS_OK
,
82 "Getting event rule from event rule condition");
83 ok(tracepoint
== tracepoint_tmp
,
84 "lttng_condition_event_rule_get_rule provides a reference to the original rule");
86 ret
= lttng_condition_serialize(condition
, &buffer
);
87 ok(ret
== 0, "Condition serialized");
90 struct lttng_payload_view view
= lttng_payload_view_from_payload(&buffer
, 0, -1);
92 (void) lttng_condition_create_from_payload(&view
, &condition_from_buffer
);
95 ok(condition_from_buffer
, "Condition created from payload is non-null");
97 ok(lttng_condition_is_equal(condition
, condition_from_buffer
),
98 "Serialized and de-serialized conditions are equal");
100 lttng_payload_reset(&buffer
);
101 lttng_event_rule_destroy(tracepoint
);
102 lttng_condition_destroy(condition
);
103 lttng_condition_destroy(condition_from_buffer
);
104 lttng_log_level_rule_destroy(log_level_rule_at_least_as_severe
);
109 plan_tests(NUM_TESTS
);
110 test_condition_event_rule();
111 return exit_status();