Skip to content

Commit 3d408ec

Browse files
Martin Belangerclaude
andcommitted
test: correct several bugs and robustness issues in coverage.sh.in
- SIGINT trap: replace `trap postrun_cleanup SIGINT` with `trap 'exit 130' SIGINT` so that Ctrl+C actually stops the script (the old handler ran cleanup but then resumed execution); the EXIT trap already handles cleanup on exit. - Silent unit test failures: run_unit_test now captures the exit code and calls log_file_contents when non-zero, making test failures visible in the terminal instead of silently discarded. - Missing D-Bus name: the final sd_start "stafd" call was missing the second argument, causing systemd-run to be invoked with an empty BusName= under Type=dbus; fixed to pass @STAFD_DBUS_NAME@. - Fragile argument passing in run_unit_test: replaced the args=$@/ "${args}" pattern with a proper array (local -a cov_cmd) and "$@", which correctly handles arguments with spaces. - Conditional __pycache__ chown: replaced unconditional chown of __pycache__ directories (which fails silently on first run before they exist) with `find -maxdepth 0 -type d | xargs -r chown`. - Scratch file: replaced all hardcoded /tmp/output.txt references with a SCRATCH_FILE variable for consistency and easier customisation. Signed-off-by: Martin Belanger <martin.belanger@dell.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a711387 commit 3d408ec

1 file changed

Lines changed: 34 additions & 28 deletions

File tree

coverage.sh.in

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PRIMARY_GRP=$( id -ng )
44
PRIMARY_USR=$( id -nu )
55
PYTHON_PATH=.:./subprojects/nvme-cli/libnvme
66
AVAHI_PUBLISHER=mdns_publisher.service
7+
SCRATCH_FILE=/tmp/stas-coverage-out.txt
78

89
file=/tmp/stafd.conf.XXXXXX
910
stafd_conf_fname=$(mktemp $file)
@@ -55,9 +56,9 @@ sd_stop() {
5556
unit="${app}"-cov.service
5657
if systemctl-exists "${unit}" >/dev/null 2>&1; then
5758
log "Stop ${app}"
58-
sudo systemctl stop "${unit}" >/tmp/output.txt 2>&1
59-
if [ -s /tmp/output.txt ]; then
60-
log_file_contents $? /tmp/output.txt
59+
sudo systemctl stop "${unit}" >${SCRATCH_FILE} 2>&1
60+
if [ -s ${SCRATCH_FILE} ]; then
61+
log_file_contents $? ${SCRATCH_FILE}
6162
else
6263
printf " sudo systemctl stop %s\n" "${unit}"
6364
fi
@@ -87,8 +88,8 @@ sd_start() {
8788
sudo systemctl reset-failed "${unit}" >/dev/null 2>&1
8889

8990
log "Start ${app}"
90-
sudo systemd-run --unit="${unit}" --working-directory=. --property=Type=dbus --property=BusName="${dbus}" --property="SyslogIdentifier=${app}" --setenv=PYTHONPATH=${PYTHON_PATH} --setenv=RUNTIME_DIRECTORY=${RUNTIME_DIRECTORY} coverage run --rcfile=.coveragerc ${cmd} >/tmp/output.txt 2>&1
91-
log_file_contents $? /tmp/output.txt
91+
sudo systemd-run --unit="${unit}" --working-directory=. --property=Type=dbus --property=BusName="${dbus}" --property="SyslogIdentifier=${app}" --setenv=PYTHONPATH=${PYTHON_PATH} --setenv=RUNTIME_DIRECTORY=${RUNTIME_DIRECTORY} coverage run --rcfile=.coveragerc ${cmd} >${SCRATCH_FILE} 2>&1
92+
log_file_contents $? ${SCRATCH_FILE}
9293
printf "\n"
9394
sleep 1
9495
}
@@ -99,8 +100,8 @@ sd_restart() {
99100

100101
if systemctl is-active "${unit}" >/dev/null 2>&1; then
101102
log "Restart ${app}"
102-
sudo systemctl restart "${unit}" && printf "systemctl restart %s\n" "${unit}" >/tmp/output.txt 2>&1
103-
log_file_contents $? /tmp/output.txt
103+
sudo systemctl restart "${unit}" && printf "systemctl restart %s\n" "${unit}" >${SCRATCH_FILE} 2>&1
104+
log_file_contents $? ${SCRATCH_FILE}
104105
sleep 1
105106
else
106107
msg="Cannot restart ${app}, which is not currently running."
@@ -114,24 +115,29 @@ reload_cfg() {
114115
unit="${app}"-cov.service
115116
pid=$( systemctl show --property MainPID --value "${unit}" )
116117
log "Reload config ${app} - SIGHUP ${pid}"
117-
#sudo systemctl reload "${unit}" && printf "systemctl reload %s\n" "${unit}" >/tmp/output.txt 2>&1
118-
sudo kill -HUP "${pid}" >/tmp/output.txt 2>&1
119-
log_file_contents $? /tmp/output.txt
118+
#sudo systemctl reload "${unit}" && printf "systemctl reload %s\n" "${unit}" >${SCRATCH_FILE} 2>&1
119+
sudo kill -HUP "${pid}" >${SCRATCH_FILE} 2>&1
120+
log_file_contents $? ${SCRATCH_FILE}
120121
printf "\n"
121122
sleep 1
122123
}
123124

124125
run_unit_test() {
125-
input=$@
126+
local input="$*"
127+
local -a cov_cmd
126128
if [ "$1" == "sudo" ]; then
127129
shift
128-
COVERAGE="sudo coverage"
130+
cov_cmd=(sudo coverage)
129131
else
130-
COVERAGE="coverage"
132+
cov_cmd=(coverage)
131133
fi
132-
args=$@
133134
log "Run unit test: ${input}"
134-
PYTHONPATH=${PYTHON_PATH} ${COVERAGE} run --rcfile=.coveragerc "${args}" >/dev/null 2>&1
135+
PYTHONPATH=${PYTHON_PATH} "${cov_cmd[@]}" run --rcfile=.coveragerc "$@" >${SCRATCH_FILE} 2>&1
136+
local rc=$?
137+
if [ ${rc} -ne 0 ]; then
138+
log "WARNING: Unit test FAILED (rc=${rc}): ${input}"
139+
log_file_contents ${rc} ${SCRATCH_FILE}
140+
fi
135141
}
136142

137143
run_cmd_coverage() {
@@ -144,16 +150,16 @@ run_cmd_coverage() {
144150
fi
145151
cmd="$@"
146152
log "Invoke: ${input}"
147-
${COVERAGE} run --rcfile=.coveragerc ${cmd} >/tmp/output.txt 2>&1
148-
log_file_contents $? /tmp/output.txt
153+
${COVERAGE} run --rcfile=.coveragerc ${cmd} >${SCRATCH_FILE} 2>&1
154+
log_file_contents $? ${SCRATCH_FILE}
149155
printf "\n"
150156
}
151157

152158
run_cmd() {
153159
cmd="$@"
154-
${cmd} >/tmp/output.txt 2>&1
155-
if [ -s /tmp/output.txt ]; then
156-
log_file_contents $? /tmp/output.txt
160+
${cmd} >${SCRATCH_FILE} 2>&1
161+
if [ -s ${SCRATCH_FILE} ]; then
162+
log_file_contents $? ${SCRATCH_FILE}
157163
else
158164
printf " %s\n" "${cmd}"
159165
fi
@@ -183,8 +189,8 @@ postrun_cleanup() {
183189
sd_stop "stacd"
184190

185191
log "Stop nvmet"
186-
sudo ../utils/nvmet/nvmet.py clean >/tmp/output.txt 2>&1
187-
log_file_contents $? /tmp/output.txt
192+
sudo ../utils/nvmet/nvmet.py clean >${SCRATCH_FILE} 2>&1
193+
log_file_contents $? ${SCRATCH_FILE}
188194
printf "\n"
189195

190196
log "nvme disconnect-all"
@@ -219,7 +225,7 @@ postrun_cleanup() {
219225
}
220226

221227
trap postrun_cleanup EXIT
222-
trap postrun_cleanup SIGINT
228+
trap 'exit 130' SIGINT
223229

224230
################################################################################
225231
################################################################################
@@ -312,8 +318,8 @@ run_cmd_coverage stacctl invalid-command
312318
# Start nvme target simulator
313319
log "Start nvmet"
314320
sudo ../utils/nvmet/nvmet.py clean >/dev/null 2>&1
315-
sudo ../utils/nvmet/nvmet.py create -f ../utils/nvmet/nvmet.conf >/tmp/output.txt 2>&1
316-
log_file_contents $? /tmp/output.txt
321+
sudo ../utils/nvmet/nvmet.py create -f ../utils/nvmet/nvmet.conf >${SCRATCH_FILE} 2>&1
322+
log_file_contents $? ${SCRATCH_FILE}
317323
printf "\n"
318324

319325
sleep 2
@@ -668,14 +674,14 @@ sleep 2
668674

669675
sd_stop "stafd"
670676
sleep 5
671-
sd_start "stafd"
677+
sd_start "stafd" "@STAFD_DBUS_NAME@"
672678

673679

674680
#*******************************************************************************
675681
# Change ownership of files that were created as root
676682
sudo chown -R "${PRIMARY_USR}":"${PRIMARY_GRP}" coverage >/dev/null 2>&1
677-
sudo chown -R "${PRIMARY_USR}":"${PRIMARY_GRP}" staslib/__pycache__ >/dev/null 2>&1
678-
sudo chown -R "${PRIMARY_USR}":"${PRIMARY_GRP}" subprojects/nvme-cli/libnvme/libnvme/__pycache__ >/dev/null 2>&1
683+
find staslib/__pycache__ -maxdepth 0 -type d 2>/dev/null | xargs -r sudo chown -R "${PRIMARY_USR}":"${PRIMARY_GRP}"
684+
find subprojects/nvme-cli/libnvme/libnvme/__pycache__ -maxdepth 0 -type d 2>/dev/null | xargs -r sudo chown -R "${PRIMARY_USR}":"${PRIMARY_GRP}"
679685

680686
#*******************************************************************************
681687
# Run unit tests

0 commit comments

Comments
 (0)