blob: 94824d2a776303be6ebefc654d02989e6f20cb8c [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
Dennis Kempin1479e742012-07-31 17:31:01 -0700106def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -0800107 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700108 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -0700109 cases = runner.DiscoverTestCases(glob)
110
111 for case in cases:
Harry Cutts684f78c2020-04-09 18:37:05 -0700112 print("###", case.name)
Dennis Kempin1479e742012-07-31 17:31:01 -0700113 report = verifier.Verify(case)
Harry Cutts684f78c2020-04-09 18:37:05 -0700114 print(report)
Dennis Kempin1479e742012-07-31 17:31:01 -0700115
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800116
Harry Cuttsffcfa772020-04-22 16:17:10 -0700117def Run(glob, out_file=None, ref_file=None, autotest=False,
118 compact_results=False):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800119 if not autotest:
120 Compile()
Harry Cutts684f78c2020-04-09 18:37:05 -0700121 print("Running tests...")
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700122 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700123 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700124
Dennis Kempin97397c62013-02-28 10:49:59 -0800125 # load reference
126 ref = {}
127 if ref_file:
128 ref = json.load(open(ref_file))
129
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700130 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700131 sorted_results_items = sorted(results.items())
132 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800133 if len(results) > 1:
134 # only print reports for regressions or failed tests
135 if key in ref:
136 delta = value["score"] - ref[key]["score"]
137 if math.fabs(delta) < 1e-10:
138 continue
139 elif value["result"] == "success":
140 continue
141
Harry Cutts684f78c2020-04-09 18:37:05 -0700142 print("### Validation report for", key)
143 print(value["description"])
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800144 if value["disabled"]:
Harry Cutts684f78c2020-04-09 18:37:05 -0700145 print("DISABLED")
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800146 else:
Harry Cutts684f78c2020-04-09 18:37:05 -0700147 print(value["logs"]["validation"])
148 print(value["error"])
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700149
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700150 # format result table
151 table = Table()
152 table.title = "Test Results"
153 table.header("Test", "reference score", "new score", "delta")
154
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800155 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700156 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700157 def ResultStr(value):
158 # format result to string
159 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700160 msg = "success" if value["score"] >= 0.5 else "bad"
161 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700162 else:
163 return value["result"]
164
165 # format reference and delta column
166 ref_score = ""
167 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700168 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700169 ref_score = ResultStr(ref[key])
170 delta = value["score"] - ref[key]["score"]
171 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700172 # default color
173 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700174 elif delta < 0:
175 regression = True
176 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700177 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700178 else:
179 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700180 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700181 table.row(key, ref_score, ResultStr(value), delta_str)
182
Harry Cutts684f78c2020-04-09 18:37:05 -0700183 print(table)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700184
Dennis Kempin12968302012-08-09 15:37:09 -0700185 if out_file:
Harry Cuttsffcfa772020-04-22 16:17:10 -0700186 if compact_results:
187 for test_name in results:
188 r = results[test_name]
189 del r["logs"], r["events"], r["gestures"]
190
Dennis Kempin12968302012-08-09 15:37:09 -0700191 json.dump(results, open(out_file, "w"), indent=2)
Harry Cutts684f78c2020-04-09 18:37:05 -0700192 print("results stored in:", out_file)
Dennis Kempin12968302012-08-09 15:37:09 -0700193
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700194 if regression:
Harry Cutts684f78c2020-04-09 18:37:05 -0700195 print("\x1b[91mThere are regressions present in this test run!\x1b[0m")
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700196 exit(-1)
197
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800198
Dennis Kempin64f513f2012-08-10 15:09:18 -0700199def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800200 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700201 if file:
202 data = json.load(open(file))
203 results = data[test_name]
204 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700205 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700206 data = runner.RunAll(test_name)
207 results = data[test_name]
208
209 if what == "gestures-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700210 print(results["logs"]["gestures"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700211 elif what == "evdev-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700212 print(results["logs"]["evdev"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700213 elif what == "activity-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700214 print(results["logs"]["activity"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700215 elif what == "gestures":
Harry Cutts684f78c2020-04-09 18:37:05 -0700216 print(results["gestures"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700217 elif what == "events":
Harry Cutts684f78c2020-04-09 18:37:05 -0700218 print(results["events"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800219 elif what == "activity":
220 log = Log(activity=results["logs"]["activity"])
221 editor = MTEdit()
222 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700223
Dennis Kempin462c8752013-04-04 16:01:54 -0700224def GDB(test_name):
225 Compile()
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700226 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin462c8752013-04-04 16:01:54 -0700227 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800228
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800229def Add(testname, log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700230 """
231 Adds a new test case.
232 """
233 # determine test name from activity_log name
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700234 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800235 case = factory.CreateTest(testname, log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800236 if case:
Harry Cutts684f78c2020-04-09 18:37:05 -0700237 print("Test \"" + case.name + "\" created")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700238
Dennis Kempin07e90e72014-04-15 13:54:39 -0700239def AddPlatform(ip):
240 name = PlatformDatabase.RegisterPlatformFromDevice(ip)
241 if not name:
242 return
243 dirname = os.path.join(os.environ["TESTS_DIR"], name)
244 propsfile = os.path.join(dirname, "platform.props")
Dennis Kempin544dfde2014-06-09 14:02:55 -0700245 if not os.path.exists(dirname):
246 os.mkdir(dirname)
Dennis Kempin07e90e72014-04-15 13:54:39 -0700247 open(propsfile, "w").write("{\"platform\": \"%s\"}" % name)
Harry Cutts684f78c2020-04-09 18:37:05 -0700248 print(" ", propsfile)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800249
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700250def Main():
251 """
252 Main entry point for the console interface
253 """
254
255 # setup paths from environment variables
256 if "TESTS_DIR" not in os.environ:
Harry Cutts684f78c2020-04-09 18:37:05 -0700257 print("Require TESTS_DIR environment variable")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700258 exit(-1)
259
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700260 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700261
Dennis Kempinc2b59862013-02-20 14:02:25 -0800262 parser = OptionParser(usage=_help_text)
263 parser.add_option("-c", "--create",
264 dest="create", default=None,
265 help="create new test case from URL/IP or log file")
Andrew de los Reyes408678b2015-01-09 14:21:55 -0800266 parser.add_option("-p", "--platform",
267 dest="platform", default=None,
268 help="specify platform when using --create")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800269 parser.add_option("-e", "--evdev",
270 dest="evdev", default=None,
271 help="path to evdev log for creating a new test")
272 parser.add_option("-v", "--view",
273 dest="view", default=None,
274 help="view generated gestures(g), activity in mtedit(a) " +
275 "gestures-log(gl), evdev-log(el) or activity-log(al)")
276 parser.add_option("-r", "--ref",
277 dest="ref", default=None,
278 help="reference test results for detecting regressions")
279 parser.add_option("-o", "--out",
280 dest="out", default=None,
281 help="output test results to file.")
Harry Cuttsffcfa772020-04-22 16:17:10 -0700282 parser.add_option("--compact-results",
283 dest="compact_results", action="store_true", default=False,
284 help="exclude logs from the test result file when using "
285 "--out, making it much smaller")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800286 parser.add_option("-n", "--new",
287 dest="new", action="store_true", default=False,
288 help="Create new device logs before downloading. " +
289 "[Default: False]")
290 parser.add_option("--no-edit",
291 dest="noedit", action="store_true", default=False,
292 help="Skip editing when creating tests. Add original log " +
293 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700294 parser.add_option("--autotest",
295 dest="autotest", action="store_true", default=False,
296 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700297 parser.add_option("--gdb",
298 dest="gdb", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800299 help="Run the test case in GDB"),
Dennis Kempind97cd722014-01-31 13:32:07 -0800300 parser.add_option("--verbose",
301 dest="verbose", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800302 help="Verbose debug output"),
303 parser.add_option("--robot",
304 dest="robot", default=None,
305 help="Instruct robot to generate test cases")
Andrew de los Reyes8063b662014-07-11 14:58:30 -0700306 parser.add_option("--collect_from",
307 dest="collect_ip", default=None,
308 help="Interactively collect tests at given device IP");
309 parser.add_option(
310 "--overwrite",
311 dest="overwrite", action="store_true", default=False,
312 help="(use with --robot or --collect_from) Overwrite existing tests")
Dennis Kempin93a71412014-01-10 13:39:37 -0800313 parser.add_option("--no-calib",
314 dest="nocalib", action="store_true", default=False,
315 help="(use with --robot) Skip calibration step.")
316 parser.add_option("--manual-fingertips",
317 dest="manual_fingertips", action="store_true",
318 default=False,
319 help="(use with --robot) Use fingertips that are present.")
320 parser.add_option("--slow",
321 dest="slow", action="store_true", default=False,
322 help="(use with --robot) Force slow movement.")
Dennis Kempin07e90e72014-04-15 13:54:39 -0700323 parser.add_option("--add-platform",
324 dest="add_platform", default=None,
325 help="add platform from IP address of remote device.")
Dennis Kempin93a71412014-01-10 13:39:37 -0800326
Dennis Kempinc2b59862013-02-20 14:02:25 -0800327 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700328 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700329 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700330
Dennis Kempin07e90e72014-04-15 13:54:39 -0700331 if options.add_platform:
332 AddPlatform(options.add_platform)
333 return
334
Dennis Kempinc2b59862013-02-20 14:02:25 -0800335 if len(args) == 0:
336 test_name = "all"
337 elif len(args) == 1:
338 test_name = args[0]
339 else:
340 parser.print_help()
341 exit(-1)
342
Dennis Kempind97cd722014-01-31 13:32:07 -0800343 level = logging.INFO if options.verbose else logging.WARNING
344 logging.basicConfig(level=level)
345
Dennis Kempinc2b59862013-02-20 14:02:25 -0800346 if options.create:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800347 # obtain trimmed log data
348 original_log = Log(options.create, options)
349 if options.noedit:
350 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700351 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700352 editor = MTEdit()
Andrew de los Reyes408678b2015-01-09 14:21:55 -0800353 platform = options.platform or test_name.split(os.sep)[0]
Andrew de los Reyes8cc4b0a2014-10-24 12:34:58 -0700354 log = editor.Edit(original_log, force_platform=platform)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700355
Dennis Kempinc2b59862013-02-20 14:02:25 -0800356 # pass to touchtests
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800357 Add(test_name, log, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800358
359 elif options.view:
360 view = options.view
361 if view == "g":
362 view = "gestures"
363 elif view == "gl":
364 view = "gestures-log"
365 elif view == "el":
366 view = "evdev-log"
367 elif view == "al":
368 view = "activity-log"
369 elif view == "a":
370 view = "activity"
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800371 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700372 elif options.gdb:
373 GDB(test_name)
Andrew de los Reyes8063b662014-07-11 14:58:30 -0700374 elif options.collect_ip:
375 generator = TestCollector(options.collect_ip, os.environ["TESTS_DIR"])
376 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin93a71412014-01-10 13:39:37 -0800377 elif options.robot:
378 generator = RobotTestGenerator(options.robot, not options.nocalib,
379 options.slow, options.manual_fingertips, os.environ["TESTS_DIR"])
380 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700381 else:
Harry Cuttsffcfa772020-04-22 16:17:10 -0700382 Run(test_name, options.out, options.ref, options.autotest,
383 options.compact_results)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800384
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700385
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700386if __name__ == "__main__":
387 Main()