on-event: add error counter fields to condition
[lttng-tools.git] / tests / unit / test_condition.c
CommitLineData
d17cfa5d
JR
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>
e393070a 23#include <lttng/condition/on-event.h>
35a9ac41 24#include <lttng/condition/on-event-internal.h>
d17cfa5d 25#include <lttng/domain.h>
85b05318 26#include <lttng/log-level-rule.h>
d17cfa5d
JR
27#include <common/dynamic-buffer.h>
28#include <common/buffer-view.h>
29
30/* For error.h */
31int lttng_opt_quiet = 1;
32int lttng_opt_verbose;
33int lttng_opt_mi;
34
35a9ac41 35#define NUM_TESTS 14
d17cfa5d
JR
36
37static
38void test_condition_event_rule(void)
39{
40 int ret, i;
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" };
35a9ac41 50 uint64_t _error_count = 420, error_count;
85b05318 51 struct lttng_log_level_rule *log_level_rule_at_least_as_severe = NULL;
d17cfa5d
JR
52 struct lttng_payload buffer;
53
54 lttng_payload_init(&buffer);
55
85b05318
JR
56 /* Create log level rule. */
57 log_level_rule_at_least_as_severe =
58 lttng_log_level_rule_at_least_as_severe_as_create(
59 LTTNG_LOGLEVEL_WARNING);
60 assert(log_level_rule_at_least_as_severe);
61
d17cfa5d
JR
62 tracepoint = lttng_event_rule_tracepoint_create(LTTNG_DOMAIN_UST);
63 ok(tracepoint, "tracepoint UST_DOMAIN");
64
65 status = lttng_event_rule_tracepoint_set_pattern(tracepoint, pattern);
66 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting pattern");
67
68 status = lttng_event_rule_tracepoint_set_filter(tracepoint, filter);
69 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting filter");
70
85b05318
JR
71 status = lttng_event_rule_tracepoint_set_log_level_rule(
72 tracepoint, log_level_rule_at_least_as_severe);
d17cfa5d
JR
73 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting log level range");
74
75 for (i = 0; i < 3; i++) {
76 status = lttng_event_rule_tracepoint_add_exclusion(
77 tracepoint, exclusions[i]);
78 ok(status == LTTNG_EVENT_RULE_STATUS_OK,
79 "Setting exclusion pattern");
80 }
81
d602bd6a 82 condition = lttng_condition_on_event_create(tracepoint);
d17cfa5d
JR
83 ok(condition, "Created condition");
84
35a9ac41
FD
85 /* Set the error count information. */
86 lttng_condition_on_event_set_error_count(condition, _error_count);
87
d602bd6a 88 condition_status = lttng_condition_on_event_get_rule(
d17cfa5d
JR
89 condition, &tracepoint_tmp);
90 ok(condition_status == LTTNG_CONDITION_STATUS_OK,
91 "Getting event rule from event rule condition");
92 ok(tracepoint == tracepoint_tmp, "lttng_condition_event_rule_get_rule provides a reference to the original rule");
93
94 ret = lttng_condition_serialize(condition, &buffer);
95 ok(ret == 0, "Condition serialized");
96
97 {
98 struct lttng_payload_view view =
99 lttng_payload_view_from_payload(&buffer, 0, -1);
100
101 (void) lttng_condition_create_from_payload(
102 &view, &condition_from_buffer);
103 }
104
105 ok(condition_from_buffer, "Condition created from payload is non-null");
106
107 ok(lttng_condition_is_equal(condition, condition_from_buffer),
108 "Serialized and de-serialized conditions are equal");
109
35a9ac41
FD
110 /*
111 * Error count info is not considered in is_equal; test it separately.
112 */
113 error_count = lttng_condition_on_event_get_error_count(condition_from_buffer);
114 ok(error_count == _error_count, "Error count is the same. Got %" PRIu64 " Expected %" PRIu64, error_count, _error_count);
115
d17cfa5d
JR
116 lttng_payload_reset(&buffer);
117 lttng_event_rule_destroy(tracepoint);
118 lttng_condition_destroy(condition);
119 lttng_condition_destroy(condition_from_buffer);
85b05318 120 lttng_log_level_rule_destroy(log_level_rule_at_least_as_severe);
d17cfa5d
JR
121}
122
123int main(int argc, const char *argv[])
124{
125 plan_tests(NUM_TESTS);
126 test_condition_event_rule();
127 return exit_status();
128}
This page took 0.039737 seconds and 4 git commands to generate.