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