#!/bin/sh

set -e

EXCLUDE_FILE=/etc/tempest/exclude.conf
TMP_EXCLUDE=`mktemp -t nose-exclude-XXXXXX`
if [ -r "${EXCLUDE_FILE}" ] ; then
	for i in `grep -v -e '^#' -e '^[ \t]*$' ${EXCLUDE_FILE}` ; do
		if [ -n "${A}" ] ; then
			A="${A}|${i}"
		else
			A="${i}"
		fi
	done
fi

cd /var/lib/tempest

if [ ! -d .stestr ] ; then
	PYTHON=python3 stestr init
fi

# Use stestr's experimental dynamic scheduler when the installed stestr
# supports it: workers pull test ids from a shared queue so they stay
# busy until every test has been handed out, instead of the static
# pre-partitioning of the default --parallel mode.
if stestr run --help | grep -q dynamic ; then
	MODE=--dynamic
else
	MODE=--parallel
fi

if [ -z "${1}" ] && [ -n ${A} ] ; then
	PYTHON=python3 stestr run ${MODE} --concurrency=$(nproc) --subunit '^(?!('"${A}"'))' $@ | subunit-trace
else
	PYTHON=python3 stestr run ${MODE} --concurrency=$(nproc) --subunit $@ | subunit-trace
fi

#testr run --parallel --subunit $@ | subunit-trace
