forked from thesofproject/sof-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.sh
More file actions
460 lines (411 loc) · 13.3 KB
/
lib.sh
File metadata and controls
460 lines (411 loc) · 13.3 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/bash
# get test-case information
SCRIPT_HOME="$(dirname "${BASH_SOURCE[0]}")"
# get test-case parent folder name
SCRIPT_HOME=$(cd "$SCRIPT_HOME/.." && pwd)
# shellcheck disable=SC2034 # external script can use it
SCRIPT_NAME="$0" # get test-case script load name
# shellcheck disable=SC2034 # external script can use it
SCRIPT_PRAM="$*" # get test-case parameter
# Source from the relative path of current folder
# shellcheck source=case-lib/config.sh
source "$SCRIPT_HOME/case-lib/config.sh"
# shellcheck source=case-lib/opt.sh
source "$SCRIPT_HOME/case-lib/opt.sh"
# shellcheck source=case-lib/logging_ctl.sh
source "$SCRIPT_HOME/case-lib/logging_ctl.sh"
# shellcheck source=case-lib/pipeline.sh
source "$SCRIPT_HOME/case-lib/pipeline.sh"
# shellcheck source=case-lib/hijack.sh
source "$SCRIPT_HOME/case-lib/hijack.sh"
# restrict bash version for some bash feature
[[ $(echo -e "$BASH_VERSION\n4.1"|sort -V|head -n 1) == '4.1' ]] || {
dlogw "Bash version: ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} should > 4.1"
exit 2
}
# Add tools to command PATH
# this line equal `! $(echo $PATH|grep "$SCRIPT_HOME/tools")`
if [[ ! $PATH =~ $SCRIPT_HOME/tools ]]; then
export PATH=$SCRIPT_HOME/tools:$PATH
fi
# setup SOFCARD id
if [ ! "$SOFCARD" ]; then
# $1=$1 strips whitespaces
SOFCARD=$(grep -v 'sof-probe' /proc/asound/cards |
awk '/sof-[a-z]/ && $1 ~ /^[0-9]+$/ { $1=$1; print $1; exit 0;}')
fi
setup_kernel_check_point()
{
# Make the check point $SOF_TEST_INTERVAL second(s) earlier to avoid log loss.
# Note this may lead to an error caused by one test appear in the next one.
KERNEL_CHECKPOINT=$(($(date +%s) - SOF_TEST_INTERVAL))
}
# This function adds a fake error to dmesg (which is always saved by
# journald). It also adds it to kern.log, see why in comment below.
#
# It is surprisingly easy to write a test that always succeeds,
# especially in shell script and it has already happened a number of
# times. Temporarily add this function to a test under development to
# make sure it can actually report failures. Using this function is even
# more critical for testing changes to the test framework and especially
# error handling code.
#
# Sample usage: fake_kern_error "DRIVER ID: FAKE error $0 PID $$ $round"
# fake_kern_error "asix 3-12.1:1.0 enx000ec668ad2a: Failed to write reg index 0x0000: -11"
fake_kern_error()
{
local k_msg d boot_secs kern_log_prefix
k_msg="$1"
d=$(date '+%b %d %R:%S') # TODO: is this locale dependent?
boot_secs=$(awk '{ print $1 }' < /proc/uptime)
kern_log_prefix="$d $(hostname) kernel: [$boot_secs]"
printf '<3>%s >/dev/kmsg\n' "$k_msg" | sudo tee -a /dev/kmsg >/dev/null
# From https://www.kernel.org/doc/Documentation/ABI/testing/dev-kmsg
# It is not possible to inject to /dev/kmesg with the facility
# number LOG_KERN (0), to make sure that the origin of the messages
# can always be reliably determined.
printf '%s %s >> kern.log\n' "$kern_log_prefix" \
"$k_msg" | sudo tee -a /var/log/kern.log >/dev/null
}
find_ldc_file()
{
local ldcFile
# if user doesn't specify file path of sof-*.ldc, fall back to
# /etc/sof/sof-PLATFORM.ldc, which is the default path used by CI.
# and then on the standard location.
if [ -n "$SOFLDC" ]; then
ldcFile="$SOFLDC"
else
local platf; platf=$(sof-dump-status.py -p) || {
>&2 dloge "Failed to query platform with sof-dump-status.py"
return 1
}
ldcFile=/etc/sof/sof-"$platf".ldc
[ -e "$ldcFile" ] ||
ldcFile=/lib/firmware/intel/sof/sof-"$platf".ldc
fi
[[ -e "$ldcFile" ]] || {
>&2 dloge "LDC file $ldcFile not found, check the SOFLDC environment variable or copy your sof-*.ldc to /etc/sof"
return 1
}
printf '%s' "$ldcFile"
}
SOF_LOG_COLLECT=0
func_lib_start_log_collect()
{
local is_etrace=${1:-0} ldcFile
ldcFile=$(find_ldc_file) || return $?
local logopt="-t"
if [ -z "$SOFLOGGER" ]; then
SOFLOGGER=$(command -v sof-logger) || {
dlogw 'No sof-logger found in PATH'
return 1
}
fi
test -x "$SOFLOGGER" || {
dlogw "$SOFLOGGER not found or not executable"
return 2
}
if [ "X$is_etrace" == "X0" ];then
logfile=$LOG_ROOT/slogger.txt
else
logfile=$LOG_ROOT/etrace.txt
logopt=""
fi
if func_hijack_setup_sudo_level ;then
# shellcheck disable=SC2034 # external script will use it
SOF_LOG_COLLECT=1
else
>&2 dlogw "without sudo permission to run $SOFLOGGER command"
return 3
fi
local loggerCmd=("$SOFLOGGER" "$logopt" -l "$ldcFile" -o "$logfile")
dlogi "Starting ${loggerCmd[*]}"
# Cleaned up by func_exit_handler() in hijack.sh
sudo "${loggerCmd[@]}" &
}
# Calling this function is often a mistake because the error message
# from the actual sudo() function in hijack.sh is better: it is
# guaranteed to print the command that needs sudo and give more
# information. func_lib_check_sudo() is useful only when sudo is
# optional and when its warning can be ignored (with '|| true') but in
# the past it has been used to abort the test immediately and lose the
# better error message from sudo().
func_lib_check_sudo()
{
local cmd="${1:-Unknown command}"
func_hijack_setup_sudo_level || {
dlogw "$cmd needs root privilege to run, please use NOPASSWD or cached credentials"
return 2
}
}
systemctl_show_pulseaudio()
{
printf '\n'
local domain
for domain in --system --global --user; do
( set -x
systemctl "$domain" list-unit-files --all '*pulse*'
) || true
printf '\n'
done
printf 'For %s ONLY:\n' "$(id -un)"
( set -x
systemctl --user list-units --all '*pulse*'
) || true
printf '\n'
# pgrep ouput is nicer because it hides itself, however pgrep does
# not show a critical information: the userID which can be not us
# but 'gdm'!
# shellcheck disable=SC2009
ps axu | grep -i pulse
>&2 printf 'ERROR: %s fails semi-randomly when pulseaudio is running\n' \
"$(basename "$0")"
>&2 printf '\nTry: sudo systemctl --global mask pulseaudio.{socket,service} and reboot.
--global is required for session managers like "gdm"
'
# We cannot suggest "systemctl --user mask" for a 'gdm' user
# because we can't 'su' to it and access its DBUS, so we suggest
# --global to keep it simple. If --global is too strong for you, you
# can manually create a
# /var/lib/gdm3/.config/systemd/user/pulseaudio.service -> /dev/null
# symlink
}
declare -a PULSECMD_LST
declare -a PULSE_PATHS
func_lib_disable_pulseaudio()
{
[[ "${#PULSECMD_LST[@]}" -ne 0 ]] && return
# store current pulseaudio command
readarray -t PULSECMD_LST < <(ps -C pulseaudio -o user,cmd --no-header)
[[ "${#PULSECMD_LST[@]}" -eq 0 ]] && return
func_lib_check_sudo 'disabling pulseaudio'
# get all running pulseaudio paths
readarray -t PULSE_PATHS < <(ps -C pulseaudio -o cmd --no-header | awk '{print $1}'|sort -u)
for PA_PATH in "${PULSE_PATHS[@]}"
do
# rename pulseaudio before kill it
if [ -x "$PA_PATH" ]; then
sudo mv -f "$PA_PATH" "$PA_PATH.bak"
fi
done
sudo pkill -9 pulseaudio
sleep 1s # wait pulseaudio to be disabled
if [ ! "$(ps -C pulseaudio --no-header)" ]; then
dlogi "Pulseaudio disabled"
else
# if failed to disable pulseaudio before running test case, fail the test case directly.
die "Failed to disable pulseaudio"
fi
}
func_lib_restore_pulseaudio()
{
[[ "${#PULSECMD_LST[@]}" -eq 0 ]] && return
func_lib_check_sudo 're-enabling pulseaudio'
# restore pulseaudio
for PA_PATH in "${PULSE_PATHS[@]}"
do
if [ -x "$PA_PATH.bak" ]; then
sudo mv -f "$PA_PATH.bak" "$PA_PATH"
fi
done
# start pulseaudio
local line
for line in "${PULSECMD_LST[@]}"
do
# Both the user and the command are the same $line var :-(
# shellcheck disable=SC2086
nohup sudo -u $line >/dev/null &
done
# now wait for the pulseaudio restore in the ps process
timeout=10
dlogi "Restoring pulseaudio"
for wait_time in $(seq 1 $timeout)
do
sleep 1s
[ -n "$(ps -C pulseaudio --no-header)" ] && break
if [ "$wait_time" -eq $timeout ]; then
dlogi "Time out. Pulseaudio not restored in $timeout seconds"
return 1
fi
done
dlogi "Restoring pulseaudio takes $wait_time seconds"
unset PULSECMD_LST
unset PULSE_PATHS
declare -ag PULSECMD_LST
declare -ag PULSE_PATHS
return 0
}
func_lib_get_random()
{
# RANDOM: Each time this parameter is referenced, a random integer between 0 and 32767 is generated
local random_max=$1 random_min=$2 random_scope
random_scope=$(( random_max - random_min ))
if [ $# -ge 2 ];then
echo $(( RANDOM % random_scope + random_min ))
elif [ $# -eq 1 ];then
echo $(( RANDOM % random_max ))
else
echo $RANDOM
fi
}
func_lib_lsof_error_dump()
{
local file="$1" ret
# lsof exits the same '1' whether the file is missing or not open :-(
[[ ! -c "$file" ]] && return
ret=$(lsof "$file") || true
if [ "$ret" ];then
dloge "Sound device $file is in use:"
echo "$ret"
fi
}
func_lib_get_tplg_path()
{
local tplg=$1
if [[ -z "$tplg" ]]; then # tplg given is empty
return 1
elif [[ -f "$tplg" ]]; then
realpath "$tplg"
elif [[ -f "$TPLG_ROOT/$tplg" ]]; then
echo "$TPLG_ROOT/$tplg"
else
return 1
fi
return 0
}
func_lib_check_pa()
{
pactl stat || {
dloge "pactl stat failed"
return 1
}
}
# We must not quote SOF_ALSA_OPTS and disable SC2086 below for two reasons:
#
# 1. We want to support multiple parameters in a single variable, in
# other words we want to support this:
#
# SOF_ALSA_OPTS="--foo --bar"
# aplay $SOF_ALSA_OPTS ...
#
# 2. aplay does not ignore empty arguments anyway, in other words this
# does not work anyway:
#
# SOF_ALSA_OPTS=""
# aplay "$SOF_ALSA_OPTS" ...
#
# This is technically incorrect because it means our SOF_ALSA_OPTS
# cannot support whitespace, for instance this would be split in two
# options: --bar="aaaa and bbbb"
#
# SOF_ALSA_OPTS='--foo --bar="aaaa bbbb"'
#
# To do this "correctly" SOF_ALSA_OPTS etc. should be arrays.
# From https://mywiki.wooledge.org/BashGuide/Arrays "The only safe way
# to represent multiple string elements in Bash is through the use of
# arrays."
#
# However, 1. arrays would complicate the user interface 2. ALSA does not
# seem to need arguments with whitespace or globbing characters.
aplay_opts()
{
dlogc "aplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS $*"
# shellcheck disable=SC2086
aplay $SOF_ALSA_OPTS $SOF_APLAY_OPTS "$@"
}
arecord_opts()
{
dlogc "arecord $SOF_ALSA_OPTS $SOF_ARECORD_OPTS $*"
# shellcheck disable=SC2086
arecord $SOF_ALSA_OPTS $SOF_ARECORD_OPTS "$@"
}
die()
{
dloge "$@"
exit 1
}
skip_test()
{
dlogw 'SKIP test because:'
dlogw "$@"
# See func_exit_handler()
exit 2
}
is_sof_used()
{
grep -q "sof" /proc/asound/cards;
}
# a wrapper to journalctl with required style
journalctl_cmd()
{
sudo journalctl -k -q --no-pager --utc --output=short-monotonic \
--no-hostname "$@"
}
disable_kernel_check_point()
{
KERNEL_CHECKPOINT="disabled"
}
is_zephyr()
{
local ldcFile
ldcFile=$(find_ldc_file) || {
dloge '.ldc file not found, assuming XTOS firmware'
return 1
}
local znum
znum=$(strings "$ldcFile" | grep -c -i zephyr)
# As of Nov. 2021, znum ~= 30 for Zephyr and 0 for XTOS
test "$znum" -gt 10
}
logger_disabled()
{
[[ ${OPT_VAL['s']} -eq 0 ]]
}
print_module_params()
{
echo "--------- Printing module parameters ----------"
grep -H ^ /sys/module/snd_intel_dspcfg/parameters/*
# for all the *sof* modules
grep -H ^ /sys/module/*sof*/parameters/*
echo "----------------------------------------"
}
# check if NTP Synchronized, if so return 0 otherwise return 1
check_ntp_sync()
{
# Check this device time is NTP Synchronized.
timedatectl show | grep -q "NTPSynchronized=yes"
}
re_enable_ntp_sync()
{
# disable synchronization first
sudo timedatectl set-ntp false
# enable ntp sync. This will trigger initial synchronization to time server
sudo timedatectl set-ntp true
}
# check-alsabat.sh need to run optimum alsa control settings
# param1: platform name
set_alsa_settings()
{
# ZEPHYR platform shares same tplg, remove '_ZEPHYR' from platform name
local PNAME="${1%_ZEPHYR}"
dlogi "Run alsa setting for $PNAME"
case $PNAME in
APL_UP2_NOCODEC | CML_RVP_NOCODEC | JSL_RVP_NOCODEC | TGLU_RVP_NOCODEC | ADLP_RVP_NOCODEC | TGLH_RVP_NOCODEC)
# common nocodec alsa settings
"$SCRIPT_HOME"/alsa_settings/CAVS_NOCODEC.sh
;;
TGLU_RVP_NOCODEC_CI | ADLP_RVP_NOCODEC_CI)
# common nocodec_ci alsa settings
"$SCRIPT_HOME"/alsa_settings/CAVS_NOCODEC_CI.sh
;;
*)
# if script name is same as platform name, default case will handle all
if [ -f "$SCRIPT_HOME"/alsa_settings/"$PNAME".sh ]; then
"$SCRIPT_HOME"/alsa_settings/"$PNAME".sh
else
dlogw "alsa setting for $PNAME is not available"
fi
;;
esac
}