Skip to content

Commit 244d0cb

Browse files
rtla/tests: Add unit tests for _parse_args() functions
Add a test suite for the _parse_args() function of each tool that checks the params structures (struct common_params, struct osnoise_params, struct timerlat_params) returned by them for correctness. One test case is added per option, as well as a few special cases for tricky combinations of options. Test cases are ordered the same as the option arrays and help message to allow easy checking of whether all options are covered. This should help clarify what the proper command line behavior of RTLA is in case there are holes in the documentation and verify that the intended behavior is implemented correctly. A few necessary changes to the unit tests were done as part of this commit: - Unit tests now also link to libsubcmd and its dependencies. - A new global variable in_unit_test is added to RTLA's CLI interface, causing it to skip check for root if running in unit tests. This allows the CLI unit tests to run as non-root, like existing unit tests. There is quite a lot of duplication, some of it is mitigated with macros, but partially it is intentional so that future changes in behavior are tracked across tools. Link: https://lore.kernel.org/r/20260528103254.2990068-6-tglozar@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 5d9af63 commit 244d0cb

10 files changed

Lines changed: 2486 additions & 5 deletions

File tree

tools/tracing/rtla/src/cli.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
124124
if (cb_data.trace_output)
125125
actions_add_trace_output(&params->common.threshold_actions, cb_data.trace_output);
126126

127-
if (geteuid())
127+
if (geteuid() && !in_unit_test)
128128
fatal("osnoise needs root permission");
129129

130130
return &params->common;
@@ -206,7 +206,7 @@ struct common_params *osnoise_hist_parse_args(int argc, char **argv)
206206
if (cb_data.trace_output)
207207
actions_add_trace_output(&params->common.threshold_actions, cb_data.trace_output);
208208

209-
if (geteuid())
209+
if (geteuid() && !in_unit_test)
210210
fatal("rtla needs root permission");
211211

212212
if (params->common.hist.no_index && !params->common.hist.with_zeros)
@@ -301,7 +301,7 @@ struct common_params *timerlat_top_parse_args(int argc, char **argv)
301301
if (cb_data.trace_output)
302302
actions_add_trace_output(&params->common.threshold_actions, cb_data.trace_output);
303303

304-
if (geteuid())
304+
if (geteuid() && !in_unit_test)
305305
fatal("rtla needs root permission");
306306

307307
/*
@@ -427,7 +427,7 @@ struct common_params *timerlat_hist_parse_args(int argc, char **argv)
427427
if (cb_data.trace_output)
428428
actions_add_trace_output(&params->common.threshold_actions, cb_data.trace_output);
429429

430-
if (geteuid())
430+
if (geteuid() && !in_unit_test)
431431
fatal("rtla needs root permission");
432432

433433
if (params->common.hist.no_irq && params->common.hist.no_thread)

tools/tracing/rtla/src/cli.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv);
55
struct common_params *osnoise_hist_parse_args(int argc, char **argv);
66
struct common_params *timerlat_top_parse_args(int argc, char **argv);
77
struct common_params *timerlat_hist_parse_args(int argc, char **argv);
8+
9+
extern bool in_unit_test;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
unit_tests-y += utils.o
22
unit_tests-y += actions.o
33
unit_tests-y += unit_tests.o
4+
unit_tests-y += osnoise_top_cli.o
5+
unit_tests-y += osnoise_hist_cli.o
6+
unit_tests-y += timerlat_top_cli.o
7+
unit_tests-y += timerlat_hist_cli.o

tools/tracing/rtla/tests/unit/Makefile.unit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
UNIT_TESTS := $(OUTPUT)unit_tests
44
UNIT_TESTS_IN := $(UNIT_TESTS)-in.o
55

6-
$(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN)
6+
$(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN) $(LIBSUBCMD) $(LIB_STRING) $(LIB_STR_ERROR_R)
77
$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $^ $(EXTLIBS) -lcheck
88

99
$(UNIT_TESTS_IN): fixdep
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#pragma once
3+
4+
#include "../../src/timerlat.h"
5+
6+
/* Tracing Options */
7+
8+
#define CLI_ASSERT_SINGLE_EVENT(_system, _event) do {\
9+
ck_assert_ptr_nonnull(params->events);\
10+
ck_assert_str_eq(params->events->system, _system);\
11+
ck_assert_str_eq(params->events->event, _event);\
12+
ck_assert_ptr_null(params->events->next);\
13+
} while (0)
14+
15+
#define CLI_ASSERT_SINGLE_FILTER(_filter) do {\
16+
ck_assert_ptr_nonnull(params->events);\
17+
ck_assert_str_eq(params->events->filter, _filter);\
18+
ck_assert_ptr_null(params->events->next);\
19+
} while (0)
20+
21+
#define CLI_ASSERT_SINGLE_TRIGGER(_trigger) do {\
22+
ck_assert_ptr_nonnull(params->events);\
23+
ck_assert_str_eq(params->events->trigger, _trigger);\
24+
ck_assert_ptr_null(params->events->next);\
25+
} while (0)
26+
27+
/* CPU Configuration */
28+
29+
#define CLI_ASSERT_CPUSET(_field, ...) do {\
30+
int n;\
31+
int cpus[] = { __VA_ARGS__ };\
32+
for (n = 0; n < sizeof(cpus) / sizeof(int); n++)\
33+
ck_assert(CPU_ISSET(cpus[n], &params->_field));\
34+
ck_assert_int_eq(CPU_COUNT(&params->_field), n);\
35+
} while (0)
36+
37+
/* Auto Analysis and Actions */
38+
39+
#define CLI_OSNOISE_ASSERT_AUTO(_stop) do {\
40+
ck_assert_int_eq(params->stop_us, _stop);\
41+
ck_assert_int_eq(osn_params->threshold, 1);\
42+
ck_assert_int_eq(params->threshold_actions.len, 1);\
43+
ck_assert_int_eq(params->threshold_actions.list[0].type, ACTION_TRACE_OUTPUT);\
44+
ck_assert_str_eq(params->threshold_actions.list[0].trace_output, "osnoise_trace.txt");\
45+
} while (0)
46+
47+
#define CLI_TIMERLAT_ASSERT_AUTO(_threshold) do {\
48+
ck_assert_int_eq(params->stop_us, _threshold);\
49+
ck_assert_int_eq(params->stop_total_us, _threshold);\
50+
ck_assert_int_eq(tlat_params->print_stack, _threshold);\
51+
ck_assert_int_eq(params->threshold_actions.len, 1);\
52+
ck_assert_int_eq(params->threshold_actions.list[0].type, ACTION_TRACE_OUTPUT);\
53+
ck_assert_str_eq(params->threshold_actions.list[0].trace_output, "timerlat_trace.txt");\
54+
} while (0)
55+
56+
#define CLI_TIMERLAT_ASSERT_AA_ONLY(_threshold) do {\
57+
ck_assert_int_eq(params->stop_us, _threshold);\
58+
ck_assert_int_eq(params->stop_total_us, _threshold);\
59+
ck_assert_int_eq(tlat_params->print_stack, _threshold);\
60+
ck_assert_int_eq(params->threshold_actions.len, 0);\
61+
ck_assert(params->aa_only);\
62+
} while (0)
63+
64+
#define CLI_ASSERT_SINGLE_ACTION(_actions, _type, _arg, _valtype, _value) do {\
65+
ck_assert_int_eq(params->_actions.len, 1);\
66+
ck_assert_int_eq(params->_actions.list[0].type, _type);\
67+
ck_assert_##_valtype##_eq(params->_actions.list[0]._arg, _value);\
68+
} while (0)

0 commit comments

Comments
 (0)