Initial commit
[gram.git] / .sim-test.py
1 #!/usr/bin/env python3
2 import os
3 import sys
4 import pexpect
5 import time
6 from argparse import ArgumentParser
7
8
9 parser = ArgumentParser()
10 parser.add_argument("--sdram-module", type=str)
11 args = parser.parse_args()
12
13
14 tests = [
15 {
16 'id': 'litex_sim',
17 'command': f'python3 -m litex.tools.litex_sim --with-sdram --sdram-module {args.sdram_module}',
18 'cwd': os.getcwd(),
19 'checkpoints': [
20 { 'timeout': 240, 'good': [b'\n\\s*BIOS built on[^\n]+\n'] },
21 { 'timeout': 30, 'good': [b'Memtest OK'],
22 'bad': [b'(Memory initialization failed|Booting from)'] },
23 ]
24 }
25 ]
26
27
28 def run_test(id, command, cwd, checkpoints):
29 print(f'*** Test ID: {id}')
30 print(f'*** CWD: {cwd}')
31 print(f'*** Command: {command}')
32 os.chdir(cwd)
33 p = pexpect.spawn(command, timeout=None, logfile=sys.stdout.buffer)
34
35 checkpoint_id = 0
36 for cp in checkpoints:
37 good = cp.get('good', [])
38 bad = cp.get('bad', [])
39 patterns = good + bad
40 timeout = cp.get('timeout', None)
41
42 timediff = time.time()
43 try:
44 match_id = p.expect(patterns, timeout=timeout)
45 except pexpect.EOF:
46 print(f'\n*** {id}: premature termination')
47 return False;
48 except pexpect.TIMEOUT:
49 timediff = time.time() - timediff
50 print(f'\n*** {id}: timeout (checkpoint {checkpoint_id}: +{int(timediff)}s)')
51 return False;
52 timediff = time.time() - timediff
53
54 if match_id >= len(good):
55 break
56
57 sys.stdout.buffer.write(b'<<checkpoint %d: +%ds>>' % (checkpoint_id, int(timediff)))
58 checkpoint_id += 1
59
60 is_success = checkpoint_id == len(checkpoints)
61
62 # Let it print rest of line
63 match_id = p.expect_exact([b'\n', pexpect.TIMEOUT, pexpect.EOF], timeout=1)
64 p.terminate(force=True)
65
66 line_break = '\n' if match_id != 0 else ''
67 print(f'{line_break}*** {id}: {"success" if is_success else "failure"}')
68
69 return is_success
70
71
72 for test in tests:
73 success = run_test(**test)
74 if not success:
75 sys.exit(1)
76
77 sys.exit(0)