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

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