Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3.4 |
| 2 | # |
Keun Soo Yim | b05f84e | 2016-10-31 09:29:42 -0700 | [diff] [blame] | 3 | # Copyright (C) 2016 The Android Open Source Project |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
Keun Soo Yim | b05f84e | 2016-10-31 09:29:42 -0700 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Keun Soo Yim | b05f84e | 2016-10-31 09:29:42 -0700 | [diff] [blame] | 16 | # |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 17 | |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 18 | import logging |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 19 | import os |
| 20 | |
| 21 | from vts.runners.host import asserts |
| 22 | from vts.runners.host import errors |
| 23 | from vts.runners.host import keys |
| 24 | from vts.runners.host import logger |
| 25 | from vts.runners.host import records |
| 26 | from vts.runners.host import signals |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 27 | from vts.runners.host import utils |
Yuexi Ma | b34e8c7 | 2016-09-16 20:58:08 +0000 | [diff] [blame] | 28 | from vts.runners.host import const |
| 29 | from vts.utils.python.common import list_utils |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 30 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 31 | # Macro strings for test result reporting |
| 32 | TEST_CASE_TOKEN = "[Test Case]" |
| 33 | RESULT_LINE_TEMPLATE = TEST_CASE_TOKEN + " %s %s" |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 34 | STR_TEST = "test" |
| 35 | STR_GENERATE = "generate" |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | class BaseTestClass(object): |
| 39 | """Base class for all test classes to inherit from. |
| 40 | |
| 41 | This class gets all the controller objects from test_runner and executes |
| 42 | the test cases requested within itself. |
| 43 | |
| 44 | Most attributes of this class are set at runtime based on the configuration |
| 45 | provided. |
| 46 | |
| 47 | Attributes: |
| 48 | tests: A list of strings, each representing a test case name. |
| 49 | TAG: A string used to refer to a test class. Default is the test class |
| 50 | name. |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 51 | results: A records.TestResult object for aggregating test results from |
| 52 | the execution of test cases. |
| 53 | currentTestName: A string that's the name of the test case currently |
| 54 | being executed. If no test is executing, this should |
| 55 | be None. |
Yuexi Ma | b34e8c7 | 2016-09-16 20:58:08 +0000 | [diff] [blame] | 56 | include_filer: A list of string, each representing a test case name to |
| 57 | include. |
| 58 | exclude_filer: A list of string, each representing a test case name to |
| 59 | exclude. Has no effect if include_filer is not empty. |
Yuexi Ma | f362818 | 2016-11-18 17:03:54 -0800 | [diff] [blame] | 60 | abi_name: String, name of abi in use |
| 61 | abi_bitness: String, bitness of abi in use |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 62 | """ |
| 63 | |
| 64 | TAG = None |
| 65 | |
| 66 | def __init__(self, configs): |
| 67 | self.tests = [] |
| 68 | if not self.TAG: |
| 69 | self.TAG = self.__class__.__name__ |
| 70 | # Set all the controller objects and params. |
| 71 | for name, value in configs.items(): |
| 72 | setattr(self, name, value) |
| 73 | self.results = records.TestResult() |
| 74 | self.currentTestName = None |
| 75 | |
Yuexi Ma | f3afb60 | 2016-09-30 16:18:23 -0700 | [diff] [blame] | 76 | # Setup test filters (optional) |
| 77 | if keys.ConfigKeys.KEY_TEST_SUITE in self.user_params: |
| 78 | test_suite = self.user_params[keys.ConfigKeys.KEY_TEST_SUITE] |
| 79 | filters = [keys.ConfigKeys.KEY_INCLUDE_FILTER, |
| 80 | keys.ConfigKeys.KEY_EXCLUDE_FILTER] |
| 81 | for filter in filters: |
| 82 | if filter in test_suite: |
| 83 | filter_expanded = list_utils.ExpandItemDelimiters( |
| 84 | test_suite[filter], |
| 85 | const.LIST_ITEM_DELIMITER, |
| 86 | strip=True) |
| 87 | setattr(self, filter, filter_expanded) |
Yuexi Ma | b34e8c7 | 2016-09-16 20:58:08 +0000 | [diff] [blame] | 88 | |
Yuexi Ma | f362818 | 2016-11-18 17:03:54 -0800 | [diff] [blame] | 89 | # TODO: get abi information differently for multi-device support. |
| 90 | # Set other optional parameters |
| 91 | opt_param_names = [keys.ConfigKeys.IKEY_ABI_NAME, |
| 92 | keys.ConfigKeys.IKEY_ABI_BITNESS, |
| 93 | keys.ConfigKeys.IKEY_RUN_32BIT_ON_64BIT_ABI] |
| 94 | self.getUserParams(opt_param_names=opt_param_names) |
Yuexi Ma | b5132a4 | 2016-11-10 14:59:30 -0800 | [diff] [blame] | 95 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 96 | def __enter__(self): |
| 97 | return self |
| 98 | |
| 99 | def __exit__(self, *args): |
| 100 | self._exec_func(self.cleanUp) |
| 101 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 102 | def getUserParams(self, req_param_names=[], opt_param_names=[], **kwargs): |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 103 | """Unpacks user defined parameters in test config into individual |
| 104 | variables. |
| 105 | |
| 106 | Instead of accessing the user param with self.user_params["xxx"], the |
| 107 | variable can be directly accessed with self.xxx. |
| 108 | |
| 109 | A missing required param will raise an exception. If an optional param |
| 110 | is missing, an INFO line will be logged. |
| 111 | |
| 112 | Args: |
| 113 | req_param_names: A list of names of the required user params. |
| 114 | opt_param_names: A list of names of the optional user params. |
| 115 | **kwargs: Arguments that provide default values. |
| 116 | e.g. getUserParams(required_list, opt_list, arg_a="hello") |
| 117 | self.arg_a will be "hello" unless it is specified again in |
| 118 | required_list or opt_list. |
| 119 | |
| 120 | Raises: |
| 121 | BaseTestError is raised if a required user params is missing from |
| 122 | test config. |
| 123 | """ |
| 124 | for k, v in kwargs.items(): |
| 125 | setattr(self, k, v) |
| 126 | for name in req_param_names: |
| 127 | if name not in self.user_params: |
Ang Li | 05f99ab | 2016-08-04 16:48:24 -0700 | [diff] [blame] | 128 | raise errors.BaseTestError(("Missing required user param '%s' " |
| 129 | "in test configuration.") % name) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 130 | setattr(self, name, self.user_params[name]) |
| 131 | for name in opt_param_names: |
| 132 | if name not in self.user_params: |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 133 | logging.info(("Missing optional user param '%s' in " |
| 134 | "configuration, continue."), name) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 135 | else: |
| 136 | setattr(self, name, self.user_params[name]) |
| 137 | |
Yuexi Ma | fb20bb3 | 2016-11-10 15:39:30 -0800 | [diff] [blame] | 138 | def getUserParam(self, |
| 139 | param_name, |
| 140 | error_if_not_found=False, |
| 141 | default_value=None): |
Yuexi Ma | c1396c0 | 2016-11-10 15:13:29 -0800 | [diff] [blame] | 142 | """Get the value of a single user parameter. |
| 143 | |
| 144 | This method returns the value of specified user parameter. |
| 145 | Note: this method will not automatically set attribute using the parameter name and value. |
| 146 | |
| 147 | Args: |
| 148 | param_name: string or list of string, denoting user parameter names. If provided |
| 149 | a single string, self.user_params["<param_name>"] will be accessed. |
| 150 | If provided multiple strings, |
| 151 | self.user_params["<param_name1>"]["<param_name2>"]["<param_name3>"]... |
| 152 | will be accessed. |
| 153 | error_if_not_found: bool, whether to raise error if parameter not exists. Default: |
| 154 | False |
| 155 | default_value: object, default value to return if not found. If error_if_not_found is |
| 156 | True, this parameter has no effect. Default: None |
| 157 | |
| 158 | Returns: |
| 159 | object, value of the specified parameter name chain if exists; |
| 160 | <default_value> if not exists. |
| 161 | """ |
| 162 | if not param_name: |
| 163 | if error_if_not_found: |
| 164 | raise errors.BaseTestError("empty param_name provided") |
| 165 | logging.error("empty param_name") |
| 166 | return default_value |
| 167 | |
| 168 | if not isinstance(param_name, list): |
| 169 | param_name = [param_name] |
| 170 | |
| 171 | curr_obj = self.user_params |
| 172 | for param in param_name: |
| 173 | if param not in curr_obj: |
| 174 | if error_if_not_found: |
| 175 | raise errors.BaseTestError( |
| 176 | ("Missing user param '%s' " |
| 177 | "in test configuration.") % name) |
| 178 | return default_value |
| 179 | curr_obj = curr_obj[param] |
| 180 | |
| 181 | return curr_obj |
| 182 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 183 | def _setUpClass(self): |
| 184 | """Proxy function to guarantee the base implementation of setUpClass |
| 185 | is called. |
| 186 | """ |
| 187 | return self.setUpClass() |
| 188 | |
| 189 | def setUpClass(self): |
| 190 | """Setup function that will be called before executing any test case in |
| 191 | the test class. |
| 192 | |
| 193 | To signal setup failure, return False or raise an exception. If |
| 194 | exceptions were raised, the stack trace would appear in log, but the |
| 195 | exceptions would not propagate to upper levels. |
| 196 | |
| 197 | Implementation is optional. |
| 198 | """ |
Keun Soo Yim | bae361d | 2016-07-23 10:39:54 -0700 | [diff] [blame] | 199 | pass |
| 200 | |
| 201 | def _tearDownClass(self): |
| 202 | """Proxy function to guarantee the base implementation of tearDownClass |
| 203 | is called. |
| 204 | """ |
| 205 | return self.tearDownClass() |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 206 | |
| 207 | def tearDownClass(self): |
| 208 | """Teardown function that will be called after all the selected test |
| 209 | cases in the test class have been executed. |
| 210 | |
| 211 | Implementation is optional. |
| 212 | """ |
Keun Soo Yim | bae361d | 2016-07-23 10:39:54 -0700 | [diff] [blame] | 213 | pass |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 214 | |
Yuexi Ma | 4e22899 | 2016-11-15 22:48:57 -0800 | [diff] [blame] | 215 | def _testEntry(self, test_name): |
Yuexi Ma | 1307785 | 2016-11-17 18:37:02 -0800 | [diff] [blame] | 216 | """Internal function to be called upon entry of a test case.""" |
Yuexi Ma | 4e22899 | 2016-11-15 22:48:57 -0800 | [diff] [blame] | 217 | self.currentTestName = test_name |
| 218 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 219 | def _setUpTest(self, test_name): |
| 220 | """Proxy function to guarantee the base implementation of setUpTest is |
| 221 | called. |
| 222 | """ |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 223 | return self.setUpTest() |
| 224 | |
| 225 | def setUpTest(self): |
| 226 | """Setup function that will be called every time before executing each |
| 227 | test case in the test class. |
| 228 | |
| 229 | To signal setup failure, return False or raise an exception. If |
| 230 | exceptions were raised, the stack trace would appear in log, but the |
| 231 | exceptions would not propagate to upper levels. |
| 232 | |
| 233 | Implementation is optional. |
| 234 | """ |
| 235 | |
Yuexi Ma | 4e22899 | 2016-11-15 22:48:57 -0800 | [diff] [blame] | 236 | def _testExit(self, test_name): |
| 237 | """Internal function to be called upon exit of a test.""" |
| 238 | self.currentTestName = None |
| 239 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 240 | def _tearDownTest(self, test_name): |
| 241 | """Proxy function to guarantee the base implementation of tearDownTest |
| 242 | is called. |
| 243 | """ |
Yuexi Ma | 4e22899 | 2016-11-15 22:48:57 -0800 | [diff] [blame] | 244 | self.tearDownTest() |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 245 | |
| 246 | def tearDownTest(self): |
| 247 | """Teardown function that will be called every time a test case has |
| 248 | been executed. |
| 249 | |
| 250 | Implementation is optional. |
| 251 | """ |
| 252 | |
| 253 | def _onFail(self, record): |
| 254 | """Proxy function to guarantee the base implementation of onFail is |
| 255 | called. |
| 256 | |
| 257 | Args: |
| 258 | record: The records.TestResultRecord object for the failed test |
| 259 | case. |
| 260 | """ |
| 261 | test_name = record.test_name |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 262 | logging.error(record.details) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 263 | begin_time = logger.epochToLogLineTimestamp(record.begin_time) |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 264 | logging.info(RESULT_LINE_TEMPLATE, test_name, record.result) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 265 | self.onFail(test_name, begin_time) |
| 266 | |
| 267 | def onFail(self, test_name, begin_time): |
| 268 | """A function that is executed upon a test case failure. |
| 269 | |
| 270 | User implementation is optional. |
| 271 | |
| 272 | Args: |
| 273 | test_name: Name of the test that triggered this function. |
| 274 | begin_time: Logline format timestamp taken when the test started. |
| 275 | """ |
| 276 | |
| 277 | def _onPass(self, record): |
| 278 | """Proxy function to guarantee the base implementation of onPass is |
| 279 | called. |
| 280 | |
| 281 | Args: |
| 282 | record: The records.TestResultRecord object for the passed test |
| 283 | case. |
| 284 | """ |
| 285 | test_name = record.test_name |
| 286 | begin_time = logger.epochToLogLineTimestamp(record.begin_time) |
| 287 | msg = record.details |
| 288 | if msg: |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 289 | logging.info(msg) |
| 290 | logging.info(RESULT_LINE_TEMPLATE, test_name, record.result) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 291 | self.onPass(test_name, begin_time) |
| 292 | |
| 293 | def onPass(self, test_name, begin_time): |
| 294 | """A function that is executed upon a test case passing. |
| 295 | |
| 296 | Implementation is optional. |
| 297 | |
| 298 | Args: |
| 299 | test_name: Name of the test that triggered this function. |
| 300 | begin_time: Logline format timestamp taken when the test started. |
| 301 | """ |
| 302 | |
| 303 | def _onSkip(self, record): |
| 304 | """Proxy function to guarantee the base implementation of onSkip is |
| 305 | called. |
| 306 | |
| 307 | Args: |
| 308 | record: The records.TestResultRecord object for the skipped test |
| 309 | case. |
| 310 | """ |
| 311 | test_name = record.test_name |
| 312 | begin_time = logger.epochToLogLineTimestamp(record.begin_time) |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 313 | logging.info(RESULT_LINE_TEMPLATE, test_name, record.result) |
| 314 | logging.info("Reason to skip: %s", record.details) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 315 | self.onSkip(test_name, begin_time) |
| 316 | |
| 317 | def onSkip(self, test_name, begin_time): |
| 318 | """A function that is executed upon a test case being skipped. |
| 319 | |
| 320 | Implementation is optional. |
| 321 | |
| 322 | Args: |
| 323 | test_name: Name of the test that triggered this function. |
| 324 | begin_time: Logline format timestamp taken when the test started. |
| 325 | """ |
| 326 | |
Yuexi Ma | 1307785 | 2016-11-17 18:37:02 -0800 | [diff] [blame] | 327 | def _onSilent(self, record): |
| 328 | """Proxy function to guarantee the base implementation of onSilent is |
| 329 | called. |
| 330 | |
| 331 | Args: |
| 332 | record: The records.TestResultRecord object for the skipped test |
| 333 | case. |
| 334 | """ |
| 335 | test_name = record.test_name |
| 336 | begin_time = logger.epochToLogLineTimestamp(record.begin_time) |
| 337 | self.onSilent(test_name, begin_time) |
| 338 | |
| 339 | def onSilent(self, test_name, begin_time): |
| 340 | """A function that is executed upon a test case being marked as silent. |
| 341 | |
| 342 | Implementation is optional. |
| 343 | |
| 344 | Args: |
| 345 | test_name: Name of the test that triggered this function. |
| 346 | begin_time: Logline format timestamp taken when the test started. |
| 347 | """ |
| 348 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 349 | def _onException(self, record): |
| 350 | """Proxy function to guarantee the base implementation of onException |
| 351 | is called. |
| 352 | |
| 353 | Args: |
| 354 | record: The records.TestResultRecord object for the failed test |
| 355 | case. |
| 356 | """ |
| 357 | test_name = record.test_name |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 358 | logging.exception(record.details) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 359 | begin_time = logger.epochToLogLineTimestamp(record.begin_time) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 360 | self.onException(test_name, begin_time) |
| 361 | |
| 362 | def onException(self, test_name, begin_time): |
| 363 | """A function that is executed upon an unhandled exception from a test |
| 364 | case. |
| 365 | |
| 366 | Implementation is optional. |
| 367 | |
| 368 | Args: |
| 369 | test_name: Name of the test that triggered this function. |
| 370 | begin_time: Logline format timestamp taken when the test started. |
| 371 | """ |
| 372 | |
| 373 | def _exec_procedure_func(self, func, tr_record): |
| 374 | """Executes a procedure function like onPass, onFail etc. |
| 375 | |
| 376 | This function will alternate the 'Result' of the test's record if |
| 377 | exceptions happened when executing the procedure function. |
| 378 | |
| 379 | This will let signals.TestAbortAll through so abortAll works in all |
| 380 | procedure functions. |
| 381 | |
| 382 | Args: |
| 383 | func: The procedure function to be executed. |
| 384 | tr_record: The TestResultRecord object associated with the test |
| 385 | case executed. |
| 386 | """ |
| 387 | try: |
| 388 | func(tr_record) |
| 389 | except signals.TestAbortAll: |
| 390 | raise |
| 391 | except Exception as e: |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 392 | logging.exception("Exception happened when executing %s for %s.", |
| 393 | func.__name__, self.currentTestName) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 394 | tr_record.addError(func.__name__, e) |
| 395 | |
Yuexi Ma | f3afb60 | 2016-09-30 16:18:23 -0700 | [diff] [blame] | 396 | def filterOneTest(self, test_name): |
Yuexi Ma | b5132a4 | 2016-11-10 14:59:30 -0800 | [diff] [blame] | 397 | """Check test filters for a test name. |
Yuexi Ma | f3afb60 | 2016-09-30 16:18:23 -0700 | [diff] [blame] | 398 | |
Yuexi Ma | b5132a4 | 2016-11-10 14:59:30 -0800 | [diff] [blame] | 399 | The first layer of filter is user defined test filters: |
| 400 | if a include filter is not empty, only tests in include filter will |
Yuexi Ma | f3afb60 | 2016-09-30 16:18:23 -0700 | [diff] [blame] | 401 | be executed regardless whether they are also in exclude filter. Else |
| 402 | if include filter is empty, only tests not in exclude filter will be |
| 403 | executed. |
| 404 | |
Yuexi Ma | b5132a4 | 2016-11-10 14:59:30 -0800 | [diff] [blame] | 405 | The second layer of filter is checking abi bitness: |
| 406 | if a test has a suffix indicating the intended architecture bitness, |
| 407 | and the current abi bitness information is available, non matching tests |
| 408 | will be skipped. By our convention, this function will look for bitness in suffix |
| 409 | formated as "32bit", "32Bit", "32BIT", or 64 bit equivalents. |
| 410 | |
Yuexi Ma | fb20bb3 | 2016-11-10 15:39:30 -0800 | [diff] [blame] | 411 | This method assumes const.SUFFIX_32BIT and const.SUFFIX_64BIT are in lower cases. |
| 412 | |
Yuexi Ma | f3afb60 | 2016-09-30 16:18:23 -0700 | [diff] [blame] | 413 | Args: |
| 414 | test_name: string, name of a test |
| 415 | |
| 416 | Raises: |
| 417 | signals.TestSilent if a test should not be executed |
| 418 | """ |
| 419 | if (hasattr(self, keys.ConfigKeys.KEY_INCLUDE_FILTER) and |
| 420 | getattr(self, keys.ConfigKeys.KEY_INCLUDE_FILTER)): |
| 421 | if test_name not in getattr(self, |
| 422 | keys.ConfigKeys.KEY_INCLUDE_FILTER): |
| 423 | logging.info("Test case '%s' not in include filter." % |
| 424 | test_name) |
| 425 | raise signals.TestSilent( |
| 426 | "Test case '%s' not in include filter." % test_name) |
| 427 | elif (hasattr(self, keys.ConfigKeys.KEY_EXCLUDE_FILTER) and |
| 428 | test_name in getattr(self, keys.ConfigKeys.KEY_EXCLUDE_FILTER)): |
| 429 | logging.info("Test case '%s' in exclude filter." % test_name) |
| 430 | raise signals.TestSilent("Test case '%s' in exclude filter." % |
| 431 | test_name) |
| 432 | |
Yuexi Ma | f362818 | 2016-11-18 17:03:54 -0800 | [diff] [blame] | 433 | if hasattr(self, keys.ConfigKeys.IKEY_ABI_BITNESS): |
| 434 | bitness = getattr(self, keys.ConfigKeys.IKEY_ABI_BITNESS) |
Yuexi Ma | f97beaf | 2016-11-23 12:58:06 -0800 | [diff] [blame] | 435 | run_32bit_on_64bit_abi = getattr( |
Yuexi Ma | a4ba393 | 2016-11-23 15:04:18 -0800 | [diff] [blame] | 436 | self, keys.ConfigKeys.IKEY_RUN_32BIT_ON_64BIT_ABI, False) |
Yuexi Ma | b5132a4 | 2016-11-10 14:59:30 -0800 | [diff] [blame] | 437 | asserts.skipIf( |
Yuexi Ma | 9c1ec63 | 2016-11-18 18:36:55 -0800 | [diff] [blame] | 438 | (test_name.lower().endswith(const.SUFFIX_32BIT) and |
Yuexi Ma | f97beaf | 2016-11-23 12:58:06 -0800 | [diff] [blame] | 439 | bitness != "32") or |
Yuexi Ma | 9c1ec63 | 2016-11-18 18:36:55 -0800 | [diff] [blame] | 440 | (test_name.lower().endswith(const.SUFFIX_64BIT) and |
Yuexi Ma | f97beaf | 2016-11-23 12:58:06 -0800 | [diff] [blame] | 441 | bitness != "64" and not run_32bit_on_64bit_abi), |
Yuexi Ma | b5132a4 | 2016-11-10 14:59:30 -0800 | [diff] [blame] | 442 | "Test case '{}' excluded as abi bitness is {}.".format( |
Yuexi Ma | f362818 | 2016-11-18 17:03:54 -0800 | [diff] [blame] | 443 | test_name, bitness)) |
Yuexi Ma | b5132a4 | 2016-11-10 14:59:30 -0800 | [diff] [blame] | 444 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 445 | def execOneTest(self, test_name, test_func, args, **kwargs): |
| 446 | """Executes one test case and update test results. |
| 447 | |
| 448 | Executes one test case, create a records.TestResultRecord object with |
| 449 | the execution information, and add the record to the test class's test |
| 450 | results. |
| 451 | |
| 452 | Args: |
| 453 | test_name: Name of the test. |
| 454 | test_func: The test function. |
| 455 | args: A tuple of params. |
| 456 | kwargs: Extra kwargs. |
| 457 | """ |
Yuexi Ma | 8594194 | 2016-09-14 00:49:24 -0700 | [diff] [blame] | 458 | is_silenced = False |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 459 | tr_record = records.TestResultRecord(test_name, self.TAG) |
| 460 | tr_record.testBegin() |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 461 | logging.info("%s %s", TEST_CASE_TOKEN, test_name) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 462 | verdict = None |
| 463 | try: |
Yuexi Ma | 4e22899 | 2016-11-15 22:48:57 -0800 | [diff] [blame] | 464 | ret = self._testEntry(test_name) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 465 | asserts.assertTrue(ret is not False, |
Yuexi Ma | 4e22899 | 2016-11-15 22:48:57 -0800 | [diff] [blame] | 466 | "Setup test entry for %s failed." % test_name) |
| 467 | self.filterOneTest(test_name) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 468 | try: |
Yuexi Ma | 4e22899 | 2016-11-15 22:48:57 -0800 | [diff] [blame] | 469 | ret = self._setUpTest(test_name) |
| 470 | asserts.assertTrue(ret is not False, |
| 471 | "Setup for %s failed." % test_name) |
| 472 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 473 | if args or kwargs: |
| 474 | verdict = test_func(*args, **kwargs) |
| 475 | else: |
| 476 | verdict = test_func() |
| 477 | finally: |
| 478 | self._tearDownTest(test_name) |
| 479 | except (signals.TestFailure, AssertionError) as e: |
| 480 | tr_record.testFail(e) |
| 481 | self._exec_procedure_func(self._onFail, tr_record) |
| 482 | except signals.TestSkip as e: |
| 483 | # Test skipped. |
| 484 | tr_record.testSkip(e) |
| 485 | self._exec_procedure_func(self._onSkip, tr_record) |
| 486 | except (signals.TestAbortClass, signals.TestAbortAll) as e: |
| 487 | # Abort signals, pass along. |
| 488 | tr_record.testFail(e) |
| 489 | raise e |
| 490 | except signals.TestPass as e: |
| 491 | # Explicit test pass. |
| 492 | tr_record.testPass(e) |
| 493 | self._exec_procedure_func(self._onPass, tr_record) |
| 494 | except signals.TestSilent as e: |
Yuexi Ma | aeaacbc | 2016-09-15 21:28:44 -0700 | [diff] [blame] | 495 | # Suppress test reporting. |
Yuexi Ma | 8594194 | 2016-09-14 00:49:24 -0700 | [diff] [blame] | 496 | is_silenced = True |
Yuexi Ma | 1307785 | 2016-11-17 18:37:02 -0800 | [diff] [blame] | 497 | self._exec_procedure_func(self._onSilent, tr_record) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 498 | self.results.requested.remove(test_name) |
| 499 | except Exception as e: |
| 500 | # Exception happened during test. |
Yuexi Ma | aeaacbc | 2016-09-15 21:28:44 -0700 | [diff] [blame] | 501 | logging.exception(e) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 502 | tr_record.testError(e) |
| 503 | self._exec_procedure_func(self._onException, tr_record) |
| 504 | self._exec_procedure_func(self._onFail, tr_record) |
| 505 | else: |
| 506 | # Keep supporting return False for now. |
| 507 | # TODO(angli): Deprecate return False support. |
| 508 | if verdict or (verdict is None): |
| 509 | # Test passed. |
| 510 | tr_record.testPass() |
| 511 | self._exec_procedure_func(self._onPass, tr_record) |
| 512 | return |
| 513 | # Test failed because it didn't return True. |
| 514 | # This should be removed eventually. |
| 515 | tr_record.testFail() |
| 516 | self._exec_procedure_func(self._onFail, tr_record) |
| 517 | finally: |
Yuexi Ma | 8594194 | 2016-09-14 00:49:24 -0700 | [diff] [blame] | 518 | if not is_silenced: |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 519 | self.results.addRecord(tr_record) |
Yuexi Ma | 1307785 | 2016-11-17 18:37:02 -0800 | [diff] [blame] | 520 | self._testExit(test_name) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 521 | |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 522 | def runGeneratedTests(self, |
| 523 | test_func, |
| 524 | settings, |
| 525 | args=None, |
| 526 | kwargs=None, |
| 527 | tag="", |
| 528 | name_func=None): |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 529 | """Runs generated test cases. |
| 530 | |
| 531 | Generated test cases are not written down as functions, but as a list |
| 532 | of parameter sets. This way we reduce code repetition and improve |
| 533 | test case scalability. |
| 534 | |
| 535 | Args: |
| 536 | test_func: The common logic shared by all these generated test |
| 537 | cases. This function should take at least one argument, |
| 538 | which is a parameter set. |
| 539 | settings: A list of strings representing parameter sets. These are |
| 540 | usually json strings that get loaded in the test_func. |
| 541 | args: Iterable of additional position args to be passed to |
| 542 | test_func. |
| 543 | kwargs: Dict of additional keyword args to be passed to test_func |
| 544 | tag: Name of this group of generated test cases. Ignored if |
| 545 | name_func is provided and operates properly. |
| 546 | name_func: A function that takes a test setting and generates a |
| 547 | proper test name. The test name should be shorter than |
| 548 | utils.MAX_FILENAME_LEN. Names over the limit will be |
| 549 | truncated. |
| 550 | |
| 551 | Returns: |
| 552 | A list of settings that did not pass. |
| 553 | """ |
| 554 | args = args or () |
| 555 | kwargs = kwargs or {} |
| 556 | failed_settings = [] |
| 557 | for s in settings: |
| 558 | test_name = "{} {}".format(tag, s) |
| 559 | if name_func: |
| 560 | try: |
| 561 | test_name = name_func(s, *args, **kwargs) |
| 562 | except: |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 563 | logging.exception(("Failed to get test name from " |
| 564 | "test_func. Fall back to default %s"), |
| 565 | test_name) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 566 | self.results.requested.append(test_name) |
| 567 | if len(test_name) > utils.MAX_FILENAME_LEN: |
| 568 | test_name = test_name[:utils.MAX_FILENAME_LEN] |
| 569 | previous_success_cnt = len(self.results.passed) |
Ang Li | e2139f1 | 2016-05-12 17:39:06 -0700 | [diff] [blame] | 570 | self.execOneTest(test_name, test_func, (s, ) + args, **kwargs) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 571 | if len(self.results.passed) - previous_success_cnt != 1: |
| 572 | failed_settings.append(s) |
| 573 | return failed_settings |
| 574 | |
| 575 | def _exec_func(self, func, *args): |
| 576 | """Executes a function with exception safeguard. |
| 577 | |
| 578 | This will let signals.TestAbortAll through so abortAll works in all |
| 579 | procedure functions. |
| 580 | |
| 581 | Args: |
| 582 | func: Function to be executed. |
| 583 | args: Arguments to be passed to the function. |
| 584 | |
| 585 | Returns: |
| 586 | Whatever the function returns, or False if unhandled exception |
| 587 | occured. |
| 588 | """ |
| 589 | try: |
| 590 | return func(*args) |
| 591 | except signals.TestAbortAll: |
| 592 | raise |
| 593 | except: |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 594 | logging.exception("Exception happened when executing %s in %s.", |
| 595 | func.__name__, self.TAG) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 596 | return False |
| 597 | |
| 598 | def _get_all_test_names(self): |
| 599 | """Finds all the function names that match the test case naming |
| 600 | convention in this class. |
| 601 | |
| 602 | Returns: |
| 603 | A list of strings, each is a test case name. |
| 604 | """ |
| 605 | test_names = [] |
| 606 | for name in dir(self): |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 607 | if name.startswith(STR_TEST) or name.startswith(STR_GENERATE): |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 608 | attr_func = getattr(self, name) |
| 609 | if hasattr(attr_func, "__call__"): |
| 610 | test_names.append(name) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 611 | return test_names |
| 612 | |
| 613 | def _get_test_funcs(self, test_names): |
| 614 | """Obtain the actual functions of test cases based on test names. |
| 615 | |
| 616 | Args: |
| 617 | test_names: A list of strings, each string is a test case name. |
| 618 | |
| 619 | Returns: |
| 620 | A list of tuples of (string, function). String is the test case |
| 621 | name, function is the actual test case function. |
| 622 | |
| 623 | Raises: |
| 624 | errors.USERError is raised if the test name does not follow |
| 625 | naming convention "test_*". This can only be caused by user input |
| 626 | here. |
| 627 | """ |
| 628 | test_funcs = [] |
| 629 | for test_name in test_names: |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 630 | if not hasattr(self, test_name): |
| 631 | logging.warning("%s does not have test case %s.", self.TAG, |
| 632 | test_name) |
| 633 | elif (test_name.startswith(STR_TEST) or |
| 634 | test_name.startswith(STR_GENERATE)): |
| 635 | test_funcs.append((test_name, getattr(self, test_name))) |
| 636 | else: |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 637 | msg = ("Test case name %s does not follow naming convention " |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 638 | "test*, abort.") % test_name |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 639 | raise errors.USERError(msg) |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 640 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 641 | return test_funcs |
| 642 | |
| 643 | def run(self, test_names=None): |
| 644 | """Runs test cases within a test class by the order they appear in the |
| 645 | execution list. |
| 646 | |
| 647 | One of these test cases lists will be executed, shown here in priority |
| 648 | order: |
| 649 | 1. The test_names list, which is passed from cmd line. Invalid names |
| 650 | are guarded by cmd line arg parsing. |
| 651 | 2. The self.tests list defined in test class. Invalid names are |
| 652 | ignored. |
| 653 | 3. All function that matches test case naming convention in the test |
| 654 | class. |
| 655 | |
| 656 | Args: |
| 657 | test_names: A list of string that are test case names requested in |
| 658 | cmd line. |
| 659 | |
| 660 | Returns: |
| 661 | The test results object of this class. |
| 662 | """ |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 663 | logging.info("==========> %s <==========", self.TAG) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 664 | # Devise the actual test cases to run in the test class. |
| 665 | if not test_names: |
| 666 | if self.tests: |
| 667 | # Specified by run list in class. |
| 668 | test_names = list(self.tests) |
| 669 | else: |
| 670 | # No test case specified by user, execute all in the test class |
| 671 | test_names = self._get_all_test_names() |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 672 | self.results.requested = [test_name for test_name in test_names |
| 673 | if test_name.startswith(STR_TEST)] |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 674 | tests = self._get_test_funcs(test_names) |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 675 | |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 676 | # Setup for the class. |
| 677 | try: |
| 678 | if self._setUpClass() is False: |
| 679 | raise signals.TestFailure("Failed to setup %s." % self.TAG) |
| 680 | except Exception as e: |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 681 | logging.exception("Failed to setup %s.", self.TAG) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 682 | self.results.failClass(self.TAG, e) |
Keun Soo Yim | bae361d | 2016-07-23 10:39:54 -0700 | [diff] [blame] | 683 | self._exec_func(self._tearDownClass) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 684 | return self.results |
| 685 | # Run tests in order. |
| 686 | try: |
| 687 | for test_name, test_func in tests: |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 688 | if test_name.startswith(STR_GENERATE): |
Yuexi Ma | aeaacbc | 2016-09-15 21:28:44 -0700 | [diff] [blame] | 689 | logging.info( |
| 690 | "Executing generated test trigger function '%s'", |
| 691 | test_name) |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 692 | test_func() |
Yuexi Ma | aeaacbc | 2016-09-15 21:28:44 -0700 | [diff] [blame] | 693 | logging.info("Finished '%s'", test_name) |
Yuexi Ma | f87f2c6 | 2016-09-14 00:47:30 -0700 | [diff] [blame] | 694 | else: |
| 695 | self.execOneTest(test_name, test_func, None) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 696 | return self.results |
| 697 | except signals.TestAbortClass: |
Yuexi Ma | aeaacbc | 2016-09-15 21:28:44 -0700 | [diff] [blame] | 698 | logging.info("Received TestAbortClass signal") |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 699 | return self.results |
| 700 | except signals.TestAbortAll as e: |
Yuexi Ma | aeaacbc | 2016-09-15 21:28:44 -0700 | [diff] [blame] | 701 | logging.info("Received TestAbortAll signal") |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 702 | # Piggy-back test results on this exception object so we don't lose |
| 703 | # results from this test class. |
| 704 | setattr(e, "results", self.results) |
| 705 | raise e |
Yuexi Ma | f8d6e5b | 2016-10-12 21:57:53 -0700 | [diff] [blame] | 706 | except Exception as e: |
| 707 | # Exception happened during test. |
| 708 | logging.exception(e) |
| 709 | raise e |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 710 | finally: |
Keun Soo Yim | bae361d | 2016-07-23 10:39:54 -0700 | [diff] [blame] | 711 | self._exec_func(self._tearDownClass) |
Ang Li | 6492027 | 2016-05-26 18:37:43 -0700 | [diff] [blame] | 712 | logging.info("Summary for test class %s: %s", self.TAG, |
| 713 | self.results.summary()) |
Ang Li | 9342000 | 2016-05-10 19:11:44 -0700 | [diff] [blame] | 714 | |
| 715 | def cleanUp(self): |
| 716 | """A function that is executed upon completion of all tests cases |
| 717 | selected in the test class. |
| 718 | |
| 719 | This function should clean up objects initialized in the constructor by |
| 720 | user. |
| 721 | """ |