TESTS: add a unit test runner in the Makefile

`make unit-tests` would run shell scripts from tests/unit/

The run-unittests.sh script will look for any .sh in tests/unit/ and
will call it twice:

- first with the 'check' argument in order to decide if we should skip
  the test or not
- second to run the check

A simple test could be written this way:

	#!/bin/sh

	check() {
	       ${HAPROXY_PROGRAM} -cc 'feature(OPENSSL)'
	       command -v socat
	}

	run() {
		 ${HAPROXY_PROGRAM} -dI -f ${ROOTDIR}/examples/quick-test.cfg -c
	}

	case "$1" in
	       "check")
	               check
	       ;;
	       "run")
	               run
	       ;;
	esac

The tests *MUST* be written in POSIX shell in order to be portable, and
any special commands should be tested with `command -v` before using it.

Tests are run with `sh -e` so everything must be tested.
This commit is contained in:
William Lallemand 2025-03-01 17:22:45 +01:00
parent a647839954
commit 1e7478bb4e
2 changed files with 93 additions and 1 deletions

View File

@ -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

86
scripts/run-unittests.sh Executable file
View File

@ -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