diff mbox series

[05/11] selftests: ublk: support shellcheck and fix all warning

Message ID 20250303124324.3563605-6-ming.lei@redhat.com (mailing list archive)
State New
Headers show
Series selftests: ublk: bug fixes & consolidation | expand

Commit Message

Ming Lei March 3, 2025, 12:43 p.m. UTC
Add shellcheck, meantime fixes all warnings.

Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 tools/testing/selftests/ublk/Makefile        |  3 ++
 tools/testing/selftests/ublk/test_common.sh  | 57 +++++++++++---------
 tools/testing/selftests/ublk/test_loop_01.sh | 10 ++--
 tools/testing/selftests/ublk/test_loop_02.sh | 10 ++--
 tools/testing/selftests/ublk/test_loop_03.sh | 10 ++--
 tools/testing/selftests/ublk/test_loop_04.sh | 10 ++--
 tools/testing/selftests/ublk/test_null_01.sh |  6 +--
 7 files changed, 58 insertions(+), 48 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/ublk/Makefile b/tools/testing/selftests/ublk/Makefile
index 555a3ba5b481..9415f6f6df48 100644
--- a/tools/testing/selftests/ublk/Makefile
+++ b/tools/testing/selftests/ublk/Makefile
@@ -14,3 +14,6 @@  TEST_GEN_PROGS_EXTENDED = kublk
 include ../lib.mk
 
 $(TEST_GEN_PROGS_EXTENDED): kublk.c null.c file_backed.c
+
+check:
+	shellcheck -x -f gcc *.sh
diff --git a/tools/testing/selftests/ublk/test_common.sh b/tools/testing/selftests/ublk/test_common.sh
index 304f22ffda58..61044cb58138 100755
--- a/tools/testing/selftests/ublk/test_common.sh
+++ b/tools/testing/selftests/ublk/test_common.sh
@@ -3,46 +3,49 @@ 
 
 _create_backfile() {
 	local my_size=$1
-	local my_file=`mktemp ublk_bpf_${my_size}_XXXXX`
+	local my_file
 
-	truncate -s ${my_size} ${my_file}
-	echo $my_file
+	my_file=$(mktemp ublk_file_"${my_size}"_XXXXX)
+	truncate -s "${my_size}" "${my_file}"
+	echo "$my_file"
 }
 
 _remove_backfile() {
 	local file=$1
 
-	[ -f "$file" ] && rm -f $file
+	[ -f "$file" ] && rm -f "$file"
 }
 
 _create_tmp_dir() {
-	local my_file=`mktemp -d ublk_bpf_dir_XXXXX`
+	local my_file;
 
-	echo $my_file
+	my_file=$(mktemp -d ublk_dir_XXXXX)
+	echo "$my_file"
 }
 
 _remove_tmp_dir() {
 	local dir=$1
 
-	[ -d "$dir" ] && rmdir $dir
+	[ -d "$dir" ] && rmdir "$dir"
 }
 
 _mkfs_mount_test()
 {
 	local dev=$1
 	local err_code=0
-	local mnt_dir=`_create_tmp_dir`
+	local mnt_dir;
 
-	mkfs.ext4 -F $dev > /dev/null 2>&1
+	mnt_dir=$(_create_tmp_dir)
+	mkfs.ext4 -F "$dev" > /dev/null 2>&1
 	err_code=$?
 	if [ $err_code -ne 0 ]; then
 		return $err_code
 	fi
 
-	mount -t ext4 $dev $mnt_dir > /dev/null 2>&1
-	umount $dev
+	mount -t ext4 "$dev" "$mnt_dir" > /dev/null 2>&1
+	umount "$dev"
 	err_code=$?
-	_remove_tmp_dir $mnt_dir
+	_remove_tmp_dir "$mnt_dir"
 	if [ $err_code -ne 0 ]; then
 		return $err_code
 	fi
@@ -73,12 +76,12 @@  _prep_test() {
 	_check_root
 	local type=$1
 	shift 1
-	echo "ublk $type: $@"
+	echo "ublk $type: $*"
 }
 
 _show_result()
 {
-	if [ $2 -ne 0 ]; then
+	if [ "$2" -ne 0 ]; then
 		echo "$1 : [FAIL]"
 	else
 		echo "$1 : [PASS]"
@@ -86,28 +89,32 @@  _show_result()
 }
 
 _cleanup_test() {
-	${UBLK_PROG} del -n $1
+	"${UBLK_PROG}" del -n "$1"
 }
 
 _add_ublk_dev() {
-	local kublk_temp=`mktemp /tmp/kublk-XXXXXX`
-	${UBLK_PROG} add $@ > ${kublk_temp} 2>&1
-	if [ $? -ne 0 ]; then
-		echo "fail to add ublk dev $@"
-		exit -1
+	local kublk_temp;
+	local dev_id;
+
+	kublk_temp=$(mktemp /tmp/kublk-XXXXXX)
+	if ! "${UBLK_PROG}" add "$@" > "${kublk_temp}" 2>&1; then
+		echo "fail to add ublk dev $*"
+		return 255
 	fi
-	local dev_id=`grep "dev id" ${kublk_temp} | awk -F '[ :]' '{print $3}'`
+
+	dev_id=$(grep "dev id" "${kublk_temp}" | awk -F '[ :]' '{print $3}')
 	udevadm settle
-	rm -f ${kublk_temp}
-	echo ${dev_id}
+	rm -f "${kublk_temp}"
+	echo "${dev_id}"
 }
 
 _have_feature()
 {
-	if  $UBLK_PROG "features" | grep $1 > /dev/null 2>&1; then
+	if  "$UBLK_PROG" "features" | grep "$1" > /dev/null 2>&1; then
 		return 0
 	fi
 	return 1
 }
 
-export UBLK_PROG=$(pwd)/kublk
+UBLK_PROG=$(pwd)/kublk
+export UBLK_PROG
diff --git a/tools/testing/selftests/ublk/test_loop_01.sh b/tools/testing/selftests/ublk/test_loop_01.sh
index 829e8df05942..1d3f934dca4c 100755
--- a/tools/testing/selftests/ublk/test_loop_01.sh
+++ b/tools/testing/selftests/ublk/test_loop_01.sh
@@ -8,13 +8,13 @@  ERR_CODE=0
 
 _prep_test "loop" "write and verify test"
 
-backfile_0=`_create_backfile 256M`
+backfile_0=$(_create_backfile 256M)
 
-dev_id=`_add_ublk_dev -t loop $backfile_0`
+dev_id=$(_add_ublk_dev -t loop "$backfile_0")
 
 # run fio over the ublk disk
 fio --name=write_and_verify \
-    --filename=/dev/ublkb${dev_id} \
+    --filename=/dev/ublkb"${dev_id}" \
     --ioengine=libaio --iodepth=16 \
     --rw=write \
     --size=256M \
@@ -24,8 +24,8 @@  fio --name=write_and_verify \
     --bs=4k > /dev/null 2>&1
 ERR_CODE=$?
 
-_cleanup_test ${dev_id} "loop"
+_cleanup_test "${dev_id}" "loop"
 
-_remove_backfile $backfile_0
+_remove_backfile "$backfile_0"
 
 _show_result $TID $ERR_CODE
diff --git a/tools/testing/selftests/ublk/test_loop_02.sh b/tools/testing/selftests/ublk/test_loop_02.sh
index c71ae63059b8..df06b7804881 100755
--- a/tools/testing/selftests/ublk/test_loop_02.sh
+++ b/tools/testing/selftests/ublk/test_loop_02.sh
@@ -8,15 +8,15 @@  ERR_CODE=0
 
 _prep_test "loop" "mkfs & mount & umount"
 
-backfile_0=`_create_backfile 256M`
+backfile_0=$(_create_backfile 256M)
 
-dev_id=`_add_ublk_dev -t loop $backfile_0`
+dev_id=$(_add_ublk_dev -t loop "$backfile_0")
 
-_mkfs_mount_test /dev/ublkb${dev_id}
+_mkfs_mount_test /dev/ublkb"${dev_id}"
 ERR_CODE=$?
 
-_cleanup_test ${dev_id} "loop"
+_cleanup_test "${dev_id}" "loop"
 
-_remove_backfile $backfile_0
+_remove_backfile "$backfile_0"
 
 _show_result $TID $ERR_CODE
diff --git a/tools/testing/selftests/ublk/test_loop_03.sh b/tools/testing/selftests/ublk/test_loop_03.sh
index e781ac6db6b4..2255b4296590 100755
--- a/tools/testing/selftests/ublk/test_loop_03.sh
+++ b/tools/testing/selftests/ublk/test_loop_03.sh
@@ -10,13 +10,13 @@  _have_feature "ZERO_COPY" || exit 4
 
 _prep_test "loop" "write and verify over zero copy"
 
-backfile_0=`_create_backfile 256M`
+backfile_0=$(_create_backfile 256M)
 
-dev_id=`_add_ublk_dev -t loop $backfile_0 -z`
+dev_id=$(_add_ublk_dev -t loop -z "$backfile_0")
 
 # run fio over the ublk disk
 fio --name=write_and_verify \
-    --filename=/dev/ublkb${dev_id} \
+    --filename=/dev/ublkb"${dev_id}" \
     --ioengine=libaio --iodepth=64 \
     --rw=write \
     --size=256M \
@@ -26,8 +26,8 @@  fio --name=write_and_verify \
     --bs=4k > /dev/null 2>&1
 ERR_CODE=$?
 
-_cleanup_test ${dev_id} "loop"
+_cleanup_test "${dev_id}" "loop"
 
-_remove_backfile $backfile_0
+_remove_backfile "$backfile_0"
 
 _show_result $TID $ERR_CODE
diff --git a/tools/testing/selftests/ublk/test_loop_04.sh b/tools/testing/selftests/ublk/test_loop_04.sh
index 6ab67247c809..a797b25213ec 100755
--- a/tools/testing/selftests/ublk/test_loop_04.sh
+++ b/tools/testing/selftests/ublk/test_loop_04.sh
@@ -8,15 +8,15 @@  ERR_CODE=0
 
 _prep_test "loop" "mkfs & mount & umount with zero copy"
 
-backfile_0=`_create_backfile 256M`
+backfile_0=$(_create_backfile 256M)
 
-dev_id=`_add_ublk_dev -t loop -z $backfile_0`
+dev_id=$(_add_ublk_dev -t loop -z "$backfile_0")
 
-_mkfs_mount_test /dev/ublkb${dev_id}
+_mkfs_mount_test /dev/ublkb"${dev_id}"
 ERR_CODE=$?
 
-_cleanup_test ${dev_id} "loop"
+_cleanup_test "${dev_id}" "loop"
 
-_remove_backfile $backfile_0
+_remove_backfile "$backfile_0"
 
 _show_result $TID $ERR_CODE
diff --git a/tools/testing/selftests/ublk/test_null_01.sh b/tools/testing/selftests/ublk/test_null_01.sh
index 04fc3ac7c716..b048ddc4ae6f 100755
--- a/tools/testing/selftests/ublk/test_null_01.sh
+++ b/tools/testing/selftests/ublk/test_null_01.sh
@@ -8,12 +8,12 @@  ERR_CODE=0
 
 _prep_test "null" "basic IO test"
 
-dev_id=`_add_ublk_dev -t null`
+dev_id=$(_add_ublk_dev -t null)
 
 # run fio over the two disks
-fio --name=job1 --filename=/dev/ublkb${dev_id} --ioengine=libaio --rw=readwrite --iodepth=32 --size=256M > /dev/null 2>&1
+fio --name=job1 --filename=/dev/ublkb"${dev_id}" --ioengine=libaio --rw=readwrite --iodepth=32 --size=256M > /dev/null 2>&1
 ERR_CODE=$?
 
-_cleanup_test ${dev_id} "null"
+_cleanup_test "${dev_id}" "null"
 
 _show_result $TID $ERR_CODE