Files
seaweedfs/seaweed-block/.github/scripts/required-tests-check-test.py
T

129 lines
6.7 KiB
Python

"""Self-test of required-tests-check.py: event streams against the exit code
they must give. A gate that cannot fail is the defect this check exists to
prevent, so every way a named test can fail to pass is here."""
import json
import os
import subprocess
import sys
import tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
CHECK = os.path.join(HERE, 'required-tests-check.py')
def ev(action, test=None, elapsed=None):
e = {'Action': action, 'Package': 'example/cmd/blockvolume'}
if test:
e['Test'] = test
if elapsed is not None:
e['Elapsed'] = elapsed
return json.dumps(e)
def ran(test, end='pass', secs=1.5):
return [ev('run', test), ev('output', test), ev(end, test, secs)]
def run(names, *streams, status=None):
with tempfile.TemporaryDirectory() as d:
nf = os.path.join(d, 'names')
with open(nf, 'w', encoding='utf-8') as f:
f.write(names)
files = []
for i, lines in enumerate(streams):
p = os.path.join(d, 'events%d' % i)
with open(p, 'w', encoding='utf-8') as f:
f.write('\n'.join(lines) + '\n')
files.append(p)
extra = []
if status is not None:
sf = os.path.join(d, 'status')
with open(sf, 'w', encoding='utf-8') as f:
f.write(status)
extra = ['--status', sf]
r = subprocess.run([sys.executable, '-I', CHECK] + extra + [nf] + files, capture_output=True, text=True)
return r.returncode, r.stdout + r.stderr
NG = 'TestA\nTestB # nongating=#920\n'
CASES = [
# (name, want exit, names file, streams)
('a non-gating test that passed', 0, NG, [ran('TestA') + ran('TestB')]),
('a non-gating test that failed does not fail the run', 0, NG, [ran('TestA') + ran('TestB', 'fail')]),
('a non-gating failure, then its process failed: still its own', 0, NG,
[ran('TestA'), ran('TestB', 'fail') + [ev('fail')]]),
('a gating test still fails the run beside a non-gating one', 1, NG, [ran('TestA', 'fail') + ran('TestB')]),
('a process failure after a gating test still fails the run', 1, NG, [ran('TestA') + [ev('fail')], ran('TestB')]),
('a non-gating test that never ran fails (not run is not reported)', 1, NG, [ran('TestA')]),
('nongating without an issue is refused', 2, 'TestA\nTestB # nongating\n', [ran('TestA') + ran('TestB')]),
('every named test passed', 0, 'TestA\nTestB # a comment\n', [ran('TestA') + ran('TestB')]),
('passes spread over two streams', 0, 'TestA\nTestB\n', [ran('TestA'), ran('TestB')]),
('an unnamed test may fail', 0, 'TestA\n', [ran('TestA') + ran('TestOther', 'fail')]),
('a passing subtest does not stand for a skipped parent', 1, 'TestA\n',
[[ev('run', 'TestA'), ev('pass', 'TestA/sub', 1), ev('skip', 'TestA', 0)]]),
('a skip is a failure', 1, 'TestA\nTestB\n', [ran('TestA') + ran('TestB', 'skip', 0)]),
('a failed test', 1, 'TestA\n', [ran('TestA', 'fail')]),
('a test that never ran', 1, 'TestA\nTestB\n', [ran('TestA')]),
('a test that started and never ended (killed by the timeout)', 1, 'TestA\n', [[ev('run', 'TestA'), ev('output', 'TestA')]]),
('passed in one stream, failed in another', 1, 'TestA\n', [ran('TestA'), ran('TestA', 'fail')]),
('no events at all (the build failed)', 1, 'TestA\n', [['# example/cmd/blockvolume', 'build failed']]),
('a package-level failure after every named test passed', 1, 'TestA\n', [ran('TestA') + [ev('fail', None, 3)]]),
('a name listed twice', 2, 'TestA\nTestA\n', [ran('TestA')]),
('a subtest name in the list', 2, 'TestA/sub\n', [ran('TestA')]),
('an empty names file', 2, '# nothing\n', [ran('TestA')]),
]
def main():
fail = 0
for name, want, names, streams in CASES:
got, out = run(names, *streams)
if got != want:
print('FAIL %s: exit %d, want %d\n%s' % (name, got, want, out))
fail = 1
STATUS = [
('PASS events and every process exited 0', 0, 'TestA 0\nTestB 0\n'),
('PASS, then the process exited 1', 1, 'TestA 0\nTestB 1\n'),
('PASS, then the process was killed by the timeout', 1, 'TestA 0\nTestB 124\n'),
('a named test with no exit status', 1, 'TestA 0\n'),
('a named test ran twice', 1, 'TestA 0\nTestB 0\nTestB 0\n'),
('a malformed status line', 2, 'TestA 0\nTestB\n'),
]
for name, want, status in STATUS:
got, out = run('TestA\nTestB\n', ran('TestA') + ran('TestB'), status=status)
if got != want:
print('FAIL %s: exit %d, want %d\n%s' % (name, got, want, out))
fail = 1
got, out = run('TestA\n', [ev('run', 'TestA'), json.dumps({'Action': 'output', 'Test': 'TestA', 'Output': ' x_test.go:7: the reason\n'})] + [json.dumps({'Action': 'output', 'Test': 'TestA', 'Output': ' h_test.go:578: blockvolume-r%d log:\n' % i}) for i in range(20)] + [json.dumps({'Action': 'output', 'Test': 'TestA', 'Output': ' w_test.go:128: ---- master log tail ----\n'})] + [json.dumps({'Action': 'output', 'Test': 'TestA', 'Output': ' I1126 harness log line %d\n' % i}) for i in range(60)] + [ev('fail', 'TestA', 1)])
if got != 1 or 'x_test.go:7: the reason' not in out:
print('FAIL a failed test''s output is shown: %s' % out)
fail = 1
# A message whose reason is on its continuation lines (a holder's log
# tail after "its log ends:"), followed by a harness dump: the message's
# last continuation lines are shown, the dump is not.
o = lambda s: json.dumps({'Action': 'output', 'Test': 'TestA', 'Output': s + '\n'})
got, out = run('TestA\n', [ev('run', 'TestA'),
o(' f1_test.go:178: blockvolume-v5r2 exited (exit status 1) before logging "adopted"; its log ends:')]
+ [o(' holder line %d' % i) for i in range(40)]
+ [o(' holder: fatal: the reason it exited')]
+ [o(' w_test.go:128: ---- master log tail ----')]
+ [o(' I1201 harness log line %d' % i) for i in range(60)] + [ev('fail', 'TestA', 1)])
if got != 1 or 'holder: fatal: the reason it exited' not in out or 'holder line 39' not in out:
print('FAIL a failed message\'s continuation lines are shown: %s' % out)
fail = 1
if 'holder line 0\n' in out or 'harness log line 0\n' in out:
print('FAIL continuation lines are bounded and a dump\'s are not kept: %s' % out)
fail = 1
got, out = run('TestA\n', ran('TestA', secs=2.25))
if got != 0 or '2.2s' not in out and '2.3s' not in out:
print('FAIL the elapsed time is reported: %s' % out)
fail = 1
if not fail:
print('required-tests-check: %d cases ok' % (len(CASES) + len(STATUS) + 3))
return fail
if __name__ == '__main__':
sys.exit(main())