blob: 62e1934fc94422c2b102a6e1af8f1bbeb85cd219 [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 Kempin07e90e72014-04-15 13:54:39 -070018from mtlib import Log, PlatformDatabase
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
Andrew de los Reyes8063b662014-07-11 14:58:30 -070024from test_collector import TestCollector
Dennis Kempin963fb152013-01-11 15:40:19 -080025from test_runner import ParallelTestRunner as TestRunner
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070026
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080027
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070028_help_text = """\
Dennis Kempinc2b59862013-02-20 14:02:25 -080029Multitouch Regression Test Suite:
30---------------------------------
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070031
Dennis Kempinc2b59862013-02-20 14:02:25 -080032$ %prog [all|glob]
33Executes tests. Either all of them or selected tests by providing a glob match
34In order to test for regressions use:
35$ %prog all --out filename
36make a change
37$ %prog all --ref filename
38Which will display the changes in test results compared to before the change
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070039
Dennis Kempinc2b59862013-02-20 14:02:25 -080040$ %prog testname -v %info%
41Run test and display information. %info% can be:
42- a or activity: to view the touchpad activity in mtedit
43- g or gestures: to view the generated gestures
44- al or activity-log: to view the generated activity log
45- gl or gestures-log: to view the generated gestures log
46- el or evdev-log: to view the generated evdev log
47
48$ %prog test_name -c %source%
49Create a new test case from %source%. Source can be:
50- A feedback URL
51- A device IP
52- The path to an activity log file
53When using a file name test.log, %prog will look at test.log.evdev
54for the evdev log file. You can optionally supply -e to override this path.
55%prog will display an URL where you can trim the log file, the trimmed
56log file will then be used to create the new test case. Specify --no-edit in
57case you do not want to use the original files without trimming.
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070058
Dennis Kempin462c8752013-04-04 16:01:54 -070059$ %prog test_name --gdb
60Run the test using gdb for debugging the gestures library
61
Dennis Kempin22c7c4d2012-07-26 13:04:24 -070062General Info:
63-------------
64testname arguments:
65Tests are always names as [platform]/[name of test case]. You can find the tests
66available in the tests folder.
67For example: lumpy/scroll_test
68
69Tests Folder:
70The tests folder contains a folder for each platform and all the test cases.
71Each test case is made up of 3 files:
72[testcase].py which contains the validation script
73[testcase].log which contains the input_event log
74[testcase].props which contains user_defined properties passed to gestures lib.
75
76Platform folders:
77To add a new platform you need to add a new folder to the Tests folder, and
78generate a platform.dat file. This can be done using the evemu-describe tool
79on the target platform:
80
81$ gmerge utouch-evemu
82$ evemu-describe /path/to/device > platform.dat
83"""
84
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080085
86def Compile():
87 if "SRC_DIR" not in os.environ:
88 print "Requires SRC_DIR env-var. Re-run $ sudo make setup-in-place"
89 sys.exit(-1)
90
91 dir = os.environ["SRC_DIR"]
92 print "Recompiling gestures/libevdev/replay..."
Chung-yih Wang0a4ccc22014-04-25 13:24:14 +080093 print "SRC_DIR is %s" % dir
Dennis Kempina44de382013-04-29 14:53:31 -070094 process = Popen(["make", "-j", str(multiprocessing.cpu_count()),
95 "in-place"], cwd=dir, stdout=PIPE, stderr=STDOUT)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080096 ret = process.wait()
97 if ret != 0:
98 print process.stdout.read()
99 sys.exit(-1)
100
101
Dennis Kempin1479e742012-07-31 17:31:01 -0700102def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -0800103 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700104 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -0700105 cases = runner.DiscoverTestCases(glob)
106
107 for case in cases:
108 print "###", case.name
109 report = verifier.Verify(case)
110 print report
111
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800112
113def Run(glob, out_file=None, ref_file=None, autotest=False):
114 if not autotest:
115 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700116 print "Running tests..."
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700117 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700118 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700119
Dennis Kempin97397c62013-02-28 10:49:59 -0800120 # load reference
121 ref = {}
122 if ref_file:
123 ref = json.load(open(ref_file))
124
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700125 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700126 sorted_results_items = sorted(results.items())
127 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800128 if len(results) > 1:
129 # only print reports for regressions or failed tests
130 if key in ref:
131 delta = value["score"] - ref[key]["score"]
132 if math.fabs(delta) < 1e-10:
133 continue
134 elif value["result"] == "success":
135 continue
136
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700137 print "### Validation report for", key
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800138 print value["description"]
139 if value["disabled"]:
140 print "DISABLED"
141 else:
142 print value["logs"]["validation"]
143 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700144
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700145 # format result table
146 table = Table()
147 table.title = "Test Results"
148 table.header("Test", "reference score", "new score", "delta")
149
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800150 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700151 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700152 def ResultStr(value):
153 # format result to string
154 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700155 msg = "success" if value["score"] >= 0.5 else "bad"
156 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700157 else:
158 return value["result"]
159
160 # format reference and delta column
161 ref_score = ""
162 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700163 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700164 ref_score = ResultStr(ref[key])
165 delta = value["score"] - ref[key]["score"]
166 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700167 # default color
168 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700169 elif delta < 0:
170 regression = True
171 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700172 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700173 else:
174 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700175 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700176 table.row(key, ref_score, ResultStr(value), delta_str)
177
178 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700179
Dennis Kempin12968302012-08-09 15:37:09 -0700180 if out_file:
181 json.dump(results, open(out_file, "w"), indent=2)
182 print "results stored in:", out_file
183
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700184 if regression:
185 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
186 exit(-1)
187
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800188
Dennis Kempin64f513f2012-08-10 15:09:18 -0700189def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800190 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700191 if file:
192 data = json.load(open(file))
193 results = data[test_name]
194 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700195 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700196 data = runner.RunAll(test_name)
197 results = data[test_name]
198
199 if what == "gestures-log":
200 print results["logs"]["gestures"]
201 elif what == "evdev-log":
202 print results["logs"]["evdev"]
203 elif what == "activity-log":
204 print results["logs"]["activity"]
205 elif what == "gestures":
206 print results["gestures"]
207 elif what == "events":
208 print results["events"]
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800209 elif what == "activity":
210 log = Log(activity=results["logs"]["activity"])
211 editor = MTEdit()
212 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700213
Dennis Kempin462c8752013-04-04 16:01:54 -0700214def GDB(test_name):
215 Compile()
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700216 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin462c8752013-04-04 16:01:54 -0700217 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800218
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800219def Add(testname, log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700220 """
221 Adds a new test case.
222 """
223 # determine test name from activity_log name
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700224 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800225 case = factory.CreateTest(testname, log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800226 if case:
227 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700228
Dennis Kempin07e90e72014-04-15 13:54:39 -0700229def AddPlatform(ip):
230 name = PlatformDatabase.RegisterPlatformFromDevice(ip)
231 if not name:
232 return
233 dirname = os.path.join(os.environ["TESTS_DIR"], name)
234 propsfile = os.path.join(dirname, "platform.props")
Dennis Kempin544dfde2014-06-09 14:02:55 -0700235 if not os.path.exists(dirname):
236 os.mkdir(dirname)
Dennis Kempin07e90e72014-04-15 13:54:39 -0700237 open(propsfile, "w").write("{\"platform\": \"%s\"}" % name)
238 print " ", propsfile
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800239
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700240def Main():
241 """
242 Main entry point for the console interface
243 """
244
245 # setup paths from environment variables
246 if "TESTS_DIR" not in os.environ:
247 print "Require TESTS_DIR environment variable"
248 exit(-1)
249
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700250 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700251
Dennis Kempinc2b59862013-02-20 14:02:25 -0800252 parser = OptionParser(usage=_help_text)
253 parser.add_option("-c", "--create",
254 dest="create", default=None,
255 help="create new test case from URL/IP or log file")
Andrew de los Reyes408678b2015-01-09 14:21:55 -0800256 parser.add_option("-p", "--platform",
257 dest="platform", default=None,
258 help="specify platform when using --create")
Dennis Kempinc2b59862013-02-20 14:02:25 -0800259 parser.add_option("-e", "--evdev",
260 dest="evdev", default=None,
261 help="path to evdev log for creating a new test")
262 parser.add_option("-v", "--view",
263 dest="view", default=None,
264 help="view generated gestures(g), activity in mtedit(a) " +
265 "gestures-log(gl), evdev-log(el) or activity-log(al)")
266 parser.add_option("-r", "--ref",
267 dest="ref", default=None,
268 help="reference test results for detecting regressions")
269 parser.add_option("-o", "--out",
270 dest="out", default=None,
271 help="output test results to file.")
272 parser.add_option("-n", "--new",
273 dest="new", action="store_true", default=False,
274 help="Create new device logs before downloading. " +
275 "[Default: False]")
276 parser.add_option("--no-edit",
277 dest="noedit", action="store_true", default=False,
278 help="Skip editing when creating tests. Add original log " +
279 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700280 parser.add_option("--autotest",
281 dest="autotest", action="store_true", default=False,
282 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700283 parser.add_option("--gdb",
284 dest="gdb", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800285 help="Run the test case in GDB"),
Dennis Kempind97cd722014-01-31 13:32:07 -0800286 parser.add_option("--verbose",
287 dest="verbose", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800288 help="Verbose debug output"),
289 parser.add_option("--robot",
290 dest="robot", default=None,
291 help="Instruct robot to generate test cases")
Andrew de los Reyes8063b662014-07-11 14:58:30 -0700292 parser.add_option("--collect_from",
293 dest="collect_ip", default=None,
294 help="Interactively collect tests at given device IP");
295 parser.add_option(
296 "--overwrite",
297 dest="overwrite", action="store_true", default=False,
298 help="(use with --robot or --collect_from) Overwrite existing tests")
Dennis Kempin93a71412014-01-10 13:39:37 -0800299 parser.add_option("--no-calib",
300 dest="nocalib", action="store_true", default=False,
301 help="(use with --robot) Skip calibration step.")
302 parser.add_option("--manual-fingertips",
303 dest="manual_fingertips", action="store_true",
304 default=False,
305 help="(use with --robot) Use fingertips that are present.")
306 parser.add_option("--slow",
307 dest="slow", action="store_true", default=False,
308 help="(use with --robot) Force slow movement.")
Dennis Kempin07e90e72014-04-15 13:54:39 -0700309 parser.add_option("--add-platform",
310 dest="add_platform", default=None,
311 help="add platform from IP address of remote device.")
Dennis Kempin93a71412014-01-10 13:39:37 -0800312
Dennis Kempinc2b59862013-02-20 14:02:25 -0800313 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700314 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700315 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700316
Dennis Kempin07e90e72014-04-15 13:54:39 -0700317 if options.add_platform:
318 AddPlatform(options.add_platform)
319 return
320
Dennis Kempinc2b59862013-02-20 14:02:25 -0800321 if len(args) == 0:
322 test_name = "all"
323 elif len(args) == 1:
324 test_name = args[0]
325 else:
326 parser.print_help()
327 exit(-1)
328
Dennis Kempind97cd722014-01-31 13:32:07 -0800329 level = logging.INFO if options.verbose else logging.WARNING
330 logging.basicConfig(level=level)
331
Dennis Kempinc2b59862013-02-20 14:02:25 -0800332 if options.create:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800333 # obtain trimmed log data
334 original_log = Log(options.create, options)
335 if options.noedit:
336 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700337 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700338 editor = MTEdit()
Andrew de los Reyes408678b2015-01-09 14:21:55 -0800339 platform = options.platform or test_name.split(os.sep)[0]
Andrew de los Reyes8cc4b0a2014-10-24 12:34:58 -0700340 log = editor.Edit(original_log, force_platform=platform)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700341
Dennis Kempinc2b59862013-02-20 14:02:25 -0800342 # pass to touchtests
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800343 Add(test_name, log, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800344
345 elif options.view:
346 view = options.view
347 if view == "g":
348 view = "gestures"
349 elif view == "gl":
350 view = "gestures-log"
351 elif view == "el":
352 view = "evdev-log"
353 elif view == "al":
354 view = "activity-log"
355 elif view == "a":
356 view = "activity"
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800357 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700358 elif options.gdb:
359 GDB(test_name)
Andrew de los Reyes8063b662014-07-11 14:58:30 -0700360 elif options.collect_ip:
361 generator = TestCollector(options.collect_ip, os.environ["TESTS_DIR"])
362 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin93a71412014-01-10 13:39:37 -0800363 elif options.robot:
364 generator = RobotTestGenerator(options.robot, not options.nocalib,
365 options.slow, options.manual_fingertips, os.environ["TESTS_DIR"])
366 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700367 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800368 Run(test_name, options.out, options.ref, options.autotest)
369
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700370
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700371if __name__ == "__main__":
372 Main()