check: run tests in a systemd scope for mandatory test cleanup
[xfstests-dev.git] / check
diff --git a/check b/check
index c7f1dc5ee271536e25d3b3ad529f654b7b87c2f8..83f6fc8bdf3ef24f6af0cd5b2570f5bd2ae030dc 100755 (executable)
--- a/check
+++ b/check
@@ -26,6 +26,7 @@ subdir_xfile=""
 brief_test_summary=false
 do_report=false
 DUMP_OUTPUT=false
+iterations=1
 
 # This is a global variable used to pass test failure text to reporting gunk
 _err_msg=""
@@ -56,6 +57,7 @@ check options
     -glusterfs         test GlusterFS
     -cifs              test CIFS
     -9p                        test 9p
+    -virtiofs          test virtiofs
     -overlay           test overlay
     -pvfs2             test PVFS2
     -tmpfs             test TMPFS
@@ -65,6 +67,7 @@ check options
     -n                 show me, do not run tests
     -T                 output timestamps
     -r                 randomize test order
+    -i <n>             iterate the test list <n> times
     -d                 dump test output to stdout
     -b                 brief test summary
     -R fmt[,fmt]       generate report in formats specified. Supported format: [xunit]
@@ -268,6 +271,7 @@ while [ $# -gt 0 ]; do
        -glusterfs)     FSTYP=glusterfs ;;
        -cifs)          FSTYP=cifs ;;
        -9p)            FSTYP=9p ;;
+       -virtiofs)      FSTYP=virtiofs ;;
        -overlay)       FSTYP=overlay; export OVERLAY=true ;;
        -pvfs2)         FSTYP=pvfs2 ;;
        -tmpfs)         FSTYP=tmpfs ;;
@@ -295,7 +299,7 @@ while [ $# -gt 0 ]; do
 
        -n)     showme=true ;;
         -r)    randomize=true ;;
-
+       -i)     iterations=$2; shift ;;
        -T)     timestamp=true ;;
        -d)     DUMP_OUTPUT=true ;;
        -b)     brief_test_summary=true;;
@@ -387,6 +391,13 @@ _wipe_counters()
        unset try notrun bad
 }
 
+_global_log() {
+       echo "$1" >> $check.log
+       if $OPTIONS_HAVE_SECTIONS; then
+               echo "$1" >> ${REPORT_DIR}/check.log
+       fi
+}
+
 _wrapup()
 {
        seq="check"
@@ -411,10 +422,13 @@ _wrapup()
                                }' \
                                | sort -n >$tmp.out
                        mv $tmp.out $check.time
+                       if $OPTIONS_HAVE_SECTIONS; then
+                               cp $check.time ${REPORT_DIR}/check.time
+                       fi
                fi
 
-               echo "" >>$check.log
-               date >>$check.log
+               _global_log ""
+               _global_log "$(date)"
 
                echo "SECTION       -- $section" >>$tmp.summary
                echo "=========================" >>$tmp.summary
@@ -423,29 +437,33 @@ _wrapup()
                                echo "Ran:$try"
                                echo "Ran:$try" >>$tmp.summary
                        fi
-                       echo "Ran:$try" >>$check.log
+                       _global_log "Ran:$try"
                fi
 
                $interrupt && echo "Interrupted!" | tee -a $check.log
+               if $OPTIONS_HAVE_SECTIONS; then
+                       $interrupt && echo "Interrupted!" | tee -a \
+                               ${REPORT_DIR}/check.log
+               fi
 
                if [ ! -z "$notrun" ]; then
                        if [ $brief_test_summary == "false" ]; then
                                echo "Not run:$notrun"
                                echo "Not run:$notrun" >>$tmp.summary
                        fi
-                       echo "Not run:$notrun" >>$check.log
+                       _global_log "Not run:$notrun"
                fi
 
                if [ ! -z "$n_bad" -a $n_bad != 0 ]; then
                        echo "Failures:$bad"
                        echo "Failed $n_bad of $n_try tests"
-                       echo "Failures:$bad" >>$check.log
-                       echo "Failed $n_bad of $n_try tests" >>$check.log
+                       _global_log "Failures:$bad"
+                       _global_log "Failed $n_bad of $n_try tests"
                        echo "Failures:$bad" >>$tmp.summary
                        echo "Failed $n_bad of $n_try tests" >>$tmp.summary
                else
                        echo "Passed all $n_try tests"
-                       echo "Passed all $n_try tests" >>$check.log
+                       _global_log "Passed all $n_try tests"
                        echo "Passed all $n_try tests" >>$tmp.summary
                fi
                echo "" >>$tmp.summary
@@ -503,6 +521,12 @@ _expunge_test()
        return 0
 }
 
+# Can we run systemd scopes?
+HAVE_SYSTEMD_SCOPES=
+systemctl reset-failed "fstests-check" &>/dev/null
+systemd-run --quiet --unit "fstests-check" --scope bash -c "exit 77" &> /dev/null
+test $? -eq 77 && HAVE_SYSTEMD_SCOPES=yes
+
 # Make the check script unattractive to the OOM killer...
 OOM_SCORE_ADJ="/proc/self/oom_score_adj"
 test -w ${OOM_SCORE_ADJ} && echo -1000 > ${OOM_SCORE_ADJ}
@@ -510,8 +534,26 @@ test -w ${OOM_SCORE_ADJ} && echo -1000 > ${OOM_SCORE_ADJ}
 # ...and make the tests themselves somewhat more attractive to it, so that if
 # the system runs out of memory it'll be the test that gets killed and not the
 # test framework.
+#
+# If systemd is available, run the entire test script in a scope so that we can
+# kill all subprocesses of the test if it fails to clean up after itself.  This
+# is essential for ensuring that the post-test unmount succeeds.  Note that
+# systemd doesn't automatically remove transient scopes that fail to terminate
+# when systemd tells them to terminate (e.g. programs stuck in D state when
+# systemd sends SIGKILL), so we use reset-failed to tear down the scope.
 _run_seq() {
-       bash -c "test -w ${OOM_SCORE_ADJ} && echo 250 > ${OOM_SCORE_ADJ}; exec ./$seq"
+       local cmd=(bash -c "test -w ${OOM_SCORE_ADJ} && echo 250 > ${OOM_SCORE_ADJ}; exec ./$seq")
+
+       if [ -n "${HAVE_SYSTEMD_SCOPES}" ]; then
+               local unit="$(systemd-escape "fs$seq").scope"
+               systemctl reset-failed "${unit}" &> /dev/null
+               systemd-run --quiet --unit "${unit}" --scope "${cmd[@]}"
+               res=$?
+               systemctl stop "${unit}" &> /dev/null
+               return "${res}"
+       else
+               "${cmd[@]}"
+       fi
 }
 
 _detect_kmemleak
@@ -523,7 +565,10 @@ else
        trap "_wrapup; exit \$status" 0 1 2 3 15
 fi
 
-for section in $HOST_OPTIONS_SECTIONS; do
+function run_section()
+{
+       local section=$1
+
        OLD_FSTYP=$FSTYP
        OLD_TEST_FS_MOUNT_OPTS=$TEST_FS_MOUNT_OPTS
        get_next_config $section
@@ -538,7 +583,7 @@ for section in $HOST_OPTIONS_SECTIONS; do
                        fi
                done
                if $skip; then
-                       continue
+                       return
                fi
        fi
 
@@ -552,7 +597,7 @@ for section in $HOST_OPTIONS_SECTIONS; do
                        fi
                done
                if $skip; then
-                       continue
+                       return
                fi
        fi
 
@@ -698,6 +743,8 @@ for section in $HOST_OPTIONS_SECTIONS; do
                seqres="$REPORT_DIR/$seqnum"
 
                mkdir -p $RESULT_DIR
+               rm -f ${RESULT_DIR}/require_scratch*
+               rm -f ${RESULT_DIR}/require_test*
                echo -n "$seqnum"
 
                if $showme; then
@@ -754,6 +801,11 @@ for section in $HOST_OPTIONS_SECTIONS; do
                        touch ${RESULT_DIR}/check_dmesg
                fi
                _try_wipe_scratch_devs > /dev/null 2>&1
+
+               # clear the WARN_ONCE state to allow a potential problem
+               # to be reported for each test
+               (echo 1 > $DEBUGFS_MNT/clear_warn_once) > /dev/null 2>&1
+
                if [ "$DUMP_OUTPUT" = true ]; then
                        _run_seq 2>&1 | tee $tmp.out
                        # Because $? would get tee's return code
@@ -796,6 +848,15 @@ for section in $HOST_OPTIONS_SECTIONS; do
                        _check_dmesg || err=true
                fi
 
+               # Reload the module after each test to check for leaks or
+               # other problems.
+               if [ -n "${TEST_FS_MODULE_RELOAD}" ]; then
+                       _test_unmount 2> /dev/null
+                       _scratch_unmount 2> /dev/null
+                       modprobe -r fs-$FSTYP
+                       modprobe fs-$FSTYP
+               fi
+
                # Scan for memory leaks after every test so that associating
                # a leak to a particular test will be as accurate as possible.
                _check_kmemleak || err=true
@@ -856,6 +917,12 @@ for section in $HOST_OPTIONS_SECTIONS; do
 
        _test_unmount 2> /dev/null
        _scratch_unmount 2> /dev/null
+}
+
+for ((iters = 0; iters < $iterations; iters++)) do
+       for section in $HOST_OPTIONS_SECTIONS; do
+               run_section $section
+       done
 done
 
 interrupt=false