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>
This commit is contained in:
Tomas Glozar
2026-05-28 13:02:48 +02:00
parent 5d9af63e80
commit 244d0cbff2
10 changed files with 2486 additions and 5 deletions
+4 -4
View File
@@ -124,7 +124,7 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
if (cb_data.trace_output)
actions_add_trace_output(&params->common.threshold_actions, cb_data.trace_output);
if (geteuid())
if (geteuid() && !in_unit_test)
fatal("osnoise needs root permission");
return &params->common;
@@ -206,7 +206,7 @@ struct common_params *osnoise_hist_parse_args(int argc, char **argv)
if (cb_data.trace_output)
actions_add_trace_output(&params->common.threshold_actions, cb_data.trace_output);
if (geteuid())
if (geteuid() && !in_unit_test)
fatal("rtla needs root permission");
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)
if (cb_data.trace_output)
actions_add_trace_output(&params->common.threshold_actions, cb_data.trace_output);
if (geteuid())
if (geteuid() && !in_unit_test)
fatal("rtla needs root permission");
/*
@@ -427,7 +427,7 @@ struct common_params *timerlat_hist_parse_args(int argc, char **argv)
if (cb_data.trace_output)
actions_add_trace_output(&params->common.threshold_actions, cb_data.trace_output);
if (geteuid())
if (geteuid() && !in_unit_test)
fatal("rtla needs root permission");
if (params->common.hist.no_irq && params->common.hist.no_thread)
+2
View File
@@ -5,3 +5,5 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv);
struct common_params *osnoise_hist_parse_args(int argc, char **argv);
struct common_params *timerlat_top_parse_args(int argc, char **argv);
struct common_params *timerlat_hist_parse_args(int argc, char **argv);
extern bool in_unit_test;
+4
View File
@@ -1,3 +1,7 @@
unit_tests-y += utils.o
unit_tests-y += actions.o
unit_tests-y += unit_tests.o
unit_tests-y += osnoise_top_cli.o
unit_tests-y += osnoise_hist_cli.o
unit_tests-y += timerlat_top_cli.o
unit_tests-y += timerlat_hist_cli.o
+1 -1
View File
@@ -3,7 +3,7 @@
UNIT_TESTS := $(OUTPUT)unit_tests
UNIT_TESTS_IN := $(UNIT_TESTS)-in.o
$(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN)
$(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN) $(LIBSUBCMD) $(LIB_STRING) $(LIB_STR_ERROR_R)
$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $^ $(EXTLIBS) -lcheck
$(UNIT_TESTS_IN): fixdep
@@ -0,0 +1,68 @@
/* SPDX-License-Identifier: GPL-2.0 */
#pragma once
#include "../../src/timerlat.h"
/* Tracing Options */
#define CLI_ASSERT_SINGLE_EVENT(_system, _event) do {\
ck_assert_ptr_nonnull(params->events);\
ck_assert_str_eq(params->events->system, _system);\
ck_assert_str_eq(params->events->event, _event);\
ck_assert_ptr_null(params->events->next);\
} while (0)
#define CLI_ASSERT_SINGLE_FILTER(_filter) do {\
ck_assert_ptr_nonnull(params->events);\
ck_assert_str_eq(params->events->filter, _filter);\
ck_assert_ptr_null(params->events->next);\
} while (0)
#define CLI_ASSERT_SINGLE_TRIGGER(_trigger) do {\
ck_assert_ptr_nonnull(params->events);\
ck_assert_str_eq(params->events->trigger, _trigger);\
ck_assert_ptr_null(params->events->next);\
} while (0)
/* CPU Configuration */
#define CLI_ASSERT_CPUSET(_field, ...) do {\
int n;\
int cpus[] = { __VA_ARGS__ };\
for (n = 0; n < sizeof(cpus) / sizeof(int); n++)\
ck_assert(CPU_ISSET(cpus[n], &params->_field));\
ck_assert_int_eq(CPU_COUNT(&params->_field), n);\
} while (0)
/* Auto Analysis and Actions */
#define CLI_OSNOISE_ASSERT_AUTO(_stop) do {\
ck_assert_int_eq(params->stop_us, _stop);\
ck_assert_int_eq(osn_params->threshold, 1);\
ck_assert_int_eq(params->threshold_actions.len, 1);\
ck_assert_int_eq(params->threshold_actions.list[0].type, ACTION_TRACE_OUTPUT);\
ck_assert_str_eq(params->threshold_actions.list[0].trace_output, "osnoise_trace.txt");\
} while (0)
#define CLI_TIMERLAT_ASSERT_AUTO(_threshold) do {\
ck_assert_int_eq(params->stop_us, _threshold);\
ck_assert_int_eq(params->stop_total_us, _threshold);\
ck_assert_int_eq(tlat_params->print_stack, _threshold);\
ck_assert_int_eq(params->threshold_actions.len, 1);\
ck_assert_int_eq(params->threshold_actions.list[0].type, ACTION_TRACE_OUTPUT);\
ck_assert_str_eq(params->threshold_actions.list[0].trace_output, "timerlat_trace.txt");\
} while (0)
#define CLI_TIMERLAT_ASSERT_AA_ONLY(_threshold) do {\
ck_assert_int_eq(params->stop_us, _threshold);\
ck_assert_int_eq(params->stop_total_us, _threshold);\
ck_assert_int_eq(tlat_params->print_stack, _threshold);\
ck_assert_int_eq(params->threshold_actions.len, 0);\
ck_assert(params->aa_only);\
} while (0)
#define CLI_ASSERT_SINGLE_ACTION(_actions, _type, _arg, _valtype, _value) do {\
ck_assert_int_eq(params->_actions.len, 1);\
ck_assert_int_eq(params->_actions.list[0].type, _type);\
ck_assert_##_valtype##_eq(params->_actions.list[0]._arg, _value);\
} while (0)
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -5,17 +5,28 @@
#include <stdbool.h>
#include "../../src/utils.h"
#include "../../src/cli.h"
Suite *utils_suite(void);
Suite *actions_suite(void);
Suite *osnoise_top_cli_suite(void);
Suite *osnoise_hist_cli_suite(void);
Suite *timerlat_top_cli_suite(void);
Suite *timerlat_hist_cli_suite(void);
int main(int argc, char *argv[])
{
int num_failed;
SRunner *sr;
in_unit_test = true;
sr = srunner_create(utils_suite());
srunner_add_suite(sr, actions_suite());
srunner_add_suite(sr, osnoise_top_cli_suite());
srunner_add_suite(sr, osnoise_hist_cli_suite());
srunner_add_suite(sr, timerlat_top_cli_suite());
srunner_add_suite(sr, timerlat_hist_cli_suite());
srunner_run_all(sr, CK_VERBOSE);
num_failed = srunner_ntests_failed(sr);