diff --git a/Makefile b/Makefile index 669dea1a4..c43d83cb2 100644 --- a/Makefile +++ b/Makefile @@ -201,6 +201,7 @@ endif #### May be used to force running a specific set of reg-tests REG_TEST_FILES = REG_TEST_SCRIPT=./scripts/run-regtests.sh +UNIT_TEST_SCRIPT=./scripts/run-unittests.sh #### Standard C definition # Compiler-specific flags that may be used to set the standard behavior we @@ -1023,7 +1024,7 @@ help: # TARGET variable is not set since we're not building, by definition. IGNORE_OPTS=help install install-man install-doc install-bin \ uninstall clean tags cscope tar git-tar version update-version \ - opts reg-tests reg-tests-help admin/halog/halog dev/flags/flags \ + opts reg-tests reg-tests-help unit-tests admin/halog/halog dev/flags/flags \ dev/haring/haring dev/ncpu/ncpu dev/poll/poll dev/tcploop/tcploop \ dev/term_events/term_events @@ -1267,6 +1268,11 @@ reg-tests-help: .PHONY: reg-tests reg-tests-help +unit-tests: + $(Q)$(UNIT_TEST_SCRIPT) +.PHONY: unit-tests + + # "make range" iteratively builds using "make all" and the exact same build # options for all commits within RANGE. RANGE may be either a git range # such as ref1..ref2 or a single commit, in which case all commits from diff --git a/scripts/run-unittests.sh b/scripts/run-unittests.sh new file mode 100755 index 000000000..4e06b0eff --- /dev/null +++ b/scripts/run-unittests.sh @@ -0,0 +1,86 @@ +#!/bin/sh + +export HAPROXY_PROGRAM="${HAPROXY_PROGRAM:-${PWD}/haproxy}" +export HAPROXY_ARGS="${HAPROXY_ARGS--dM -dI -dW}" +export ROOTDIR="${ROOTDIR:-${PWD}}" +export TESTDIR="${TESTDIR:-./tests/unit/}" + +result=0 + +echo "" +echo "########################## Preparing to run unit tests ##########################" + +preparefailed= +if ! [ -x "$(command -v $HAPROXY_PROGRAM)" ]; then + echo "haproxy not found in path, please specify HAPROXY_PROGRAM environment variable" + preparefailed=1 +fi +if [ $preparefailed ]; then + exit 1 +fi + +{ read HAPROXY_VERSION; read TARGET; read FEATURES; read SERVICES; } << EOF +$($HAPROXY_PROGRAM $HAPROXY_ARGS -vv | grep -E 'HA-?Proxy version|TARGET.*=|^Feature|^Available services' | sed 's/.* [:=] //') +EOF + +UNITTESTS=$($HAPROXY_PROGRAM $HAPROXY_ARGS -vv|grep -E '^Unit tests list' | sed 's/.* [:=] //') +if [ -z "$UNITTESTS" ]; then + UNITTESTS="none" +fi + +HAPROXY_VERSION=$(echo $HAPROXY_VERSION | cut -d " " -f 3) +echo "Testing with haproxy version: $HAPROXY_VERSION" + +PROJECT_VERSION=$(${MAKE:-make} version 2>&1 | grep -E '^VERSION:|^SUBVERS:'|cut -f2 -d' '|tr -d '\012') +if [ -z "${PROJECT_VERSION}${MAKE}" ]; then + # try again with gmake, just in case + PROJECT_VERSION=$(gmake version 2>&1 | grep -E '^VERSION:|^SUBVERS:'|cut -f2 -d' '|tr -d '\012') +fi +FEATURES_PATTERN=" $FEATURES " +SERVICES_PATTERN=" $SERVICES " + +echo "Target : $TARGET" +echo "Options : $FEATURES" +echo "Services : $SERVICES" +echo "Unit tests: $UNITTESTS" + +succeed=0 +failed=0 +skipped=0 +testlist= + +echo "########################## Gathering tests to run ##########################" + +for test in $(find "$TESTDIR" -name "*.sh"); do + sh ${test} check + r="$?" + if [ "$r" = "0" ]; then + echo " Add test: $test" + testlist="$testlist $test" + else + skipped=$((skipped+1)) + echo " Skip $test" + fi +done + +echo "########################## Starting unit tests ##########################" + +for TEST in $testlist; do +# echo "*** run ${TEST}" + export TEST + export TESTDIR=`dirname ${TEST}` + + sh -e ${TEST} run + r="$?" +# echo "*** result ${TEST}: $r" + if [ "$r" != "0" ]; then + result=$r + failed=$((failed+1)) + else + succeed=$((succeed+1)) + fi +done + +echo "${failed} tests failed, ${skipped} tests skipped, ${succeed} tests passed" + +exit $result