#!/bin/sh
#
# This software is a part of ISAR.
# Copyright (C) 2015-2018 ilbers GmbH

CONSOLE_OUTPUT=/tmp/isar_console
PID_FILE=/tmp/qemu.pid
VERBOSE=1
TIMEOUT=300

# Error codes:
ES_OK=0
ES_FAIL=1
ES_BUG=3

RET=$ES_OK

dump_boot_log() {
    echo "Boot log:\n8<--"
    cat $CONSOLE_OUTPUT
    echo "\n8<--"
}

check_login_prompt() {
    echo -n "Check login prompt: "

    str=$(grep "isar login: " $CONSOLE_OUTPUT)

    if [ -n "$str" ]; then
        echo "PASSED"
    else
        echo "FAIL"
        RET=$ES_FAIL
        FAIL=1
    fi
}

check_example_module() {
    echo -n "Check example module: "

    str=$(grep "Just an example" $CONSOLE_OUTPUT)

    if [ -n "$str" ]; then
        echo "PASSED"
    else
        echo "FAIL"
        RET=$ES_FAIL
        FAIL=1
    fi
}

run_test () {
    ARCH=$1
    DISTRO=$2

    echo "-------------------------------------------------"
    echo "Testing Isar [$DISTRO] image for [$ARCH] machine:"

    start_vm -a $ARCH -d $DISTRO -o $CONSOLE_OUTPUT -p $PID_FILE > /dev/null 2>&1 &
    sleep 5

    if [ -z `ps -p $! -o pid=` ]; then
        echo "QEMU start: FAILED"
        RET=$ES_FAIL
        echo "Command output:\n8<--"
        start_vm -a $ARCH -d $DISTRO -o $CONSOLE_OUTPUT -p $PID_FILE
        echo "\n8<--"
    else
        sleep $TIMEOUT
        kill `cat $PID_FILE`

        FAIL=0

        check_login_prompt

        check_example_module

        [ $VERBOSE -eq 1 -o $FAIL -eq 1 ] && dump_boot_log

        rm $CONSOLE_OUTPUT
    fi

    rm $PID_FILE
}

show_help() {
    echo "This script tests the Isar images for default targets in QEMU."
    echo
    echo "Usage:"
    echo "    $0 [params]"
    echo
    echo "Parameters:"
    echo "    -f,--fast             test reduced set of supported targets."
    echo "    -o,--output FILE      specify file to store console output."
    echo "                          The default is: /tmp/isar_console"
    echo "    -p,--pid-file FILE    specify file to store QEMU process PID."
    echo "                          The default is: /tmp/qemu.pid"
    echo "    -q, --quiet           do not display boot logs for all the targets."
    echo "                          If test failed for the specific configuration,"
    echo "                          the respective boot log will be printed anyway."
    echo "    -t,--timeout SEC      specify time in seconds to wait before stop QEMU."
    echo "                          The default is: 300"
    echo "    -h, --help            display this message and exit."
    echo
    echo "Exit status:"
    echo " 0  if OK,"
    echo " 1  if test failed,"
    echo " 3  if invalid parameters are passed."
}

# Parse command line to get user configuration
while [ $# -gt 0 ]
do
    key="$1"

    case $key in
    -h|--help)
        show_help
        exit 0
        ;;
    -o|--output)
        CONSOLE_OUTPUT=$2
        shift
        ;;
    -p|--pid-file)
        PID_FILE=$2
        shift
        ;;
    -f|--fast)
        FAST_BUILD="1"
        ;;
    -q|--quiet)
        VERBOSE=0
        ;;
    -t|--timeout)
        TIMEOUT=$2
        shift
        ;;
    *)
        echo "error: invalid parameter '$key', please try '--help' to get list of supported parameters"
        exit $ES_BUG
        ;;
    esac

    shift
done

# ARM machine
run_test arm stretch
run_test arm buster

# AMD64 machine
if [ -z "$FAST_BUILD" ]; then
    run_test amd64 buster
    run_test amd64 focal
fi
run_test amd64 stretch

# i386 machine
if [ -z "$FAST_BUILD" ]; then
    run_test i386 stretch
    run_test i386 buster
fi

# ARM64 machine
run_test arm64 stretch

exit $RET
