1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
#!/bin/sh
# func-test - Unit Test Helper Functions
#
# Copyright (c) 2025 Thomas Koch <linrunner at gmx.net> and others.
# SPDX-License-Identifier: GPL-2.0-or-later
# ----------------------------------------------------------------------------
# Constants
readonly ANSI_RED="\033[31m"
readonly ANSI_GREEN="\033[32m"
readonly ANSI_BLACK="\033[m"
# ----------------------------------------------------------------------------
# Functions
printf_msg () {
# print message to stderr and logfile
# $1: format string
# $2..$n: message string(s)
local fmt="$1"
shift
# shellcheck disable=SC2154,SC2059
printf "$fmt" "$@" | tee "${_logfile:-/dev/null}" 1>&2
}
test_root () {
# test root privilege -- rc: 0=root, 1=not root
[ "$(id -u)" = "0" ]
}
cache_root_cred () {
# cache user credentials before actual testing
sudo true
}
check_tlp () {
# test if tlp installed
if [ ! -f /usr/sbin/tlp ]; then
printf_msg "Error: %s not installed." "$TLP" 1>&2
exit 254
fi
}
read_sysf () {
# read and print contents of a sysfile
# return 1 and print default if read fails
# $1: sysfile
# $2: default
# rc: 0=ok/1=error
if cat "$1" 2> /dev/null; then
return 0
else
printf "%s" "$2"
return 1
fi
}
write_sysf () { # write string to a sysfile
# $1: string
# $2: sysfile
# rc: 0=ok/1=error
{ printf '%s\n' "$1" > "$2"; } 2> /dev/null
}
compare_sysf () {
# Compare a string to the contents of a sysfile
# expression
# $1: string
# $2: file
local cmp_str="$1"
local sys_str
if [ -f "$2" ] && sys_str=$(read_sysf "$2"); then
if [ "$sys_str" != "$cmp_str" ]; then
printf_msg "\n*** Deviation at %s: %s (act) != %s (exp)\n" "$2" "$sys_str" "$cmp_str"
return 1
fi
else
printf_msg "\n*** Deviation for %s: sysfile does not exist.\n" "$2"
return 2
fi
return 0
}
glob_compare_sysf () {
# Compare a string to the contents of sysfiles selected by a glob
# expression
# $1: string
# $2..$n: file, ...
local cmp_str="$1"
local file_pat="$*"
file_pat="${file_pat#* }"
local sys_str
local cnt=0
while shift && [ $# -gt 0 ]; do
if [ -f "$1" ] && sys_str=$(read_sysf "$1"); then
cnt=$((cnt + 1))
if [ "$sys_str" != "$cmp_str" ]; then
printf_msg "\n*** Deviation at %s: %s (act) != %s (exp)\n" "$1" "$sys_str" "$cmp_str"
return 1
fi
fi
done
if [ "$cnt" -eq 0 ]; then
printf_msg "\n*** Deviation for %s: no matching sysfile(s) exist(s).\n" "$file_pat"
return 2
fi
return 0
}
print_nth_arg () {
# Get n-th argument
# $1: n
# $2..$m: arguments
local n="$1"
[ "$1" -gt 0 ] || return
until [ "$n" -eq 0 ] || [ $# -eq 0 ]; do
shift
n=$((n - 1))
done
printf "%s" "$1"
}
on_ac () {
# Detect AC power
# rc: 0=AC/1=BAT
# Note: compared to get_sys_power_supply() this is primitive. but it will do.
upower -i /org/freedesktop/UPower/devices/line_power_AC 2> /dev/null | grep -qE 'online:\s+yes'
}
bat_present () {
# Check for battery
# $1: battery name
# rc: 0=present/1=absent
[ "$(read_sysf "/sys/class/power_supply/$1/present")" = "1" ]
}
run_clitest () {
# Run clitest script and record result line to file
# $1: script filepath
# $2: suffix
# global param: $_report_file
if [ -f "$_report_file" ]; then
printf "%-50s --> " "${1##*/} $2" >> "$_report_file"
clitest --color always "$1" | tee /dev/fd/2 | grep -E '(OK|FAIL):' >> "$_report_file"
else
clitest --color always "$1" 1>&2
fi
printf "\n" 1>&2
}
is_uint () { # check for unsigned integer -- $1: string
printf "%s" "$1" | grep -E -q "^[0-9]+$" 2> /dev/null
}
start_report () {
# Create report file
# retval: $_report_file, $_nest_level
if [ -z "$_report_file" ]; then
# first call -> create report temp file
if ! _report_file="$(mktemp --tmpdir "tlp-test-report.XXX")"; then
printf "Error: failed to create report file.\n" 1>&2
fi
export _report_file
export _nest_level=0
else
# increment level
if is_uint "$_nest_level"; then
export _nest_level="$((_nest_level + 1))"
else
export _nest_level=1
fi
fi
}
report_test () {
# Write test name to report
if [ -f "$_report_file" ]; then
printf "%-50s --> " "$1" >> "$_report_file"
fi
}
report_line () {
# Write text line to report
# $1: text
if [ -f "$_report_file" ]; then
# note: use output string in format for proper ansi esc sequence interpolation
# shellcheck disable=SC2059
printf "$1\n" >> "$_report_file"
fi
}
report_result () {
# Write test result to terminal and report
# $1: # of tests
# $2: # of errors
if [ "$2" -eq 0 ]; then
printf_msg "${ANSI_GREEN}OK:${ANSI_BLACK} %s of %s tests passed\n\n" "$1" "$1"
report_line "${ANSI_GREEN}OK:${ANSI_BLACK} $1 of $1 tests passed"
else
printf_msg "${ANSI_RED}FAIL:${ANSI_BLACK} %s of %s tests failed\n\n" "$2" "$1"
report_line "${ANSI_RED}FAIL:${ANSI_BLACK} $2 of $1 tests failed"
fi
}
print_report () {
[ "$_nest_level" -gt 0 ] && return
if [ -f "$_report_file" ]; then
printf "+++ Test Summary ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
cat "$_report_file"
printf "\n"
rm -f "$_report_file"
else
printf "Error: missing report file ''%s''.\n" "$_report_file" 1>&2
fi
}
|