mirror of
https://github.com/systemd/systemd.git
synced 2024-10-28 03:25:31 +03:00
test: combine stdout/stderr from failed test
Printing stdout and stderr from a failed test makes it harder to interpret what the specific problem was; instead let's print out the lines in order as we got them when the test was run Also save failed test output to file if ARTIFACT_DIRECTORY is defined
This commit is contained in:
parent
f1e696221f
commit
d57e871c60
@ -4,6 +4,7 @@ import argparse
|
|||||||
import dataclasses
|
import dataclasses
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
try:
|
try:
|
||||||
@ -27,6 +28,8 @@ def argument_parser():
|
|||||||
p = argparse.ArgumentParser()
|
p = argparse.ArgumentParser()
|
||||||
p.add_argument('-u', '--unsafe', action='store_true',
|
p.add_argument('-u', '--unsafe', action='store_true',
|
||||||
help='run "unsafe" tests too')
|
help='run "unsafe" tests too')
|
||||||
|
p.add_argument('-A', '--artifact_directory',
|
||||||
|
help='store output from failed tests in this dir')
|
||||||
return p
|
return p
|
||||||
|
|
||||||
opts = argument_parser().parse_args()
|
opts = argument_parser().parse_args()
|
||||||
@ -35,11 +38,14 @@ tests = glob.glob('/usr/lib/systemd/tests/test-*')
|
|||||||
if opts.unsafe:
|
if opts.unsafe:
|
||||||
tests += glob.glob('/usr/lib/systemd/tests/unsafe/test-*')
|
tests += glob.glob('/usr/lib/systemd/tests/unsafe/test-*')
|
||||||
|
|
||||||
|
if not opts.artifact_directory and os.getenv('ARTIFACT_DIRECTORY'):
|
||||||
|
opts.artifact_directory = os.getenv('ARTIFACT_DIRECTORY')
|
||||||
|
|
||||||
total = Total(total=len(tests))
|
total = Total(total=len(tests))
|
||||||
for test in tests:
|
for test in tests:
|
||||||
name = os.path.basename(test)
|
name = os.path.basename(test)
|
||||||
|
|
||||||
ex = subprocess.run(test, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
ex = subprocess.run(test, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||||
if ex.returncode == 0:
|
if ex.returncode == 0:
|
||||||
print(f'{GREEN}PASS: {name}{RESET_ALL}')
|
print(f'{GREEN}PASS: {name}{RESET_ALL}')
|
||||||
total.good += 1
|
total.good += 1
|
||||||
@ -50,12 +56,23 @@ for test in tests:
|
|||||||
print(f'{RED}FAIL: {name}{RESET_ALL}')
|
print(f'{RED}FAIL: {name}{RESET_ALL}')
|
||||||
total.fail += 1
|
total.fail += 1
|
||||||
|
|
||||||
# stdout/stderr might not be valid unicode, let's just dump it to the terminal.
|
output_file = None
|
||||||
# Also let's reset the style afterwards, in case our output sets something.
|
if opts.artifact_directory:
|
||||||
sys.stdout.buffer.write(ex.stdout)
|
output_dir = pathlib.Path(opts.artifact_directory) / 'unit-tests'
|
||||||
print(f'{RESET_ALL}{BRIGHT}')
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
sys.stdout.buffer.write(ex.stderr)
|
output_file = output_dir / name
|
||||||
print(f'{RESET_ALL}')
|
output_file.write_bytes(ex.stdout)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print(ex.stdout.decode('utf-8'))
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
print(f'{BRIGHT}Note, some test output shown here is not UTF-8')
|
||||||
|
if output_file:
|
||||||
|
print(f'For actual test output see artifact file {output_file}')
|
||||||
|
print(f'{RESET_ALL}')
|
||||||
|
print(ex.stdout.decode('utf-8', errors='replace'))
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
print(f'{BRIGHT}OK: {total.good} SKIP: {total.skip} FAIL: {total.fail}{RESET_ALL}')
|
print(f'{BRIGHT}OK: {total.good} SKIP: {total.skip} FAIL: {total.fail}{RESET_ALL}')
|
||||||
sys.exit(total.fail > 0)
|
sys.exit(total.fail > 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user