blob: 8bc32e6e58c0e9129a13d9a837889f27d8ef8ff6 [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..."
Dennis Kempina44de382013-04-29 14:53:31 -070092 process = Popen(["make", "-j", str(multiprocessing.cpu_count()),
93 "in-place"], cwd=dir, stdout=PIPE, stderr=STDOUT)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -080094 ret = process.wait()
95 if ret != 0:
96 print process.stdout.read()
97 sys.exit(-1)
98
99
Dennis Kempin1479e742012-07-31 17:31:01 -0700100def Verify(device, glob):
Dennis Kempin774b0622013-02-08 13:14:54 -0800101 verifier = TestVerifier(os.environ["TESTS_DIR"], device)
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700102 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin1479e742012-07-31 17:31:01 -0700103 cases = runner.DiscoverTestCases(glob)
104
105 for case in cases:
106 print "###", case.name
107 report = verifier.Verify(case)
108 print report
109
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800110
111def Run(glob, out_file=None, ref_file=None, autotest=False):
112 if not autotest:
113 Compile()
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700114 print "Running tests..."
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700115 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700116 results = runner.RunAll(glob)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700117
Dennis Kempin97397c62013-02-28 10:49:59 -0800118 # load reference
119 ref = {}
120 if ref_file:
121 ref = json.load(open(ref_file))
122
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700123 # print reports
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700124 sorted_results_items = sorted(results.items())
125 for key, value in sorted_results_items:
Dennis Kempin97397c62013-02-28 10:49:59 -0800126 if len(results) > 1:
127 # only print reports for regressions or failed tests
128 if key in ref:
129 delta = value["score"] - ref[key]["score"]
130 if math.fabs(delta) < 1e-10:
131 continue
132 elif value["result"] == "success":
133 continue
134
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700135 print "### Validation report for", key
Dennis Kempin9cfc4842013-02-28 10:39:24 -0800136 print value["description"]
137 if value["disabled"]:
138 print "DISABLED"
139 else:
140 print value["logs"]["validation"]
141 print value["error"]
Dennis Kempin4bc397b2012-08-08 16:51:47 -0700142
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700143 # format result table
144 table = Table()
145 table.title = "Test Results"
146 table.header("Test", "reference score", "new score", "delta")
147
Chung-yih Wangd5aa52f2013-01-22 15:45:26 +0800148 regression = False
Andrew de los Reyesbf5fa422012-10-17 16:45:45 -0700149 for key, value in sorted_results_items:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700150 def ResultStr(value):
151 # format result to string
152 if value["result"] == "success":
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700153 msg = "success" if value["score"] >= 0.5 else "bad"
154 return "%s (%.4f)" % (msg, value["score"])
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700155 else:
156 return value["result"]
157
158 # format reference and delta column
159 ref_score = ""
160 delta_str = ""
Dennis Kempin12968302012-08-09 15:37:09 -0700161 if key in ref:
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700162 ref_score = ResultStr(ref[key])
163 delta = value["score"] - ref[key]["score"]
164 if math.fabs(delta) < 1e-10:
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700165 # default color
166 delta_str = "\x1b[0m%.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700167 elif delta < 0:
168 regression = True
169 # color red
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700170 delta_str = "\x1b[31m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700171 else:
172 # color green
Dennis Kempin43e2beb2013-03-21 14:10:01 -0700173 delta_str = "\x1b[32m%+.4f\x1b[0m" % delta
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700174 table.row(key, ref_score, ResultStr(value), delta_str)
175
176 print table
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700177
Dennis Kempin12968302012-08-09 15:37:09 -0700178 if out_file:
179 json.dump(results, open(out_file, "w"), indent=2)
180 print "results stored in:", out_file
181
Dennis Kempin725ee5b2012-08-10 14:17:15 -0700182 if regression:
183 print "\x1b[91mThere are regressions present in this test run!\x1b[0m"
184 exit(-1)
185
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800186
Dennis Kempin64f513f2012-08-10 15:09:18 -0700187def Get(test_name, what, file=None):
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800188 Compile()
Dennis Kempin64f513f2012-08-10 15:09:18 -0700189 if file:
190 data = json.load(open(file))
191 results = data[test_name]
192 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700193 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin64f513f2012-08-10 15:09:18 -0700194 data = runner.RunAll(test_name)
195 results = data[test_name]
196
197 if what == "gestures-log":
198 print results["logs"]["gestures"]
199 elif what == "evdev-log":
200 print results["logs"]["evdev"]
201 elif what == "activity-log":
202 print results["logs"]["activity"]
203 elif what == "gestures":
204 print results["gestures"]
205 elif what == "events":
206 print results["events"]
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800207 elif what == "activity":
208 log = Log(activity=results["logs"]["activity"])
209 editor = MTEdit()
210 editor.View(log)
Dennis Kempin64f513f2012-08-10 15:09:18 -0700211
Dennis Kempin462c8752013-04-04 16:01:54 -0700212def GDB(test_name):
213 Compile()
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700214 runner = TestRunner(os.environ["TESTS_DIR"])
Dennis Kempin462c8752013-04-04 16:01:54 -0700215 data = runner.RunGDB(test_name)
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800216
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800217def Add(testname, log, gdb):
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700218 """
219 Adds a new test case.
220 """
221 # determine test name from activity_log name
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700222 factory = TestFactory(os.environ["TESTS_DIR"])
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800223 case = factory.CreateTest(testname, log, gdb)
Dennis Kempin15967a42013-02-25 12:41:54 -0800224 if case:
225 print "Test \"" + case.name + "\" created"
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700226
Dennis Kempin07e90e72014-04-15 13:54:39 -0700227def AddPlatform(ip):
228 name = PlatformDatabase.RegisterPlatformFromDevice(ip)
229 if not name:
230 return
231 dirname = os.path.join(os.environ["TESTS_DIR"], name)
232 propsfile = os.path.join(dirname, "platform.props")
233 os.mkdir(dirname)
234 open(propsfile, "w").write("{\"platform\": \"%s\"}" % name)
235 print " ", propsfile
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800236
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700237def Main():
238 """
239 Main entry point for the console interface
240 """
241
242 # setup paths from environment variables
243 if "TESTS_DIR" not in os.environ:
244 print "Require TESTS_DIR environment variable"
245 exit(-1)
246
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700247 TestCase.tests_path = os.environ["TESTS_DIR"]
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700248
Dennis Kempinc2b59862013-02-20 14:02:25 -0800249 parser = OptionParser(usage=_help_text)
250 parser.add_option("-c", "--create",
251 dest="create", default=None,
252 help="create new test case from URL/IP or log file")
253 parser.add_option("-e", "--evdev",
254 dest="evdev", default=None,
255 help="path to evdev log for creating a new test")
256 parser.add_option("-v", "--view",
257 dest="view", default=None,
258 help="view generated gestures(g), activity in mtedit(a) " +
259 "gestures-log(gl), evdev-log(el) or activity-log(al)")
260 parser.add_option("-r", "--ref",
261 dest="ref", default=None,
262 help="reference test results for detecting regressions")
263 parser.add_option("-o", "--out",
264 dest="out", default=None,
265 help="output test results to file.")
266 parser.add_option("-n", "--new",
267 dest="new", action="store_true", default=False,
268 help="Create new device logs before downloading. " +
269 "[Default: False]")
270 parser.add_option("--no-edit",
271 dest="noedit", action="store_true", default=False,
272 help="Skip editing when creating tests. Add original log " +
273 "[Default: False]")
Dennis Kempin8e3201c2013-03-14 13:49:01 -0700274 parser.add_option("--autotest",
275 dest="autotest", action="store_true", default=False,
276 help="Run in autotest mode. Skips recompilation.")
Dennis Kempin462c8752013-04-04 16:01:54 -0700277 parser.add_option("--gdb",
278 dest="gdb", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800279 help="Run the test case in GDB"),
Dennis Kempind97cd722014-01-31 13:32:07 -0800280 parser.add_option("--verbose",
281 dest="verbose", action="store_true", default=False,
Dennis Kempin93a71412014-01-10 13:39:37 -0800282 help="Verbose debug output"),
283 parser.add_option("--robot",
284 dest="robot", default=None,
285 help="Instruct robot to generate test cases")
286 parser.add_option("--overwrite",
287 dest="overwrite", action="store_true", default=False,
288 help="(use with --robot) Overwrite existing tests")
289 parser.add_option("--no-calib",
290 dest="nocalib", action="store_true", default=False,
291 help="(use with --robot) Skip calibration step.")
292 parser.add_option("--manual-fingertips",
293 dest="manual_fingertips", action="store_true",
294 default=False,
295 help="(use with --robot) Use fingertips that are present.")
296 parser.add_option("--slow",
297 dest="slow", action="store_true", default=False,
298 help="(use with --robot) Force slow movement.")
Dennis Kempin07e90e72014-04-15 13:54:39 -0700299 parser.add_option("--add-platform",
300 dest="add_platform", default=None,
301 help="add platform from IP address of remote device.")
Dennis Kempin93a71412014-01-10 13:39:37 -0800302
Dennis Kempinc2b59862013-02-20 14:02:25 -0800303 (options, args) = parser.parse_args()
Andrew de los Reyes8c5585b2013-05-20 15:32:05 -0700304 options.download = False # For compatibility with mtedit
Andrew de los Reyes0489c662013-06-04 12:47:50 -0700305 options.screenshot = False # For compatibility with mtedit
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700306
Dennis Kempin07e90e72014-04-15 13:54:39 -0700307 if options.add_platform:
308 AddPlatform(options.add_platform)
309 return
310
Dennis Kempinc2b59862013-02-20 14:02:25 -0800311 if len(args) == 0:
312 test_name = "all"
313 elif len(args) == 1:
314 test_name = args[0]
315 else:
316 parser.print_help()
317 exit(-1)
318
Dennis Kempind97cd722014-01-31 13:32:07 -0800319 level = logging.INFO if options.verbose else logging.WARNING
320 logging.basicConfig(level=level)
321
Dennis Kempinc2b59862013-02-20 14:02:25 -0800322 if options.create:
Dennis Kempinc2b59862013-02-20 14:02:25 -0800323 # obtain trimmed log data
324 original_log = Log(options.create, options)
325 if options.noedit:
326 log = original_log
Dennis Kempin12968302012-08-09 15:37:09 -0700327 else:
Dennis Kempinaad2bbb2013-06-18 13:48:35 -0700328 editor = MTEdit()
Dennis Kempinc2b59862013-02-20 14:02:25 -0800329 log = editor.Edit(original_log)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700330
Dennis Kempinc2b59862013-02-20 14:02:25 -0800331 # pass to touchtests
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800332 Add(test_name, log, options.gdb)
Dennis Kempinc2b59862013-02-20 14:02:25 -0800333
334 elif options.view:
335 view = options.view
336 if view == "g":
337 view = "gestures"
338 elif view == "gl":
339 view = "gestures-log"
340 elif view == "el":
341 view = "evdev-log"
342 elif view == "al":
343 view = "activity-log"
344 elif view == "a":
345 view = "activity"
Dennis Kempin3b1fca72014-01-09 11:54:59 -0800346 Get(test_name, view)
Dennis Kempin462c8752013-04-04 16:01:54 -0700347 elif options.gdb:
348 GDB(test_name)
Dennis Kempin93a71412014-01-10 13:39:37 -0800349 elif options.robot:
350 generator = RobotTestGenerator(options.robot, not options.nocalib,
351 options.slow, options.manual_fingertips, os.environ["TESTS_DIR"])
352 generator.GenerateAll(test_name, options.overwrite)
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700353 else:
Dennis Kempin1c7ffab2013-02-28 11:43:45 -0800354 Run(test_name, options.out, options.ref, options.autotest)
355
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700356
Dennis Kempin22c7c4d2012-07-26 13:04:24 -0700357if __name__ == "__main__":
358 Main()