Blame SOURCES/Add-k5test-expected_msg-expected_trace.patch

963210
From c099e896f28d8c5ccacc9df086a8f4297c6b484e Mon Sep 17 00:00:00 2001
963210
From: Greg Hudson <ghudson@mit.edu>
963210
Date: Tue, 17 Jan 2017 11:24:41 -0500
963210
Subject: [PATCH] Add k5test expected_msg, expected_trace
963210
963210
In k5test.py, add the optional keyword argument "expected_msg" to
963210
methods that run commands, to make it easier to look for substrings in
963210
the command output.  Add the optional keyword "expected_trace" to run
963210
the command with KRB5_TRACE enabled and look for an ordered series of
963210
substrings in the trace output.
963210
963210
(cherry picked from commit 8bb5fce69a4aa6c3082fa7def66a93974e10e17a)
963210
[rharwood@redhat.com: back out .gitignore]
963210
---
963210
 src/config/post.in |  2 +-
963210
 src/util/k5test.py | 37 ++++++++++++++++++++++++++++++++++---
963210
 2 files changed, 35 insertions(+), 4 deletions(-)
963210
963210
diff --git a/src/config/post.in b/src/config/post.in
963210
index 77a9bffdf..aecac9d3b 100644
963210
--- a/src/config/post.in
963210
+++ b/src/config/post.in
963210
@@ -156,7 +156,7 @@ clean: clean-$(WHAT)
963210
 
963210
 clean-unix::
963210
 	$(RM) $(OBJS) $(DEPTARGETS_CLEAN) $(EXTRA_FILES)
963210
-	$(RM) et-[ch]-*.et et-[ch]-*.[ch] testlog
963210
+	$(RM) et-[ch]-*.et et-[ch]-*.[ch] testlog testtrace
963210
 	-$(RM) -r testdir
963210
 
963210
 clean-windows::
963210
diff --git a/src/util/k5test.py b/src/util/k5test.py
963210
index c3d026377..4d30baf40 100644
963210
--- a/src/util/k5test.py
963210
+++ b/src/util/k5test.py
963210
@@ -223,8 +223,11 @@ Scripts may use the following realm methods and attributes:
963210
   command-line debugging options.  Fail if the command does not return
963210
   0.  Log the command output appropriately, and return it as a single
963210
   multi-line string.  Keyword arguments can contain input='string' to
963210
-  send an input string to the command, and expected_code=N to expect a
963210
-  return code other than 0.
963210
+  send an input string to the command, expected_code=N to expect a
963210
+  return code other than 0, expected_msg=MSG to expect a substring in
963210
+  the command output, and expected_trace=('a', 'b', ...) to expect an
963210
+  ordered series of line substrings in the command's KRB5_TRACE
963210
+  output.
963210
 
963210
 * realm.kprop_port(): Returns a port number based on realm.portbase
963210
   intended for use by kprop and kpropd.
963210
@@ -647,10 +650,31 @@ def _stop_or_shell(stop, shell, env, ind):
963210
         subprocess.call(os.getenv('SHELL'), env=env)
963210
 
963210
 
963210
-def _run_cmd(args, env, input=None, expected_code=0):
963210
+# Read tracefile and look for the expected strings in successive lines.
963210
+def _check_trace(tracefile, expected):
963210
+    output('*** Trace output for previous command:\n')
963210
+    i = 0
963210
+    with open(tracefile, 'r') as f:
963210
+        for line in f:
963210
+            output(line)
963210
+            if i < len(expected) and expected[i] in line:
963210
+                i += 1
963210
+    if i < len(expected):
963210
+        fail('Expected string not found in trace output: ' + expected[i])
963210
+
963210
+
963210
+def _run_cmd(args, env, input=None, expected_code=0, expected_msg=None,
963210
+             expected_trace=None):
963210
     global null_input, _cmd_index, _last_cmd, _last_cmd_output, _debug
963210
     global _stop_before, _stop_after, _shell_before, _shell_after
963210
 
963210
+    if expected_trace is not None:
963210
+        tracefile = 'testtrace'
963210
+        if os.path.exists(tracefile):
963210
+            os.remove(tracefile)
963210
+        env = env.copy()
963210
+        env['KRB5_TRACE'] = tracefile
963210
+
963210
     if (_match_cmdnum(_debug, _cmd_index)):
963210
         return _debug_cmd(args, env, input)
963210
 
963210
@@ -679,6 +703,13 @@ def _run_cmd(args, env, input=None, expected_code=0):
963210
     # Check the return code and return the output.
963210
     if code != expected_code:
963210
         fail('%s failed with code %d.' % (args[0], code))
963210
+
963210
+    if expected_msg is not None and expected_msg not in outdata:
963210
+        fail('Expected string not found in command output: ' + expected_msg)
963210
+
963210
+    if expected_trace is not None:
963210
+        _check_trace(tracefile, expected_trace)
963210
+
963210
     return outdata
963210
 
963210