Skip to content

Commit

Permalink
Cherry pick updated test.sh into release/v1.0 branch. (#3453)
Browse files Browse the repository at this point in the history
This fixes the issues in TeamCity due to the -c flag not being present
in the version of test.sh in this branch.
  • Loading branch information
martinmr authored May 21, 2019
1 parent 3c53679 commit f6f9edc
Showing 1 changed file with 172 additions and 33 deletions.
205 changes: 172 additions & 33 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,93 @@
#!/bin/bash
#
# usage: test.sh [-v] [pkg_regex]
# usage: test.sh [pkg_regex]

# Notes for testing under macOS (Sierra and up)
# Required Homebrew (https://brew.sh/) packages:
# - bash
# - curl
# - coreutils
# - gnu-getop
# - findutils
#
# Your $PATH must have all required packages in .bashrc:
# PATH="/usr/local/opt/gnu-getopt/bin:$PATH"
# PATH="/usr/local/opt/curl/bin:$PATH"
# PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
# PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH"
# export PATH
#
# After brew packages and PATHs are set, run tests with:
# /usr/local/bin/bash test.sh
#
# Keep in mind that the test build will overwrite the "dgraph"
# binary in your $GOPATH/bin with the Linux-ELF binary for Docker.

readonly ME=${0##*/}
readonly DGRAPH_ROOT=${GOPATH:-$HOME}/src/github.com/dgraph-io/dgraph

source $DGRAPH_ROOT/contrib/scripts/functions.sh

PATH+=:$DGRAPH_ROOT/contrib/scripts/
GO_TEST_OPTS=( )
TEST_FAILED=0
RUN_ALL=
GO_TEST_OPTS=( "-short=true" )

TMP_DIR=$(mktemp --tmpdir --directory $ME.tmp-XXXXXX)
MATCHING_TESTS=$TMP_DIR/tests
CUSTOM_CLUSTER_TESTS=$TMP_DIR/custom
trap "rm -rf $TMP_DIR" EXIT
TEST_SET="unit"
BUILD_TAGS=

#
# Functions
#

function Usage {
echo "usage: $ME [opts] [pkg_regex]
options:
-h --help output this help message
-u --unit run unit tests only
-c --cluster run unit tests and custom cluster test
-f --full run all tests
--oss run tests with 'oss' tagging
-v --verbose run tests in verbose mode
-n --no-cache re-run test even if previous result is in cache
notes:
Specifying pkg_regex implies -c.
Tests are always run with -short=true."
}

function Info {
echo -e "\e[1;36mINFO: $*\e[0m"
}

function FmtTime {
local secs=$(($1 % 60)) min=$(($1 / 60 % 60)) hrs=$(($1 / 60 / 60))

[[ $hrs -gt 0 ]] && printf "%dh " $hrs
[[ $hrs -gt 0 || $min -gt 0 ]] && printf "%dm " $min
printf "%ds" $secs
}

function IsCi {
[[ ! -z "$TEAMCITY_VERSION" ]]
}

function TestFailed {
TEST_FAILED=1
[[ $CURRENT_TEST ]] && echo $CURRENT_TEST >> $FAILED_TESTS
}

function ListFailedTests {
echo -en "\e[1;31m"
sed 's/^/ /' $FAILED_TESTS
echo -en "\e[0m"
}

function FindCustomClusterTests {
# look for directories containing a docker compose and *_test.go files
touch $CUSTOM_CLUSTER_TESTS
for FILE in $(find -type f -name docker-compose.yml); do
DIR=$(dirname $FILE)
if grep -q $DIR $MATCHING_TESTS && ls $DIR | grep -q "_test.go$"; then
Expand All @@ -35,19 +96,47 @@ function FindCustomClusterTests {
done
}

function FindDefaultClusterTests {
touch $DEFAULT_CLUSTER_TESTS
for PKG in $(grep -v -f $CUSTOM_CLUSTER_TESTS $MATCHING_TESTS); do
echo $PKG >> $DEFAULT_CLUSTER_TESTS
done
}

function Run {
set -o pipefail
echo -en "...\r"
if IsCi; then
go test -v ${GO_TEST_OPTS[*]} $@ | go-test-teamcity
return
fi
go test ${GO_TEST_OPTS[*]} $@ \
| GREP_COLORS='mt=01;32' egrep --line-buffered --color=always '^ok\ .*|$' \
| GREP_COLORS='mt=00;38;5;226' egrep --line-buffered --color=always '^\?\ .*|$' \
| GREP_COLORS='mt=01;31' egrep --line-buffered --color=always '.*FAIL.*|$'
| GREP_COLORS='ne:mt=01;32' egrep --line-buffered --color=always '^ok\ .*|$' \
| GREP_COLORS='ne:mt=00;38;5;226' egrep --line-buffered --color=always '^\?\ .*|$' \
| GREP_COLORS='ne:mt=01;31' egrep --line-buffered --color=always '.*FAIL.*|$'
}

function RunCmd {
CURRENT_TEST=$1
IsCi && echo "##teamcity[testStarted name='$1' captureStandardOutput='true']"
if eval "$@"; then
echo -e "\e[1;32mok $1\e[0m"
IsCi && echo "##teamcity[testFinished name='$1']"
return 0
else
echo -e "\e[1;31mfail $1\e[0m"
IsCi && echo "##teamcity[testFailed name='$1']"
return 1
fi
}

function RunDefaultClusterTests {
for PKG in $(grep -v -f $CUSTOM_CLUSTER_TESTS $MATCHING_TESTS); do
while read -r PKG; do
Info "Running test for $PKG"
Run $PKG || TEST_FAILED=1
done
CURRENT_TEST=$PKG
Run $PKG || TestFailed
done < $DEFAULT_CLUSTER_TESTS
CURRENT_TEST=
return $TEST_FAILED
}

Expand All @@ -58,65 +147,115 @@ function RunCustomClusterTests {
Info "Running tests in directory $DIR"
restartCluster $DIR/docker-compose.yml
pushd $DIR >/dev/null
Run || TEST_FAILED=1
CURRENT_TEST=$DIR
Run || TestFailed
popd >/dev/null
done < $CUSTOM_CLUSTER_TESTS
CURRENT_TEST=
return $TEST_FAILED
}

#
# MAIN
#


ARGS=$(/usr/bin/getopt -n$ME -o"v" -- "$@") || exit 1
ARGS=$(getopt -n$ME -o"hucfvn" -l"help,unit,cluster,full,oss,verbose,no-cache" -- "$@") \
|| exit 1
eval set -- "$ARGS"
while true; do
case "$1" in
-v) GO_TEST_OPTS+=( "-v" ) ;;
--) shift; break ;;
-h|--help) Usage; exit 0 ;;
-u|--unit) TEST_SET="unit" ;;
-c|--cluster) TEST_SET="unit:cluster" ;;
-f|--full) TEST_SET="unit:cluster:full" ;;
-v|--verbose) GO_TEST_OPTS+=( "-v" ) ;;
-n|--no-cache) GO_TEST_OPTS+=( "-count=1" ) ;;
--oss) GO_TEST_OPTS+=( "-tags=oss" ) ;;
--) shift; break ;;
esac
shift
done

cd $DGRAPH_ROOT

# tests should put temp files under this directory for easier cleanup
export TMPDIR=$(mktemp --tmpdir --directory $ME.tmp-XXXXXX)
trap "rm -rf $TMPDIR" EXIT

# docker-compose files may use this to run as user instead of as root
export UID

MATCHING_TESTS=$TMPDIR/tests
CUSTOM_CLUSTER_TESTS=$TMPDIR/custom
DEFAULT_CLUSTER_TESTS=$TMPDIR/default
FAILED_TESTS=$TMPDIR/failures

if [[ $# -eq 0 ]]; then
go list ./... > $MATCHING_TESTS
RUN_ALL=yes
if [[ $TEST_SET == unit ]]; then
Info "Running only unit tests"
GO_TEST_OPTS+=( "-short=true" )
fi
elif [[ $# -eq 1 ]]; then
go list ./... | grep $1 > $MATCHING_TESTS
Info "Running only tests matching $1"
REGEX=${1%/}
go list ./... | grep $REGEX > $MATCHING_TESTS
Info "Running only tests matching '$REGEX'"
RUN_ALL=
else
echo >&2 "usage: $ME [regex]"
echo >&2 "usage: $ME [pkg_regex]"
exit 1
fi

# assemble list of tests before executing any
FindCustomClusterTests
FindDefaultClusterTests

Info "Running tests using the default cluster"
restartCluster
RunDefaultClusterTests || TEST_FAILED=1
# abort all tests on Ctrl-C, not just the current one
trap "echo >&2 SIGINT ; exit 2" SIGINT

Info "Running tests using custom clusters"
RunCustomClusterTests || TEST_FAILED=1
START_TIME=$(date +%s)

if [[ $RUN_ALL ]]; then
Info "Running load-test.sh"
./contrib/scripts/load-test.sh
if [[ :${TEST_SET}: == *:unit:* ]]; then
if [[ -s $DEFAULT_CLUSTER_TESTS ]]; then
Info "Running tests using the default cluster"
restartCluster
RunDefaultClusterTests || TestFailed
else
Info "Skipping default cluster tests because none match"
fi
fi

if [[ :${TEST_SET}: == *:cluster:* ]]; then
if [[ -s $CUSTOM_CLUSTER_TESTS ]]; then
Info "Running tests using custom clusters"
RunCustomClusterTests || TestFailed
else
Info "Skipping custom cluster tests because none match"
fi
fi

if [[ :${TEST_SET}: == *:full:* ]]; then
Info "Running small load test"
RunCmd ./contrib/scripts/load-test.sh || TestFailed

Info "Running custom test scripts"
./contrib/scripts/test-backup-restore.sh
./dgraph/cmd/bulk/systest/test-bulk-schema.sh
RunCmd ./dgraph/cmd/bulk/systest/test-bulk-schema.sh || TestFailed

Info "Running large load test"
RunCmd ./systest/21million/test-21million.sh || TestFailed
fi

Info "Stopping cluster"
stopCluster

END_TIME=$(date +%s)
Info "Tests completed in" $( FmtTime $((END_TIME - START_TIME)) )

if [[ $TEST_FAILED -eq 0 ]]; then
Info "\e[1;32mAll tests passed!"
else
Info "\e[1;31m*** One or more tests failed! ***"
ListFailedTests
fi

exit $TEST_FAILED

0 comments on commit f6f9edc

Please sign in to comment.