#!/bin/bash

# Network interfaces come up and down and can be quite noisy
# and since we are often on the console when running ptests
# let's just quiet things some
dmesg -n 1

# Blacklisted test will be skipped
blacklist=""
# Not applicable
blacklist="$blacklist lxc-test-apparmor"
blacklist="$blacklist lxc-test-apparmor-mount"
#lxc-test-get_item would report Built without AppArmor support error
blacklist="$blacklist lxc-test-get_item"
# This is a broken case, also fails on opensuse, already reported to
# upstream https://github.com/lxc/lxc/issues/4296
blacklist="$blacklist lxc-test-no-new-privs"
# lxc doesn't adapt the criu's new mount flag, that cause restore fails
# already reported to upstream https://github.com/lxc/lxc/issues/4435
blacklist="$blacklist lxc-test-checkpoint-restore"
# if we run "run-ptest" under /usr/lib64/lxc/ptest, the result is normal
# but it would be failed when using "ptest-runner lxc"
blacklist="$blacklist lxc-test-usernic"

# Tests in firstrunlist will be run first
firstrunlist=""
firstrunlist="$firstrunlist lxc-test-unpriv"

passed=0
failed=0
skipped=0

# Create logs dir and clear old logs if any
mkdir logs 2> /dev/null
rm -f logs/*

echo "### Starting LXC ptest ###"

for test in $firstrunlist
do
	./tests/$test >logs/$(basename $test).log 2>&1
	if [ $? -eq 0 ]
	then
		echo "PASS: $(basename $test)"
		passed=$((passed+1))
	else
		echo "FAIL: $(basename $test)"
		failed=$((failed+1))
	fi
done

for test in ./tests/*
do
    if [[ ! $blacklist = *$(basename $test)*  ]]
    then
	if [[ ! $firstrunlist = *$(basename $test)* ]]
	then
		$test >logs/$(basename $test).log 2>&1
	else
		continue;
	fi
    else
	echo "SKIPPED: $(basename $test)"
	skipped=$((skipped+1))
	continue
    fi

    if [ $? -eq 0 ]
    then
	echo "PASS: $(basename $test)"
	passed=$((passed+1))
    else
	echo "FAIL: $(basename $test)"
	failed=$((failed+1))
    fi
done


echo ""
echo "Results:"
echo "    PASSED = $passed"
echo "    FAILED = $failed"
echo "    SKIPPED = $skipped"
echo "(for details check individual test log in ./logs directory)"
echo ""
echo "### LXC ptest complete ###"

# restore dmesg to console
dmesg -n 6
