forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·349 lines (270 loc) · 10.6 KB
/
test.py
File metadata and controls
executable file
·349 lines (270 loc) · 10.6 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
import subprocess
import sys
import os
import argparse
import json
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1) # line buffering
sys.path.insert(0, ".")
from prettify import color as pretty
import validate_test
import validate_all
startdir = os.getcwd()
test_categories = ['fs', 'hw', 'kernel', 'mod', 'net', 'performance', 'platform', 'posix', 'stl', 'util']
test_types = ['integration', 'stress', 'unit', 'examples']
"""
Script used for running all the valid tests in the terminal.
"""
parser = argparse.ArgumentParser(
description="IncludeOS testrunner. By default runs all the valid integration tests \
found in subfolders, the stress test and all unit tests.")
parser.add_argument("-c", "--clean-all", dest="clean", action="store_true",
help="Run make clean before building test")
parser.add_argument("-s", "--skip", nargs="*", dest="skip", default=[],
help="Tests to skip. Valid names: 'unit' (all unit tests), \
'stress' (stresstest), 'integration' (all integration tests), examples \
or the name of a single integration test folder (leaf node name, e.g. 'udp') ")
parser.add_argument("-t", "--tests", nargs="*", dest="tests", default=[],
help="Tests to do. Valid names: see '--skip' ")
parser.add_argument("-f", "--fail-early", dest="fail", action="store_true",
help="Exit on first failed test")
args = parser.parse_args()
test_count = 0
def print_skipped(tests):
for test in tests:
if test.skip_:
print pretty.WARNING("* Skipping " + test.name_)
print " Reason: {0:40}".format(test.skip_reason_)
class Test:
""" A class to start a test as a subprocess and pretty-print status """
def __init__(self, path, clean=False, command=['sudo', '-E', 'python', 'test.py'], name=None):
self.command_ = command
self.proc_ = None
self.path_ = path
self.output_ = None
# Extract category and type from the path variable
# Category is linked to the top level folder e.g. net, fs, hw
# Type is linked to the type of test e.g. integration, unit, stress
if self.path_ == 'stress':
self.category_ = 'stress'
self.type_ = 'stress'
elif self.path_ == 'examples':
self.category_ = 'examples'
self.type_ = 'examples'
elif self.path_ == 'mod/gsl':
self.category_ = 'mod'
self.type_ = 'mod'
elif self.path_ == '.':
self.category_ = 'unit'
self.type_ = 'unit'
else:
self.category_ = self.path_.split('/')[-3]
self.type_ = self.path_.split('/')[-2]
if not name:
self.name_ = path
else:
self.name_ = name
# Check if the test is valid or not
self.check_valid()
if clean:
subprocess.check_output(["make","clean"])
print pretty.C_GRAY + "\t Cleaned, now start... ", pretty.C_ENDC
def __str__(self):
""" Print output about the test object """
return ('name_: {x[name_]} \n'
'path_: {x[path_]} \n'
'command_: {x[command_]} \n'
'proc_: {x[proc_]} \n'
'output_: {x[output_]} \n'
'category_: {x[category_]} \n'
'type_: {x[type_]} \n'
'skip: {x[skip_]} \n'
'skip_reason: {x[skip_reason_]} \n'
).format(x=self.__dict__)
def start(self):
os.chdir(startdir + "/" + self.path_)
self.proc_ = subprocess.Popen(self.command_, shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
os.chdir(startdir)
return self
def print_start(self):
print "* {0:66} ".format(self.name_),
sys.stdout.flush()
def wait_status(self):
self.print_start()
# Start and wait for the process
self.output_ = self.proc_.communicate()
if self.proc_.returncode == 0:
print pretty.PASS_INLINE()
else:
print pretty.FAIL_INLINE()
print pretty.INFO("Process stdout")
print pretty.DATA(self.output_[0])
print pretty.INFO("Process stderr")
print pretty.DATA(self.output_[1])
return self.proc_.returncode
def check_valid(self):
""" Will check if a test is valid. The following points can declare a test valid:
1. Contains the files required
2. Not listed in the skipped_tests.json
3. Not listed in the args.skip cmd line argument
Arguments:
self: Class function
"""
# Test 1
if not validate_test.validate_path(self.path_, verb = False):
self.skip_ = True
self.skip_reason_ = 'Failed validate_test, missing files'
return
# Test 2
# Figure out if the test should be skipped
skip_json = json.loads(open("skipped_tests.json").read())
for skip in skip_json:
if self.path_ == skip['name']:
self.skip_ = True
self.skip_reason_ = 'Defined in skipped_tests.json'
return
# Test 3
if self.path_ in args.skip or self.category_ in args.skip:
self.skip_ = True
self.skip_reason_ = 'Defined by cmd line argument'
return
self.skip_ = False
self.skip_reason_ = None
return
def unit_tests():
"""Perform unit tests"""
global test_count
test_count += 1
if ("unit" in args.skip):
print pretty.WARNING("Unit tests skipped")
return 0
print pretty.HEADER("Building and running unit tests")
build_status = Test(".", name="Unit test build", command=["make"], clean = args.clean).start().wait_status()
unit_status = Test(".", name="Unit tests", command = ["./test.lest"]).start().wait_status()
if (build_status or unit_status) and args.fail:
print pretty.FAIL("Unit tests failed")
sys.exit(max(build_status, unit_status))
return max(build_status, unit_status)
def stress_test():
"""Perform stresstest"""
global test_count
test_count += 1
if ("stress" in args.skip):
print pretty.WARNING("Stress test skipped")
return 0
if (not validate_test.validate_path("stress")):
raise Exception("Stress test failed validation")
print pretty.HEADER("Starting stress test")
stress = Test("stress", clean = args.clean).start()
if (stress and args.fail):
print pretty.FAIL("Stress test failed")
sys.exit(stress)
return 1 if stress.wait_status() else 0
def examples_working():
global test_count
if ("examples" in args.skip):
print pretty.WARNING("Examples test skipped")
return 0
examples_dir = '../examples'
dirs = os.walk(examples_dir).next()[1]
print pretty.HEADER("Building " + str(len(dirs)) + " examples")
test_count += len(dirs)
fail_count = 0
for directory in dirs:
example = examples_dir + "/" + directory
print "Building Example ", example
build = Test(example, command = ["make"], name = directory + " build").start().wait_status()
run = 0 #TODO: Make a 'test' folder for each example, containing test.py, vm.json etc.
fail_count += 1 if build or run else 0
return fail_count
def integration_tests(tests):
""" Function that runs the tests that are passed to it.
Filters out any invalid tests before running
Arguments:
tests: List containing test objects to be run
Returns:
integer: Number of tests that failed
"""
# Only run the valid tests
tests = [ x for x in tests if not x.skip_ and x.type_ == 'integration' ]
# Print info before starting to run
print pretty.HEADER("Starting " + str(len(tests)) + " integration test(s)")
for test in tests:
print pretty.INFO("Test"), "starting", test.name_
processes = []
fail_count = 0
global test_count
test_count += len(tests)
# Start running tests in parallell
for test in tests:
processes.append(test.start())
# Collect test results
print pretty.HEADER("Collecting integration test results")
for p in processes:
fail_count += 1 if p.wait_status() else 0
# Exit early if any tests failed
if fail_count and args.fail:
print pretty.FAIL(str(fail_count) + "integration tests failed")
sys.exit(fail_count)
return fail_count
def find_leaf_nodes():
""" Used to find all leaf nodes in the test directory,
this is to help identify all possible test directories.
Only looks in folders that actually store tests
Returns:
List: list of string with path to all leaf nodes
"""
leaf_nodes = []
for dirpath, dirnames, filenames in os.walk('.'):
# Will now skip any path that is not defined as a test category
# or ends with unit or integration -> no tests in those folders were
# created
if dirpath[2:].split('/')[0] in test_categories and dirpath.split('/')[-1] not in ['unit', 'integration']:
if len(dirpath[2:].split('/')) <= 3 and not dirnames:
leaf_nodes.append(dirpath[2:])
return leaf_nodes
def main():
# Find leaf nodes
leaves = find_leaf_nodes()
# Populate test objects
all_tests = [ Test(path) for path in leaves ]
# Figure out which tests are to be run
test_categories_to_run = []
test_types_to_run = []
if args.tests:
for argument in args.tests:
if argument in test_categories and argument not in args.skip:
test_categories_to_run.append(argument)
elif argument in test_types and argument not in args.skip:
test_types_to_run.append(argument)
else:
print 'Test specified is not recognised, exiting'
sys.exit(1)
else:
test_types_to_run = test_types
if test_categories_to_run:
# This means that a specific category has been requested
specific_tests = [ test for test in all_tests if test.category_ in test_categories_to_run ]
# Print which tests are skipped
print_skipped(specific_tests)
# Run the tests
integration = integration_tests(specific_tests)
else:
# Print which tests are skipped
print_skipped(all_tests)
# Run the tests
integration = integration_tests(all_tests) if "integration" in test_types_to_run else 0
stress = stress_test() if "stress" in test_types_to_run else 0
unit = unit_tests() if "unit" in test_types_to_run else 0
examples = examples_working() if "examples" in test_types_to_run else 0
status = max(integration, stress, unit, examples)
if (status == 0):
print pretty.SUCCESS(str(test_count - status) + " / " + str(test_count)
+ " tests passed, exiting with code 0")
else:
print pretty.FAIL(str(status) + " / " + str(test_count) + " tests failed ")
sys.exit(status)
if __name__ == '__main__':
main()