blob: b0fc953b942dca3d833b8ecdce07ae354f704fa5 [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
Harry Cutts684f78c2020-04-09 18:37:05 -07007from __future__ import absolute_import
8from __future__ import division
9from __future__ import print_function
10
Dennis Kempinc2b59862013-02-20 14:02:25 -080011from optparse import OptionParser
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080012from subprocess import Popen, PIPE, STDOUT
Dennis Kempinc2b59862013-02-20 14:02:25 -080013from tempfile import NamedTemporaryFile
Dennis Kempin725ee5b2012-08-10 14:17:15 -070014import json
Dennis Kempind97cd722014-01-31 13:32:07 -080015import logging
Dennis Kempin725ee5b2012-08-10 14:17:15 -070016import math
Dennis Kempina44de382013-04-29 14:53:31 -070017import multiprocessing
Dennis Kempin725ee5b2012-08-10 14:17:15 -070018import os
Dennis Kempin725ee5b2012-08-10 14:17:15 -070019import sys
20
Dennis Kempin5495f8a2013-06-13 13:42:03 -070021from mtedit import MTEdit
Dennis Kempin07e90e72014-04-15 13:54:39 -070022from mtlib import Log, PlatformDatabase
Dennis Kempinc2b59862013-02-20 14:02:25 -080023
Dennis Kempin725ee5b2012-08-10 14:17:15 -070024from table import Table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070025from test_case import TestCase
Dennis Kempin31c0fbd2012-07-30 13:20:37 -070026from test_factory import TestFactory
Dennis Kempin93a71412014-01-10 13:39:37 -080027from test_robot import RobotTestGenerator
Andrew de los Reyes8063b662014-07-11 14:58:30 -070028from test_collector import TestCollector
Dennis Kempin963fb152013-01-11 15:40:19 -080029from test_runner import ParallelTestRunner as TestRunner
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070030
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080031
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070032_help_text = """\
Dennis Kempinc2b59862013-02-20 14:02:25 -080033Multitouch Regression Test Suite:
34---------------------------------
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070035
Dennis Kempinc2b59862013-02-20 14:02:25 -080036$ %prog [all|glob]
37Executes tests. Either all of them or selected tests by providing a glob match
38In order to test for regressions use:
39$ %prog all --out filename
40make a change
41$ %prog all --ref filename
42Which will display the changes in test results compared to before the change
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070043
Dennis Kempinc2b59862013-02-20 14:02:25 -080044$ %prog testname -v %info%
45Run test and display information. %info% can be:
46- a or activity: to view the touchpad activity in mtedit
47- g or gestures: to view the generated gestures
48- al or activity-log: to view the generated activity log
49- gl or gestures-log: to view the generated gestures log
50- el or evdev-log: to view the generated evdev log
51
52$ %prog test_name -c %source%
53Create a new test case from %source%. Source can be:
54- A feedback URL
55- A device IP
56- The path to an activity log file
57When using a file name test.log, %prog will look at test.log.evdev
58for the evdev log file. You can optionally supply -e to override this path.
59%prog will display an URL where you can trim the log file, the trimmed
60log file will then be used to create the new test case. Specify --no-edit in
61case you do not want to use the original files without trimming.
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070062
Dennis Kempin462c8752013-04-04 16:01:54 -070063$ %prog test_name --gdb
64Run the test using gdb for debugging the gestures library
65
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070066General Info:
67-------------
68testname arguments:
69Tests are always names as [platform]/[name of test case]. You can find the tests
70available in the tests folder.
71For example: lumpy/scroll_test
72
73Tests Folder:
74The tests folder contains a folder for each platform and all the test cases.
75Each test case is made up of 3 files:
76[testcase].py which contains the validation script
77[testcase].log which contains the input_event log
78[testcase].props which contains user_defined properties passed to gestures lib.
79
80Platform folders:
81To add a new platform you need to add a new folder to the Tests folder, and
82generate a platform.dat file. This can be done using the evemu-describe tool
83on the target platform:
84
85$ gmerge utouch-evemu
86$ evemu-describe /path/to/device > platform.dat
87"""
88
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080089
90def Compile():
91 if "SRC_DIR" not in os.environ:
Harry Cutts684f78c2020-04-09 18:37:05 -070092 print("Requires SRC_DIR env-var. Re-run $ sudo make setup-in-place")
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080093 sys.exit(-1)
94
95 dir = os.environ["SRC_DIR"]
Harry Cutts684f78c2020-04-09 18:37:05 -070096 print("Recompiling gestures/libevdev/replay...")
97 print("SRC_DIR is %s" % dir)
Dennis Kempina44de382013-04-29 14:53:31 -070098 process = Popen(["make", "-j", str(multiprocessing.cpu_count()),
99 "in-place"], cwd=dir, stdout=PIPE, stderr=STDOUT)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800100 ret = process.wait()
101 if ret != 0:
Harry Cutts684f78c2020-04-09 18:37:05 -0700102 print(process.stdout.read())
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800103 sys.exit(-1)
104
105
Harry Cuttsffcfa772020-04-22 16:17:10 -0700106def Run(glob, out_file=None, ref_file=None, autotest=False,
107 compact_results=False):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800108 if not autotest:
109 Compile()
Harry Cutts684f78c2020-04-09 18:37:05 -0700110 print("Running tests...")
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700111 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700112 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700113
Dennis Kempin97397c62013-02-28 10:49:59 -0800114 # load reference
115 ref = {}
116 if ref_file:
117 ref = json.load(open(ref_file))
118
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700119 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700120 sorted_results_items = sorted(results.items())
121 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800122 if len(results) > 1:
123 # only print reports for regressions or failed tests
124 if key in ref:
125 delta = value["score"] - ref[key]["score"]
126 if math.fabs(delta) < 1e-10:
127 continue
128 elif value["result"] == "success":
129 continue
130
Harry Cutts684f78c2020-04-09 18:37:05 -0700131 print("### Validation report for", key)
132 print(value["description"])
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800133 if value["disabled"]:
Harry Cutts684f78c2020-04-09 18:37:05 -0700134 print("DISABLED")
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800135 else:
Harry Cutts684f78c2020-04-09 18:37:05 -0700136 print(value["logs"]["validation"])
137 print(value["error"])
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700138
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700139 # format result table
140 table = Table()
141 table.title = "Test Results"
142 table.header("Test", "reference score", "new score", "delta")
143
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800144 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700145 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700146 def ResultStr(value):
147 # format result to string
148 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700149 msg = "success" if value["score"] >= 0.5 else "bad"
150 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700151 else:
152 return value["result"]
153
154 # format reference and delta column
155 ref_score = ""
156 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700157 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700158 ref_score = ResultStr(ref[key])
159 delta = value["score"] - ref[key]["score"]
160 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700161 # default color
162 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700163 elif delta < 0:
164 regression = True
165 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700166 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700167 else:
168 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700169 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700170 table.row(key, ref_score, ResultStr(value), delta_str)
171
Harry Cutts684f78c2020-04-09 18:37:05 -0700172 print(table)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700173
Dennis Kempin12968302012-08-09 15:37:09 -0700174 if out_file:
Harry Cuttsffcfa772020-04-22 16:17:10 -0700175 if compact_results:
176 for test_name in results:
177 r = results[test_name]
178 del r["logs"], r["events"], r["gestures"]
179
Dennis Kempin12968302012-08-09 15:37:09 -0700180 json.dump(results, open(out_file, "w"), indent=2)
Harry Cutts684f78c2020-04-09 18:37:05 -0700181 print("results stored in:", out_file)
Dennis Kempin12968302012-08-09 15:37:09 -0700182
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700183 if regression:
Harry Cutts684f78c2020-04-09 18:37:05 -0700184 print("\x1b[91mThere are regressions present in this test run!\x1b[0m")
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700185 exit(-1)
186
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800187
Dennis Kempin64f513f2012-08-10 15:09:18 -0700188def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800189 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700190 if file:
191 data = json.load(open(file))
192 results = data[test_name]
193 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700194 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700195 data = runner.RunAll(test_name)
196 results = data[test_name]
197
198 if what == "gestures-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700199 print(results["logs"]["gestures"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700200 elif what == "evdev-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700201 print(results["logs"]["evdev"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700202 elif what == "activity-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700203 print(results["logs"]["activity"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700204 elif what == "gestures":
Harry Cutts684f78c2020-04-09 18:37:05 -0700205 print(results["gestures"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700206 elif what == "events":
Harry Cutts684f78c2020-04-09 18:37:05 -0700207 print(results["events"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800208 elif what == "activity":
209 log = Log(activity=results["logs"]["activity"])
210 editor = MTEdit()
211 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700212
Dennis Kempin462c8752013-04-04 16:01:54 -0700213def GDB(test_name):
214 Compile()
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700215 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin462c8752013-04-04 16:01:54 -0700216 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800217
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800218def Add(testname, log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700219 """
220 Adds a new test case.
221 """
222 # determine test name from activity_log name
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700223 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800224 case = factory.CreateTest(testname, log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800225 if case:
Harry Cutts684f78c2020-04-09 18:37:05 -0700226 print("Test \"" + case.name + "\" created")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700227
Dennis Kempin07e90e72014-04-15 13:54:39 -0700228def AddPlatform(ip):
229 name = PlatformDatabase.RegisterPlatformFromDevice(ip)
230 if not name:
231 return
232 dirname = os.path.join(os.environ["TESTS_DIR"], name)
233 propsfile = os.path.join(dirname, "platform.props")
Dennis Kempin544dfde2014-06-09 14:02:55 -0700234 if not os.path.exists(dirname):
235 os.mkdir(dirname)
Dennis Kempin07e90e72014-04-15 13:54:39 -0700236 open(propsfile, "w").write("{\"platform\": \"%s\"}" % name)
Harry Cutts684f78c2020-04-09 18:37:05 -0700237 print(" ", propsfile)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800238
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700239def Main():
240 """
241 Main entry point for the console interface
242 """
243
244 # setup paths from environment variables
245 if "TESTS_DIR" not in os.environ:
Harry Cutts684f78c2020-04-09 18:37:05 -0700246 print("Require TESTS_DIR environment variable")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700247 exit(-1)
248
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700249 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700250
Dennis Kempinc2b59862013-02-20 14:02:25 -0800251 parser = OptionParser(usage=_help_text)
252 parser.add_option("-c", "--create",
253 dest="create", default=None,
254 help="create new test case from URL/IP or log file")
Andrew de los Reyes408678b2015-01-09 14:21:55 -0800255 parser.add_option("-p", "--platform",
256 dest="platform", default=None,
257 help="specify platform when using --create")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800258 parser.add_option("-e", "--evdev",
259 dest="evdev", default=None,
260 help="path to evdev log for creating a new test")
261 parser.add_option("-v", "--view",
262 dest="view", default=None,
263 help="view generated gestures(g), activity in mtedit(a) " +
264 "gestures-log(gl), evdev-log(el) or activity-log(al)")
265 parser.add_option("-r", "--ref",
266 dest="ref", default=None,
267 help="reference test results for detecting regressions")
268 parser.add_option("-o", "--out",
269 dest="out", default=None,
270 help="output test results to file.")
Harry Cuttsffcfa772020-04-22 16:17:10 -0700271 parser.add_option("--compact-results",
272 dest="compact_results", action="store_true", default=False,
273 help="exclude logs from the test result file when using "
274 "--out, making it much smaller")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800275 parser.add_option("-n", "--new",
276 dest="new", action="store_true", default=False,
277 help="Create new device logs before downloading. " +
278 "[Default: False]")
279 parser.add_option("--no-edit",
280 dest="noedit", action="store_true", default=False,
281 help="Skip editing when creating tests. Add original log " +
282 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700283 parser.add_option("--autotest",
284 dest="autotest", action="store_true", default=False,
285 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700286 parser.add_option("--gdb",
287 dest="gdb", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800288 help="Run the test case in GDB"),
Dennis Kempind97cd722014-01-31 13:32:07 -0800289 parser.add_option("--verbose",
290 dest="verbose", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800291 help="Verbose debug output"),
292 parser.add_option("--robot",
293 dest="robot", default=None,
294 help="Instruct robot to generate test cases")
Andrew de los Reyes8063b662014-07-11 14:58:30 -0700295 parser.add_option("--collect_from",
296 dest="collect_ip", default=None,
297 help="Interactively collect tests at given device IP");
298 parser.add_option(
299 "--overwrite",
300 dest="overwrite", action="store_true", default=False,
301 help="(use with --robot or --collect_from) Overwrite existing tests")
Dennis Kempin93a71412014-01-10 13:39:37 -0800302 parser.add_option("--no-calib",
303 dest="nocalib", action="store_true", default=False,
304 help="(use with --robot) Skip calibration step.")
305 parser.add_option("--manual-fingertips",
306 dest="manual_fingertips", action="store_true",
307 default=False,
308 help="(use with --robot) Use fingertips that are present.")
309 parser.add_option("--slow",
310 dest="slow", action="store_true", default=False,
311 help="(use with --robot) Force slow movement.")
Dennis Kempin07e90e72014-04-15 13:54:39 -0700312 parser.add_option("--add-platform",
313 dest="add_platform", default=None,
314 help="add platform from IP address of remote device.")
Dennis Kempin93a71412014-01-10 13:39:37 -0800315
Dennis Kempinc2b59862013-02-20 14:02:25 -0800316 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700317 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700318 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700319
Dennis Kempin07e90e72014-04-15 13:54:39 -0700320 if options.add_platform:
321 AddPlatform(options.add_platform)
322 return
323
Dennis Kempinc2b59862013-02-20 14:02:25 -0800324 if len(args) == 0:
325 test_name = "all"
326 elif len(args) == 1:
327 test_name = args[0]
328 else:
329 parser.print_help()
330 exit(-1)
331
Dennis Kempind97cd722014-01-31 13:32:07 -0800332 level = logging.INFO if options.verbose else logging.WARNING
333 logging.basicConfig(level=level)
334
Dennis Kempinc2b59862013-02-20 14:02:25 -0800335 if options.create:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800336 # obtain trimmed log data
337 original_log = Log(options.create, options)
338 if options.noedit:
339 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700340 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700341 editor = MTEdit()
Andrew de los Reyes408678b2015-01-09 14:21:55 -0800342 platform = options.platform or test_name.split(os.sep)[0]
Andrew de los Reyes8cc4b0a2014-10-24 12:34:58 -0700343 log = editor.Edit(original_log, force_platform=platform)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700344
Dennis Kempinc2b59862013-02-20 14:02:25 -0800345 # pass to touchtests
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800346 Add(test_name, log, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800347
348 elif options.view:
349 view = options.view
350 if view == "g":
351 view = "gestures"
352 elif view == "gl":
353 view = "gestures-log"
354 elif view == "el":
355 view = "evdev-log"
356 elif view == "al":
357 view = "activity-log"
358 elif view == "a":
359 view = "activity"
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800360 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700361 elif options.gdb:
362 GDB(test_name)
Andrew de los Reyes8063b662014-07-11 14:58:30 -0700363 elif options.collect_ip:
364 generator = TestCollector(options.collect_ip, os.environ["TESTS_DIR"])
365 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin93a71412014-01-10 13:39:37 -0800366 elif options.robot:
367 generator = RobotTestGenerator(options.robot, not options.nocalib,
368 options.slow, options.manual_fingertips, os.environ["TESTS_DIR"])
369 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700370 else:
Harry Cuttsffcfa772020-04-22 16:17:10 -0700371 Run(test_name, options.out, options.ref, options.autotest,
372 options.compact_results)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800373
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700374
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700375if __name__ == "__main__":
376 Main()