-
Notifications
You must be signed in to change notification settings - Fork 53
/
bft
executable file
·326 lines (289 loc) · 13.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
#!/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 junitxml
import json
# 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 board_decider, 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):
'''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))
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
# 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:
tftp_server = tftp_dev[0]['ipaddr']
tftp_port = tftp_dev[0].get('port', '22')
else:
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' ]
tftp_server = tftp_dev[0]['ipaddr']
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
# Connect to board
config.console = board_decider.board(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))
print_bold("dut device console = %s" % colored("black", 'grey'))
setup_legacy_devices(config)
if 'devices' in config.board:
setup_dynamic_devices(config)
# legacy wan device
if tftp_server is None and hasattr(config, 'wan'):
saved = config.console.logfile_read
config.console.logfile_read = None
config.console.tftp_server = config.wan.get_ip_addr("eth0")
config.console.tftp_username = "root"
config.console.tftp_password = "bigfoot1"
config.console.tftp_port = "22"
config.console.logfile_read = saved
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")
result_name = os.path.join(config.output_dir + "test_results.xml")
result_file = open(result_name, "w")
result = junitxml.JUnitXmlResult(result_file)
result.startTestRun()
tests_to_run = []
suite = unittest2.TestSuite()
# Add tests from specified suite
print_bold('==========\nTest suite "%s" has been specified, will attempt to run tests:' % config.TEST_SUITE)
import tests
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)
for x in tests_to_run:
suite.addTest(x)
print_bold('==========')
try:
print_bold(suite.run(result))
except KeyboardInterrupt:
print_bold("Run interrupted. Wrapping up...")
result.stopTestRun()
try:
config.console.close()
config.lan.close()
config.wan.close()
except Exception as e:
print(e)
print_bold("For some reason, could not close a connection.")
print_bold("Wrote %s" % result_name)
library.print_board_info(config.board)
result_file.close()
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")
# Write test result messages to a file
full_results = library.process_test_results(tests_to_run)
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:
if hasattr(t, 'override_kibana_name'):
n = t.override_kibana_name
else:
n = t.__class__.__name__
for k, v in t.logged.items():
info_for_remote_log[n + '-' + k] = v
if hasattr(t, 'result_grade'):
info_for_remote_log[n + "-result"] = t.result_grade
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()