#!/bin/bash

# This script is normally executed by ./test in the topdir source directory.
# In this case, $GOPATH is already set.
#
# However, when developing a new functional test, it is useful to run the
# tests manually without recompiling rkt. GOPATH is set to the same directory
# as ./build in the topdir source directory does.
#
# To run all tests:
#     $ cd tests && ./test
#
# To run only one test:
#     $ cd tests && ./test -run=TestEnv

# Sudo does not preserve $PATH. Use go from the user's $PATH instead of
# root's $PATH. It is needed when a home-built go does not have the same
# version as the system-wide installed go.
GO=`which go`

HOST_RUNNING_SYSTEMD=0
if [ $(basename $(sudo readlink /proc/1/exe)) == systemd ] ; then
	HOST_RUNNING_SYSTEMD=1
fi

STAGE1_SRC_FROM_HOST=0
if tar xOvf ../bin/stage1.aci manifest|grep -q usr-from-host ; then
	STAGE1_SRC_FROM_HOST=1
fi

if [ $HOST_RUNNING_SYSTEMD -eq 0 -a $STAGE1_SRC_FROM_HOST -eq 1 ] ; then
	echo "Cannot use stage1 compiled with usr-from-host because systemd is not installed."
	echo "Functional tests disabled."
	exit 0
fi

sudo GOPATH="${PWD}/../gopath" GOROOT="$GOROOT" $GO test -v $*
