blob: 695afd7409eb0ee48e0c14fa44405c79e5c5e713 [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
Dennis Kempind97cd722014-01-31 13:32:07 -080011import logging
Dennis Kempin725ee5b2012-08-10 14:17:15 -070012import math
Dennis Kempina44de382013-04-29 14:53:31 -070013import multiprocessing
Dennis Kempin725ee5b2012-08-10 14:17:15 -070014import os
Dennis Kempin725ee5b2012-08-10 14:17:15 -070015import sys
16
Dennis Kempin5495f8a2013-06-13 13:42:03 -070017from mtedit import MTEdit
Dennis Kempinaad2bbb2013-06-18 13:48:35 -070018from mtlib.log import Log
Dennis Kempinc2b59862013-02-20 14:02:25 -080019
Dennis Kempin725ee5b2012-08-10 14:17:15 -070020from table import Table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070021from test_case import TestCase
Dennis Kempin31c0fbd2012-07-30 13:20:37 -070022from test_factory import TestFactory
Dennis Kempin963fb152013-01-11 15:40:19 -080023from test_runner import ParallelTestRunner as TestRunner
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070024
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080025
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070026_help_text = """\
Dennis Kempinc2b59862013-02-20 14:02:25 -080027Multitouch Regression Test Suite:
28---------------------------------
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070029
Dennis Kempinc2b59862013-02-20 14:02:25 -080030$ %prog [all|glob]
31Executes tests. Either all of them or selected tests by providing a glob match
32In order to test for regressions use:
33$ %prog all --out filename
34make a change
35$ %prog all --ref filename
36Which will display the changes in test results compared to before the change
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070037
Dennis Kempinc2b59862013-02-20 14:02:25 -080038$ %prog testname -v %info%
39Run test and display information. %info% can be:
40- a or activity: to view the touchpad activity in mtedit
41- g or gestures: to view the generated gestures
42- al or activity-log: to view the generated activity log
43- gl or gestures-log: to view the generated gestures log
44- el or evdev-log: to view the generated evdev log
45
46$ %prog test_name -c %source%
47Create a new test case from %source%. Source can be:
48- A feedback URL
49- A device IP
50- The path to an activity log file
51When using a file name test.log, %prog will look at test.log.evdev
52for the evdev log file. You can optionally supply -e to override this path.
53%prog will display an URL where you can trim the log file, the trimmed
54log file will then be used to create the new test case. Specify --no-edit in
55case you do not want to use the original files without trimming.
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070056
Dennis Kempin462c8752013-04-04 16:01:54 -070057$ %prog test_name --gdb
58Run the test using gdb for debugging the gestures library
59
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070060General Info:
61-------------
62testname arguments:
63Tests are always names as [platform]/[name of test case]. You can find the tests
64available in the tests folder.
65For example: lumpy/scroll_test
66
67Tests Folder:
68The tests folder contains a folder for each platform and all the test cases.
69Each test case is made up of 3 files:
70[testcase].py which contains the validation script
71[testcase].log which contains the input_event log
72[testcase].props which contains user_defined properties passed to gestures lib.
73
74Platform folders:
75To add a new platform you need to add a new folder to the Tests folder, and
76generate a platform.dat file. This can be done using the evemu-describe tool
77on the target platform:
78
79$ gmerge utouch-evemu
80$ evemu-describe /path/to/device > platform.dat
81"""
82
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080083
84def Compile():
85 if "SRC_DIR" not in os.environ:
86 print "Requires SRC_DIR env-var. Re-run $ sudo make setup-in-place"
87 sys.exit(-1)
88
89 dir = os.environ["SRC_DIR"]
90 print "Recompiling gestures/libevdev/replay..."
Dennis Kempina44de382013-04-29 14:53:31 -070091 process = Popen(["make", "-j", str(multiprocessing.cpu_count()),
92 "in-place"], cwd=dir, stdout=PIPE, stderr=STDOUT)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080093 ret = process.wait()
94 if ret != 0:
95 print process.stdout.read()
96 sys.exit(-1)
97
98
Dennis Kempin1479e742012-07-31 17:31:01 -070099def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -0800100 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700101 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -0700102 cases = runner.DiscoverTestCases(glob)
103
104 for case in cases:
105 print "###", case.name
106 report = verifier.Verify(case)
107 print report
108
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800109
110def Run(glob, out_file=None, ref_file=None, autotest=False):
111 if not autotest:
112 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700113 print "Running tests..."
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700114 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700115 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700116
Dennis Kempin97397c62013-02-28 10:49:59 -0800117 # load reference
118 ref = {}
119 if ref_file:
120 ref = json.load(open(ref_file))
121
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700122 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700123 sorted_results_items = sorted(results.items())
124 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800125 if len(results) > 1:
126 # only print reports for regressions or failed tests
127 if key in ref:
128 delta = value["score"] - ref[key]["score"]
129 if math.fabs(delta) < 1e-10:
130 continue
131 elif value["result"] == "success":
132 continue
133
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700134 print "### Validation report for", key
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800135 print value["description"]
136 if value["disabled"]:
137 print "DISABLED"
138 else:
139 print value["logs"]["validation"]
140 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700141
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700142 # format result table
143 table = Table()
144 table.title = "Test Results"
145 table.header("Test", "reference score", "new score", "delta")
146
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800147 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700148 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700149 def ResultStr(value):
150 # format result to string
151 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700152 msg = "success" if value["score"] >= 0.5 else "bad"
153 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700154 else:
155 return value["result"]
156
157 # format reference and delta column
158 ref_score = ""
159 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700160 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700161 ref_score = ResultStr(ref[key])
162 delta = value["score"] - ref[key]["score"]
163 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700164 # default color
165 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700166 elif delta < 0:
167 regression = True
168 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700169 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700170 else:
171 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700172 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700173 table.row(key, ref_score, ResultStr(value), delta_str)
174
175 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700176
Dennis Kempin12968302012-08-09 15:37:09 -0700177 if out_file:
178 json.dump(results, open(out_file, "w"), indent=2)
179 print "results stored in:", out_file
180
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700181 if regression:
182 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
183 exit(-1)
184
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800185
Dennis Kempin64f513f2012-08-10 15:09:18 -0700186def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800187 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700188 if file:
189 data = json.load(open(file))
190 results = data[test_name]
191 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700192 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700193 data = runner.RunAll(test_name)
194 results = data[test_name]
195
196 if what == "gestures-log":
197 print results["logs"]["gestures"]
198 elif what == "evdev-log":
199 print results["logs"]["evdev"]
200 elif what == "activity-log":
201 print results["logs"]["activity"]
202 elif what == "gestures":
203 print results["gestures"]
204 elif what == "events":
205 print results["events"]
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800206 elif what == "activity":
207 log = Log(activity=results["logs"]["activity"])
208 editor = MTEdit()
209 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700210
Dennis Kempin462c8752013-04-04 16:01:54 -0700211def GDB(test_name):
212 Compile()
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700213 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin462c8752013-04-04 16:01:54 -0700214 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800215
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800216def Add(testname, log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700217 """
218 Adds a new test case.
219 """
220 # determine test name from activity_log name
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700221 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800222 case = factory.CreateTest(testname, log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800223 if case:
224 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700225
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800226
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700227def Main():
228 """
229 Main entry point for the console interface
230 """
231
232 # setup paths from environment variables
233 if "TESTS_DIR" not in os.environ:
234 print "Require TESTS_DIR environment variable"
235 exit(-1)
236
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700237 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700238
Dennis Kempinc2b59862013-02-20 14:02:25 -0800239 parser = OptionParser(usage=_help_text)
240 parser.add_option("-c", "--create",
241 dest="create", default=None,
242 help="create new test case from URL/IP or log file")
243 parser.add_option("-e", "--evdev",
244 dest="evdev", default=None,
245 help="path to evdev log for creating a new test")
246 parser.add_option("-v", "--view",
247 dest="view", default=None,
248 help="view generated gestures(g), activity in mtedit(a) " +
249 "gestures-log(gl), evdev-log(el) or activity-log(al)")
250 parser.add_option("-r", "--ref",
251 dest="ref", default=None,
252 help="reference test results for detecting regressions")
253 parser.add_option("-o", "--out",
254 dest="out", default=None,
255 help="output test results to file.")
256 parser.add_option("-n", "--new",
257 dest="new", action="store_true", default=False,
258 help="Create new device logs before downloading. " +
259 "[Default: False]")
260 parser.add_option("--no-edit",
261 dest="noedit", action="store_true", default=False,
262 help="Skip editing when creating tests. Add original log " +
263 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700264 parser.add_option("--autotest",
265 dest="autotest", action="store_true", default=False,
266 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700267 parser.add_option("--gdb",
268 dest="gdb", action="store_true", default=False,
269 help="Run the test case in GDB")
Dennis Kempind97cd722014-01-31 13:32:07 -0800270 parser.add_option("--verbose",
271 dest="verbose", action="store_true", default=False,
272 help="Verbose debug output")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800273 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700274 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700275 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700276
Dennis Kempinc2b59862013-02-20 14:02:25 -0800277 if len(args) == 0:
278 test_name = "all"
279 elif len(args) == 1:
280 test_name = args[0]
281 else:
282 parser.print_help()
283 exit(-1)
284
Dennis Kempind97cd722014-01-31 13:32:07 -0800285 level = logging.INFO if options.verbose else logging.WARNING
286 logging.basicConfig(level=level)
287
Dennis Kempinc2b59862013-02-20 14:02:25 -0800288 if options.create:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800289 # obtain trimmed log data
290 original_log = Log(options.create, options)
291 if options.noedit:
292 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700293 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700294 editor = MTEdit()
Dennis Kempinc2b59862013-02-20 14:02:25 -0800295 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700296
Dennis Kempinc2b59862013-02-20 14:02:25 -0800297 # pass to touchtests
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800298 Add(test_name, log, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800299
300 elif options.view:
301 view = options.view
302 if view == "g":
303 view = "gestures"
304 elif view == "gl":
305 view = "gestures-log"
306 elif view == "el":
307 view = "evdev-log"
308 elif view == "al":
309 view = "activity-log"
310 elif view == "a":
311 view = "activity"
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800312 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700313 elif options.gdb:
314 GDB(test_name)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700315 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800316 Run(test_name, options.out, options.ref, options.autotest)
317
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700318
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700319if __name__ == "__main__":
320 Main()