forked from mattsm/boardfarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bft
executable file
·404 lines (353 loc) · 16.7 KB
/
bft
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python
# Copyright (c) 2015
#
# All rights reserved.
#
# This file is distributed under the Clear BSD license.
# The full text can be found in LICENSE in the root directory.
import datetime
import inspect
import os
import random
import sys
import unittest2
import json
import hashlib
import time
# Put this directory into the python path, so
# that devices may be imported.
import site
site.addsitedir(os.path.dirname(os.path.realpath(__file__)))
from devices import debian, logstash, elasticlogger
def setup_legacy_devices(config):
'''This will setup legacy hard coded devices to into the config'''
config.wan = None
config.lan = None
config.wlan = None
config.wlan5g = None
config.wlan2g = None
if config.board.get('wan_device'):
config.wan = debian.DebianBox(config.board.get('wan_device'),
color='cyan', reboot=config.reboot_vms,
location=config.board.get('location'),
username=config.board.get('wan_username', "root"),
password=config.board.get('wan_password', "bigfoot1"),
port=config.board.get('wan_port', "22"))
if config.board.get('lan_device'):
config.lan = debian.DebianBox(config.board.get('lan_device'), color='blue', reboot=config.reboot_vms,
username=config.board.get('lan_username', "root"),
password=config.board.get('lan_password', "bigfoot1"),
port=config.board.get('lan_port', "22"))
if config.board.get('wlan_device'):
config.wlan = debian.DebianBox(config.board.get('wlan_device'), color='green', reboot=config.reboot_vms,
username=config.board.get('wlan_username', "root"),
password=config.board.get('wlan_password', "bigfoot1"),
port=config.board.get('wlan_port', "22"))
if config.board.get('5g_device'):
config.wlan5g = debian.DebianBox(config.board.get('5g_device'), color='grey', reboot=config.reboot_vms,
username=config.board.get('5g_username', "root"),
password=config.board.get('5g_password', "bigfoot1"),
port=config.board.get('5g_port', "22"))
if config.board.get('2g_device'):
config.wlan2g = debian.DebianBox(config.board.get('2g_device'), color='magenta', reboot=config.reboot_vms,
username=config.board.get('2g_username', "root"),
password=config.board.get('2g_password', "bigfoot1"),
port=config.board.get('2g_port', "22"))
def setup_dynamic_devices(config, env=None):
'''Sets up dynamic devices from devices node in JSON config file'''
config.devices = []
for device in config.board['devices']:
if device['type'] == 'debian':
d = debian.DebianBox(name=device.get('ipaddr', None),
color=device.get('color', 'black'),
reboot=config.reboot_vms,
username=device.get('username', "root"),
password=device.get('password', "bigfoot1"),
port=device.get('port', "22"),
pre_cmd_host=device.get('pre_cmd_host', None),
cmd=device.get('cmd', None),
post_cmd_host=device.get('post_cmd_host', None),
post_cmd=device.get('post_cmd', None),
cleanup_cmd=device.get('cleanup_cmd', None),
env=env,
lan_network=config.console.lan_network,
lan_gateway=config.console.lan_gateway,
config=device)
setattr(config, device['name'], d)
config.devices.append(device['name'])
else:
print("Unknown device type for %d", device)
def main():
'''Connect to devices, run tests, record results.'''
# Read command-line arguments
import arguments
config = arguments.parse()
import library
import devices
from termcolor import colored
from library import print_bold
os.environ["TERM"] = "dumb"
# Connect to any board in list
connected_to_board = False
random.shuffle(config.BOARD_NAMES)
for name in config.BOARD_NAMES:
try:
config.board = config.boardfarm_config[name]
except Exception as e:
print(e)
print("Error reading info about board %s from board farm configuration." % name)
break
print_bold("Connecting to board named = %s, type = %s ..." % (name, config.board['board_type']))
try:
# If we find a dynamic device with the tftpd-server option let's use that for the server
# otherwise we use the device with the 'wan' name... finally if we don't find an ip address let's
# manually figure it out later
tftp_server = None
tftp_port = "22"
try:
if 'devices' in config.board:
tftp_dev = [ x for x in config.board['devices'] if 'tftpd-server' in x.get('options', "") ]
if len(tftp_dev) == 1:
if 'ipaddr' in tftp_dev[0]:
tftp_server = tftp_dev[0]['ipaddr']
else:
tftp_server = tftp_dev[0]['name']
tftp_port = tftp_dev[0].get('port', '22')
elif len(tftp_dev) > 1:
print("WARN: more than one TFTP server is configured, please pick one")
elif tftp_server is None:
if len(tftp_dev) == 1:
tftp_dev = [ x for x in config.board['devices'] if x['name'] == 'wan' ]
if 'ipaddr' in tftp_dev[0]:
tftp_server = tftp_dev[0]['ipaddr']
else:
tftp_server = tftp_dev[0]['name']
tftp_port = tftp_dev[0].get('port', '22')
else:
print("WARN: more than one TFTP server is configured, please pick one")
except:
# if not by ipaddr let's connect to the lan device later and update
pass
uniqid = hashlib.md5("%0.100f" % time.time()).hexdigest()[:15]
env = {"wan_iface": "wan%s" % uniqid[:12],
"lan_iface": "lan%s" % uniqid[:12],
"uniq_id": uniqid}
# Connect to board
config.console = devices.board_decider(config.board['board_type'],
conn_cmd=config.board['conn_cmd'],
power_ip=config.board.get('powerip', None),
power_outlet=config.board.get('powerport', None),
web_proxy=config.board.get('lan_device', None),
tftp_server=config.board.get('wan_device', tftp_server),
tftp_username=config.board.get('wan_username', 'root'),
tftp_password=config.board.get('wan_password', 'bigfoot1'),
tftp_port=config.board.get('wan_port', tftp_port),
connection_type=config.board.get('connection_type', None),
ssh_password=config.board.get('ssh_password', None),
power_username=config.board.get('power_username', None),
power_password=config.board.get('power_password', None),
rootfs=config.ROOTFS,
kernel=config.KERNEL,
env=env)
print_bold("dut device console = %s" % colored("black", 'grey'))
setup_legacy_devices(config)
if 'devices' in config.board:
setup_dynamic_devices(config, env=env)
def get_tftp_config(dev):
saved = config.console.logfile_read
config.console.logfile_read = None
config.console.tftp_server = dev.get_interface_ipaddr("eth0")
config.console.tftp_username = "root"
config.console.tftp_password = "bigfoot1"
config.console.tftp_port = "22"
config.console.logfile_read = saved
# check devices after they start for tftpd-server option if
# if we still have not configured a tftp server
if tftp_server is None:
for x in config.board['devices']:
if 'tftpd-server' in x.get('options', ""):
get_tftp_config(getattr(config, x['name']))
# TODO: how do we handle multiple tftp servers, break for now
break
else:
# check if the tftp_server is an unresolved name and resolve the ip
for x in config.board['devices']:
if tftp_server == x.get('name', ""):
get_tftp_config(getattr(config, tftp_server))
# legacy wan device
if tftp_server is None and getattr(config, 'wan') is not None:
get_tftp_config(config.wan)
except Exception as e:
print(e)
connected_to_board = False
continue
connected_to_board = True
break
if not connected_to_board:
print_bold("Failed to connect to any board")
sys.exit(2)
try:
print_bold("Using Board %s, User %s" % (name, os.environ['BUILD_USER_ID']))
except:
print_bold("Using Board %s, User %s" % (name, os.environ['USER']))
# Make devices (board, lan, wan, available to tests easily)
devices.initialize_devices(config)
# Write board info to json file and stdout
config.board['station'] = name
print_bold('\n==========')
library.print_board_info(config.board)
# Run tests
os.environ['TEST_START_TIME'] = datetime.datetime.now().strftime("%s")
tests_to_run = []
# Add tests from specified suite
print_bold('==========\nTest suite "%s" has been specified, will attempt to run tests:' % config.TEST_SUITE)
import tests
tests.init(config)
import testsuites
for i, name in enumerate(testsuites.list_tests[config.TEST_SUITE]):
if isinstance(name, str):
test = getattr(tests, name)
else:
test = name
print_bold(" %s %s from %s" % (i+1, test.__name__, inspect.getfile(test)))
tests_to_run.append(test(config))
if hasattr(config, 'EXTRA_TESTS') and config.EXTRA_TESTS:
if tests_to_run[-1].__class__.__name__ == "Interact":
print_bold("Last test is interact in testsuite, removing")
tests_to_run.pop()
print_bold("Extra tests specified on command line:")
try:
for t in [getattr(tests, name) for name in config.EXTRA_TESTS]:
print_bold(" %s" % t)
tests_to_run.append(t(config))
except:
print_bold("Unable to find specified extra tests, aborting...")
sys.exit(1)
print_bold('==========')
try:
tests_pass = tests_fail = tests_skip = 0
curr_test = None
for test in tests_to_run:
curr_test = test
test.run()
curr_test = None
grade = getattr(test, "result_grade", None)
if grade == "OK" or grade == "Unexp OK":
tests_pass += 1
elif grade == "FAIL" or grade == "Exp FAIL":
tests_fail += 1
elif grade == "SKIP" or grade is None:
tests_skip += 1
except KeyboardInterrupt:
print_bold("Run interrupted. Wrapping up...")
if curr_test is not None:
curr_test.recover()
print_bold("Results run=%d failures=%d skipped=%d" % (tests_pass, tests_fail, tests_skip))
try:
config.console.close()
if 'devices' in config.board:
for device in config.devices:
getattr(config, device).close()
else:
if config.lan is not None:
config.lan.close()
if config.wan is not None:
config.wan.close()
except Exception as e:
print(e)
print_bold("For some reason, could not close a connection.")
library.print_board_info(config.board)
with open(os.path.join(config.output_dir, 'console.log'), 'w') as clog:
clog.write(config.console.log)
os.environ['TEST_END_TIME'] = datetime.datetime.now().strftime("%s")
# grab golden master results
try:
if config.golden is not None:
import requests
golden = requests.get(config.golden).json()
else:
golden = {}
except:
print_bold("Failed to fetch golden master results, skipping...")
golden = {}
# Write test result messages to a file
full_results = library.process_test_results(tests_to_run, golden)
json.dump(full_results,
open(os.path.join(config.output_dir + 'test_results.json'), 'w'),
indent=4,
sort_keys=True)
# run all analysis classes (post processing)
# also, never fail so we don't block automation
try:
import analysis
for cstr in dir(analysis):
c = getattr(analysis, cstr)
if inspect.isclass(c) and issubclass(c, analysis.Analysis):
c().analyze(config.console.log, config.output_dir)
except Exception as e:
if not issubclass(type(e), (StopIteration)):
print("Failed to run anaylsis:")
print(e)
# Try to remotely log information about this run
info_for_remote_log = dict(config.board)
info_for_remote_log.update(full_results)
try:
info_for_remote_log['duration'] = int(os.environ['TEST_END_TIME'])-int(os.environ['TEST_START_TIME'])
except:
pass
if hasattr(config, 'TEST_SUITE'):
info_for_remote_log['test_suite'] = str(config.TEST_SUITE)
# logstash cannot handle multi-level json, remove full test results
info_for_remote_log.pop('test_results', None)
# but we will add back specific test results data
for t in tests_to_run:
def prepare_results_for_kibana(test, prefix=""):
if hasattr(test, 'override_kibana_name'):
n = test.override_kibana_name
elif hasattr(test, 'name'):
n = test.name
else:
n = test.__class__.__name__
n = prefix + n
for k, v in test.logged.items():
info_for_remote_log[n + '-' + k] = v
if hasattr(test, 'result_grade'):
info_for_remote_log[n + "-result"] = test.result_grade
return n
prefix = prepare_results_for_kibana(t) + "-"
for subtest in t.subtests:
prepare_results_for_kibana(subtest, prefix=prefix)
try:
if config.logging_server is not None:
logstash.RemoteLogger(config.logging_server).log(info_for_remote_log)
except Exception as e:
print(e)
print("Unable to access logging_server specified in config. "
"Results stored only locally.")
try:
if config.elasticsearch_server is not None:
elasticlogger.ElasticsearchLogger(config.elasticsearch_server).log(info_for_remote_log)
else:
print("No elasticsearch_server specified in config. Results stored locally")
except Exception as e:
print(e)
print("Unable to store results to elasticsearch_server specified in config. "
"Results stored locally.")
# Create Pretty HTML output
import make_human_readable
try:
title_str = make_human_readable.get_title()
make_human_readable.xmlresults_to_html(full_results['test_results'], title=title_str,
output_name=os.path.join(config.output_dir, "results.html"),
board_info=config.board)
except Exception as e:
print(e)
print("Unable to create HTML results")
# Send url of pretty html results to MySQL build database
try:
library.send_results_to_myqsl(config.TEST_SUITE, config.output_dir)
except Exception as e:
print(e)
print("Unable to log results to mysql database.")
if __name__ == '__main__':
main()