blob: 1d3f786636378c43429553b54b29465c2b3aa75f [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 Kempin93a71412014-01-10 13:39:37 -080023from test_robot import RobotTestGenerator
Dennis Kempin963fb152013-01-11 15:40:19 -080024from test_runner import ParallelTestRunner as TestRunner
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070025
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080026
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070027_help_text = """\
Dennis Kempinc2b59862013-02-20 14:02:25 -080028Multitouch Regression Test Suite:
29---------------------------------
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070030
Dennis Kempinc2b59862013-02-20 14:02:25 -080031$ %prog [all|glob]
32Executes tests. Either all of them or selected tests by providing a glob match
33In order to test for regressions use:
34$ %prog all --out filename
35make a change
36$ %prog all --ref filename
37Which will display the changes in test results compared to before the change
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070038
Dennis Kempinc2b59862013-02-20 14:02:25 -080039$ %prog testname -v %info%
40Run test and display information. %info% can be:
41- a or activity: to view the touchpad activity in mtedit
42- g or gestures: to view the generated gestures
43- al or activity-log: to view the generated activity log
44- gl or gestures-log: to view the generated gestures log
45- el or evdev-log: to view the generated evdev log
46
47$ %prog test_name -c %source%
48Create a new test case from %source%. Source can be:
49- A feedback URL
50- A device IP
51- The path to an activity log file
52When using a file name test.log, %prog will look at test.log.evdev
53for the evdev log file. You can optionally supply -e to override this path.
54%prog will display an URL where you can trim the log file, the trimmed
55log file will then be used to create the new test case. Specify --no-edit in
56case you do not want to use the original files without trimming.
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070057
Dennis Kempin462c8752013-04-04 16:01:54 -070058$ %prog test_name --gdb
59Run the test using gdb for debugging the gestures library
60
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070061General Info:
62-------------
63testname arguments:
64Tests are always names as [platform]/[name of test case]. You can find the tests
65available in the tests folder.
66For example: lumpy/scroll_test
67
68Tests Folder:
69The tests folder contains a folder for each platform and all the test cases.
70Each test case is made up of 3 files:
71[testcase].py which contains the validation script
72[testcase].log which contains the input_event log
73[testcase].props which contains user_defined properties passed to gestures lib.
74
75Platform folders:
76To add a new platform you need to add a new folder to the Tests folder, and
77generate a platform.dat file. This can be done using the evemu-describe tool
78on the target platform:
79
80$ gmerge utouch-evemu
81$ evemu-describe /path/to/device > platform.dat
82"""
83
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080084
85def Compile():
86 if "SRC_DIR" not in os.environ:
87 print "Requires SRC_DIR env-var. Re-run $ sudo make setup-in-place"
88 sys.exit(-1)
89
90 dir = os.environ["SRC_DIR"]
91 print "Recompiling gestures/libevdev/replay..."
Dennis Kempina44de382013-04-29 14:53:31 -070092 process = Popen(["make", "-j", str(multiprocessing.cpu_count()),
93 "in-place"], cwd=dir, stdout=PIPE, stderr=STDOUT)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080094 ret = process.wait()
95 if ret != 0:
96 print process.stdout.read()
97 sys.exit(-1)
98
99
Dennis Kempin1479e742012-07-31 17:31:01 -0700100def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -0800101 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700102 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -0700103 cases = runner.DiscoverTestCases(glob)
104
105 for case in cases:
106 print "###", case.name
107 report = verifier.Verify(case)
108 print report
109
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800110
111def Run(glob, out_file=None, ref_file=None, autotest=False):
112 if not autotest:
113 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700114 print "Running tests..."
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700115 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700116 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700117
Dennis Kempin97397c62013-02-28 10:49:59 -0800118 # load reference
119 ref = {}
120 if ref_file:
121 ref = json.load(open(ref_file))
122
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700123 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700124 sorted_results_items = sorted(results.items())
125 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800126 if len(results) > 1:
127 # only print reports for regressions or failed tests
128 if key in ref:
129 delta = value["score"] - ref[key]["score"]
130 if math.fabs(delta) < 1e-10:
131 continue
132 elif value["result"] == "success":
133 continue
134
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700135 print "### Validation report for", key
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800136 print value["description"]
137 if value["disabled"]:
138 print "DISABLED"
139 else:
140 print value["logs"]["validation"]
141 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700142
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700143 # format result table
144 table = Table()
145 table.title = "Test Results"
146 table.header("Test", "reference score", "new score", "delta")
147
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800148 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700149 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700150 def ResultStr(value):
151 # format result to string
152 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700153 msg = "success" if value["score"] >= 0.5 else "bad"
154 return "%s (%.4f)" % (msg, value["score"])
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 Kempinaad2bbb2013-06-18 13:48:35 -0700193 runner = TestRunner(os.environ["TESTS_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 Kempin3b1fca72014-01-09 11:54:59 -0800207 elif what == "activity":
208 log = Log(activity=results["logs"]["activity"])
209 editor = MTEdit()
210 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700211
Dennis Kempin462c8752013-04-04 16:01:54 -0700212def GDB(test_name):
213 Compile()
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700214 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin462c8752013-04-04 16:01:54 -0700215 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800216
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800217def Add(testname, log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700218 """
219 Adds a new test case.
220 """
221 # determine test name from activity_log name
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700222 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800223 case = factory.CreateTest(testname, log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800224 if case:
225 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700226
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800227
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700228def Main():
229 """
230 Main entry point for the console interface
231 """
232
233 # setup paths from environment variables
234 if "TESTS_DIR" not in os.environ:
235 print "Require TESTS_DIR environment variable"
236 exit(-1)
237
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700238 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700239
Dennis Kempinc2b59862013-02-20 14:02:25 -0800240 parser = OptionParser(usage=_help_text)
241 parser.add_option("-c", "--create",
242 dest="create", default=None,
243 help="create new test case from URL/IP or log file")
244 parser.add_option("-e", "--evdev",
245 dest="evdev", default=None,
246 help="path to evdev log for creating a new test")
247 parser.add_option("-v", "--view",
248 dest="view", default=None,
249 help="view generated gestures(g), activity in mtedit(a) " +
250 "gestures-log(gl), evdev-log(el) or activity-log(al)")
251 parser.add_option("-r", "--ref",
252 dest="ref", default=None,
253 help="reference test results for detecting regressions")
254 parser.add_option("-o", "--out",
255 dest="out", default=None,
256 help="output test results to file.")
257 parser.add_option("-n", "--new",
258 dest="new", action="store_true", default=False,
259 help="Create new device logs before downloading. " +
260 "[Default: False]")
261 parser.add_option("--no-edit",
262 dest="noedit", action="store_true", default=False,
263 help="Skip editing when creating tests. Add original log " +
264 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700265 parser.add_option("--autotest",
266 dest="autotest", action="store_true", default=False,
267 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700268 parser.add_option("--gdb",
269 dest="gdb", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800270 help="Run the test case in GDB"),
Dennis Kempind97cd722014-01-31 13:32:07 -0800271 parser.add_option("--verbose",
272 dest="verbose", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800273 help="Verbose debug output"),
274 parser.add_option("--robot",
275 dest="robot", default=None,
276 help="Instruct robot to generate test cases")
277 parser.add_option("--overwrite",
278 dest="overwrite", action="store_true", default=False,
279 help="(use with --robot) Overwrite existing tests")
280 parser.add_option("--no-calib",
281 dest="nocalib", action="store_true", default=False,
282 help="(use with --robot) Skip calibration step.")
283 parser.add_option("--manual-fingertips",
284 dest="manual_fingertips", action="store_true",
285 default=False,
286 help="(use with --robot) Use fingertips that are present.")
287 parser.add_option("--slow",
288 dest="slow", action="store_true", default=False,
289 help="(use with --robot) Force slow movement.")
290
Dennis Kempinc2b59862013-02-20 14:02:25 -0800291 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700292 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700293 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700294
Dennis Kempinc2b59862013-02-20 14:02:25 -0800295 if len(args) == 0:
296 test_name = "all"
297 elif len(args) == 1:
298 test_name = args[0]
299 else:
300 parser.print_help()
301 exit(-1)
302
Dennis Kempind97cd722014-01-31 13:32:07 -0800303 level = logging.INFO if options.verbose else logging.WARNING
304 logging.basicConfig(level=level)
305
Dennis Kempinc2b59862013-02-20 14:02:25 -0800306 if options.create:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800307 # obtain trimmed log data
308 original_log = Log(options.create, options)
309 if options.noedit:
310 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700311 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700312 editor = MTEdit()
Dennis Kempinc2b59862013-02-20 14:02:25 -0800313 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700314
Dennis Kempinc2b59862013-02-20 14:02:25 -0800315 # pass to touchtests
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800316 Add(test_name, log, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800317
318 elif options.view:
319 view = options.view
320 if view == "g":
321 view = "gestures"
322 elif view == "gl":
323 view = "gestures-log"
324 elif view == "el":
325 view = "evdev-log"
326 elif view == "al":
327 view = "activity-log"
328 elif view == "a":
329 view = "activity"
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800330 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700331 elif options.gdb:
332 GDB(test_name)
Dennis Kempin93a71412014-01-10 13:39:37 -0800333 elif options.robot:
334 generator = RobotTestGenerator(options.robot, not options.nocalib,
335 options.slow, options.manual_fingertips, os.environ["TESTS_DIR"])
336 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700337 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800338 Run(test_name, options.out, options.ref, options.autotest)
339
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700340
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700341if __name__ == "__main__":
342 Main()