forked from felipec/milaatu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner
executable file
·199 lines (160 loc) · 3.57 KB
/
runner
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
#!/usr/bin/env python
import os, sys
import json
from datetime import datetime
from optparse import OptionParser
import gobject
parser = OptionParser()
parser.add_option("-t", "--test")
parser.add_option("-f", "--file")
parser.add_option("-r", "--results", default="/tmp/results.json")
parser.add_option("-u", "--user", default="")
parser.add_option("-w", "--hardware", default="")
(options, args) = parser.parse_args()
user = options.user
hardware = options.hardware
tests_file = options.file
test_results = options.results
test_name = options.test
class Pipe(object):
def __init__(self):
r, w = os.pipe()
r, w = os.fdopen(r, 'r', 0), os.fdopen(w, 'w', 0)
self.r, self.w = r, w
def close_in(self):
self.r.close()
def close_out(self):
self.w.close()
def dump(self, obj):
json.dump(obj, self.w)
print >> self.w
def load(self):
s = self.r.readline()
if not s: return None
try:
return json.loads(s)
except ValueError:
print "bad data"
return None
def run_child():
gobject.threads_init()
while True:
data = p2.load()
if not data: break
base = data['base']
env = data['env']
if env:
var, val = env.rsplit('=')
os.putenv(var, val)
try:
mname = "tests." + base
__import__(mname)
module = sys.modules[mname]
t = module.test_class()
for kv in data['in'].iteritems():
setattr(t, *kv)
t.start()
if t.error:
data['error'] = t.error
if hasattr(t, 'out') and t.out:
data['out'] = t.out
if hasattr(t, 'checks') and t.checks:
data['checks'] = t.checks
total = len(t.checks)
result = 0.0
for x in t.checks.values():
result += 100 * float(x) / total
data['result'] = result
except ImportError, e:
data['error'] = "bad test '%s': %s" % (base, e)
p1.dump(data)
if env:
os.unsetenv(var)
sys.exit(0)
def respawn():
global p1, p2
p1 = Pipe()
p2 = Pipe()
pid = os.fork()
if pid:
# Parent
p1.close_out()
p2.close_in()
return pid
else:
# Child
p1.close_in()
p2.close_out()
run_child()
def get_date():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def run_test(t):
global pid
n = {}
n['name'] = t.pop('name')
n['base'] = t.pop('base')
n['in'] = t
n['env'] = t.pop('env', None)
p2.dump(n)
data = p1.load()
if not data:
p, s = os.waitpid(pid, os.P_NOWAIT)
pid = respawn()
data = n
data['error'] = "process died: signal %i" % s
elif data['in'].get('respawn', False):
pid = respawn()
if 'error' in data:
r = 0.0
elif 'result' in data:
r = data['result']
else:
r = 100.0
if data['in'].get('expected_failure', False):
r = 100.0 - r
data['result'] = r
data['date'] = get_date()
return data
if not tests_file:
tests_file = os.environ.get('MILAATU_TESTS_FILE')
if not tests_file:
tests_file = "tests.json"
run = {}
run['hw'] = hardware
run['user'] = user
run['date'] = get_date()
f = open(tests_file, "r")
tests = json.load(f)
results = []
pid = respawn()
to_run = None
for t in tests:
if test_name:
if t['name'] != test_name:
continue
to_run = t
break
else:
print "== %s ==" % t['name']
data = run_test(t)
results.append(data)
if test_name:
if not to_run:
print >> sys.stderr, "test not found"
sys.exit(-1)
else:
print "== %s ==" % t['name']
data = run_test(t)
if 'error' in data:
print >> sys.stderr, "error: %s" % data['error']
r = data['result']
if (r == 100.0):
e = 0
else:
print >> sys.stderr, "result: %d" % (r)
e = -1
print json.dumps(data, indent = 4, sort_keys = True)
sys.exit(e)
run['results'] = results
o = open(test_results, "w")
print >> o, json.dumps(run, indent = 4, sort_keys = True)