#!/bin/bash

# The whole test suite may take up to 40 minutes to run, so setting -t 2400
# parameter in ptest-runner is necessary to not kill it before completion

cd tests
export BPFTRACE_RUNTIME_TEST_EXECUTABLE=/usr/bin

PASS_CNT=0
FAIL_CNT=0
SKIP_CNT=0
FAILED=()

# Start unit tests
for test_case in $(./bpftrace_test --gtest_list_tests | grep -v "^ "); do
    if ./bpftrace_test --gtest_filter="${test_case}*" > /dev/null 2>&1 ; then
        echo PASS: Unit test $test_case
        PASS_CNT=$(($PASS_CNT + 1))
    else
        echo FAIL: Unit test $test_case
        FAIL_CNT=$(($FAIL_CNT + 1))
        FAILED+=("unit:${test_case}")
    fi
done

# Start runtime tests
for test_case in $(ls runtime); do
    # Ignore test cases that hang the suite forever (bpftrace v0.16.0)
    case $test_case in
        sigint)
            ;&
        watchpoint)
            echo SKIP: Runtime test $test_case
            SKIP_CNT=$(($SKIP_CNT + 1))
            continue
            ;;
    esac
    if ./runtime-tests.sh --filter="${test_case}.*" > /dev/null 2>&1 ; then
        echo PASS: Runtime test $test_case
        PASS_CNT=$(($PASS_CNT + 1))
    else
        echo FAIL: Runtime test $test_case
        FAIL_CNT=$(($FAIL_CNT + 1))
        FAILED+=("runtime:${test_case}")
    fi
done

echo "#### bpftrace tests summary ####"
echo "# TOTAL: $(($PASS_CNT + $FAIL_CNT + $SKIP_CNT))"
echo "# PASS:  $PASS_CNT"
echo "# FAIL:  $FAIL_CNT (${FAILED[*]})"
echo "# SKIP:  $SKIP_CNT"
echo "################################"
