git » libfiu » commit 76326ce

tests: Introduce tests/utils

author Alberto Bertogli
2014-05-25 19:01:08 UTC
committer Alberto Bertogli
2014-05-25 23:45:16 UTC
parent fa8ae484ebd9431c0fc326372cd4626ab6803a7d

tests: Introduce tests/utils

This patch introduces basic tests for the command-line utilities (fiu-run,
fiu-ls, fiu-ctrl).

They are not intended to cover all of libfiu's capabilities, but mostly check
remote control handling from those utilities.

tests/Makefile +5 -3
tests/utils/.gitignore +2 -0
tests/utils/Makefile +51 -0
tests/utils/test-basic_ctrl.py +63 -0
tests/utils/test-basic_run.sh +17 -0
tests/utils/wrap +26 -0

diff --git a/tests/Makefile b/tests/Makefile
index cdc478d..955e181 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -25,7 +25,7 @@ default: tests
 
 all: tests
 
-tests: c-tests py-tests gen-tests
+tests: c-tests py-tests gen-tests utils-tests
 
 #
 # C tests
@@ -69,12 +69,14 @@ py-run-%: %.py
 	$(NICE_PY) ./$<
 
 #
-# Generated tests
+# Sub-directory tests
 #
 
 gen-tests:
 	$(MAKE) -C generated
 
+utils-tests:
+	$(MAKE) -C utils
 
 #
 # Cleanup
@@ -91,7 +93,7 @@ clean:
 FORCE:
 
 .PHONY: default all clean \
-	tests c-tests py-tests \
+	tests c-tests py-tests gen-tests utils-tests \
 	.force-build-flags
 
 
diff --git a/tests/utils/.gitignore b/tests/utils/.gitignore
new file mode 100644
index 0000000..e4c47bc
--- /dev/null
+++ b/tests/utils/.gitignore
@@ -0,0 +1,2 @@
+output-*.txt
+libs/
diff --git a/tests/utils/Makefile b/tests/utils/Makefile
new file mode 100644
index 0000000..fc78d2e
--- /dev/null
+++ b/tests/utils/Makefile
@@ -0,0 +1,51 @@
+
+ifneq ($(V), 1)
+	NICE_RUN = @echo "  RUN $<"; LD_LIBRARY_PATH=../../libfiu/
+	NICE_LN = @echo "  LN $@"; ln -f
+else
+	NICE_RUN = LD_LIBRARY_PATH=../../libfiu/
+	NICE_LN = ln -f
+endif
+
+
+default: all
+
+all: lnlibs tests
+
+
+# Link the libraries to a single place, as the scripts expect.
+libs:
+	mkdir -p libs/
+
+libs/fiu_posix_preload.so: ../../preload/posix/fiu_posix_preload.so libs
+	$(NICE_LN) $< libs/
+
+libs/fiu_run_preload.so: ../../preload/run/fiu_run_preload.so libs
+	$(NICE_LN) $< libs/
+
+lnlibs: libs/fiu_posix_preload.so libs/fiu_run_preload.so
+
+
+#
+# Tests
+#
+
+SRCS := $(wildcard test-*)
+
+tests: $(patsubst %,run-%,$(SRCS))
+
+run-%: %
+	$(NICE_RUN) ./$< > output-$<.txt 2>&1
+
+
+#
+# Cleanup
+#
+
+clean:
+	rm -f libs/*.so output-*.txt
+	rmdir libs/
+
+.PHONY: default all clean \
+	tests lnlibs
+
diff --git a/tests/utils/test-basic_ctrl.py b/tests/utils/test-basic_ctrl.py
new file mode 100755
index 0000000..e21cb35
--- /dev/null
+++ b/tests/utils/test-basic_ctrl.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+
+import subprocess
+import time
+
+def fiu_ctrl(p, args):
+    subprocess.check_call("./wrap fiu-ctrl".split() + args + [str(p.pid)])
+
+def launch_sh():
+    # We use cat as a subprocess as it is reasonably ubiquitous, simple and
+    # straightforward (which helps debugging and troubleshooting), but at the
+    # same time it is interactive and we can make it do the operations we
+    # want.
+    p = subprocess.Popen("./wrap fiu-run -x cat".split(),
+            stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE)
+
+    # Give it a moment to initialize and create the control files.
+    time.sleep(0.2)
+    return p
+
+def send_cmd(p, cmd):
+    p.stdin.write(cmd)
+    p.stdin.close()
+
+    # Give the control thread a moment to process the command.
+    time.sleep(0.2)
+    return p.stdout.read(), p.stderr.read()
+
+
+# Launch a subprocess and check that it shows up in fiu-ls.
+p = launch_sh()
+out = subprocess.check_output("./wrap fiu-ls".split())
+assert ("%s: cat" % p.pid) in out, out
+
+# Send it a command and check that it works.
+# Nothing interesting here from libfiu's perspective, but it helps make sure
+# the test environment is sane.
+out, err = send_cmd(p, "test\n")
+assert out == 'test\n', out
+assert err == '', err
+
+# Launch and then make I/O fail at runtime.
+p = launch_sh()
+fiu_ctrl(p, ["-c", "enable name=posix/io/*"])
+out, err = send_cmd(p, "test\n")
+assert out == '', out
+assert 'error' in err
+
+# Same, but with failinfo.
+p = launch_sh()
+fiu_ctrl(p, ["-c", "enable name=posix/io/*,failinfo=3"])
+out, err = send_cmd(p, "test\n")
+assert out == '', out
+assert 'error' in err
+
+# Same, but with probability.
+p = launch_sh()
+fiu_ctrl(p, ["-c", "enable_random name=posix/io/*,probability=0.999"])
+out, err = send_cmd(p, "test\n")
+assert out == '', out
+assert 'error' in err
+
diff --git a/tests/utils/test-basic_run.sh b/tests/utils/test-basic_run.sh
new file mode 100755
index 0000000..4d2ba60
--- /dev/null
+++ b/tests/utils/test-basic_run.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+set -ex
+
+# Very basic test, nothing enabled.
+./wrap fiu-run true
+
+# Same as above, but including the posix preloader.
+./wrap fiu-run -x true
+
+# Now with an unused failure point.
+./wrap fiu-run -c "enable name=p1" true
+
+# open() failure.
+! ./wrap fiu-run -x -c "enable name=posix/io/oc/open" cat /dev/null
+
+
diff --git a/tests/utils/wrap b/tests/utils/wrap
new file mode 100755
index 0000000..ad32b9d
--- /dev/null
+++ b/tests/utils/wrap
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# Wrap the utilities (fiu-ctrl, fiu-ls, fiu-run) so that we can run them
+# easily from within the development tree, agains the locally-built libraries.
+#
+# The first argument is the utility to run.
+
+case "$1" in
+"fiu-ctrl")
+	BIN=../../utils/fiu-ctrl
+	ARGS=
+	;;
+"fiu-ls")
+	BIN=../../utils/fiu-ls
+	ARGS=
+	;;
+"fiu-run")
+	BIN=../../preload/run/fiu-run
+	ARGS="-l ./libs"
+	;;
+esac
+
+shift
+
+exec $BIN $ARGS "$@"
+