Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 216 lines (173 sloc) 5.71 KB
#!/bin/bash
set -e
ARGS=$@
PROG=$(basename $0)
IMAGE_ID=
LOOP=
LIVE=
LOCAL=1
TARGET_HOST=
TARGET_PORT=8080
NO_SSL_CHECK=
RUNPATH=
usage() {
cat << EOF
USAGE: $0 [--debug] [--loop] [--live|--image <image-id>|--host X --port Y] <tests_path>
Run acceptance tests against a local server, a local docker container or a
remote live server. If not arguments are provided, execute all test files under
the local directory 'testaccept/' against the host and port set in the
environment variables PYM_SERVER_HOST and PYM_SERVER_PORT.
OPTIONS:
--image <image-id> ID of a docker image to start and test against.
--live Run tests against the server pointed at by 'eb status'.
--host <host> Run tests against a specific IP address or hostname.
--port <port> Run tests against a specific TCP port.
--loop Run the tests in a loop, until failure
--no-ssl-check Don't check SSL certificate
USAGE:
# Run all test files matching testaccept/test_v1_*
# against api running at 127.0.0.1:8080
export PYM_SERVER_HOST=127.0.0.1
export PYM_SERVER_PORT=8080
pymtest testaccept/test_v1_*
# Run tests against the live api whose hostname
# is specified in pym-config.yaml
pymtest --live testaccept/test_v1_*
# Start the given pymacaron image in a local container and run
# tests against it
pymtest --image c7f755f84b68 testaccept/test_v1_*
# Run tests against a given host and port
pymtest --host api.foobar.com --port 443 testaccept/test_v1_*
EOF
}
parse_args() {
while [ "$1" != "" ]; do
case $1 in
"--debug") set -x;;
"--live") export LIVE=1; export LOCAL=;;
"--image") shift; IMAGE_ID=$1; export LOCAL=;;
"--loop") export LOOP=1;;
"--host") shift; TARGET_HOST=$1;;
"--port") shift; TARGET_PORT=$1;;
"--no-ssl-check") export NO_SSL_CHECK=1;;
"-h" | "--help") usage; exit 0;;
*) RUNPATH="$RUNPATH $1";;
esac
shift
done
}
parse_args $ARGS
# Force re-generation of recent auth token
unset PYM_JWT_TOKEN
set_runpath() {
if [ -z "$RUNPATH" ]; then
RUNPATH="testaccept/"
HERE=$PWD
# Let's append all symlinked dirs's testaccept/ to RUNPATH
for DIR in $(pymconfig --include-links)
do
cd $DIR
ROOTDIR=$(git rev-parse --show-toplevel)
if [ -d "$ROOTDIR/testaccept" ]; then
echo "=> Appending tests from $ROOTDIR"
RUNPATH="$RUNPATH $ROOTDIR/testaccept"
fi
cd $HERE
done
echo "=> Will execute tests $RUNPATH"
fi
}
# Make sure we can connect to aws
if [ -z $AWS_SECRET_ACCESS_KEY ]; then
SECRETS_FILE=~/.env.secrets
if [ ! -e "$SECRETS_FILE" ]; then
echo "ERROR: no secrets file at $SECRETS_FILE"
else
source $SECRETS_FILE
fi
fi
if [ ! -z "$LIVE" ]; then
echo "=> Finding out CNAME of current Elastic Beanstalk environment"
PYM_SERVER_HOST=$(pymconfig --live-host)
HAS_CERT=$(pymconfig --aws-cert-arn)
if [ -z "$HAS_CERT" ]; then
PYM_SERVER_PORT=80
else
PYM_SERVER_PORT=443
fi
set_runpath
elif [ ! -z "$LOCAL" ]; then
echo "=> Running against $PYM_SERVER_HOST:$PYM_SERVER_PORT"
set_runpath
# TODO: check that server is indeed running locally. If not, start it
elif [ ! -z "$TARGET_HOST" ]; then
echo "=> Running against $TARGET_HOST:$TARGET_PORT"
PYM_SERVER_HOST=$TARGET_HOST
PYM_SERVER_PORT=$TARGET_PORT
set_runpath
elif [ ! -z "$IMAGE_ID" ]; then
set_runpath
# Start the container, passing all secrets as environment variables,
# just like in the amazon EBS setup
CMD="docker run"
for VAR in $(pymconfig --env-secrets)
do
echo "=> Passing env variable $VAR"
VALUE=$(env | grep "^$VAR=" | cut -d '=' -f 2)
if [ -z "$VALUE" ]; then
echo "ERROR: variable $VAR has no value in env"
exit 1
fi
CMD="$CMD -e $VAR='$VALUE'"
done
CMD="$CMD --rm -P $IMAGE_ID"
echo "=> Starting container for image $IMAGE_ID"
echo "EXEC [$CMD]"
eval $CMD &
RC=$?
if [ $RC != 0 ]; then
echo "ERROR: failed to start docker container"
exit 1
fi
echo "=> Waiting for container to start"
sleep 7
CONTAINER_ID=$(docker ps | grep $IMAGE_ID | awk '{ print $1 }')
if [ "$OS" == 'Darwin' ]; then
PYM_SERVER_HOST=$(docker-machine ip default)
# Make sure docker-machine's vm has a working dns resolver
docker-machine ssh default "echo 'nameserver 8.8.8.8' > /etc/resolv.conf"
PYM_SERVER_PORT=$(docker port $CONTAINER_ID | cut -d ':' -f 2)
else
PYM_SERVER_HOST=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${CONTAINER_ID})
PYM_SERVER_PORT=80
fi
if [ -z "$PYM_SERVER_PORT" ]; then
echo "ERROR: cannot find port exposed by container."
echo " did the container crash?"
exit 1
fi
fi
if [ -z "$PYM_JWT_TOKEN" ]; then
echo "=> Getting an access token for the test user"
export PYM_JWT_TOKEN=$(get_test_jwt_token | grep 'token:' | cut -d ':' -f 2)
echo "=> Using token: $PYM_JWT_TOKEN"
if [ -z "$PYM_JWT_TOKEN" ]; then
echo "ERROR: failed to get an auth token for test user"
fi
fi
echo "=> Running acceptance tests against $PYM_SERVER_HOST:$PYM_SERVER_PORT"
RC=0
if [ -z "$LOOP" ]; then
nosetests -xv $RUNPATH || RC=$?
else
while true; do
nosetests -xv $RUNPATH || RC=$?
done
fi
if [ ! -z "$CONTAINER_ID" ]; then
echo "=> Stopping container $CONTAINER_ID"
docker kill $CONTAINER_ID
fi
if [ "$RC" -ne 0 ]; then
exit $RC
fi
You can’t perform that action at this time.