git » libjio » commit b02aff4

tests/util: Rewrite the scripts in Python

author Alberto Bertogli
2011-02-26 20:33:48 UTC
committer Alberto Bertogli
2011-02-26 20:33:48 UTC
parent 9075b326ff2699f01aef95a758aa418a2557e14f

tests/util: Rewrite the scripts in Python

The old scripts had issues when ran from directories with spaces on them,
among other bugs and portability problems.

This commit rewrites the scripts in Python to make them more robust and
portable. There is no user-facing changes when invoked via make.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

Makefile +3 -3
tests/util/build_lib_env.sh +0 -26
tests/util/quick-test-run +72 -0
tests/util/quick-test-run.sh +0 -62
tests/util/wrap-python +47 -25

diff --git a/Makefile b/Makefile
index 2a02f2d..96b28c5 100644
--- a/Makefile
+++ b/Makefile
@@ -31,14 +31,14 @@ preload_install: preload
 	$(MAKE) -C bindings/preload/ install
 
 tests: all python2 python3
-	tests/util/quick-test-run.sh normal
+	tests/util/quick-test-run normal
 
 tests-fi: all python2 python3
 	@if [ "$(FI)" != "1" ]; then \
 		echo "Error: $@ has to be run using:  make FI=1 $@"; \
 		exit 1; \
 	fi
-	tests/util/quick-test-run.sh fiu
+	tests/util/quick-test-run fiu
 
 clean:
 	$(MAKE) -C libjio/ clean
@@ -49,5 +49,5 @@ clean:
 .PHONY: default all libjio install \
 	python2 python2_install python3 python3_install \
 	preload preload_install \
-	tests tests-fiu clean
+	tests tests-fi clean
 
diff --git a/tests/util/build_lib_env.sh b/tests/util/build_lib_env.sh
deleted file mode 100755
index ffa870c..0000000
--- a/tests/util/build_lib_env.sh
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/usr/bin/env bash
-
-# Apply the environment changes needed to use the built (and not the
-# installed) versions of the library and Python bindings.
-#
-# They are useful for running tests before installing.
-#
-# Is intended use is to be sourced in the shell, for example:
-#
-#   $ source build_lib_env.sh
-#   $ ./some-binary-using-the-library
-#
-# Although the most common users will be the other test scripts.
-
-# We assume we're in the tests/util directory of the libjio source tree.
-OURDIR=$(readlink -f $(dirname $0))
-
-LIBBIN=$(readlink -f "$OURDIR"/../../libjio/build/libjio.so)
-
-if ! [ -x "$LIBBIN" ]; then
-	echo "Can't find library (run make)"
-	exit 1
-fi
-
-export "LD_LIBRARY_PATH=$(dirname $LIBBIN):$LD_LIBRARY_PATH"
-
diff --git a/tests/util/quick-test-run b/tests/util/quick-test-run
new file mode 100755
index 0000000..87061d5
--- /dev/null
+++ b/tests/util/quick-test-run
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+
+# This is a convenience script for running some of the other tests without
+# manual intervention, as a means of a fast and easy correctness test.
+#
+# If you are making an intrusive or risky change, please take the time to run
+# the other tests by hand with more intensive parameters, and check the
+# coverage and use the other tools mentioned in the README.
+
+import sys
+import os
+import subprocess
+import random
+
+
+# Go to our directory, which we will use to find the other tools
+os.chdir(os.path.dirname(sys.argv[0]))
+
+if len(sys.argv) != 2 or sys.argv[1] not in ("normal", "fiu"):
+	sys.stderr.write("Usage: %s [normal|fiu]" %
+				os.path.basename(sys.argv[0]))
+	sys.exit(1)
+
+def run_behaviour_tests(test_type):
+	ret = subprocess.call(["./wrap-python", "2", "../behaviour/runtests",
+				test_type])
+	if ret != 0:
+		sys.exit(ret)
+
+def run_stress_tests(nops = 0, nprocs = 0, fi = False, fsize = 20):
+	# Create a temporary path. We can't use os.tempnam() because it emits
+	# a warning about a security risk, although it is safe for us because
+	# of how jiostress opens the file.
+	tmp_path = "%s/libjio-tests-%d-%d" % ( \
+			os.environ.get("TMPDIR", "/tmp"),
+			os.getpid(),
+			random.randint(0, 1000000000))
+
+	args = ["./wrap-python", "3", "../stress/jiostress",
+			tmp_path, str(fsize)]
+	if nops:
+		args += ["-n", str(nops)]
+	if nprocs:
+		args += ["-p", str(nprocs)]
+	if fi:
+		args += ["--fi"]
+
+	ret = subprocess.call(args)
+	if ret != 0:
+		sys.exit(ret)
+
+if sys.argv[1] == "normal":
+	print "behaviour tests (normal)"
+	run_behaviour_tests("normal")
+	print
+	print "stress tests (normal)"
+	run_stress_tests(nops = 50, nprocs = 3)
+else:
+	print "behaviour tests (all)"
+	run_behaviour_tests("all")
+	print
+	print "stress tests (normal)"
+	run_stress_tests(nops = 50, nprocs = 3)
+	print
+	print "stress tests (fiu)"
+	run_stress_tests(nops = 400, fi = True)
+
+print
+print
+print "Tests completed successfuly"
+print
+
diff --git a/tests/util/quick-test-run.sh b/tests/util/quick-test-run.sh
deleted file mode 100755
index 9f7ffd0..0000000
--- a/tests/util/quick-test-run.sh
+++ /dev/null
@@ -1,62 +0,0 @@
-#!/usr/bin/env bash
-
-# This is a convenience script for running some of the other tests without
-# manual intervention, as a means of a fast and easy correctness test.
-#
-# If you are making an intrusive or risky change, please take the time to run
-# the other tests by hand with more intensive parameters, and check the
-# coverage and use the other tools mentioned in the README.
-
-# Find our directory, which we will use to find the other tools
-OURDIR=$(readlink -f $(dirname $0))
-
-
-# Find the built versions of the library and Python bindings and add them to
-# the lookup paths, so we run against them
-. $OURDIR/build_lib_env.sh
-
-cd $OURDIR/
-
-if [ "$1" != "normal" ] && [ "$1" != "fiu" ]; then
-	echo "Usage: `basename $0` [normal|fiu]"
-	exit 1
-fi
-
-set -e
-
-function get_tmp() {
-	if which tempfile > /dev/null 2> /dev/null; then
-		tempfile -p libjio-tests
-	else
-		echo ${TMPDIR:-/tmp}/libjio-tests-$$.tmp
-	fi
-}
-
-case "$1" in
-	normal)
-		echo "behaviour tests (normal)"
-		./wrap-python 2 ../behaviour/runtests normal
-		echo
-		echo "stress tests (normal)"
-		./wrap-python 3 ../stress/jiostress \
-			$(get_tmp -p libjio-tests) 20 -n 50 -p 3
-		;;
-	fiu)
-		echo "behaviour tests (all)"
-		./wrap-python 2 ../behaviour/runtests all
-		echo
-		echo "stress tests (normal)"
-		./wrap-python 3 ../stress/jiostress \
-			$(get_tmp -p libjio-tests) 20 -n 50 -p 3
-		echo
-		echo "stress tests (fiu)"
-		./wrap-python 3 ../stress/jiostress \
-			$(get_tmp -p libjio-tests) 20 -n 400 --fi
-		;;
-esac
-
-echo
-echo
-echo Tests completed successfuly
-echo
-
diff --git a/tests/util/wrap-python b/tests/util/wrap-python
index e478bc8..930c139 100755
--- a/tests/util/wrap-python
+++ b/tests/util/wrap-python
@@ -1,37 +1,59 @@
-#!/usr/bin/env bash
+#!/usr/bin/env python
 
-# Python 2 wrapper, which makes it able to import the build (and not the
+# Python wrapper, which makes it able to import the built (and not the
 # installed) version of libjio.
+#
 # The first parameter must be the python version (2 or 3)
 
-PYVER="$1"
-shift
+import sys
+import os
+import glob
 
-if [ "$PYVER" != 2 ] && [ "$PYVER" != 3 ]; then
-	echo "Error: the first argument must be the version (2 or 3)"
-	exit 1
-fi
 
-# Find our directory, which we will use to find the other tools
-OURDIR=$(readlink -f $(dirname $0))
+if len(sys.argv) < 2 or sys.argv[1] not in ("2", "3"):
+	sys.stderr.write("Error: the first argument must be the " +
+				"version (2 or 3)\n")
+	sys.exit(1)
 
-# Find the built versions of the library and Python bindings and add them to
-# the lookup paths, so we run against them
-. $OURDIR/build_lib_env.sh
+py_ver = sys.argv[1]
 
-MODBIN=$(readlink -f \
-		"$OURDIR"/../../bindings/python/build/lib*-$PYVER.*/libjio.so)
 
-if ! [ -x "$MODBIN" ]; then
-	echo "Can't find python$PYVER bindings (run make python$PYVER)"
-	exit 1
-fi
+# Find the path where the library was built and add it to the lookup paths, so
+# we run against it
+lib_bin = os.path.dirname(sys.argv[0]) + "/../../libjio/build/libjio.so"
 
-export "PYTHONPATH=$(dirname $MODBIN):$PYTHONPATH"
+if not os.path.exists(lib_bin):
+	sys.stderr.write("Can't find library (run make)\n")
+	sys.exit(1)
 
-if [ "$PYVER" == 2 ]; then
-	exec python "$@"
-else
-	exec python3 "$@"
-fi
+lib_path = os.path.dirname(os.path.abspath(lib_bin))
+os.environ["LD_LIBRARY_PATH"] = ":".join([lib_path, \
+				os.environ.get("LD_LIBRARY_PATH", "")])
+
+
+# Find out the corresponding module path for the desired python version. The
+# path must be absolute
+mod_bins = glob.glob(os.path.dirname(sys.argv[0]) +
+			"/../../bindings/python/build/lib*-%s.*/libjio.so" \
+				% py_ver)
+if not mod_bins:
+	sys.stderr.write(("Can't find python%s bindings, run " +
+				"make python%s\n") % (py_ver, py_ver))
+	sys.exit(1)
+
+if len(mod_bins) > 1:
+	sys.stderr.write("Found too many matching python%s bindings" \
+				% py_ver)
+	sys.exit(1)
+
+mod_path = os.path.dirname(os.path.abspath(mod_bins[0]))
+os.environ["PYTHONPATH"] = ":".join([mod_path,
+					os.environ.get("PYTHONPATH", "")])
+
+if py_ver == '2':
+	py_bin = "python"
+else:
+	py_bin = "python3"
+
+os.execvp(py_bin, [py_bin] + sys.argv[2:])