[v4,08/12] zdtm: implement capturing of test hooks stdout and stderr

Submitted by Eugene Batalov on Sept. 11, 2016, 5:14 p.m.

Details

Message ID 1473614091-4734-9-git-send-email-eabatalov89@gmail.com
State Rejected
Series "Series without cover letter"
Headers show

Commit Message

Eugene Batalov Sept. 11, 2016, 5:14 p.m.
Using this feature hooks can return arbitrary data
to zdtm.py. This will be used in gc tests to tell
zdtm.py what garbage should be found by criu gc --show.

Also added function run_hook for cases when test hook always
should exist. This is needed for gc tests too.

Also this 2 features are generic and can be used in other cases.

Signed-off-by: Eugene Batalov <eabatalov89@gmail.com>
---
 test/zdtm.py | 42 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 7 deletions(-)

Patch hide | download patch | download mbox

diff --git a/test/zdtm.py b/test/zdtm.py
index b04abbd..0873f1f 100755
--- a/test/zdtm.py
+++ b/test/zdtm.py
@@ -948,13 +948,41 @@  class criu:
 			sys.exit(1)
 
 
-def try_run_hook(test, args):
-	hname = test.getname() + '.hook'
-	if os.access(hname, os.X_OK):
-		print "Running %s(%s)" % (hname, ', '.join(args))
-		hook = subprocess.Popen([hname] + args)
-		if hook.wait() != 0:
-			raise test_fail_exc("hook " + " ".join(args))
+def hook_name(test):
+	return test.getname() + '.hook'
+
+
+def run_hook(test, args, cap_output = False):
+	result = {'stdout': None, 'stderr': None}
+
+	subproc_args = {"args": [hook_name(test)] + args}
+	if cap_output:
+		subproc_args["stdout"] = subprocess.PIPE
+		subproc_args["stderr"] = subprocess.PIPE
+	print "Running %s" % str(subproc_args)
+	try:
+		hook = subprocess.Popen(**subproc_args)
+		if cap_output:
+			result['stdout'], result['stderr'] = hook.communicate()
+		else:
+			hook.wait()
+		if hook.returncode != 0:
+			print "Hook %s return code %d" % (
+				hook_name(test), hook.returncode)
+			if cap_output:
+				print result['stdout']
+				print result['stderr']
+			raise Exception()
+	except:
+		raise test_fail_exc("hook " + " ".join(args))
+
+	return result
+
+
+def try_run_hook(test, args, cap_output = False):
+	if os.access(hook_name(test), os.X_OK):
+		return run_hook(test, args, cap_output)
+	return {'stdout': None, 'stderr': None}
 
 #
 # Step by step execution