Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 1 | # 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 Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 7 | from optparse import OptionParser |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 8 | from subprocess import Popen, PIPE, STDOUT |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 9 | from tempfile import NamedTemporaryFile |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 10 | import json |
| 11 | import math |
| 12 | import os |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 13 | import sys |
| 14 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 15 | from mtedit.editor import LogEditor |
| 16 | from mtedit.log import Log |
| 17 | |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 18 | from table import Table |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 19 | from test_case import TestCase |
Dennis Kempin | 31c0fbd | 2012-07-30 13:20:37 -0700 | [diff] [blame] | 20 | from test_factory import TestFactory |
Dennis Kempin | 963fb15 | 2013-01-11 15:40:19 -0800 | [diff] [blame] | 21 | from test_runner import ParallelTestRunner as TestRunner |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 22 | |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 23 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 24 | _help_text = """\ |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 25 | Multitouch Regression Test Suite: |
| 26 | --------------------------------- |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 27 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 28 | $ %prog [all|glob] |
| 29 | Executes tests. Either all of them or selected tests by providing a glob match |
| 30 | In order to test for regressions use: |
| 31 | $ %prog all --out filename |
| 32 | make a change |
| 33 | $ %prog all --ref filename |
| 34 | Which will display the changes in test results compared to before the change |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 35 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 36 | $ %prog testname -v %info% |
| 37 | Run test and display information. %info% can be: |
| 38 | - a or activity: to view the touchpad activity in mtedit |
| 39 | - g or gestures: to view the generated gestures |
| 40 | - al or activity-log: to view the generated activity log |
| 41 | - gl or gestures-log: to view the generated gestures log |
| 42 | - el or evdev-log: to view the generated evdev log |
| 43 | |
| 44 | $ %prog test_name -c %source% |
| 45 | Create a new test case from %source%. Source can be: |
| 46 | - A feedback URL |
| 47 | - A device IP |
| 48 | - The path to an activity log file |
| 49 | When using a file name test.log, %prog will look at test.log.evdev |
| 50 | for the evdev log file. You can optionally supply -e to override this path. |
| 51 | %prog will display an URL where you can trim the log file, the trimmed |
| 52 | log file will then be used to create the new test case. Specify --no-edit in |
| 53 | case you do not want to use the original files without trimming. |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 54 | |
Dennis Kempin | 462c875 | 2013-04-04 16:01:54 -0700 | [diff] [blame] | 55 | $ %prog test_name --gdb |
| 56 | Run the test using gdb for debugging the gestures library |
| 57 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 58 | General Info: |
| 59 | ------------- |
| 60 | testname arguments: |
| 61 | Tests are always names as [platform]/[name of test case]. You can find the tests |
| 62 | available in the tests folder. |
| 63 | For example: lumpy/scroll_test |
| 64 | |
| 65 | Tests Folder: |
| 66 | The tests folder contains a folder for each platform and all the test cases. |
| 67 | Each test case is made up of 3 files: |
| 68 | [testcase].py which contains the validation script |
| 69 | [testcase].log which contains the input_event log |
| 70 | [testcase].props which contains user_defined properties passed to gestures lib. |
| 71 | |
| 72 | Platform folders: |
| 73 | To add a new platform you need to add a new folder to the Tests folder, and |
| 74 | generate a platform.dat file. This can be done using the evemu-describe tool |
| 75 | on the target platform: |
| 76 | |
| 77 | $ gmerge utouch-evemu |
| 78 | $ evemu-describe /path/to/device > platform.dat |
| 79 | """ |
| 80 | |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 81 | |
| 82 | def Compile(): |
| 83 | if "SRC_DIR" not in os.environ: |
| 84 | print "Requires SRC_DIR env-var. Re-run $ sudo make setup-in-place" |
| 85 | sys.exit(-1) |
| 86 | |
| 87 | dir = os.environ["SRC_DIR"] |
| 88 | print "Recompiling gestures/libevdev/replay..." |
| 89 | process = Popen(["make", "in-place"], cwd=dir, |
| 90 | stdout=PIPE, stderr=STDOUT) |
| 91 | ret = process.wait() |
| 92 | if ret != 0: |
| 93 | print process.stdout.read() |
| 94 | sys.exit(-1) |
| 95 | |
| 96 | |
Dennis Kempin | 1479e74 | 2012-07-31 17:31:01 -0700 | [diff] [blame] | 97 | def Verify(device, glob): |
Dennis Kempin | 774b062 | 2013-02-08 13:14:54 -0800 | [diff] [blame] | 98 | verifier = TestVerifier(os.environ["TESTS_DIR"], device) |
| 99 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"]) |
Dennis Kempin | 1479e74 | 2012-07-31 17:31:01 -0700 | [diff] [blame] | 100 | cases = runner.DiscoverTestCases(glob) |
| 101 | |
| 102 | for case in cases: |
| 103 | print "###", case.name |
| 104 | report = verifier.Verify(case) |
| 105 | print report |
| 106 | |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 107 | |
| 108 | def Run(glob, out_file=None, ref_file=None, autotest=False): |
| 109 | if not autotest: |
| 110 | Compile() |
Dennis Kempin | 4bc397b | 2012-08-08 16:51:47 -0700 | [diff] [blame] | 111 | print "Running tests..." |
Dennis Kempin | 774b062 | 2013-02-08 13:14:54 -0800 | [diff] [blame] | 112 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"]) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 113 | results = runner.RunAll(glob) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 114 | |
Dennis Kempin | 97397c6 | 2013-02-28 10:49:59 -0800 | [diff] [blame] | 115 | # load reference |
| 116 | ref = {} |
| 117 | if ref_file: |
| 118 | ref = json.load(open(ref_file)) |
| 119 | |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 120 | # print reports |
Andrew de los Reyes | bf5fa42 | 2012-10-17 16:45:45 -0700 | [diff] [blame] | 121 | sorted_results_items = sorted(results.items()) |
| 122 | for key, value in sorted_results_items: |
Dennis Kempin | 97397c6 | 2013-02-28 10:49:59 -0800 | [diff] [blame] | 123 | if len(results) > 1: |
| 124 | # only print reports for regressions or failed tests |
| 125 | if key in ref: |
| 126 | delta = value["score"] - ref[key]["score"] |
| 127 | if math.fabs(delta) < 1e-10: |
| 128 | continue |
| 129 | elif value["result"] == "success": |
| 130 | continue |
| 131 | |
Dennis Kempin | 4bc397b | 2012-08-08 16:51:47 -0700 | [diff] [blame] | 132 | print "### Validation report for", key |
Dennis Kempin | 9cfc484 | 2013-02-28 10:39:24 -0800 | [diff] [blame] | 133 | print value["description"] |
| 134 | if value["disabled"]: |
| 135 | print "DISABLED" |
| 136 | else: |
| 137 | print value["logs"]["validation"] |
| 138 | print value["error"] |
Dennis Kempin | 4bc397b | 2012-08-08 16:51:47 -0700 | [diff] [blame] | 139 | |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 140 | # format result table |
| 141 | table = Table() |
| 142 | table.title = "Test Results" |
| 143 | table.header("Test", "reference score", "new score", "delta") |
| 144 | |
Chung-yih Wang | d5aa52f | 2013-01-22 15:45:26 +0800 | [diff] [blame] | 145 | regression = False |
Andrew de los Reyes | bf5fa42 | 2012-10-17 16:45:45 -0700 | [diff] [blame] | 146 | for key, value in sorted_results_items: |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 147 | def ResultStr(value): |
| 148 | # format result to string |
| 149 | if value["result"] == "success": |
Dennis Kempin | 43e2beb | 2013-03-21 14:10:01 -0700 | [diff] [blame] | 150 | msg = "success" if value["score"] >= 0.5 else "bad" |
| 151 | return "%s (%.4f)" % (msg, value["score"]) |
Dennis Kempin | 9cfc484 | 2013-02-28 10:39:24 -0800 | [diff] [blame] | 152 | elif value["disabled"]: |
| 153 | return "disabled" |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 154 | else: |
| 155 | return value["result"] |
| 156 | |
| 157 | # format reference and delta column |
| 158 | ref_score = "" |
| 159 | delta_str = "" |
Dennis Kempin | 1296830 | 2012-08-09 15:37:09 -0700 | [diff] [blame] | 160 | if key in ref: |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 161 | ref_score = ResultStr(ref[key]) |
| 162 | delta = value["score"] - ref[key]["score"] |
| 163 | if math.fabs(delta) < 1e-10: |
Dennis Kempin | 43e2beb | 2013-03-21 14:10:01 -0700 | [diff] [blame] | 164 | # default color |
| 165 | delta_str = "\x1b[0m%.4f\x1b[0m" % delta |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 166 | elif delta < 0: |
| 167 | regression = True |
| 168 | # color red |
Dennis Kempin | 43e2beb | 2013-03-21 14:10:01 -0700 | [diff] [blame] | 169 | delta_str = "\x1b[31m%+.4f\x1b[0m" % delta |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 170 | else: |
| 171 | # color green |
Dennis Kempin | 43e2beb | 2013-03-21 14:10:01 -0700 | [diff] [blame] | 172 | delta_str = "\x1b[32m%+.4f\x1b[0m" % delta |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 173 | table.row(key, ref_score, ResultStr(value), delta_str) |
| 174 | |
| 175 | print table |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 176 | |
Dennis Kempin | 1296830 | 2012-08-09 15:37:09 -0700 | [diff] [blame] | 177 | if out_file: |
| 178 | json.dump(results, open(out_file, "w"), indent=2) |
| 179 | print "results stored in:", out_file |
| 180 | |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 181 | if regression: |
| 182 | print "\x1b[91mThere are regressions present in this test run!\x1b[0m" |
| 183 | exit(-1) |
| 184 | |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 185 | |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 186 | def Get(test_name, what, file=None): |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 187 | Compile() |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 188 | if file: |
| 189 | data = json.load(open(file)) |
| 190 | results = data[test_name] |
| 191 | else: |
Dennis Kempin | 774b062 | 2013-02-08 13:14:54 -0800 | [diff] [blame] | 192 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"]) |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 193 | data = runner.RunAll(test_name) |
| 194 | results = data[test_name] |
| 195 | |
| 196 | if what == "gestures-log": |
| 197 | print results["logs"]["gestures"] |
| 198 | elif what == "evdev-log": |
| 199 | print results["logs"]["evdev"] |
| 200 | elif what == "activity-log": |
| 201 | print results["logs"]["activity"] |
| 202 | elif what == "gestures": |
| 203 | print results["gestures"] |
| 204 | elif what == "events": |
| 205 | print results["events"] |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 206 | elif what == "return-activity": |
| 207 | return results["logs"]["activity"] |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 208 | |
Dennis Kempin | 462c875 | 2013-04-04 16:01:54 -0700 | [diff] [blame] | 209 | def GDB(test_name): |
| 210 | Compile() |
| 211 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"]) |
| 212 | data = runner.RunGDB(test_name) |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 213 | |
Dennis Kempin | 4e2e689 | 2013-04-19 17:04:00 -0700 | [diff] [blame] | 214 | def Add(testname, activity_log, event_log, gdb): |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 215 | """ |
| 216 | Adds a new test case. |
| 217 | """ |
| 218 | # determine test name from activity_log name |
Dennis Kempin | 2eb653b | 2013-04-03 14:58:32 -0700 | [diff] [blame] | 219 | factory = TestFactory(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"]) |
Dennis Kempin | 4e2e689 | 2013-04-19 17:04:00 -0700 | [diff] [blame] | 220 | case = factory.CreateTest(testname, activity_log, event_log, gdb) |
Dennis Kempin | 15967a4 | 2013-02-25 12:41:54 -0800 | [diff] [blame] | 221 | if case: |
| 222 | print "Test \"" + case.name + "\" created" |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 223 | |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 224 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 225 | def Main(): |
| 226 | """ |
| 227 | Main entry point for the console interface |
| 228 | """ |
| 229 | |
| 230 | # setup paths from environment variables |
| 231 | if "TESTS_DIR" not in os.environ: |
| 232 | print "Require TESTS_DIR environment variable" |
| 233 | exit(-1) |
| 234 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 235 | TestCase.tests_path = os.environ["TESTS_DIR"] |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 236 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 237 | parser = OptionParser(usage=_help_text) |
| 238 | parser.add_option("-c", "--create", |
| 239 | dest="create", default=None, |
| 240 | help="create new test case from URL/IP or log file") |
| 241 | parser.add_option("-e", "--evdev", |
| 242 | dest="evdev", default=None, |
| 243 | help="path to evdev log for creating a new test") |
| 244 | parser.add_option("-v", "--view", |
| 245 | dest="view", default=None, |
| 246 | help="view generated gestures(g), activity in mtedit(a) " + |
| 247 | "gestures-log(gl), evdev-log(el) or activity-log(al)") |
| 248 | parser.add_option("-r", "--ref", |
| 249 | dest="ref", default=None, |
| 250 | help="reference test results for detecting regressions") |
| 251 | parser.add_option("-o", "--out", |
| 252 | dest="out", default=None, |
| 253 | help="output test results to file.") |
| 254 | parser.add_option("-n", "--new", |
| 255 | dest="new", action="store_true", default=False, |
| 256 | help="Create new device logs before downloading. " + |
| 257 | "[Default: False]") |
| 258 | parser.add_option("--no-edit", |
| 259 | dest="noedit", action="store_true", default=False, |
| 260 | help="Skip editing when creating tests. Add original log " + |
| 261 | "[Default: False]") |
Dennis Kempin | 8e3201c | 2013-03-14 13:49:01 -0700 | [diff] [blame] | 262 | parser.add_option("--autotest", |
| 263 | dest="autotest", action="store_true", default=False, |
| 264 | help="Run in autotest mode. Skips recompilation.") |
Dennis Kempin | 462c875 | 2013-04-04 16:01:54 -0700 | [diff] [blame] | 265 | parser.add_option("--gdb", |
| 266 | dest="gdb", action="store_true", default=False, |
| 267 | help="Run the test case in GDB") |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 268 | (options, args) = parser.parse_args() |
Andrew de los Reyes | 8c5585b | 2013-05-20 15:32:05 -0700 | [diff] [blame] | 269 | options.download = False # For compatibility with mtedit |
Andrew de los Reyes | 0489c66 | 2013-06-04 12:47:50 -0700 | [diff] [blame^] | 270 | options.screenshot = False # For compatibility with mtedit |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 271 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 272 | if len(args) == 0: |
| 273 | test_name = "all" |
| 274 | elif len(args) == 1: |
| 275 | test_name = args[0] |
| 276 | else: |
| 277 | parser.print_help() |
| 278 | exit(-1) |
| 279 | |
| 280 | if options.create: |
| 281 | # create temporary files for storing activity and evdev log |
| 282 | tmp = NamedTemporaryFile("w") |
| 283 | tmp_evdev = NamedTemporaryFile("w") |
| 284 | |
| 285 | # obtain trimmed log data |
| 286 | original_log = Log(options.create, options) |
| 287 | if options.noedit: |
| 288 | log = original_log |
Dennis Kempin | 1296830 | 2012-08-09 15:37:09 -0700 | [diff] [blame] | 289 | else: |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 290 | editor = LogEditor(persistent=False) |
| 291 | log = editor.Edit(original_log) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 292 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 293 | # write to temporary files |
| 294 | tmp.write(log.activity) |
| 295 | tmp.flush() |
| 296 | tmp_evdev.write(log.evdev) |
| 297 | tmp_evdev.flush() |
| 298 | |
| 299 | # pass to touchtests |
Dennis Kempin | 4e2e689 | 2013-04-19 17:04:00 -0700 | [diff] [blame] | 300 | Add(test_name, tmp.name, tmp_evdev.name, options.gdb) |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 301 | |
| 302 | # cleanup |
| 303 | tmp.close() |
| 304 | tmp_evdev.close() |
| 305 | |
| 306 | elif options.view: |
| 307 | view = options.view |
| 308 | if view == "g": |
| 309 | view = "gestures" |
| 310 | elif view == "gl": |
| 311 | view = "gestures-log" |
| 312 | elif view == "el": |
| 313 | view = "evdev-log" |
| 314 | elif view == "al": |
| 315 | view = "activity-log" |
| 316 | elif view == "a": |
| 317 | view = "activity" |
| 318 | |
| 319 | if view == "activity": |
| 320 | activity = Get(test_name, "return-activity") |
| 321 | tmp = NamedTemporaryFile("w") |
| 322 | tmp.write(activity) |
| 323 | tmp.flush() |
| 324 | log = Log(tmp.name, options) |
| 325 | editor = LogEditor(persistent=False) |
| 326 | editor.View(log) |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 327 | else: |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 328 | Get(test_name, view) |
Dennis Kempin | 462c875 | 2013-04-04 16:01:54 -0700 | [diff] [blame] | 329 | elif options.gdb: |
| 330 | GDB(test_name) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 331 | else: |
Dennis Kempin | 1c7ffab | 2013-02-28 11:43:45 -0800 | [diff] [blame] | 332 | Run(test_name, options.out, options.ref, options.autotest) |
| 333 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 334 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 335 | if __name__ == "__main__": |
| 336 | Main() |