#!/bin/bash
# Run the vagrant tests

# Perforce version being tested
P4VER=15.2

oses=""

# Allow the specific platform to be specified via parameter
if [ ,"$1" = ,"ubuntu" ] ; then
	oses="ubuntu"
elif [ ,"$1" = ,"centos" ] ; then
	oses="centos"
else
	oses="ubuntu centos"
fi

all_test_output=./sdp/alltests.out
if [ -f $all_test_output ] ; then
	rm $all_test_output
fi

# Make sure that p4 binaries are present and correct
origdir=`pwd`
cd ./sdp/Server/Unix/p4/common/bin
if [ ! -f p4 ]; then
	wget -nv ftp.perforce.com/perforce/r$P4VER/bin.linux26x86_64/p4
fi
if [ ! -f p4d ]; then
	wget -nv ftp.perforce.com/perforce/r$P4VER/bin.linux26x86_64/p4d
fi
cd $origdir

echo Running SDP tests
tests_failed=0
for os in $oses
do
	dockerfilebase=sdp/test/docker/Dockerfile.${os}.base
	dockerfilesdp=sdp/test/docker/Dockerfile.${os}.sdp
	docker build --rm=true -t=${os}-base -f ${dockerfilebase} .
	docker build --rm=true -t=${os}-sdp -f ${dockerfilesdp} .
	docker run --rm  -v ${origdir}/sdp:/sdp ${os}-sdp /p4/docker_entry.sh 
	echo $os>> $all_test_output
	test_output="test-$os.out"
	# Avoid Jenkins immediately failing job without letting us cat the output
	set +e
	cat ./sdp/$test_output>> $all_test_output
	set -e
	if [ $tests_failed -ne 0 ]; then
		break
	fi 
done
cat $all_test_output
exit $tests_failed
