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 |
Ilja H. Friedel | a35b013 | 2013-03-13 21:48:14 -0700 | [diff] [blame] | 8 | from os import path |
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 |
Ilja H. Friedel | a35b013 | 2013-03-13 21:48:14 -0700 | [diff] [blame] | 13 | import pprint |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 14 | import sys |
| 15 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 16 | from mtedit.editor import LogEditor |
| 17 | from mtedit.log import Log |
| 18 | |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 19 | from table import Table |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 20 | from test_case import TestCase |
Dennis Kempin | 31c0fbd | 2012-07-30 13:20:37 -0700 | [diff] [blame] | 21 | from test_factory import TestFactory |
Dennis Kempin | 963fb15 | 2013-01-11 15:40:19 -0800 | [diff] [blame] | 22 | from test_runner import ParallelTestRunner as TestRunner |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 23 | |
| 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 | 1479e74 | 2012-07-31 17:31:01 -0700 | [diff] [blame] | 78 | def Verify(device, glob): |
Dennis Kempin | 774b062 | 2013-02-08 13:14:54 -0800 | [diff] [blame] | 79 | verifier = TestVerifier(os.environ["TESTS_DIR"], device) |
| 80 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"]) |
Dennis Kempin | 1479e74 | 2012-07-31 17:31:01 -0700 | [diff] [blame] | 81 | cases = runner.DiscoverTestCases(glob) |
| 82 | |
| 83 | for case in cases: |
| 84 | print "###", case.name |
| 85 | report = verifier.Verify(case) |
| 86 | print report |
| 87 | |
Dennis Kempin | 1296830 | 2012-08-09 15:37:09 -0700 | [diff] [blame] | 88 | def Run(glob, out_file=None, ref_file=None): |
Dennis Kempin | 4bc397b | 2012-08-08 16:51:47 -0700 | [diff] [blame] | 89 | print "Running tests..." |
Dennis Kempin | 774b062 | 2013-02-08 13:14:54 -0800 | [diff] [blame] | 90 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"]) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 91 | results = runner.RunAll(glob) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 92 | |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 93 | # print reports |
Andrew de los Reyes | bf5fa42 | 2012-10-17 16:45:45 -0700 | [diff] [blame] | 94 | sorted_results_items = sorted(results.items()) |
| 95 | for key, value in sorted_results_items: |
Dennis Kempin | 4bc397b | 2012-08-08 16:51:47 -0700 | [diff] [blame] | 96 | print "### Validation report for", key |
Ilja H. Friedel | d88f14f | 2013-03-13 21:58:37 -0700 | [diff] [blame] | 97 | print value["logs"]["validation"] |
| 98 | print value["error"] |
Dennis Kempin | 4bc397b | 2012-08-08 16:51:47 -0700 | [diff] [blame] | 99 | |
Ilja H. Friedel | b8e567c | 2013-03-13 21:54:57 -0700 | [diff] [blame] | 100 | # load reference |
| 101 | ref = {} |
| 102 | if ref_file: |
| 103 | ref = json.load(open(ref_file)) |
| 104 | |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 105 | # format result table |
| 106 | table = Table() |
| 107 | table.title = "Test Results" |
| 108 | table.header("Test", "reference score", "new score", "delta") |
| 109 | |
Chung-yih Wang | d5aa52f | 2013-01-22 15:45:26 +0800 | [diff] [blame] | 110 | regression = False |
Andrew de los Reyes | bf5fa42 | 2012-10-17 16:45:45 -0700 | [diff] [blame] | 111 | for key, value in sorted_results_items: |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 112 | def ResultStr(value): |
| 113 | # format result to string |
| 114 | if value["result"] == "success": |
| 115 | return "%s (%.4f)" % (value["result"], value["score"]) |
| 116 | else: |
| 117 | return value["result"] |
| 118 | |
| 119 | # format reference and delta column |
| 120 | ref_score = "" |
| 121 | delta_str = "" |
Dennis Kempin | 1296830 | 2012-08-09 15:37:09 -0700 | [diff] [blame] | 122 | if key in ref: |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 123 | ref_score = ResultStr(ref[key]) |
| 124 | delta = value["score"] - ref[key]["score"] |
| 125 | if math.fabs(delta) < 1e-10: |
| 126 | # don't color, but line up with other values |
| 127 | delta_str = " %.4f " % delta |
| 128 | elif delta < 0: |
| 129 | regression = True |
| 130 | # color red |
| 131 | delta_str = "\x1b[91m%+.4f\x1b[0m" % delta |
| 132 | else: |
| 133 | # color green |
| 134 | delta_str = "\x1b[92m%+.4f\x1b[0m" % delta |
| 135 | table.row(key, ref_score, ResultStr(value), delta_str) |
| 136 | |
| 137 | print table |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 138 | |
Dennis Kempin | 1296830 | 2012-08-09 15:37:09 -0700 | [diff] [blame] | 139 | if out_file: |
| 140 | json.dump(results, open(out_file, "w"), indent=2) |
| 141 | print "results stored in:", out_file |
| 142 | |
Dennis Kempin | 725ee5b | 2012-08-10 14:17:15 -0700 | [diff] [blame] | 143 | if regression: |
| 144 | print "\x1b[91mThere are regressions present in this test run!\x1b[0m" |
| 145 | exit(-1) |
| 146 | |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 147 | def Get(test_name, what, file=None): |
| 148 | if file: |
| 149 | data = json.load(open(file)) |
| 150 | results = data[test_name] |
| 151 | else: |
Dennis Kempin | 774b062 | 2013-02-08 13:14:54 -0800 | [diff] [blame] | 152 | runner = TestRunner(os.environ["TESTS_DIR"], os.environ["XORG_CONF_DIR"]) |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 153 | data = runner.RunAll(test_name) |
| 154 | results = data[test_name] |
| 155 | |
| 156 | if what == "gestures-log": |
| 157 | print results["logs"]["gestures"] |
| 158 | elif what == "evdev-log": |
| 159 | print results["logs"]["evdev"] |
| 160 | elif what == "activity-log": |
| 161 | print results["logs"]["activity"] |
| 162 | elif what == "gestures": |
| 163 | print results["gestures"] |
| 164 | elif what == "events": |
| 165 | print results["events"] |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 166 | elif what == "return-activity": |
| 167 | return results["logs"]["activity"] |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 168 | |
Dennis Kempin | 7a2f40b | 2012-08-10 15:13:54 -0700 | [diff] [blame] | 169 | def Add(testname, activity_log, event_log): |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 170 | """ |
| 171 | Adds a new test case. |
| 172 | """ |
| 173 | # determine test name from activity_log name |
Dennis Kempin | 774b062 | 2013-02-08 13:14:54 -0800 | [diff] [blame] | 174 | factory = TestFactory(os.environ["TESTS_DIR"]) |
Dennis Kempin | 31c0fbd | 2012-07-30 13:20:37 -0700 | [diff] [blame] | 175 | case = factory.CreateTest(testname, activity_log, event_log) |
Dennis Kempin | 15967a4 | 2013-02-25 12:41:54 -0800 | [diff] [blame] | 176 | if case: |
| 177 | print "Test \"" + case.name + "\" created" |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 178 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 179 | def Main(): |
| 180 | """ |
| 181 | Main entry point for the console interface |
| 182 | """ |
| 183 | |
| 184 | # setup paths from environment variables |
| 185 | if "TESTS_DIR" not in os.environ: |
| 186 | print "Require TESTS_DIR environment variable" |
| 187 | exit(-1) |
| 188 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 189 | TestCase.tests_path = os.environ["TESTS_DIR"] |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 190 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 191 | parser = OptionParser(usage=_help_text) |
| 192 | parser.add_option("-c", "--create", |
| 193 | dest="create", default=None, |
| 194 | help="create new test case from URL/IP or log file") |
| 195 | parser.add_option("-e", "--evdev", |
| 196 | dest="evdev", default=None, |
| 197 | help="path to evdev log for creating a new test") |
| 198 | parser.add_option("-v", "--view", |
| 199 | dest="view", default=None, |
| 200 | help="view generated gestures(g), activity in mtedit(a) " + |
| 201 | "gestures-log(gl), evdev-log(el) or activity-log(al)") |
| 202 | parser.add_option("-r", "--ref", |
| 203 | dest="ref", default=None, |
| 204 | help="reference test results for detecting regressions") |
| 205 | parser.add_option("-o", "--out", |
| 206 | dest="out", default=None, |
| 207 | help="output test results to file.") |
| 208 | parser.add_option("-n", "--new", |
| 209 | dest="new", action="store_true", default=False, |
| 210 | help="Create new device logs before downloading. " + |
| 211 | "[Default: False]") |
| 212 | parser.add_option("--no-edit", |
| 213 | dest="noedit", action="store_true", default=False, |
| 214 | help="Skip editing when creating tests. Add original log " + |
| 215 | "[Default: False]") |
Dennis Kempin | 8e3201c | 2013-03-14 13:49:01 -0700 | [diff] [blame^] | 216 | parser.add_option("--autotest", |
| 217 | dest="autotest", action="store_true", default=False, |
| 218 | help="Run in autotest mode. Skips recompilation.") |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 219 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 220 | (options, args) = parser.parse_args() |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 221 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 222 | if len(args) == 0: |
| 223 | test_name = "all" |
| 224 | elif len(args) == 1: |
| 225 | test_name = args[0] |
| 226 | else: |
| 227 | parser.print_help() |
| 228 | exit(-1) |
| 229 | |
| 230 | if options.create: |
| 231 | # create temporary files for storing activity and evdev log |
| 232 | tmp = NamedTemporaryFile("w") |
| 233 | tmp_evdev = NamedTemporaryFile("w") |
| 234 | |
| 235 | # obtain trimmed log data |
| 236 | original_log = Log(options.create, options) |
| 237 | if options.noedit: |
| 238 | log = original_log |
Dennis Kempin | 1296830 | 2012-08-09 15:37:09 -0700 | [diff] [blame] | 239 | else: |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 240 | editor = LogEditor(persistent=False) |
| 241 | log = editor.Edit(original_log) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 242 | |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 243 | # write to temporary files |
| 244 | tmp.write(log.activity) |
| 245 | tmp.flush() |
| 246 | tmp_evdev.write(log.evdev) |
| 247 | tmp_evdev.flush() |
| 248 | |
| 249 | # pass to touchtests |
| 250 | Add(test_name, tmp.name, tmp_evdev.name) |
| 251 | |
| 252 | # cleanup |
| 253 | tmp.close() |
| 254 | tmp_evdev.close() |
| 255 | |
| 256 | elif options.view: |
| 257 | view = options.view |
| 258 | if view == "g": |
| 259 | view = "gestures" |
| 260 | elif view == "gl": |
| 261 | view = "gestures-log" |
| 262 | elif view == "el": |
| 263 | view = "evdev-log" |
| 264 | elif view == "al": |
| 265 | view = "activity-log" |
| 266 | elif view == "a": |
| 267 | view = "activity" |
| 268 | |
| 269 | if view == "activity": |
| 270 | activity = Get(test_name, "return-activity") |
| 271 | tmp = NamedTemporaryFile("w") |
| 272 | tmp.write(activity) |
| 273 | tmp.flush() |
| 274 | log = Log(tmp.name, options) |
| 275 | editor = LogEditor(persistent=False) |
| 276 | editor.View(log) |
Dennis Kempin | 64f513f | 2012-08-10 15:09:18 -0700 | [diff] [blame] | 277 | else: |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 278 | Get(test_name, view) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 279 | |
| 280 | else: |
Dennis Kempin | c2b5986 | 2013-02-20 14:02:25 -0800 | [diff] [blame] | 281 | Run(test_name, options.out, options.ref) |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 282 | |
Dennis Kempin | 22c7c4d | 2012-07-26 13:04:24 -0700 | [diff] [blame] | 283 | if __name__ == "__main__": |
| 284 | Main() |