test: Avoid hardcoding the chain name in combine_logs

This commit is contained in:
MarcoFalke 2019-07-31 14:34:17 -04:00
parent fa8a1d7ba3
commit faf36838bd
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -8,6 +8,7 @@ If no argument is provided, the most recent test directory will be used."""
import argparse import argparse
from collections import defaultdict, namedtuple from collections import defaultdict, namedtuple
import glob
import heapq import heapq
import itertools import itertools
import os import os
@ -76,9 +77,17 @@ def read_logs(tmp_dir):
Delegates to generator function get_log_events() to provide individual log events Delegates to generator function get_log_events() to provide individual log events
for each of the input log files.""" for each of the input log files."""
# Find out what the folder is called that holds the debug.log file
chain = glob.glob("{}/node0/*/debug.log".format(tmp_dir))
if chain:
chain = chain[0] # pick the first one if more than one chain was found (should never happen)
chain = re.search('node0/(.+?)/debug\.log$', chain).group(1) # extract the chain name
else:
chain = 'regtest' # fallback to regtest (should only happen when none exists)
files = [("test", "%s/test_framework.log" % tmp_dir)] files = [("test", "%s/test_framework.log" % tmp_dir)]
for i in itertools.count(): for i in itertools.count():
logfile = "{}/node{}/regtest/debug.log".format(tmp_dir, i) logfile = "{}/node{}/{}/debug.log".format(tmp_dir, i, chain)
if not os.path.isfile(logfile): if not os.path.isfile(logfile):
break break
files.append(("node%d" % i, logfile)) files.append(("node%d" % i, logfile))
@ -164,25 +173,26 @@ def get_log_events(source, logfile):
def print_logs_plain(log_events, colors): def print_logs_plain(log_events, colors):
"""Renders the iterator of log events into text.""" """Renders the iterator of log events into text."""
for event in log_events: for event in log_events:
lines = event.event.splitlines() lines = event.event.splitlines()
print("{0} {1: <5} {2} {3}".format(colors[event.source.rstrip()], event.source, lines[0], colors["reset"])) print("{0} {1: <5} {2} {3}".format(colors[event.source.rstrip()], event.source, lines[0], colors["reset"]))
if len(lines) > 1: if len(lines) > 1:
for line in lines[1:]: for line in lines[1:]:
print("{0}{1}{2}".format(colors[event.source.rstrip()], line, colors["reset"])) print("{0}{1}{2}".format(colors[event.source.rstrip()], line, colors["reset"]))
def print_logs_html(log_events): def print_logs_html(log_events):
"""Renders the iterator of log events into html.""" """Renders the iterator of log events into html."""
try: try:
import jinja2 import jinja2
except ImportError: except ImportError:
print("jinja2 not found. Try `pip install jinja2`") print("jinja2 not found. Try `pip install jinja2`")
sys.exit(1) sys.exit(1)
print(jinja2.Environment(loader=jinja2.FileSystemLoader('./')) print(jinja2.Environment(loader=jinja2.FileSystemLoader('./'))
.get_template('combined_log_template.html') .get_template('combined_log_template.html')
.render(title="Combined Logs from testcase", log_events=[event._asdict() for event in log_events])) .render(title="Combined Logs from testcase", log_events=[event._asdict() for event in log_events]))
if __name__ == '__main__': if __name__ == '__main__':
main() main()