fstests: refactor test boilerplate code
authorDarrick J. Wong <djwong@kernel.org>
Mon, 21 Jun 2021 15:54:54 +0000 (08:54 -0700)
committerEryu Guan <guaneryu@gmail.com>
Sun, 27 Jun 2021 14:49:40 +0000 (22:49 +0800)
Create two new helper functions to deal with boilerplate test code:

A helper function to set the seq and seqnum variables.  We will expand
on this in the next patch so that fstests can autogenerate group files
from now on.

A helper function to register cleanup code that will run if the test
exits or trips over a standard range of signals.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Chandan Babu R <chandanrlinux@gmail.com>
Reviewed-by: Allison Henderson <allison.henderson@oracle.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
common/preamble [new file with mode: 0644]
new

diff --git a/common/preamble b/common/preamble
new file mode 100644 (file)
index 0000000..fba766d
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2021 Oracle.  All Rights Reserved.
+
+# Boilerplate fstests functionality
+
+# Standard cleanup function.  Individual tests can override this.
+_cleanup()
+{
+       cd /
+       rm -r -f $tmp.*
+}
+
+# Install the supplied cleanup code as a signal handler for HUP, INT, QUIT,
+# TERM, or when the test exits.  Extra signals can be specified as subsequent
+# parameters.
+_register_cleanup()
+{
+       local cleanup="$1"
+       shift
+
+       test -n "$cleanup" && cleanup="${cleanup}; "
+       trap "${cleanup}exit \$status" EXIT HUP INT QUIT TERM $*
+}
+
+# Prepare to run a fstest by initializing the required global variables to
+# their defaults, sourcing common functions, registering a cleanup function,
+# and removing the $seqres.full file.
+#
+# The list of group memberships for this test (e.g. auto quick rw) must be
+# passed as arguments to this helper.  It is not necessary to name this test
+# explicitly as a member of the 'all' group.
+_begin_fstest()
+{
+       if [ -n "$seq" ]; then
+               echo "_begin_fstest can only be called once!"
+               exit 1
+       fi
+
+       seq=`basename $0`
+       seqres=$RESULT_DIR/$seq
+       echo "QA output created by $seq"
+
+       here=`pwd`
+       tmp=/tmp/$$
+       status=1        # failure is the default!
+
+       _register_cleanup _cleanup
+
+       . ./common/rc
+
+       # remove previous $seqres.full before test
+       rm -f $seqres.full
+
+}
diff --git a/new b/new
index 357983d93edd7525f2328e9c7f5376a7e269160d..531fd1236b4b2a6d0e8a667babc08361508c6006 100755 (executable)
--- a/new
+++ b/new
@@ -153,27 +153,18 @@ cat <<End-of-File >$tdir/$id
 #
 # what am I here for?
 #
 #
 # what am I here for?
 #
-seq=\`basename \$0\`
-seqres=\$RESULT_DIR/\$seq
-echo "QA output created by \$seq"
-
-here=\`pwd\`
-tmp=/tmp/\$\$
-status=1       # failure is the default!
-trap "_cleanup; exit \\\$status" 0 1 2 3 15
-
-_cleanup()
-{
-       cd /
-       rm -f \$tmp.*
-}
-
-# get standard environment, filters and checks
-. ./common/rc
-. ./common/filter
-
-# remove previous \$seqres.full before test
-rm -f \$seqres.full
+. ./common/preamble
+_begin_fstest group list here
+
+# Override the default cleanup function.
+# _cleanup()
+# {
+#      cd /
+#      rm -r -f \$tmp.*
+# }
+
+# Import common functions.
+# . ./common/filter
 
 # real QA test starts here
 
 
 # real QA test starts here