-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
112 lines (98 loc) · 3.26 KB
/
test.py
File metadata and controls
112 lines (98 loc) · 3.26 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
#!/usr/bin/python3
import argparse
import os
import sys
from subprocess import Popen, PIPE, STDOUT, TimeoutExpired
"""
Run the script in the directory where all students' submissions are
"""
# The number of seconds it takes to run the test suite
TIMEOUT = {
1: 15,
2: 30,
3: 30,
}
# ANSI color
ANSI_RED = '\033[31m'
ANSI_GREEN = '\033[32m'
ANSI_RESET = '\033[0m'
# test state
PASSED = 1
FAILED = 0
def check_output(out, test, ofs):
error = False
test_passed = False
out.seek(ofs)
# read from ofs to EOF
for line in out:
if ANSI_GREEN+"passed "+ANSI_RESET+test in line:
test_passed = True
if "ERROR" in line or "Assertion failed" in line or "PANIC" in line:
error = True # can't break here, need to finish reading to EOF
return test_passed and not error
def test_summary(test_stats, lab):
score = 0
if lab == 1:
for test, result in test_stats.items():
if result == PASSED:
score += 10
assert score <= 100
if lab == 2:
for test, result in test_stats.items():
if result == PASSED:
score += 10
if "pipe-robust" in test:
score += 10
elif "race-test" in test:
score += 10
assert score <= 100
if lab == 3:
for test, result in test_stats.items():
if result == PASSED:
score += 10
if "cow-fork" in test:
score += 20
assert score <= 100
print("lab{0}test score: {1}/100".format(lab, score))
def main():
parser = argparse.ArgumentParser(description="Run osv tests")
parser.add_argument('lab_number', type=int, help='lab number')
args = parser.parse_args()
test_stats = {}
lab = args.lab_number
out = open("lab"+str(lab)+"output", "w+")
# retrieve list of tests for the lab
testdir = os.path.join(os.getcwd(), "user/lab"+str(lab))
out.write("test dir: "+testdir+"\n")
for test in os.listdir(testdir):
if test.endswith(".c"):
test = test[:-2]
out.write("running test: "+test+"\n")
print("running test: "+test)
# found test, run in a subprocess
try:
ofs = out.tell()
qemu = Popen(["make", "qemu", "--quiet"], stdin=PIPE, stdout=out, stderr=STDOUT, universal_newlines=True)
try:
qemu.communicate(timeout=TIMEOUT[lab], input=test+"\rquit\r")
except TimeoutExpired as e:
#print("Exceeded Timeout "+str(TIMEOUT[lab])+ " seconds")
qemu.terminate()
pass
# read output from test
if check_output(out, test, ofs) == True:
print(ANSI_GREEN + "passed " + ANSI_RESET + test)
test_stats[test] = PASSED
else:
test_stats[test] = FAILED
print(ANSI_RED + "failed "+ ANSI_RESET + test)
print('-------------------------------')
except BrokenPipeError:
print("fails to start qemu")
# This just means that QEMU never started
pass
# examine test stats
test_summary(test_stats, lab)
out.close()
if __name__ == "__main__":
main()