diff mbox series

[RFC,ndctl,1/9] test/common: Ensure to unload test modules

Message ID 165030179767.3224737.3430509039595994936.stgit@lep8c.aus.stglabs.ibm.com (mailing list archive)
State New
Headers show
Series test: Enable PAPR test family by default | expand

Commit Message

Shivaprasad G Bhat April 18, 2022, 5:10 p.m. UTC
The _cleanup is easily missed to be called at places where trap
ERR is set, ex security.sh.

The patch moves all the modprobes into the _init in test/common
and sets the trap on exit for "any" of the reasons to invoke
_cleanup.

The patch also gracefully skips the test if the module load fails
instead of continuing, which otherwise makes the tests fail
erroneously in random places during the tests.

Signed-off-by: Shivaprasad G Bhat <sbhat@linux.ibm.com>
---
 test/btt-check.sh               |    3 +--
 test/btt-errors.sh              |    3 +--
 test/btt-pad-compat.sh          |    3 +--
 test/clear.sh                   |    4 +---
 test/common                     |   12 ++++++++++++
 test/create.sh                  |    4 +---
 test/daxdev-errors.sh           |    4 +---
 test/firmware-update.sh         |    3 +--
 test/inject-error.sh            |    3 +--
 test/inject-smart.sh            |    3 +--
 test/label-compat.sh            |    4 +---
 test/max_available_extent_ns.sh |    3 +--
 test/monitor.sh                 |    3 +--
 test/multi-dax.sh               |    4 +---
 test/pfn-meta-errors.sh         |    3 +--
 test/pmem-errors.sh             |    3 +--
 test/rescan-partitions.sh       |    3 +--
 test/sector-mode.sh             |    4 +---
 test/security.sh                |    3 +--
 test/track-uuid.sh              |    3 +--
 20 files changed, 31 insertions(+), 44 deletions(-)
diff mbox series

Patch

diff --git a/test/btt-check.sh b/test/btt-check.sh
index 65b5c58b..1039d0eb 100755
--- a/test/btt-check.sh
+++ b/test/btt-check.sh
@@ -150,10 +150,9 @@  do_tests()
 }
 
 # setup (reset nfit_test dimms, create the BTT namespace)
-modprobe nfit_test
+_init
 rc=1
 reset && create
 do_tests
 reset
-_cleanup
 exit 0
diff --git a/test/btt-errors.sh b/test/btt-errors.sh
index 18518d50..ea1a6627 100755
--- a/test/btt-errors.sh
+++ b/test/btt-errors.sh
@@ -42,7 +42,7 @@  mkdir -p $MNT
 trap 'err $LINENO cleanup' ERR
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 resetV
 
 rc=1
@@ -144,5 +144,4 @@  dd if=/dev/$blockdev of=/dev/null iflag=direct bs=4096 count=1 && err $LINENO ||
 # done, exit
 reset
 cleanup
-_cleanup
 exit 0
diff --git a/test/btt-pad-compat.sh b/test/btt-pad-compat.sh
index 005316a2..de85fec7 100755
--- a/test/btt-pad-compat.sh
+++ b/test/btt-pad-compat.sh
@@ -172,11 +172,10 @@  do_tests()
 	ns_info_wipe
 }
 
-modprobe nfit_test
+_init
 check_prereq xxd
 rc=1
 reset
 do_tests
 reset
-_cleanup
 exit 0
diff --git a/test/clear.sh b/test/clear.sh
index c4d02d54..9b785e35 100755
--- a/test/clear.sh
+++ b/test/clear.sh
@@ -13,7 +13,7 @@  check_min_kver "4.6" || do_skip "lacks clear poison support"
 trap 'err $LINENO' ERR
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 reset
 
 rc=1
@@ -72,6 +72,4 @@  if check_min_kver "4.9"; then
 	fi
 fi
 
-_cleanup
-
 exit 0
diff --git a/test/common b/test/common
index fb487958..d3216a0c 100644
--- a/test/common
+++ b/test/common
@@ -113,6 +113,18 @@  _cleanup()
 	modprobe -r nfit_test
 }
 
+_init()
+{
+	set +e
+	modprobe nfit_test
+	if [ $? -ne 0 ]; then
+		echo "Could not load the nfit_test module."
+		exit 77
+	fi
+	set -e
+	trap _cleanup EXIT INT TERM HUP PIPE
+}
+
 # json2var
 # stdin: json
 #
diff --git a/test/create.sh b/test/create.sh
index 9a6f3733..c2fdee2e 100755
--- a/test/create.sh
+++ b/test/create.sh
@@ -14,7 +14,7 @@  check_min_kver "4.5" || do_skip "may lack namespace mode attribute"
 trap 'err $LINENO' ERR
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 reset
 
 rc=1
@@ -40,6 +40,4 @@  eval $(echo $json | json2var)
 # free capacity for blk creation
 $NDCTL destroy-namespace -f $dev
 
-_cleanup
-
 exit 0
diff --git a/test/daxdev-errors.sh b/test/daxdev-errors.sh
index 7f797181..f32f8b80 100755
--- a/test/daxdev-errors.sh
+++ b/test/daxdev-errors.sh
@@ -14,7 +14,7 @@  check_prereq "jq"
 trap 'err $LINENO' ERR
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 reset
 
 rc=1
@@ -71,6 +71,4 @@  if read sector len < /sys/bus/platform/devices/nfit_test.0/$busdev/$region/badbl
 fi
 [ -n "$sector" ] && echo "fail: $LINENO" && exit 1
 
-_cleanup
-
 exit 0
diff --git a/test/firmware-update.sh b/test/firmware-update.sh
index 93ce166e..1fac9dc1 100755
--- a/test/firmware-update.sh
+++ b/test/firmware-update.sh
@@ -70,11 +70,10 @@  do_tests()
 
 check_min_kver "4.16" || do_skip "may lack firmware update test handling"
 
-modprobe nfit_test
+_init
 fwupd_reset
 detect
 rc=1
 do_tests
 rm -f $image
-_cleanup
 exit 0
diff --git a/test/inject-error.sh b/test/inject-error.sh
index fd823b6c..15d0dbe8 100755
--- a/test/inject-error.sh
+++ b/test/inject-error.sh
@@ -79,10 +79,9 @@  do_tests()
 	check_status
 }
 
-modprobe nfit_test
+_init
 rc=1
 reset && create
 do_tests
 reset
-_cleanup
 exit 0
diff --git a/test/inject-smart.sh b/test/inject-smart.sh
index 046322bf..80af058a 100755
--- a/test/inject-smart.sh
+++ b/test/inject-smart.sh
@@ -167,7 +167,7 @@  do_tests()
 
 check_min_kver "4.19" || do_skip "kernel $KVER may not support smart (un)injection"
 check_prereq "jq"
-modprobe nfit_test
+_init
 rc=1
 
 jlist=$($TEST_PATH/list-smart-dimm -b $bus)
@@ -175,5 +175,4 @@  dimm="$(jq '.[]."dev"?, ."dev"?' <<< $jlist | sort | head -1 | xargs)"
 test -n "$dimm"
 
 do_tests
-_cleanup
 exit 0
diff --git a/test/label-compat.sh b/test/label-compat.sh
index 7ae4d5ef..4ccf69fa 100755
--- a/test/label-compat.sh
+++ b/test/label-compat.sh
@@ -15,7 +15,7 @@  check_prereq "jq"
 trap 'err $LINENO' ERR
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 $NDCTL disable-region -b $NFIT_TEST_BUS0 all
 $NDCTL init-labels -f -b $NFIT_TEST_BUS0 all
 
@@ -43,6 +43,4 @@  if [ -z $len ]; then
 	exit 1
 fi
 
-_cleanup
-
 exit 0
diff --git a/test/max_available_extent_ns.sh b/test/max_available_extent_ns.sh
index 47a921f5..98235424 100755
--- a/test/max_available_extent_ns.sh
+++ b/test/max_available_extent_ns.sh
@@ -31,9 +31,8 @@  do_test()
 	$NDCTL create-namespace -r $region -t pmem
 }
 
-modprobe nfit_test
+_init
 rc=1
 reset
 do_test
-_cleanup
 exit 0
diff --git a/test/monitor.sh b/test/monitor.sh
index e58c908b..10e65374 100755
--- a/test/monitor.sh
+++ b/test/monitor.sh
@@ -161,10 +161,9 @@  do_tests()
 	test_filter_dimmevent
 }
 
-modprobe nfit_test
+_init
 rc=1
 reset
 set_smart_supported_bus
 do_tests
-_cleanup
 exit 0
diff --git a/test/multi-dax.sh b/test/multi-dax.sh
index 04070adb..2b21c28c 100755
--- a/test/multi-dax.sh
+++ b/test/multi-dax.sh
@@ -16,7 +16,7 @@  trap 'err $LINENO' ERR
 ALIGN_SIZE=`getconf PAGESIZE`
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 reset
 rc=1
 
@@ -28,6 +28,4 @@  chardev1=$(echo $json | jq ". | select(.mode == \"devdax\") | .daxregion.devices
 json=$($NDCTL create-namespace -b $NFIT_TEST_BUS0 -r $region -t pmem -m devdax -a $ALIGN_SIZE -s 16M)
 chardev2=$(echo $json | jq ". | select(.mode == \"devdax\") | .daxregion.devices[0].chardev")
 
-_cleanup
-
 exit 0
diff --git a/test/pfn-meta-errors.sh b/test/pfn-meta-errors.sh
index 63148979..ccf5b7a1 100755
--- a/test/pfn-meta-errors.sh
+++ b/test/pfn-meta-errors.sh
@@ -28,7 +28,7 @@  set -e
 trap 'err $LINENO' ERR
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 reset
 
 rc=1
@@ -67,5 +67,4 @@  if read -r sector len < "/sys/block/$raw_bdev/badblocks"; then
 	false
 fi
 
-_cleanup
 exit 0
diff --git a/test/pmem-errors.sh b/test/pmem-errors.sh
index 9a59c25d..550114ac 100755
--- a/test/pmem-errors.sh
+++ b/test/pmem-errors.sh
@@ -25,7 +25,7 @@  mkdir -p $MNT
 trap 'err $LINENO cleanup' ERR
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 reset
 
 rc=1
@@ -110,6 +110,5 @@  echo $((start_sect + 1)) 1 > /sys/block/$blockdev/badblocks
 dd if=$MNT/$FILE of=/dev/null iflag=direct bs=4096 count=1 && err $LINENO || true
 
 cleanup
-_cleanup
 
 exit 0
diff --git a/test/rescan-partitions.sh b/test/rescan-partitions.sh
index 51bbd731..f46e17ed 100755
--- a/test/rescan-partitions.sh
+++ b/test/rescan-partitions.sh
@@ -65,11 +65,10 @@  test_mode()
 	$NDCTL destroy-namespace $dev
 }
 
-modprobe nfit_test
+_init
 rc=1
 reset
 test_mode "raw"
 test_mode "fsdax"
 test_mode "sector"
-_cleanup
 exit 0
diff --git a/test/sector-mode.sh b/test/sector-mode.sh
index f70b0f17..d4a62bf3 100755
--- a/test/sector-mode.sh
+++ b/test/sector-mode.sh
@@ -14,7 +14,7 @@  trap 'err $LINENO' ERR
 ALIGN_SIZE=`getconf PAGESIZE`
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 reset
 reset1
 
@@ -27,6 +27,4 @@  NAMESPACE=$($NDCTL create-namespace --no-autolabel -r $REGION -m sector -f -l 4K
 $NDCTL create-namespace --no-autolabel -e $NAMESPACE -m dax -f -a $ALIGN_SIZE
 $NDCTL create-namespace --no-autolabel -e $NAMESPACE -m sector -f -l 4K
 
-_cleanup
-
 exit 0
diff --git a/test/security.sh b/test/security.sh
index 34c4977b..7ae6e88c 100755
--- a/test/security.sh
+++ b/test/security.sh
@@ -246,7 +246,7 @@  if [ "$uid" -ne 0 ]; then
 	do_skip "run as root or with a sudo login shell for test to work"
 fi
 
-modprobe nfit_test
+_init
 setup
 check_prereq "keyctl"
 rc=1
@@ -275,5 +275,4 @@  test_6_load_keys
 
 test_cleanup
 post_cleanup
-_cleanup
 exit 0
diff --git a/test/track-uuid.sh b/test/track-uuid.sh
index a967d0e4..954afe7c 100755
--- a/test/track-uuid.sh
+++ b/test/track-uuid.sh
@@ -11,7 +11,7 @@  set -e
 trap 'err $LINENO' ERR
 
 # setup (reset nfit_test dimms)
-modprobe nfit_test
+_init
 reset
 
 rc=1
@@ -34,5 +34,4 @@  $NDCTL disable-namespace $dev
 uuidgen > /sys/bus/nd/devices/$dev/uuid
 $NDCTL enable-namespace $dev
 
-_cleanup
 exit 0