SoW-2020-0003: Trace Hit Counters
[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>
ebdb334b
JR
23#include <lttng/condition/on-event.h>
24#include <lttng/condition/on-event-internal.h>
d17cfa5d 25#include <lttng/domain.h>
ebdb334b 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
ebdb334b 35#define NUM_TESTS 15
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" };
ebdb334b
JR
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;
d17cfa5d
JR
52 struct lttng_payload buffer;
53
54 lttng_payload_init(&buffer);
55
ebdb334b
JR
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);
59
d17cfa5d
JR
60 tracepoint = lttng_event_rule_tracepoint_create(LTTNG_DOMAIN_UST);
61 ok(tracepoint, "tracepoint UST_DOMAIN");
62
63 status = lttng_event_rule_tracepoint_set_pattern(tracepoint, pattern);
64 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting pattern");
65
66 status = lttng_event_rule_tracepoint_set_filter(tracepoint, filter);
67 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting filter");
68
ebdb334b 69 status = lttng_event_rule_tracepoint_set_log_level_rule(tracepoint, log_level_rule_at_least_as_severe);
d17cfa5d
JR
70 ok(status == LTTNG_EVENT_RULE_STATUS_OK, "Setting log level range");
71
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");
77 }
78
ebdb334b 79 condition = lttng_condition_on_event_create(tracepoint);
d17cfa5d
JR
80 ok(condition, "Created condition");
81
ebdb334b
JR
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);
85
86 condition_status = lttng_condition_on_event_get_rule(
d17cfa5d
JR
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");
91
92 ret = lttng_condition_serialize(condition, &buffer);
93 ok(ret == 0, "Condition serialized");
94
95 {
96 struct lttng_payload_view view =
97 lttng_payload_view_from_payload(&buffer, 0, -1);
98
99 (void) lttng_condition_create_from_payload(
100 &view, &condition_from_buffer);
101 }
102
103 ok(condition_from_buffer, "Condition created from payload is non-null");
104
105 ok(lttng_condition_is_equal(condition, condition_from_buffer),
106 "Serialized and de-serialized conditions are equal");
107
ebdb334b
JR
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);
113
d17cfa5d
JR
114 lttng_payload_reset(&buffer);
115 lttng_event_rule_destroy(tracepoint);
116 lttng_condition_destroy(condition);
117 lttng_condition_destroy(condition_from_buffer);
ebdb334b 118 lttng_log_level_rule_destroy(log_level_rule_at_least_as_severe);
d17cfa5d
JR
119}
120
121int main(int argc, const char *argv[])
122{
123 plan_tests(NUM_TESTS);
124 test_condition_event_rule();
125 return exit_status();
126}
This page took 0.029331 seconds and 5 git commands to generate.