-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtester_gui.py
More file actions
executable file
·390 lines (338 loc) · 14.4 KB
/
tester_gui.py
File metadata and controls
executable file
·390 lines (338 loc) · 14.4 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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidgetItem, \
QMessageBox
from PyQt5.uic import loadUiType
import time
import traceback
import serial
from scpi.errors import TimeoutError
import fwtp_core
import fwtp_engine
main_form, base_class = loadUiType('mainwindow.ui')
def get_html_color_tags(color):
return '<font color="%s">' % color, '</font>'
HTML_RESULT_MAPS = {
fwtp_core.TEST_NA:
"%s N/A%s" % get_html_color_tags("blue"),
fwtp_core.TEST_ABORTED:
"%sABORTED%s" % get_html_color_tags("magenta"),
fwtp_core.TEST_OK:
"%s OK%s" % get_html_color_tags("green"),
fwtp_core.TEST_FAIL:
"%s FAIL%s" % get_html_color_tags("red")
}
class MainWindowImpl(QMainWindow, main_form):
""" MainWindows implementation class """
def load_tests(self):
""" Make list of all known tests """
for i in fwtp_core.TestSuiteConfig.KNOWN_TESTS_DESC.keys():
ti = fwtp_core.TestSuiteConfig.KNOWN_TESTS_DESC[i]
item = QListWidgetItem(ti.testname, self.listWidget)
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
item.setCheckState(Qt.Checked)
item.setToolTip(ti.INFO)
def load_testsuite(self):
""" Load the testsuite """
def new_test_decorator(path, ti, args):
""" proxy function to the actual GUI call """
return self.on_test_visit(path, ti, args)
fwtp_core.TestSuiteConfig.DECORATOR_DEFAULT = new_test_decorator
self.texec = fwtp_engine.TestExecutor(
open(self.testscript, "r").read())
def __init__(self, app, testscript, *args):
super(MainWindowImpl, self).__init__(*args)
self.app = app
self.test_ok = False
self.texec = None
self.started = False
self.tests = {}
self.args = {}
self.tests_debug = True
self.tr = None
self.testscript = testscript
self.setupUi(self)
self.load_testsuite()
self.load_tests()
self.cbHosts.addItems(["manual", "local"])
self.cbDevice.clear()
def enable_controls(self, en):
self.listWidget.setEnabled(en)
self.btAll.setEnabled(en)
self.btNone.setEnabled(en)
self.btFind.setEnabled(en)
self.cbHosts.setEnabled(en)
self.lnPort.setEnabled(en)
self.cbDevice.setEnabled(en)
self.cbCh1.setEnabled(en)
self.cbCh2.setEnabled(en)
self.spArfcn.setEnabled(en)
@pyqtSlot()
def on_btStartStop_clicked(self):
if self.started:
self.aborted = True
self.started = False
self.on_stop()
self.btStartStop.setText("Start")
self.enable_controls(True)
else:
self.aborted = False
self.started = True
self.btStartStop.setText("Stop")
self.enable_controls(False)
self.test_ok = True
# self.on_start()
# return
try:
self.on_start()
except serial.serialutil.SerialException as e:
self.test_ok = False
self.txConsole.appendHtml("<br><br><font color=\"red\">" +
"CMD57 exception:" +
"</font><br><b>%s</b>" % str(e))
# scpi.errors.TimeoutError
except:
if self.tests_debug:
traceback.print_exc()
self.test_ok = False
self.txConsole.appendHtml("<br><br><font color=\"red\">" +
"Unknown exception:" +
"</font><br><b>%s</b>" %
str(sys.exc_info()))
finally:
if not self.test_ok:
self.txConsole.appendHtml(
"<br><br><h2><font color=\"red\">" +
"TEST FAILED</font></h2>")
elif not self.started:
self.txConsole.appendHtml(
"<br><br><h2><font color=\"yellow\">" +
"TEST ABORTED</font></h2>")
else:
self.txConsole.appendHtml(
"<br><br><h2><font color=\"green\">" +
"TEST OK</font></h2>")
self.btStartStop.setText("Start")
self.enable_controls(True)
self.started = False
@pyqtSlot()
def on_btFind_clicked(self):
""" Get all DUTs in the local network """
self.cbHosts.clear()
self.cbHosts.addItems(["manual", "local"])
# ips = subprocess.check_output(
# 'nmap -sP 192.168.1.0/24 | grep "Nmap scan report for "',
# shell=True).decode('utf-8').replace("Nmap scan report" +
# " for ", "").split()
# if len(ips) == 0:
# self.txConsole.appendHtml("No hosts were found")
# return
#
# for ip in ips:
# self.app.processEvents()
# try:
# b = bts_test.BtsControlSsh(ip, 22)
# u = b.get_uname()
# s = b.get_umtrx_eeprom_val("serial")
# self.txConsole.appendHtml(
# "Host <b>%s</b>-<i>%s</i>-%s" % (ip, u, s))
# self.cbHosts.addItem(ip)
# except ConnectionRefusedError:
# self.txConsole.appendHtml("Host %s is unreachable" % ip)
# except socket.timeout:
# self.txConsole.appendHtml("Host %s connection " +
# "timed out" % ip)
@pyqtSlot()
def on_btAll_clicked(self):
self.set_to_all_tests(True)
@pyqtSlot()
def on_btNone_clicked(self):
self.set_to_all_tests(False)
@pyqtSlot()
def on_btBlink_clicked(self):
pass
# bts = self.create_bts()
# bts.bts_led_blink(1)
# QMessageBox.question(
# self, 'LED Blinking',
# 'LED is blinking on "%s"\nPress OK to stop blinking' %
# self.cbHosts.currentText(), QMessageBox.Ok)
# bts.bts_led_on()
def set_to_all_tests(self, en=True):
""" Enable or disable all known tests """
s = Qt.Checked if en else Qt.Unchecked
for i in range(self.listWidget.count()):
self.listWidget.item(i).setCheckState(s)
def on_test_visit(self, path, ti, kwargs):
""" visit function that print progress during test execution """
testname = "%s/%s" % (path, ti.testname)
func = ti.func
self.app.processEvents()
if not self.started:
self.txConsole.appendHtml(("<pre>[%s] <font color=\"red\">" +
"Aborting %60s" +
"</font></pre>") % (self.get_ts(),
testname))
return fwtp_core.TEST_ABORTED
if not self.tests[ti.testname]:
self.txConsole.appendHtml(("<pre>[%s] <font color=\"red\">" +
"Skipping %60s" +
"</font></pre>") % (self.get_ts(),
testname))
return fwtp_core.TEST_NA
self.txConsole.appendHtml(("<pre>[%s] <font color=\"blue\">" +
"Executing %60s" +
"</font></pre>") % (self.get_ts(),
testname))
try:
val = func(kwargs)
res = self.tr.check_test_result(path, ti, val, **kwargs)
except TimeoutError as e:
res = fwtp_core.TEST_FAIL
self.tr.set_test_result(path, ti, res)
self.txConsole.appendHtml(("<pre>[%s] <font color=\"red\">" +
"Timeout (%s) %60s" +
"</font></pre>") % (self.get_ts(),
e, testname))
except:
if self.tests_debug:
traceback.print_exc()
res = fwtp_core.TEST_ABORTED
self.tr.set_test_result(path, ti, res)
return res
def on_start(self):
""" Start the test execution """
self.app.processEvents()
self.tests = {self.listWidget.item(i).text():
self.listWidget.item(i).checkState() == Qt.Checked
for i in range(
len(fwtp_core.TestSuiteConfig.KNOWN_TESTS_DESC.keys()))}
dut = self.cbDevice.currentText()
arfcn = int(self.spArfcn.value())
# Initialize test results structure
self.tr = MyTestResults(self.on_test_result,
self.on_test_progress, self.on_enter_bundle)
# Initialize basic variables that used by the tests
self.args = {
"DUT": dut,
"ARFCN": arfcn,
"BTS_IP": self.cbHosts.currentText(),
"CMD57_PORT": self.lnPort.text(),
"TR": self.tr,
"UI": self,
"CHAN": ""
}
self.texec.run(self.args)
sm = self.tr.summary()
for res in sm:
self.txConsole.appendHtml(
"<pre>%s: %2d</pre>" % (HTML_RESULT_MAPS[res], sm[res]))
failed = sm.setdefault(fwtp_core.TEST_NA, 0) + sm.setdefault(
fwtp_core.TEST_ABORTED, 0) + sm.setdefault(fwtp_core.TEST_FAIL, 0)
if failed > 0:
self.txConsole.appendHtml(
"<br>%s<h1>WARNING! NOT ALL TEST PASSED!</h1>%s<br>" %
get_html_color_tags("red"))
if self.started:
self.test_ok = False
else:
return # Don't wirte JSON in case of abort
test_id = str(self.args["TEST_ID"]) if "TEST_ID" in self.args else None
if test_id is not None:
f = open("out/bts-test." + test_id + ".json", 'w')
f.write(self.tr.json())
f.close()
else:
self.txConsole.appendHtml(
("<br>%sTEST_ID variable wasn't declared during test, " +
"skiping writing results") % get_html_color_tags("red"))
def on_stop(self):
self.txConsole.appendPlainText("ABORT requested")
self.app.processEvents()
def ask(self, text):
self.app.processEvents()
if self.aborted:
return False
reply = QMessageBox.question(self, 'Message',
text, QMessageBox.Ok |
QMessageBox.Cancel, QMessageBox.Ok)
if reply == QMessageBox.Ok:
return True
# self.started = False
# self.on_stop()
return False
def on_enter_bundle(self, t, path, bundle, disc):
""" Function is called before test execution """
self.app.processEvents()
tcolot = get_html_color_tags("blue")
self.txConsole.appendHtml(("<pre>[%s] Bundle %s<b>%50s</b>%s: %s" +
"</pre>") % (self.get_ts(t),
tcolot[0],
"%s/%s" % (path, bundle),
tcolot[1],
disc))
self.app.processEvents()
def on_test_result(self, t, path, ti, result, value, old_result,
old_value, delta, reason=None):
""" Function is called when the test result is known """
if old_result == result or old_result is None:
tcolot = ("", "")
elif old_result != fwtp_core.TEST_OK and result == fwtp_core.TEST_OK:
tcolot = get_html_color_tags("green")
elif old_result == fwtp_core.TEST_OK and result != fwtp_core.TEST_OK:
tcolot = get_html_color_tags("red")
else:
tcolot = get_html_color_tags("orange")
sdelta = " [%+f]" % delta if delta is not None else ""
was = " (%7s)%s" % (HTML_RESULT_MAPS[old_result], sdelta) if \
old_result is not None else ""
exr = "" if reason is None else " (%s)" % reason
self.txConsole.appendHtml(("<pre>[%s] %s<b>%50s</b>%s: %s %s%s %s" +
"</pre>") % (self.get_ts(t),
tcolot[0],
ti.INFO,
tcolot[1],
HTML_RESULT_MAPS[result],
was,
exr,
"" if value is None else str(
value)))
@staticmethod
def get_ts(t=None):
""" Format time for the GUI """
if t is None:
t = time.time()
return time.strftime("%d-%m-%Y %H:%M:%S", time.localtime(t))
def on_test_progress(self, string):
""" General test progress update """
self.app.processEvents()
self.txConsole.appendHtml("<pre>%s</pre>" % string)
self.app.processEvents()
class MyTestResults(fwtp_engine.TestResults):
""" TestResults class that uses proxy function to do the actual GUI
updates """
def __init__(self, proxyfn, output_progressfn, enter_bundlefn):
super().__init__()
self.proxyfn = proxyfn
self.output_progressfn = output_progressfn
self.enter_bundlefn = enter_bundlefn
def print_result(self, t, path, ti, result, value, old_result,
old_value, delta, reason=None):
self.proxyfn(t, path, ti, result, value,
old_result, old_value, delta, reason)
def output_progress(self, string):
self.output_progressfn(string)
def enter_bundle(self, t, path, bundle, disc):
self.enter_bundlefn(t, path, bundle, disc)
if __name__ == '__main__':
app = QApplication(sys.argv)
fwtp_engine.TestExecutor.trace_calls = True
# load collection of test
# some IDE marks this as unreferenced module but we actually handle
# callback from the decorator inside
import testsuite_bts
main = MainWindowImpl(app, "./oc.yaml")
main.show()
sys.exit(app.exec_())