krb5 commit: Add return_trace option to k5test.py

Greg Hudson <[email protected]>
Newsgroups gmane.comp.encryption.kerberos.cvs
Message-ID <[email protected]>
https://github.com/krb5/krb5/commit/e76d1ee90f2b15639cc1d9c420d3b82080895214
commit e76d1ee90f2b15639cc1d9c420d3b82080895214
Author: Greg Hudson <[email protected]>
Date:   Tue Jan 7 15:05:08 2020 -0500

    Add return_trace option to k5test.py
    
    If return_trace=True is specified when running a command in a Python
    test, collect the trace output and return it in a tuple with the
    regular output.

 src/util/k5test.py |   32 ++++++++++++++++++--------------
 1 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/util/k5test.py b/src/util/k5test.py
index 78f42c7..e3614d7 100644
--- a/src/util/k5test.py
+++ b/src/util/k5test.py
@@ -241,7 +241,8 @@ Scripts may use the following realm methods and attributes:
   return code other than 0, expected_msg=MSG to expect a substring in
   the command output, and expected_trace=('a', 'b', ...) to expect an
   ordered series of line substrings in the command's KRB5_TRACE
-  output.
+  output, or return_trace=True to return a tuple of the command output
+  and the trace output.
 
 * realm.kprop_port(): Returns a port number based on realm.portbase
   intended for use by kprop and kpropd.
@@ -673,25 +674,23 @@ def _stop_or_shell(stop, shell, env, ind):
         subprocess.call(os.getenv('SHELL'), env=env)
 
 
-# Read tracefile and look for the expected strings in successive lines.
-def _check_trace(tracefile, expected):
-    output('*** Trace output for previous command:\n')
+# Look for the expected strings in successive lines of trace.
+def _check_trace(trace, expected):
     i = 0
-    with open(tracefile, 'r') as f:
-        for line in f:
-            output(line)
-            if i < len(expected) and expected[i] in line:
-                i += 1
+    for line in trace.splitlines():
+        if i < len(expected) and expected[i] in line:
+            i += 1
     if i < len(expected):
         fail('Expected string not found in trace output: ' + expected[i])
 
 
 def _run_cmd(args, env, input=None, expected_code=0, expected_msg=None,
-             expected_trace=None):
+             expected_trace=None, return_trace=False):
     global null_input, _cmd_index, _last_cmd, _last_cmd_output, _debug
     global _stop_before, _stop_after, _shell_before, _shell_after
 
-    if expected_trace is not None:
+    tracefile = None
+    if expected_trace is not None or return_trace:
         tracefile = 'testtrace'
         if os.path.exists(tracefile):
             os.remove(tracefile)
@@ -731,10 +730,15 @@ def _run_cmd(args, env, input=None, expected_code=0, expected_msg=None,
     if expected_msg is not None and expected_msg not in outdata:
         fail('Expected string not found in command output: ' + expected_msg)
 
-    if expected_trace is not None:
-        _check_trace(tracefile, expected_trace)
+    if tracefile is not None:
+        with open(tracefile, 'r') as f:
+            trace = f.read()
+        output('*** Trace output for previous command:\n')
+        output(trace)
+        if expected_trace is not None:
+            _check_trace(trace, expected_trace)
 
-    return outdata
+    return (outdata, trace) if return_trace else outdata
 
 
 def _debug_cmd(args, env, input):
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.