xref: /linux/tools/tracing/rtla/tests/engine.sh (revision 40648d246fa4307ef11d185933cb0d79fc9ff46c)
1*ab16714fSTomas Glozar#!/bin/bash
2*ab16714fSTomas Glozar# SPDX-License-Identifier: GPL-2.0
3*ab16714fSTomas Glozartest_begin() {
4*ab16714fSTomas Glozar	# Count tests to allow the test harness to double-check if all were
5*ab16714fSTomas Glozar	# included correctly.
6*ab16714fSTomas Glozar	ctr=0
7*ab16714fSTomas Glozar	[ -z "$RTLA" ] && RTLA="./rtla"
8*ab16714fSTomas Glozar	[ -n "$TEST_COUNT" ] && echo "1..$TEST_COUNT"
9*ab16714fSTomas Glozar}
10*ab16714fSTomas Glozar
11*ab16714fSTomas Glozarcheck() {
12*ab16714fSTomas Glozar	# Simple check: run rtla with given arguments and test exit code.
13*ab16714fSTomas Glozar	# If TEST_COUNT is set, run the test. Otherwise, just count.
14*ab16714fSTomas Glozar	ctr=$(($ctr + 1))
15*ab16714fSTomas Glozar	if [ -n "$TEST_COUNT" ]
16*ab16714fSTomas Glozar	then
17*ab16714fSTomas Glozar		# Run rtla; in case of failure, include its output as comment
18*ab16714fSTomas Glozar		# in the test results.
19*ab16714fSTomas Glozar		result=$(stdbuf -oL $TIMEOUT "$RTLA" $2 2>&1); exitcode=$?
20*ab16714fSTomas Glozar		if [ $exitcode -eq 0 ]
21*ab16714fSTomas Glozar		then
22*ab16714fSTomas Glozar			echo "ok $ctr - $1"
23*ab16714fSTomas Glozar		else
24*ab16714fSTomas Glozar			echo "not ok $ctr - $1"
25*ab16714fSTomas Glozar			# Add rtla output and exit code as comments in case of failure
26*ab16714fSTomas Glozar			echo "$result" | col -b | while read line; do echo "# $line"; done
27*ab16714fSTomas Glozar			printf "#\n# exit code %s\n" $exitcode
28*ab16714fSTomas Glozar		fi
29*ab16714fSTomas Glozar	fi
30*ab16714fSTomas Glozar}
31*ab16714fSTomas Glozar
32*ab16714fSTomas Glozarset_timeout() {
33*ab16714fSTomas Glozar	TIMEOUT="timeout -v -k 15s $1"
34*ab16714fSTomas Glozar}
35*ab16714fSTomas Glozar
36*ab16714fSTomas Glozarunset_timeout() {
37*ab16714fSTomas Glozar	unset TIMEOUT
38*ab16714fSTomas Glozar}
39*ab16714fSTomas Glozar
40*ab16714fSTomas Glozartest_end() {
41*ab16714fSTomas Glozar	# If running without TEST_COUNT, tests are not actually run, just
42*ab16714fSTomas Glozar	# counted. In that case, re-run the test with the correct count.
43*ab16714fSTomas Glozar	[ -z "$TEST_COUNT" ] && TEST_COUNT=$ctr exec bash $0 || true
44*ab16714fSTomas Glozar}
45*ab16714fSTomas Glozar
46*ab16714fSTomas Glozar# Avoid any environmental discrepancies
47*ab16714fSTomas Glozarexport LC_ALL=C
48*ab16714fSTomas Glozarunset_timeout
49