blob: 486045d86110f1ebd4ad26df9408659d6dc17f5c [file] [log] [blame]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4#
5# This module is the main module for the console interface. It takes care
6# of parsing the command line arguments and formating the output
Dennis Kempinc2b59862013-02-20 14:02:25 -08007from optparse import OptionParser
Dennis Kempin1c7ffab2013-02-28 11:43:45 -08008from subprocess import Popen, PIPE, STDOUT
Dennis Kempinc2b59862013-02-20 14:02:25 -08009from tempfile import NamedTemporaryFile
Dennis Kempin725ee5b2012-08-10 14:17:15 -070010import json
11import math
Dennis Kempina44de382013-04-29 14:53:31 -070012import multiprocessing
Dennis Kempin725ee5b2012-08-10 14:17:15 -070013import os
Dennis Kempin725ee5b2012-08-10 14:17:15 -070014import sys
15
Dennis Kempinc2b59862013-02-20 14:02:25 -080016from mtedit.editor import LogEditor
17from mtedit.log import Log
18
Dennis Kempin725ee5b2012-08-10 14:17:15 -070019from table import Table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070020from test_case import TestCase
Dennis Kempin31c0fbd2012-07-30 13:20:37 -070021from test_factory import TestFactory
Dennis Kempin963fb152013-01-11 15:40:19 -080022from test_runner import ParallelTestRunner as TestRunner
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070023
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080024
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070025_help_text = """\
Dennis Kempinc2b59862013-02-20 14:02:25 -080026Multitouch Regression Test Suite:
27---------------------------------
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070028
Dennis Kempinc2b59862013-02-20 14:02:25 -080029$ %prog [all|glob]
30Executes tests. Either all of them or selected tests by providing a glob match
31In order to test for regressions use:
32$ %prog all --out filename
33make a change
34$ %prog all --ref filename
35Which will display the changes in test results compared to before the change
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070036
Dennis Kempinc2b59862013-02-20 14:02:25 -080037$ %prog testname -v %info%
38Run test and display information. %info% can be:
39- a or activity: to view the touchpad activity in mtedit
40- g or gestures: to view the generated gestures
41- al or activity-log: to view the generated activity log
42- gl or gestures-log: to view the generated gestures log
43- el or evdev-log: to view the generated evdev log
44
45$ %prog test_name -c %source%
46Create a new test case from %source%. Source can be:
47- A feedback URL
48- A device IP
49- The path to an activity log file
50When using a file name test.log, %prog will look at test.log.evdev
51for the evdev log file. You can optionally supply -e to override this path.
52%prog will display an URL where you can trim the log file, the trimmed
53log file will then be used to create the new test case. Specify --no-edit in
54case you do not want to use the original files without trimming.
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070055
Dennis Kempin462c8752013-04-04 16:01:54 -070056$ %prog test_name --gdb
57Run the test using gdb for debugging the gestures library
58
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070059General Info:
60-------------
61testname arguments:
62Tests are always names as [platform]/[name of test case]. You can find the tests
63available in the tests folder.
64For example: lumpy/scroll_test
65
66Tests Folder:
67The tests folder contains a folder for each platform and all the test cases.
68Each test case is made up of 3 files:
69[testcase].py which contains the validation script
70[testcase].log which contains the input_event log
71[testcase].props which contains user_defined properties passed to gestures lib.
72
73Platform folders:
74To add a new platform you need to add a new folder to the Tests folder, and
75generate a platform.dat file. This can be done using the evemu-describe tool
76on the target platform:
77
78$ gmerge utouch-evemu
79$ evemu-describe /path/to/device > platform.dat
80"""
81
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080082
83def Compile():
84 if "SRC_DIR" not in os.environ:
85 print "Requires SRC_DIR env-var. Re-run $ sudo make setup-in-place"
86 sys.exit(-1)
87
88 dir = os.environ["SRC_DIR"]
89 print "Recompiling gestures/libevdev/replay..."
Dennis Kempina44de382013-04-29 14:53:31 -070090 process = Popen(["make", "-j", str(multiprocessing.cpu_count()),
91 "in-place"], cwd=dir, stdout=PIPE, stderr=STDOUT)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080092 ret = process.wait()
93 if ret != 0:
94 print process.stdout.read()
95 sys.exit(-1)
96
97
Dennis Kempin1479e742012-07-31 17:31:01 -070098def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -080099 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
100 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -0700101 cases = runner.DiscoverTestCases(glob)
102
103 for case in cases:
104 print "###", case.name
105 report = verifier.Verify(case)
106 print report
107
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800108
109def Run(glob, out_file=None, ref_file=None, autotest=False):
110 if not autotest:
111 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700112 print "Running tests..."
Dennis Kempin774b0622013-02-08 13:14:54 -0800113 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700114 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700115
Dennis Kempin97397c62013-02-28 10:49:59 -0800116 # load reference
117 ref = {}
118 if ref_file:
119 ref = json.load(open(ref_file))
120
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700121 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700122 sorted_results_items = sorted(results.items())
123 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800124 if len(results) > 1:
125 # only print reports for regressions or failed tests
126 if key in ref:
127 delta = value["score"] - ref[key]["score"]
128 if math.fabs(delta) < 1e-10:
129 continue
130 elif value["result"] == "success":
131 continue
132
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700133 print "### Validation report for", key
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800134 print value["description"]
135 if value["disabled"]:
136 print "DISABLED"
137 else:
138 print value["logs"]["validation"]
139 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700140
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700141 # format result table
142 table = Table()
143 table.title = "Test Results"
144 table.header("Test", "reference score", "new score", "delta")
145
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800146 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700147 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700148 def ResultStr(value):
149 # format result to string
150 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700151 msg = "success" if value["score"] >= 0.5 else "bad"
152 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800153 elif value["disabled"]:
154 return "disabled"
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700155 else:
156 return value["result"]
157
158 # format reference and delta column
159 ref_score = ""
160 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700161 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700162 ref_score = ResultStr(ref[key])
163 delta = value["score"] - ref[key]["score"]
164 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700165 # default color
166 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700167 elif delta < 0:
168 regression = True
169 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700170 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700171 else:
172 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700173 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700174 table.row(key, ref_score, ResultStr(value), delta_str)
175
176 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700177
Dennis Kempin12968302012-08-09 15:37:09 -0700178 if out_file:
179 json.dump(results, open(out_file, "w"), indent=2)
180 print "results stored in:", out_file
181
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700182 if regression:
183 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
184 exit(-1)
185
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800186
Dennis Kempin64f513f2012-08-10 15:09:18 -0700187def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800188 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700189 if file:
190 data = json.load(open(file))
191 results = data[test_name]
192 else:
Dennis Kempin774b0622013-02-08 13:14:54 -0800193 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700194 data = runner.RunAll(test_name)
195 results = data[test_name]
196
197 if what == "gestures-log":
198 print results["logs"]["gestures"]
199 elif what == "evdev-log":
200 print results["logs"]["evdev"]
201 elif what == "activity-log":
202 print results["logs"]["activity"]
203 elif what == "gestures":
204 print results["gestures"]
205 elif what == "events":
206 print results["events"]
Dennis Kempinc2b59862013-02-20 14:02:25 -0800207 elif what == "return-activity":
208 return results["logs"]["activity"]
Dennis Kempin64f513f2012-08-10 15:09:18 -0700209
Dennis Kempin462c8752013-04-04 16:01:54 -0700210def GDB(test_name):
211 Compile()
212 runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
213 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800214
Dennis Kempin4e2e6892013-04-19 17:04:00 -0700215def Add(testname, activity_log, event_log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700216 """
217 Adds a new test case.
218 """
219 # determine test name from activity_log name
Dennis Kempin2eb653b2013-04-03 14:58:32 -0700220 factory = TestFactory(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"])
Dennis Kempin4e2e6892013-04-19 17:04:00 -0700221 case = factory.CreateTest(testname, activity_log, event_log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800222 if case:
223 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700224
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800225
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700226def Main():
227 """
228 Main entry point for the console interface
229 """
230
231 # setup paths from environment variables
232 if "TESTS_DIR" not in os.environ:
233 print "Require TESTS_DIR environment variable"
234 exit(-1)
235
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700236 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700237
Dennis Kempinc2b59862013-02-20 14:02:25 -0800238 parser = OptionParser(usage=_help_text)
239 parser.add_option("-c", "--create",
240 dest="create", default=None,
241 help="create new test case from URL/IP or log file")
242 parser.add_option("-e", "--evdev",
243 dest="evdev", default=None,
244 help="path to evdev log for creating a new test")
245 parser.add_option("-v", "--view",
246 dest="view", default=None,
247 help="view generated gestures(g), activity in mtedit(a) " +
248 "gestures-log(gl), evdev-log(el) or activity-log(al)")
249 parser.add_option("-r", "--ref",
250 dest="ref", default=None,
251 help="reference test results for detecting regressions")
252 parser.add_option("-o", "--out",
253 dest="out", default=None,
254 help="output test results to file.")
255 parser.add_option("-n", "--new",
256 dest="new", action="store_true", default=False,
257 help="Create new device logs before downloading. " +
258 "[Default: False]")
259 parser.add_option("--no-edit",
260 dest="noedit", action="store_true", default=False,
261 help="Skip editing when creating tests. Add original log " +
262 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700263 parser.add_option("--autotest",
264 dest="autotest", action="store_true", default=False,
265 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700266 parser.add_option("--gdb",
267 dest="gdb", action="store_true", default=False,
268 help="Run the test case in GDB")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800269 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700270 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700271 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700272
Dennis Kempinc2b59862013-02-20 14:02:25 -0800273 if len(args) == 0:
274 test_name = "all"
275 elif len(args) == 1:
276 test_name = args[0]
277 else:
278 parser.print_help()
279 exit(-1)
280
281 if options.create:
282 # create temporary files for storing activity and evdev log
283 tmp = NamedTemporaryFile("w")
284 tmp_evdev = NamedTemporaryFile("w")
285
286 # obtain trimmed log data
287 original_log = Log(options.create, options)
288 if options.noedit:
289 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700290 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800291 editor = LogEditor(persistent=False)
292 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700293
Dennis Kempinc2b59862013-02-20 14:02:25 -0800294 # write to temporary files
295 tmp.write(log.activity)
296 tmp.flush()
297 tmp_evdev.write(log.evdev)
298 tmp_evdev.flush()
299
300 # pass to touchtests
Dennis Kempin4e2e6892013-04-19 17:04:00 -0700301 Add(test_name, tmp.name, tmp_evdev.name, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800302
303 # cleanup
304 tmp.close()
305 tmp_evdev.close()
306
307 elif options.view:
308 view = options.view
309 if view == "g":
310 view = "gestures"
311 elif view == "gl":
312 view = "gestures-log"
313 elif view == "el":
314 view = "evdev-log"
315 elif view == "al":
316 view = "activity-log"
317 elif view == "a":
318 view = "activity"
319
320 if view == "activity":
321 activity = Get(test_name, "return-activity")
322 tmp = NamedTemporaryFile("w")
323 tmp.write(activity)
324 tmp.flush()
325 log = Log(tmp.name, options)
326 editor = LogEditor(persistent=False)
327 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700328 else:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800329 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700330 elif options.gdb:
331 GDB(test_name)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700332 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800333 Run(test_name, options.out, options.ref, options.autotest)
334
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700335
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700336if __name__ == "__main__":
337 Main()