]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
testsuite: add test environment script
authorIvana Hutarova Varekova <varekova@redhat.com>
Mon, 7 Mar 2011 09:48:19 +0000 (10:48 +0100)
committerJan Safranek <jsafrane@redhat.com>
Mon, 7 Mar 2011 09:48:30 +0000 (10:48 +0100)
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 <varekova@redhat.com>
Signed-off-by: Jan Safranek <jsafrane@redhat.com>
configure.in
tests/tools/testenv.sh.in [new file with mode: 0644]

index bea714a1aaf52d1dd0513935cbdf25c71f9edaa1..d7ad70e73a0b0de5ad282ff79ac90c3681a3e941 100644 (file)
@@ -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 (file)
index 0000000..464a440
--- /dev/null
@@ -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
+# <do some testing>
+# 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 </proc/mounts >$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"