Tests: unit: lttng_condition_event_rule
[lttng-tools.git] / tests / unit / test_condition.c
1 /*
2 * test_condition.c
3 *
4 * Unit tests for the condition API.
5 *
6 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
7 *
8 * SPDX-License-Identifier: GPL-2.0-only
9 *
10 */
11
12 #include <assert.h>
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include <tap/tap.h>
19
20 #include <lttng/event.h>
21 #include <lttng/event-rule/tracepoint.h>
22 #include <lttng/condition/condition-internal.h>
23 #include <lttng/condition/event-rule.h>
24 #include <lttng/domain.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/buffer-view.h>
27
28 /* For error.h */
29 int lttng_opt_quiet = 1;
30 int lttng_opt_verbose;
31 int lttng_opt_mi;
32
33 #define NUM_TESTS 13
34
35 static
36 void test_condition_event_rule(void)
37 {
38 int ret, i;
39 struct lttng_event_rule *tracepoint = NULL;
40 const struct lttng_event_rule *tracepoint_tmp = NULL;
41 enum lttng_event_rule_status status;
42 struct lttng_condition *condition = NULL;
43 struct lttng_condition *condition_from_buffer = NULL;
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_payload buffer;
49
50 lttng_payload_init(&buffer);
51
52 tracepoint = lttng_event_rule_tracepoint_create(LTTNG_DOMAIN_UST);
53 ok(tracepoint, "tracepoint UST_DOMAIN");
54
55 status = lttng_event_rule_tracepoint_set_pattern(tracepoint, pattern);
56 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting pattern");
57
58 status = lttng_event_rule_tracepoint_set_filter(tracepoint, filter);
59 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting filter");
60
61 status = lttng_event_rule_tracepoint_set_log_level_range_lower_bound(
62 tracepoint, LTTNG_LOGLEVEL_WARNING);
63 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting log level range");
64
65 for (i = 0; i < 3; i++) {
66 status = lttng_event_rule_tracepoint_add_exclusion(
67 tracepoint, exclusions[i]);
68 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
69 "Setting exclusion pattern");
70 }
71
72 condition = lttng_condition_event_rule_create(tracepoint);
73 ok(condition, "Created condition");
74
75 condition_status = lttng_condition_event_rule_get_rule(
76 condition, &tracepoint_tmp);
77 ok(condition_status == LTTNG_CONDITION_STATUS_OK,
78 "Getting event rule from event rule condition");
79 ok(tracepoint == tracepoint_tmp, "lttng_condition_event_rule_get_rule provides a reference to the original rule");
80
81 ret = lttng_condition_serialize(condition, &buffer);
82 ok(ret == 0, "Condition serialized");
83
84 {
85 struct lttng_payload_view view =
86 lttng_payload_view_from_payload(&buffer, 0, -1);
87
88 (void) lttng_condition_create_from_payload(
89 &view, &condition_from_buffer);
90 }
91
92 ok(condition_from_buffer, "Condition created from payload is non-null");
93
94 ok(lttng_condition_is_equal(condition, condition_from_buffer),
95 "Serialized and de-serialized conditions are equal");
96
97 lttng_payload_reset(&buffer);
98 lttng_event_rule_destroy(tracepoint);
99 lttng_condition_destroy(condition);
100 lttng_condition_destroy(condition_from_buffer);
101 }
102
103 int main(int argc, const char *argv[])
104 {
105 plan_tests(NUM_TESTS);
106 test_condition_event_rule();
107 return exit_status();
108 }
This page took 0.045128 seconds and 4 git commands to generate.