blob: e6616e81ba9aa13ce8f9b4a4ed3d0678b55eba1d [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
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..."
Chung-yih Wang0a4ccc22014-04-25 13:24:14 +080092 print "SRC_DIR is %s" % dir
Dennis Kempina44de382013-04-29 14:53:31 -070093 process = Popen(["make", "-j", str(multiprocessing.cpu_count()),
94 "in-place"], cwd=dir, stdout=PIPE, stderr=STDOUT)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080095 ret = process.wait()
96 if ret != 0:
97 print process.stdout.read()
98 sys.exit(-1)
99
100
Dennis Kempin1479e742012-07-31 17:31:01 -0700101def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -0800102 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700103 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -0700104 cases = runner.DiscoverTestCases(glob)
105
106 for case in cases:
107 print "###", case.name
108 report = verifier.Verify(case)
109 print report
110
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800111
112def Run(glob, out_file=None, ref_file=None, autotest=False):
113 if not autotest:
114 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700115 print "Running tests..."
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700116 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700117 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700118
Dennis Kempin97397c62013-02-28 10:49:59 -0800119 # load reference
120 ref = {}
121 if ref_file:
122 ref = json.load(open(ref_file))
123
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700124 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700125 sorted_results_items = sorted(results.items())
126 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800127 if len(results) > 1:
128 # only print reports for regressions or failed tests
129 if key in ref:
130 delta = value["score"] - ref[key]["score"]
131 if math.fabs(delta) < 1e-10:
132 continue
133 elif value["result"] == "success":
134 continue
135
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700136 print "### Validation report for", key
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800137 print value["description"]
138 if value["disabled"]:
139 print "DISABLED"
140 else:
141 print value["logs"]["validation"]
142 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700143
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700144 # format result table
145 table = Table()
146 table.title = "Test Results"
147 table.header("Test", "reference score", "new score", "delta")
148
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800149 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700150 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700151 def ResultStr(value):
152 # format result to string
153 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700154 msg = "success" if value["score"] >= 0.5 else "bad"
155 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700156 else:
157 return value["result"]
158
159 # format reference and delta column
160 ref_score = ""
161 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700162 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700163 ref_score = ResultStr(ref[key])
164 delta = value["score"] - ref[key]["score"]
165 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700166 # default color
167 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700168 elif delta < 0:
169 regression = True
170 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700171 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700172 else:
173 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700174 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700175 table.row(key, ref_score, ResultStr(value), delta_str)
176
177 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700178
Dennis Kempin12968302012-08-09 15:37:09 -0700179 if out_file:
180 json.dump(results, open(out_file, "w"), indent=2)
181 print "results stored in:", out_file
182
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700183 if regression:
184 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
185 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":
199 print results["logs"]["gestures"]
200 elif what == "evdev-log":
201 print results["logs"]["evdev"]
202 elif what == "activity-log":
203 print results["logs"]["activity"]
204 elif what == "gestures":
205 print results["gestures"]
206 elif what == "events":
207 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:
226 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)
237 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:
246 print "Require TESTS_DIR environment variable"
247 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")
255 parser.add_option("-e", "--evdev",
256 dest="evdev", default=None,
257 help="path to evdev log for creating a new test")
258 parser.add_option("-v", "--view",
259 dest="view", default=None,
260 help="view generated gestures(g), activity in mtedit(a) " +
261 "gestures-log(gl), evdev-log(el) or activity-log(al)")
262 parser.add_option("-r", "--ref",
263 dest="ref", default=None,
264 help="reference test results for detecting regressions")
265 parser.add_option("-o", "--out",
266 dest="out", default=None,
267 help="output test results to file.")
268 parser.add_option("-n", "--new",
269 dest="new", action="store_true", default=False,
270 help="Create new device logs before downloading. " +
271 "[Default: False]")
272 parser.add_option("--no-edit",
273 dest="noedit", action="store_true", default=False,
274 help="Skip editing when creating tests. Add original log " +
275 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700276 parser.add_option("--autotest",
277 dest="autotest", action="store_true", default=False,
278 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700279 parser.add_option("--gdb",
280 dest="gdb", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800281 help="Run the test case in GDB"),
Dennis Kempind97cd722014-01-31 13:32:07 -0800282 parser.add_option("--verbose",
283 dest="verbose", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800284 help="Verbose debug output"),
285 parser.add_option("--robot",
286 dest="robot", default=None,
287 help="Instruct robot to generate test cases")
288 parser.add_option("--overwrite",
289 dest="overwrite", action="store_true", default=False,
290 help="(use with --robot) Overwrite existing tests")
291 parser.add_option("--no-calib",
292 dest="nocalib", action="store_true", default=False,
293 help="(use with --robot) Skip calibration step.")
294 parser.add_option("--manual-fingertips",
295 dest="manual_fingertips", action="store_true",
296 default=False,
297 help="(use with --robot) Use fingertips that are present.")
298 parser.add_option("--slow",
299 dest="slow", action="store_true", default=False,
300 help="(use with --robot) Force slow movement.")
Dennis Kempin07e90e72014-04-15 13:54:39 -0700301 parser.add_option("--add-platform",
302 dest="add_platform", default=None,
303 help="add platform from IP address of remote device.")
Dennis Kempin93a71412014-01-10 13:39:37 -0800304
Dennis Kempinc2b59862013-02-20 14:02:25 -0800305 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700306 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700307 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700308
Dennis Kempin07e90e72014-04-15 13:54:39 -0700309 if options.add_platform:
310 AddPlatform(options.add_platform)
311 return
312
Dennis Kempinc2b59862013-02-20 14:02:25 -0800313 if len(args) == 0:
314 test_name = "all"
315 elif len(args) == 1:
316 test_name = args[0]
317 else:
318 parser.print_help()
319 exit(-1)
320
Dennis Kempind97cd722014-01-31 13:32:07 -0800321 level = logging.INFO if options.verbose else logging.WARNING
322 logging.basicConfig(level=level)
323
Dennis Kempinc2b59862013-02-20 14:02:25 -0800324 if options.create:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800325 # obtain trimmed log data
326 original_log = Log(options.create, options)
327 if options.noedit:
328 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700329 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700330 editor = MTEdit()
Dennis Kempinc2b59862013-02-20 14:02:25 -0800331 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700332
Dennis Kempinc2b59862013-02-20 14:02:25 -0800333 # pass to touchtests
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800334 Add(test_name, log, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800335
336 elif options.view:
337 view = options.view
338 if view == "g":
339 view = "gestures"
340 elif view == "gl":
341 view = "gestures-log"
342 elif view == "el":
343 view = "evdev-log"
344 elif view == "al":
345 view = "activity-log"
346 elif view == "a":
347 view = "activity"
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800348 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700349 elif options.gdb:
350 GDB(test_name)
Dennis Kempin93a71412014-01-10 13:39:37 -0800351 elif options.robot:
352 generator = RobotTestGenerator(options.robot, not options.nocalib,
353 options.slow, options.manual_fingertips, os.environ["TESTS_DIR"])
354 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700355 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800356 Run(test_name, options.out, options.ref, options.autotest)
357
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700358
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700359if __name__ == "__main__":
360 Main()