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