]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - test/test-functions
test(ISCSI): make test-30 use the test dracut modules
[thirdparty/dracut.git] / test / test-functions
index 451837f04887f0a7948e3f7be4cfb92b9357b790..abd20ec62806e2dc5e5ab520e079b0b890e33bf9 100644 (file)
 #!/bin/bash
-# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
-# ex: ts=8 sw=4 sts=4 et filetype=sh
-PATH=/sbin:/bin:/usr/sbin:/usr/bin
+PATH=/usr/sbin:/usr/bin:/sbin:/bin
 export PATH
 
-[[ -e .testdir ]] && . .testdir
-if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
-   TESTDIR=$(mktemp -d -t dracut-test.XXXXXX)
+# shellcheck disable=SC1090
+[[ -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
+echo "TESTDIR=\"$TESTDIR\"" > .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
 export TESTDIR
 
-command -v test_check &>/dev/null || test_check() {
+KVERSION=${KVERSION-$(uname -r)}
+
+[ -z "$USE_NETWORK" ] && USE_NETWORK="network-legacy"
+
+if [[ -z $basedir ]]; then basedir="$(realpath ../..)"; fi
+
+test_dracut() {
+    TEST_DRACUT_ARGS+=" --local --no-hostonly --no-early-microcode --add test --kver $KVERSION"
+
+    # shellcheck disable=SC2162
+    IFS=' ' read -a TEST_DRACUT_ARGS_ARRAY <<< "$TEST_DRACUT_ARGS"
+
+    "$basedir"/dracut.sh "$@" \
+        --kernel-cmdline "panic=1 oops=panic softlockup_panic=1 systemd.crash_reboot selinux=0 console=ttyS0,115200n81 $DEBUGFAIL" \
+        "${TEST_DRACUT_ARGS_ARRAY[@]}" || return 1
+}
+
+command -v test_check &> /dev/null || test_check() {
     :
 }
 
+command -v test_cleanup &> /dev/null || test_cleanup() {
+    :
+}
+
+# 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
+}
+
+# generate qemu arguments for named raw disks
+#
+# qemu_add_drive_args <index> <args> <filename> <id-name> [<bootindex>]
+#
+# index: name of the index variable (set to 0 at start)
+# args: name of the argument array variable (set to () at start)
+# filename: filename of the raw disk image
+# id-name: name of the disk in /dev/disk/by-id -> /dev/disk/by-id/ata-disk_$name
+# size: optional file size in MiB (0 implies size is not set)
+# bootindex: optional bootindex number
+#
+# to be used later with `qemu … "${args[@]}" …`
+# The <index> variable will be incremented each time the function is called.
+#
+# can't be easier than this :-/
+#
+# # EXAMPLES
+# ```
+#   declare -a disk_args=()
+#   declare -i disk_index=0
+#   qemu_add_drive_args disk_index disk_args "$TESTDIR"/root.ext3 root 0 1
+#   qemu_add_drive_args disk_index disk_args "$TESTDIR"/client.img client
+#   qemu_add_drive_args disk_index disk_args "$TESTDIR"/iscsidisk2.img iscsidisk2
+#   qemu_add_drive_args disk_index disk_args "$TESTDIR"/iscsidisk3.img iscsidisk3
+#   qemu "${disk_args[@]}"
+# ```
+qemu_add_drive_args() {
+    local index=${!1}
+    local file=$3
+    local name=${4:-$index}
+    local size=${5:-0}
+    local bootindex=$6
+
+    if [ "${size}" -ne 0 ]; then
+        dd if=/dev/zero of="${file}" bs=1MiB count="${size}"
+    fi
+
+    eval "${2}"'+=(' \
+        -drive "if=none,format=raw,file=${file},id=drive-sata${index}" \
+        -device "ide-hd,bus=ide.${index},drive=drive-sata${index},id=sata${index},${bootindex:+bootindex=$bootindex,}model=disk,serial=${name}" \
+        ')'
+
+    # shellcheck disable=SC2219
+    let "${1}++"
+}
+
+test_marker_reset() {
+    dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
+}
+
+test_marker_check() {
+    local marker=${1:-dracut-root-block-success}
+    local file=${2:-marker.img}
+
+    grep -U --binary-files=binary -F -m 1 -q "$marker" "$TESTDIR/$file"
+    return $?
+}
+
 while (($# > 0)); do
     case $1 in
         --run)
-           echo "TEST RUN: $TEST_DESCRIPTION"
-           test_check && test_run
-           exit $?;;
+            check_root
+            echo "TEST RUN: $TEST_DESCRIPTION"
+            test_check && test_run
+            exit $?
+            ;;
         --setup)
-           echo "TEST SETUP: $TEST_DESCRIPTION"
-           test_check && test_setup
-           exit $?;;
+            check_root
+            echo "TEST SETUP: $TEST_DESCRIPTION"
+            test_check && test_setup
+            exit $?
+            ;;
         --clean)
-           echo "TEST CLEANUP: $TEST_DESCRIPTION"
-           test_cleanup
-           rm -fr "$TESTDIR"
-           rm -f .testdir
-           exit $?;;
+            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 ";
-            if ! test_check 2&>test.log ; then
-                echo "[SKIPPED]"
-               exit 0;
+            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 -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[STARTED]" "$COLOR_NORMAL"
+            fi
+            if [[ $V == "1" ]]; then
+                set -o pipefail
+                (
+                    test_setup && test_run
+                    ret=$?
+                    test_cleanup
+                    if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
+                        mv "$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"
+            elif [[ $V == "2" ]]; then
+                set -o pipefail
+                # shellcheck disable=SC2154
+                (
+                    test_setup && test_run
+                    ret=$?
+                    test_cleanup
+                    if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
+                        mv "$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 | "$basedir/logtee" "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
+            else
+                (
+                    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
+            fi
+            ret=$?
+            set +o pipefail
+            if [ $ret -eq 0 ]; then
+                rm -- test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
+                echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[OK]" "$COLOR_NORMAL"
+            else
+                echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_FAILURE" "[FAILED]" "$COLOR_NORMAL"
+                if [ "$V" == "2" ]; then
+                    tail -c 1048576 "$(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
-           (
-               test_setup && test_run
-               ret=$?
-               test_cleanup
-               rm -fr "$TESTDIR"
-               rm -f .testdir
-               exit $ret
-           ) </dev/null >test.log 2>&1
-           ret=$?
-           if [ $ret -eq 0 ]; then
-               rm test.log
-              echo "[OK]"
-           else
-              echo "[FAILED]"
-              echo "see $(pwd)/test.log"
-           fi
-           exit $ret;;
+            exit $ret
+            ;;
         *) break ;;
     esac
     shift