From: Ivana Hutarova Varekova Date: Mon, 7 Mar 2011 09:48:19 +0000 (+0100) Subject: testsuite: add test environment script X-Git-Tag: v0.38~127 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6924b98e27c54b5b43d54bd12da836e56d6a3576;p=thirdparty%2Flibcgroup.git testsuite: add test environment script changelog: * test moved to separate subdirectory tools changelog v2: * set the directory in makefile * add CONFIGDIR variable This patch create general functions and variables (made by Jan Safranek): functions: die: # Print an error message and exit # Usage: # cgclear || die "cgclear failed" cleanup() # Clear everything that was created at loading this script, i.e. # remove the temporary directory # Usage: # cleanup prepare_config # Copies a file to $TMP and replaces all occurrences of TMP in the file with # value of $TMP. The function prints the name of the new file to its std. # output. # # Usage: # cgconfigparser -l `prepare_config config/sample.conf` variables: TMP # unique temporary dir TOOLSDIR # tools directory CONFIGDIR # config files directory Signed-off-by: Ivana Hutarova Varekova Signed-off-by: Jan Safranek --- diff --git a/configure.in b/configure.in index bea714a1..d7ad70e7 100644 --- a/configure.in +++ b/configure.in @@ -189,6 +189,7 @@ fi AC_CONFIG_FILES([Makefile tests/Makefile + tests/tools/testenv.sh src/Makefile src/daemon/Makefile src/tools/Makefile diff --git a/tests/tools/testenv.sh.in b/tests/tools/testenv.sh.in new file mode 100644 index 00000000..464a440e --- /dev/null +++ b/tests/tools/testenv.sh.in @@ -0,0 +1,74 @@ +#!/bin/bash + +# Simple test framework for libcgroup. +# Upon loading it does following: +# - sets $TMP, where all temporary files should be stored. +# - backs-up /etc/cgrules.conf in the $TMP +# (so it does not interfere with tests) +# +# Function cleanup() or die() must be called to destroy this temporary +# directory and restore previous cgrules.conf! +# +# Usage: +# . ./testenv.sh +# +# cleanup +# exit 0 + +# Print an error message and exit +# Usage: +# cgclear || die "cgclear failed" +function die() +{ + echo $* + cgclear + mv $TMP/cgrules-backup.conf /etc/cgrules.conf &>/dev/null + rm -rf $TMP + unset TMP + exit 1 +} + +# Clear everything that was created at loading this script, i.e. +# remove the temporary directory +# In addition, check that nothing remained mounted. +# Usage: +# cleanup +function cleanup() +{ + RET=0 + grep -v systemd $TMP/mounts + if grep "^cgroup" $TMP/mounts &>/dev/null; then + echo "Error: cleanup has found mounted cgroup:" + grep cgroup /proc/mounts + RET=1 + cgclear + fi + mv $TMP/cgrules-backup.conf /etc/cgrules.conf &>/dev/null + rm -rf $TMP + unset TMP + [ -z $RET ] || exit $RET +} + +# Copies a file to $TMP and replaces all occurrences of TMP in the file with +# value of $TMP. The function prints the name of the new file to its std. +# output. +# Usage: +# cgconfigparser -l `prepare_config config/sample.conf` +function prepare_config() +{ + INPUT=$1 + OUTPUT=$TMP/`basename $INPUT` + # echo -n "m4_changequote()" | m4 -P -D "TMP=$TMP" - $FILE >$OUTPUT + sed -e "s!\bTMP\b!$TMP!g" <$INPUT >$OUTPUT || ( + echo "Error: prepare config failed!" >&2; exit 1 + ) + echo $OUTPUT +} + +export TMP=`mktemp --tmpdir -d libcgroup-tmp-XXXXXXX` +# store the rules on safe place, we don't want them to mess up the tests +mv /etc/cgrules.conf $TMP/cgrules-backup.conf &>/dev/null +# tools directory +export TOOLSDIR="@abs_top_srcdir@/libtool --mode=execute @abs_top_srcdir@/src/tools/" +# config files directory +export CONFIGDIR="@abs_top_srcdir@/samples"