Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions env/freeipa_env/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
DEFAULT_LINES, DEFAULT_TAIL, ScanCache, cache_prefix)
from .htmlreport import render_html
from . import installtiming
from . import logscript
from .mdreport import render_markdown
from . import container as container_mode
from .detached import LocalDetached
Expand Down Expand Up @@ -99,6 +100,11 @@ def cmd_report(args):
collected log tree resolve on the artifact server and locally).
"""
workdir = args.workdir
for cand in (workdir, args.env):
adopted = cand and _adopt_download(cand)
if adopted:
workdir, args.env = adopted, None
break
if not workdir:
if not args.env:
print('error: give an env file (to derive the default workdir) '
Expand Down Expand Up @@ -446,6 +452,18 @@ def _timing_report(store, workdir, args):
return 0


def _script_report(store, args):
paths = store.console_paths()
if not paths:
print('error: no run.log console log in this workdir',
file=sys.stderr)
return 2
with open(paths[0], errors='replace') as f:
for line in logscript.transcript(f, args.script, args.host):
print(line)
return 0


def _pick_job(src, root, quiet=False):
"""A batch download holds one <preset>/ workdir per preset under root:
return the only one; several is an error; none is None when quiet."""
Expand Down Expand Up @@ -538,6 +556,8 @@ def cmd_logs(args):
else:
args.pattern_compiled = None
store = LogStore(workdir)
if args.script is not None:
return _script_report(store, args)
if args.timing:
return _timing_report(store, workdir, args)
if args.html:
Expand Down Expand Up @@ -1562,6 +1582,11 @@ def add_common(sp):
help='instead of the terminal view, write a '
'self-contained results.html and exit (default '
'output: <workdir>/logs/results.html)')
sp.add_argument('--script', nargs='?', const='', metavar='TEST',
help='print the commands the run executed, with their '
'output, as a shell transcript; TEST (substring of '
'the test id) limits it to those tests; --host '
'limits it to one host')
sp.add_argument('--timing', action='store_true',
help='time spent in each installer service and step, '
'rated red/amber/green, with the warnings, errors, '
Expand Down
18 changes: 16 additions & 2 deletions env/freeipa_env/htmlreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import os
import sys

from . import logscript
from .loganalyze import (CATEGORIES, CATEGORY_NAMES, LogStore, parse_xunit,
xunit_testcases, load_results, overall_status,
scan_file, ptest_status, parse_pytest_html)
Expand Down Expand Up @@ -297,6 +298,12 @@ def _ptest_section(store):
for nodeid, e in parse_pytest_html(ph).items():
logmap[nodeid.replace('/', '-').replace('::', '-')] = e

cmds = {}
for rl in store.console_paths():
with open(rl, errors='replace') as f:
for nodeid, ls in logscript.per_test(f).items():
cmds[nodeid.replace('/', '-').replace('::', '-')] = ls

tcs = []
for xp in store.xunit_paths():
try:
Expand All @@ -312,7 +319,8 @@ def _ptest_section(store):

# (1) pytest captured output for this nodeid
out_html = []
out_html.append('<div class="file"><div class="path">pytest output</div>')
out_html.append('<details class="file"><summary class="path">'
'pytest output</summary>')
if pe is not None:
log = pe.get('log') or ''
if log and log.strip():
Expand All @@ -335,7 +343,13 @@ def _ptest_section(store):
'<div class="note">no per-test captured output for this '
'entry (setup/teardown phase, or not in the pytest report)'
'</div>')
out_html.append('</div>')
out_html.append('</details>')

# (1b) the commands the test ran, from the console log, shown first
if cmds.get(test):
out_html.insert(0, '<div class="file"><div class="path">commands'
'</div><pre>' + _esc('\n'.join(cmds[test]))
+ '</pre></div>')

# (2) links to the collected log files, per host/phase
file_html = out_html
Expand Down
61 changes: 61 additions & 0 deletions env/freeipa_env/logscript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""`logs --script`: replay the commands a test run executed, as a shell
transcript (``[host]# cmd`` followed by its output) from the console log."""
import ast
import re
import shlex

_CMD = re.compile(r'^\[ipatests\.\S*?Host\.(?P<host>[\w-]+)\.cmd\d+\] '
r'(?P<msg>.*)$')
_HEADER = re.compile(r'^(?P<id>\S+\.py::\S+)')
_NOISE = re.compile(r'^(Exit code: \d+$|Warning: Permanently added |'
r'bash: line \d+: (cd: |/root/ipatests/env\.sh))')


def _command(msg):
"""The command text of a `RUN ...` message: argv lists become a quoted
shell line, a plain string (shell=True) is kept as is."""
cmd = msg[4:]
if cmd.startswith('['):
try:
return shlex.join(ast.literal_eval(cmd))
except (ValueError, SyntaxError):
pass
return cmd


def transcript(lines, test=None, host=None):
"""Yield transcript lines for the tests whose node id contains `test`
(all when None) and, when `host` is given, only that host."""
wanted = test is None
cur = None
for line in lines:
line = line.rstrip('\n')
h = _HEADER.match(line)
Comment thread
abbra marked this conversation as resolved.
Resolved
if h:
wanted = test is None or test in h['id']
if wanted:
yield f'# == {h["id"]}'
m = _CMD.match(line)
if not (wanted and m) or (host and m['host'] != host):
continue
msg = m['msg']
if msg.startswith('RUN '):
cmd = _command(msg)
if cmd.startswith('-o ') or cmd == 'true' or '/root/ipatests' in cmd:
cur = None # ssh keepalive / framework bookkeeping, not a test step
continue
cur = m['host']
yield f'[{cur}]# {cmd}'
elif cur and not _NOISE.match(msg):
yield msg


def per_test(lines):
"""{node id: transcript lines} for every test in the console log."""
out, cur = {}, None
for line in transcript(lines):
if line.startswith('# == '):
cur = out.setdefault(line[5:], [])
elif cur is not None:
cur.append(line)
return out
Loading