-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-xv6.py
More file actions
216 lines (188 loc) · 5.43 KB
/
test-xv6.py
File metadata and controls
216 lines (188 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
#
# python script that tests xv6 without having to boot it and type to its shell
#
# ./test-xv6.py usertests (runs usertests)
# ./test-xv6.py -q usertests (runs the quick tests of usertests)
# ./test-xv6.py crash (runs the crash tests)
# ./test-xv6.py log (runs the log crash test)
import argparse, os, inspect, re, signal, subprocess, sys, time
from subprocess import run
parser = argparse.ArgumentParser()
parser.add_argument('testrex', help="test name or regular expression")
parser.add_argument("-q", action='store_true', help="usertests quick")
args = parser.parse_args()
class QEMU(object):
def __init__(self, reset=False):
if reset:
self.build_xv6()
self.reset_fs()
q = ["make", "qemu"]
self.proc = subprocess.Popen(q, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
self.output = ""
self.outbytes = bytearray()
time.sleep(1)
def reset_fs(self):
try:
run(["rm", "fs.img"], check=True)
run(["make", "fs.img"], check=True)
except subprocess.CalledProcessError as e:
print(f"Command failed with exit code {e.returncode}")
def build_xv6(self):
try:
run(["make", "kernel/kernel"], check=True)
except subprocess.CalledProcessError as e:
print(f"Command failed with exit code {e.returncode}")
def save_output(self):
try:
with open("test-xv6.out", "w") as f:
f.write(self.out)
f.close()
except OSError as e:
print("Provided a bad results path. Error:", e)
def cmd(self, c):
if isinstance(c, str):
c = c.encode('utf-8')
self.proc.stdin.write(c)
self.proc.stdin.flush()
def crash(self):
ps = run(['ps', '-opid', '--no-headers', '--ppid', str(self.proc.pid)], stdout=subprocess.PIPE, encoding='utf8')
kids = [int(line) for line in ps.stdout.splitlines()]
if len(kids) == 0:
print("no qemu")
os.exit(1)
print("kill", kids[0])
os.kill(kids[0], signal.SIGKILL)
def stop(self):
self.proc.terminate()
def read(self):
buf = os.read(self.proc.stdout.fileno(), 4096)
self.outbytes.extend(buf)
self.output = self.outbytes.decode("utf-8", "replace")
def lines(self):
return self.output.splitlines()
def error(self):
print("FAIL: match failed", regexps)
self.save_output()
self.stop()
sys.exit(1)
def match(self, *regexps, exit=True):
lines = self.lines()
last = -1
for i, line in enumerate(lines):
if any(re.match(r, line) for r in regexps):
print(line)
last = i
if last == -1 and exit:
self.error()
l = ""
if last >= 0:
l = lines[last]
return last >= 0, l
def monitor(self, *regexps, progress="", timeout):
deadline = time.time() + timeout
while True:
time.sleep(1)
timeleft = deadline - time.time()
if timeleft < 0:
self.error()
self.read()
ok, _ = self.match(*regexps, exit=False)
if ok:
return
ok, line = self.match(progress, exit=False)
if ok:
print(line)
def crash_log():
q = QEMU(True)
q.cmd("logstress f0 f1 f2 f3 f4 f5\n")
time.sleep(2)
q.crash()
q.stop()
def recover_log():
q = QEMU()
time.sleep(2)
q.read()
ok, _ = q.match('^recovering', exit=False)
if ok:
q.cmd("ls\n")
time.sleep(2)
q.read()
q.match('f5')
q.stop()
return ok
def forphan():
q = QEMU(True)
q.cmd("forphan\n")
time.sleep(5)
q.read()
q.match('wait')
q.crash()
q.stop()
def dorphan():
q = QEMU(True)
q.cmd("dorphan\n")
time.sleep(5)
q.read()
q.match('wait')
q.crash()
q.stop()
def recover_orphan():
q = QEMU()
time.sleep(2)
q.read()
q.match('^ireclaim')
q.stop()
def test_log():
print("Test recovery of log")
for i in range(5):
crash_log()
ok = recover_log()
if ok:
print("OK")
return
print("log attempt ", i+1)
print("FAIL")
sys.exit(1)
def test_forphan():
print("Test recovery of an orphaned file")
forphan()
recover_orphan()
print("OK")
def test_dorphan():
print("Test recovery of an orphaned file")
dorphan()
recover_orphan()
print("OK")
def test_crash():
test_log()
test_forphan()
test_dorphan()
def test_usertests(test=""):
timeout = 600
opt = ""
if args.q:
opt = " -q"
timeout = 300
elif test != "":
opt += " " + test
q = QEMU(True)
q.cmd("usertests" + opt + "\n")
q.monitor('^ALL TESTS PASSED', progress='test', timeout=timeout)
q.stop()
def main():
print(args)
rex = r'%s' % args.testrex
funcs = [(obj,name) for name,obj in inspect.getmembers(sys.modules[__name__])
if (inspect.isfunction(obj) and
name.startswith('test'))]
none = True
for (f,n) in funcs:
if re.search(rex, n):
none = False
f()
if none:
test_usertests(test=args.testrex)
main()