#!/bin/sh
#
# Copyright (c) 2025 Mark Johnston <markj@FreeBSD.org>
#
# SPDX-License-Identifier: BSD-2-Clause
#

usage()
{
    cat <<__EOF__ >&2
usage: run-kyua [-c <count>] [-d <disks>] [-j <parallelism>] [-r <report DB path>]
                [<test path> [<test path> ...]]

Run the tests under <test path>, relative to /usr/tests, or if no test paths
are specified, run the entire regression test suite.

This is a helper script copied into VM images by bricoler in order to run the
test suite.

Options:
-c <count>         Run the test suite <count> times, stop if any failures occur
                   default is 1.
-d <disks>         Provide the specified space-separated block devices as
                   scratch space for tests which want them.
-j <parallelism>   Run up to <parallelism> tests in parallel, default is the
                   number of CPUs in the system.
-o <report path>   Write a textual report to the specified file, default is
                   ./kyua-report.txt.
-r <report DB path> Store the test results in the specified report DB, default
                   is ./kyua.db.
__EOF__
}

COUNT=1
DISK_LIST=" "
PARALLELISM=$(sysctl -n hw.ncpu)
REPORT_DB=./kyua.db
REPORT_LOG=./kyua-report.txt

while getopts "c:d:f:j:o:r:" opt; do
    case $opt in
    c)
        COUNT=$OPTARG
        ;;
    d)
        DISK_LIST=$OPTARG
        ;;
    f)
        RESULTS_FILTER="--results-filter $OPTARG"
        ;;
    j)
        PARALLELISM=$OPTARG
        ;;
    o)
        REPORT_LOG=$OPTARG
        ;;
    r)
        REPORT_DB=$OPTARG
        ;;
    *)
        usage
        exit 1
        ;;
    esac
done
shift $((OPTIND - 1))
TEST_PATH=$@

FIBS=$(seq 0 $(($(sysctl -n net.fibs) - 1)))

for i in $(jot $COUNT); do
    # kyua refuses to overwrite its output database.
    rm -f $REPORT_DB

    time kyua -v parallelism=$PARALLELISM -v execplan=time \
        -v test_suites.FreeBSD.unprivileged_user=tests \
        -v test_suites.FreeBSD.allow_sysctl_side_effects=1 \
        -v test_suites.FreeBSD.fibs="$FIBS" \
        -v test_suites.FreeBSD.disks="$DISK_LIST" \
        test -k /usr/tests/Kyuafile -r $REPORT_DB $TEST_PATH
    status=$?

    kyua report --verbose \
        $RESULTS_FILTER \
        --output=$REPORT_LOG -r $REPORT_DB

    if [ $status -ne 0 ]; then
        exit $?
    fi
done

exit 0
