blob: 319603b9bce43ca8d8a3ad695d4275358a6a6faa [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
117def Run(glob, out_file=None, ref_file=None, autotest=False):
118 if not autotest:
119 Compile()
Harry Cutts684f78c2020-04-09 18:37:05 -0700120 print("Running tests...")
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700121 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700122 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700123
Dennis Kempin97397c62013-02-28 10:49:59 -0800124 # load reference
125 ref = {}
126 if ref_file:
127 ref = json.load(open(ref_file))
128
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700129 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700130 sorted_results_items = sorted(results.items())
131 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800132 if len(results) > 1:
133 # only print reports for regressions or failed tests
134 if key in ref:
135 delta = value["score"] - ref[key]["score"]
136 if math.fabs(delta) < 1e-10:
137 continue
138 elif value["result"] == "success":
139 continue
140
Harry Cutts684f78c2020-04-09 18:37:05 -0700141 print("### Validation report for", key)
142 print(value["description"])
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800143 if value["disabled"]:
Harry Cutts684f78c2020-04-09 18:37:05 -0700144 print("DISABLED")
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800145 else:
Harry Cutts684f78c2020-04-09 18:37:05 -0700146 print(value["logs"]["validation"])
147 print(value["error"])
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700148
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700149 # format result table
150 table = Table()
151 table.title = "Test Results"
152 table.header("Test", "reference score", "new score", "delta")
153
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800154 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700155 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700156 def ResultStr(value):
157 # format result to string
158 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700159 msg = "success" if value["score"] >= 0.5 else "bad"
160 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700161 else:
162 return value["result"]
163
164 # format reference and delta column
165 ref_score = ""
166 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700167 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700168 ref_score = ResultStr(ref[key])
169 delta = value["score"] - ref[key]["score"]
170 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700171 # default color
172 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700173 elif delta < 0:
174 regression = True
175 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700176 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700177 else:
178 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700179 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700180 table.row(key, ref_score, ResultStr(value), delta_str)
181
Harry Cutts684f78c2020-04-09 18:37:05 -0700182 print(table)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700183
Dennis Kempin12968302012-08-09 15:37:09 -0700184 if out_file:
185 json.dump(results, open(out_file, "w"), indent=2)
Harry Cutts684f78c2020-04-09 18:37:05 -0700186 print("results stored in:", out_file)
Dennis Kempin12968302012-08-09 15:37:09 -0700187
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700188 if regression:
Harry Cutts684f78c2020-04-09 18:37:05 -0700189 print("\x1b[91mThere are regressions present in this test run!\x1b[0m")
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700190 exit(-1)
191
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800192
Dennis Kempin64f513f2012-08-10 15:09:18 -0700193def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800194 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700195 if file:
196 data = json.load(open(file))
197 results = data[test_name]
198 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700199 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700200 data = runner.RunAll(test_name)
201 results = data[test_name]
202
203 if what == "gestures-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700204 print(results["logs"]["gestures"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700205 elif what == "evdev-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700206 print(results["logs"]["evdev"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700207 elif what == "activity-log":
Harry Cutts684f78c2020-04-09 18:37:05 -0700208 print(results["logs"]["activity"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700209 elif what == "gestures":
Harry Cutts684f78c2020-04-09 18:37:05 -0700210 print(results["gestures"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700211 elif what == "events":
Harry Cutts684f78c2020-04-09 18:37:05 -0700212 print(results["events"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800213 elif what == "activity":
214 log = Log(activity=results["logs"]["activity"])
215 editor = MTEdit()
216 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700217
Dennis Kempin462c8752013-04-04 16:01:54 -0700218def GDB(test_name):
219 Compile()
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700220 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin462c8752013-04-04 16:01:54 -0700221 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800222
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800223def Add(testname, log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700224 """
225 Adds a new test case.
226 """
227 # determine test name from activity_log name
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700228 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800229 case = factory.CreateTest(testname, log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800230 if case:
Harry Cutts684f78c2020-04-09 18:37:05 -0700231 print("Test \"" + case.name + "\" created")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700232
Dennis Kempin07e90e72014-04-15 13:54:39 -0700233def AddPlatform(ip):
234 name = PlatformDatabase.RegisterPlatformFromDevice(ip)
235 if not name:
236 return
237 dirname = os.path.join(os.environ["TESTS_DIR"], name)
238 propsfile = os.path.join(dirname, "platform.props")
Dennis Kempin544dfde2014-06-09 14:02:55 -0700239 if not os.path.exists(dirname):
240 os.mkdir(dirname)
Dennis Kempin07e90e72014-04-15 13:54:39 -0700241 open(propsfile, "w").write("{\"platform\": \"%s\"}" % name)
Harry Cutts684f78c2020-04-09 18:37:05 -0700242 print(" ", propsfile)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800243
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700244def Main():
245 """
246 Main entry point for the console interface
247 """
248
249 # setup paths from environment variables
250 if "TESTS_DIR" not in os.environ:
Harry Cutts684f78c2020-04-09 18:37:05 -0700251 print("Require TESTS_DIR environment variable")
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700252 exit(-1)
253
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700254 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700255
Dennis Kempinc2b59862013-02-20 14:02:25 -0800256 parser = OptionParser(usage=_help_text)
257 parser.add_option("-c", "--create",
258 dest="create", default=None,
259 help="create new test case from URL/IP or log file")
Andrew de los Reyes408678b2015-01-09 14:21:55 -0800260 parser.add_option("-p", "--platform",
261 dest="platform", default=None,
262 help="specify platform when using --create")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800263 parser.add_option("-e", "--evdev",
264 dest="evdev", default=None,
265 help="path to evdev log for creating a new test")
266 parser.add_option("-v", "--view",
267 dest="view", default=None,
268 help="view generated gestures(g), activity in mtedit(a) " +
269 "gestures-log(gl), evdev-log(el) or activity-log(al)")
270 parser.add_option("-r", "--ref",
271 dest="ref", default=None,
272 help="reference test results for detecting regressions")
273 parser.add_option("-o", "--out",
274 dest="out", default=None,
275 help="output test results to file.")
276 parser.add_option("-n", "--new",
277 dest="new", action="store_true", default=False,
278 help="Create new device logs before downloading. " +
279 "[Default: False]")
280 parser.add_option("--no-edit",
281 dest="noedit", action="store_true", default=False,
282 help="Skip editing when creating tests. Add original log " +
283 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700284 parser.add_option("--autotest",
285 dest="autotest", action="store_true", default=False,
286 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700287 parser.add_option("--gdb",
288 dest="gdb", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800289 help="Run the test case in GDB"),
Dennis Kempind97cd722014-01-31 13:32:07 -0800290 parser.add_option("--verbose",
291 dest="verbose", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800292 help="Verbose debug output"),
293 parser.add_option("--robot",
294 dest="robot", default=None,
295 help="Instruct robot to generate test cases")
Andrew de los Reyes8063b662014-07-11 14:58:30 -0700296 parser.add_option("--collect_from",
297 dest="collect_ip", default=None,
298 help="Interactively collect tests at given device IP");
299 parser.add_option(
300 "--overwrite",
301 dest="overwrite", action="store_true", default=False,
302 help="(use with --robot or --collect_from) Overwrite existing tests")
Dennis Kempin93a71412014-01-10 13:39:37 -0800303 parser.add_option("--no-calib",
304 dest="nocalib", action="store_true", default=False,
305 help="(use with --robot) Skip calibration step.")
306 parser.add_option("--manual-fingertips",
307 dest="manual_fingertips", action="store_true",
308 default=False,
309 help="(use with --robot) Use fingertips that are present.")
310 parser.add_option("--slow",
311 dest="slow", action="store_true", default=False,
312 help="(use with --robot) Force slow movement.")
Dennis Kempin07e90e72014-04-15 13:54:39 -0700313 parser.add_option("--add-platform",
314 dest="add_platform", default=None,
315 help="add platform from IP address of remote device.")
Dennis Kempin93a71412014-01-10 13:39:37 -0800316
Dennis Kempinc2b59862013-02-20 14:02:25 -0800317 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700318 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700319 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700320
Dennis Kempin07e90e72014-04-15 13:54:39 -0700321 if options.add_platform:
322 AddPlatform(options.add_platform)
323 return
324
Dennis Kempinc2b59862013-02-20 14:02:25 -0800325 if len(args) == 0:
326 test_name = "all"
327 elif len(args) == 1:
328 test_name = args[0]
329 else:
330 parser.print_help()
331 exit(-1)
332
Dennis Kempind97cd722014-01-31 13:32:07 -0800333 level = logging.INFO if options.verbose else logging.WARNING
334 logging.basicConfig(level=level)
335
Dennis Kempinc2b59862013-02-20 14:02:25 -0800336 if options.create:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800337 # obtain trimmed log data
338 original_log = Log(options.create, options)
339 if options.noedit:
340 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700341 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700342 editor = MTEdit()
Andrew de los Reyes408678b2015-01-09 14:21:55 -0800343 platform = options.platform or test_name.split(os.sep)[0]
Andrew de los Reyes8cc4b0a2014-10-24 12:34:58 -0700344 log = editor.Edit(original_log, force_platform=platform)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700345
Dennis Kempinc2b59862013-02-20 14:02:25 -0800346 # pass to touchtests
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800347 Add(test_name, log, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800348
349 elif options.view:
350 view = options.view
351 if view == "g":
352 view = "gestures"
353 elif view == "gl":
354 view = "gestures-log"
355 elif view == "el":
356 view = "evdev-log"
357 elif view == "al":
358 view = "activity-log"
359 elif view == "a":
360 view = "activity"
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800361 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700362 elif options.gdb:
363 GDB(test_name)
Andrew de los Reyes8063b662014-07-11 14:58:30 -0700364 elif options.collect_ip:
365 generator = TestCollector(options.collect_ip, os.environ["TESTS_DIR"])
366 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin93a71412014-01-10 13:39:37 -0800367 elif options.robot:
368 generator = RobotTestGenerator(options.robot, not options.nocalib,
369 options.slow, options.manual_fingertips, os.environ["TESTS_DIR"])
370 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700371 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800372 Run(test_name, options.out, options.ref, options.autotest)
373
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700374
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700375if __name__ == "__main__":
376 Main()