]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - test/test-functions
test: add TEST_RUN_ID
[thirdparty/dracut.git] / test / test-functions
index 20ac3b26ffb620f78fb5a4ea43b120ffedf50f80..bde5f7424d65c285e50f47b2506fc13bdc753a01 100644 (file)
+#!/bin/bash
 PATH=/sbin:/bin:/usr/sbin:/usr/bin
 export PATH
 
+[[ -e .testdir${TEST_RUN_ID:+-$TEST_RUN_ID} ]] && . .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
+if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
+    TESTDIR=$(mktemp -d -p "/var/tmp" -t dracut-test.XXXXXX)
+fi
+echo "TESTDIR=\"$TESTDIR\"" > .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
+export TESTDIR
+
+command -v test_check &>/dev/null || test_check() {
+        :
+    }
+
+# terminal sequence to set color to a 'success' color (currently: green)
+function SETCOLOR_SUCCESS() { echo -en '\033[0;32m'; }
+# terminal sequence to set color to a 'failure' color (currently: red)
+function SETCOLOR_FAILURE() { echo -en '\033[0;31m'; }
+# terminal sequence to set color to a 'warning' color (currently: yellow)
+function SETCOLOR_WARNING() { echo -en '\033[0;33m'; }
+# terminal sequence to reset to the default color.
+function SETCOLOR_NORMAL() { echo -en '\033[0;39m'; }
+
+COLOR_SUCCESS='\033[0;32m'
+COLOR_FAILURE='\033[0;31m'
+COLOR_WARNING='\033[0;33m'
+COLOR_NORMAL='\033[0;39m'
+
+check_root() {
+    if (( $EUID != 0 )); then
+        SETCOLOR_FAILURE; echo "Tests must be run as root! Please use 'sudo'."; SETCOLOR_NORMAL
+        exit 1
+    fi
+}
 
 while (($# > 0)); do
     case $1 in
-        --run) 
+        --run)
+            check_root
            echo "TEST RUN: $TEST_DESCRIPTION"
-           test_run
+           test_check && test_run
            exit $?;;
-        --setup) 
+        --setup)
+            check_root
            echo "TEST SETUP: $TEST_DESCRIPTION"
-           test_setup
+           test_check && test_setup
            exit $?;;
-        --clean) 
+        --clean)
            echo "TEST CLEANUP: $TEST_DESCRIPTION"
            test_cleanup
+           rm -fr -- "$TESTDIR"
+           rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
            exit $?;;
-        --all) 
-           echo -n "TEST: $TEST_DESCRIPTION ";
-           (
-               test_setup && test_run
-               ret=$?
-               test_cleanup
-               exit $ret
-           ) </dev/null >test.log 2>&1
+        --all)
+            check_root
+            if ! test_check 2&>test-${TEST_RUN_ID:+-$TEST_RUN_ID}.log ; then
+               echo -e "TEST: $TEST_DESCRIPTION " $COLOR_WARNING "[SKIPPED]" $COLOR_NORMAL
+               exit 0;
+            else
+               echo "TEST: $TEST_DESCRIPTION [STARTED]";
+            fi
+            if [ "$V" != "1" ]; then
+               (
+                   test_setup && test_run
+                   ret=$?
+                   test_cleanup
+                   rm -fr -- "$TESTDIR"
+                   rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
+                   exit $ret
+               ) </dev/null >test-${TEST_RUN_ID:+-$TEST_RUN_ID}.log 2>&1
+            else
+                set -o pipefail
+                (
+                   test_setup && test_run
+                   ret=$?
+                   test_cleanup
+                    if ((ret!=0)) && [[ -f "$TESTDIR"/server.log ]]; then
+                        mv [[ -f "$TESTDIR"/server.log ]] ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
+                    fi
+                   rm -fr -- "$TESTDIR"
+                   rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
+                   exit $ret
+               ) </dev/null 2>&1 | tee test-${TEST_RUN_ID:+-$TEST_RUN_ID}.log
+            fi
            ret=$?
+            set +o pipefail
            if [ $ret -eq 0 ]; then
-               rm test.log
-              echo "[OK]"
+                rm -- test-${TEST_RUN_ID:+-$TEST_RUN_ID}.log
+               echo -e "TEST: $TEST_DESCRIPTION " $COLOR_SUCCESS "[OK]" $COLOR_NORMAL
            else
-              echo "[FAILED]"
-              echo "see $(pwd)/test.log"
+               echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
+                if [ "$V" == "2" ]; then
+                   cat $(pwd)/server${TEST_RUN_ID:+-$TEST_RUN_ID}.log $(pwd)/test-${TEST_RUN_ID:+-$TEST_RUN_ID}.log
+                   echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
+                else
+                   echo "see $(pwd)/test-${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
+                fi
            fi
            exit $ret;;
         *) break ;;