Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 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 | |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 6 | import abc |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 7 | |
| 8 | import common |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 9 | from autotest_lib.server.cros import provision_actionables as actionables |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | ### Constants for label prefixes |
| 13 | CROS_VERSION_PREFIX = 'cros-version' |
Dan Shi | 0723bf5 | 2015-06-24 10:52:38 -0700 | [diff] [blame] | 14 | FW_RW_VERSION_PREFIX = 'fwrw-version' |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 15 | FW_RO_VERSION_PREFIX = 'fwro-version' |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 16 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 17 | # Default number of provisions attempts to try if we believe the devserver is |
| 18 | # flaky. |
| 19 | FLAKY_DEVSERVER_ATTEMPTS = 2 |
| 20 | |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 21 | |
| 22 | ### Helpers to convert value to label |
| 23 | def cros_version_to_label(image): |
| 24 | """ |
| 25 | Returns the proper label name for a ChromeOS build of |image|. |
| 26 | |
| 27 | @param image: A string of the form 'lumpy-release/R28-3993.0.0' |
| 28 | @returns: A string that is the appropriate label name. |
| 29 | |
| 30 | """ |
| 31 | return CROS_VERSION_PREFIX + ':' + image |
| 32 | |
| 33 | |
Tom Wai-Hong Tam | 2d00cb2 | 2016-01-08 06:40:50 +0800 | [diff] [blame] | 34 | def fwro_version_to_label(image): |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 35 | """ |
Tom Wai-Hong Tam | 2d00cb2 | 2016-01-08 06:40:50 +0800 | [diff] [blame] | 36 | Returns the proper label name for a RO firmware build of |image|. |
| 37 | |
| 38 | @param image: A string of the form 'lumpy-release/R28-3993.0.0' |
| 39 | @returns: A string that is the appropriate label name. |
| 40 | |
| 41 | """ |
| 42 | return FW_RO_VERSION_PREFIX + ':' + image |
| 43 | |
| 44 | |
| 45 | def fwrw_version_to_label(image): |
| 46 | """ |
| 47 | Returns the proper label name for a RW firmware build of |image|. |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 48 | |
| 49 | @param image: A string of the form 'lumpy-release/R28-3993.0.0' |
| 50 | @returns: A string that is the appropriate label name. |
| 51 | |
| 52 | """ |
Dan Shi | 0723bf5 | 2015-06-24 10:52:38 -0700 | [diff] [blame] | 53 | return FW_RW_VERSION_PREFIX + ':' + image |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 54 | |
| 55 | |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 56 | class _SpecialTaskAction(object): |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 57 | """ |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 58 | Base class to give a template for mapping labels to tests. |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 59 | """ |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 60 | |
| 61 | __metaclass__ = abc.ABCMeta |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 62 | |
| 63 | |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 64 | # One cannot do |
| 65 | # @abc.abstractproperty |
| 66 | # _actions = {} |
| 67 | # so this is the next best thing |
| 68 | @abc.abstractproperty |
| 69 | def _actions(self): |
| 70 | """A dictionary mapping labels to test names.""" |
| 71 | pass |
| 72 | |
| 73 | |
| 74 | @abc.abstractproperty |
| 75 | def name(self): |
| 76 | """The name of this special task to be used in output.""" |
| 77 | pass |
| 78 | |
| 79 | |
| 80 | @classmethod |
| 81 | def acts_on(cls, label): |
| 82 | """ |
| 83 | Returns True if the label is a label that we recognize as something we |
| 84 | know how to act on, given our _actions. |
| 85 | |
| 86 | @param label: The label as a string. |
| 87 | @returns: True if there exists a test to run for this label. |
| 88 | |
| 89 | """ |
| 90 | return label.split(':')[0] in cls._actions |
| 91 | |
| 92 | |
| 93 | @classmethod |
| 94 | def test_for(cls, label): |
| 95 | """ |
| 96 | Returns the test associated with the given (string) label name. |
| 97 | |
| 98 | @param label: The label for which the action is being requested. |
| 99 | @returns: The string name of the test that should be run. |
| 100 | @raises KeyError: If the name was not recognized as one we care about. |
| 101 | |
| 102 | """ |
| 103 | return cls._actions[label] |
| 104 | |
| 105 | |
Alex Miller | aa77200 | 2014-04-10 17:51:21 -0700 | [diff] [blame] | 106 | @classmethod |
| 107 | def partition(cls, labels): |
| 108 | """ |
| 109 | Filter a list of labels into two sets: those labels that we know how to |
| 110 | act on and those that we don't know how to act on. |
| 111 | |
| 112 | @param labels: A list of strings of labels. |
| 113 | @returns: A tuple where the first element is a set of unactionable |
| 114 | labels, and the second element is a set of the actionable |
| 115 | labels. |
| 116 | """ |
| 117 | capabilities = set() |
| 118 | configurations = set() |
| 119 | |
| 120 | for label in labels: |
| 121 | if cls.acts_on(label): |
| 122 | configurations.add(label) |
| 123 | else: |
| 124 | capabilities.add(label) |
| 125 | |
| 126 | return capabilities, configurations |
| 127 | |
| 128 | |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 129 | class Verify(_SpecialTaskAction): |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 130 | """ |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 131 | Tests to verify that the DUT is in a sane, known good state that we can run |
| 132 | tests on. Failure to verify leads to running Repair. |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 133 | """ |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 134 | |
| 135 | _actions = { |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 136 | 'modem_repair': actionables.TestActionable('cellular_StaleModemReboot'), |
Don Garrett | 6f7e800 | 2015-07-23 22:45:37 +0000 | [diff] [blame] | 137 | # TODO(crbug.com/404421): set rpm action to power_RPMTest after the RPM |
| 138 | # is stable in lab (destiny). The power_RPMTest failure led to reset job |
| 139 | # failure and that left dut in Repair Failed. Since the test will fail |
| 140 | # anyway due to the destiny lab issue, and test retry will retry the |
| 141 | # test in another DUT. |
| 142 | # This change temporarily disable the RPM check in reset job. |
| 143 | # Another way to do this is to remove rpm dependency from tests' control |
| 144 | # file. That will involve changes on multiple control files. This one |
| 145 | # line change here is a simple temporary fix. |
| 146 | 'rpm': actionables.TestActionable('dummy_PassServer'), |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | name = 'verify' |
| 150 | |
| 151 | |
| 152 | class Provision(_SpecialTaskAction): |
| 153 | """ |
| 154 | Provisioning runs to change the configuration of the DUT from one state to |
| 155 | another. It will only be run on verified DUTs. |
| 156 | """ |
| 157 | |
| 158 | # TODO(milleral): http://crbug.com/249555 |
| 159 | # Create some way to discover and register provisioning tests so that we |
| 160 | # don't need to hand-maintain a list of all of them. |
| 161 | _actions = { |
Fang Deng | 9d54874 | 2015-02-03 11:35:02 -0800 | [diff] [blame] | 162 | CROS_VERSION_PREFIX: actionables.TestActionable( |
| 163 | 'provision_AutoUpdate', |
| 164 | extra_kwargs={'disable_sysinfo': False, |
| 165 | 'disable_before_test_sysinfo': False, |
| 166 | 'disable_before_iteration_sysinfo': True, |
| 167 | 'disable_after_test_sysinfo': True, |
| 168 | 'disable_after_iteration_sysinfo': True}), |
Tom Wai-Hong Tam | 9a23761 | 2016-01-08 03:41:46 +0800 | [diff] [blame] | 169 | FW_RO_VERSION_PREFIX: actionables.TestActionable( |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 170 | 'provision_FirmwareUpdate'), |
Tom Wai-Hong Tam | 9a23761 | 2016-01-08 03:41:46 +0800 | [diff] [blame] | 171 | FW_RW_VERSION_PREFIX: actionables.TestActionable( |
| 172 | 'provision_FirmwareUpdate', |
| 173 | extra_kwargs={'rw_only': True}), |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | name = 'provision' |
| 177 | |
| 178 | |
| 179 | class Cleanup(_SpecialTaskAction): |
| 180 | """ |
| 181 | Cleanup runs after a test fails to try and remove artifacts of tests and |
| 182 | ensure the DUT will be in a sane state for the next test run. |
| 183 | """ |
| 184 | |
| 185 | _actions = { |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 186 | 'cleanup-reboot': actionables.RebootActionable(), |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | name = 'cleanup' |
| 190 | |
| 191 | |
| 192 | class Repair(_SpecialTaskAction): |
| 193 | """ |
| 194 | Repair runs when one of the other special tasks fails. It should be able |
| 195 | to take a component of the DUT that's in an unknown state and restore it to |
| 196 | a good state. |
| 197 | """ |
| 198 | |
| 199 | _actions = { |
| 200 | } |
| 201 | |
| 202 | name = 'repair' |
| 203 | |
| 204 | |
Alex Miller | 1968edf | 2014-02-27 18:11:36 -0800 | [diff] [blame] | 205 | |
Alex Miller | aa77200 | 2014-04-10 17:51:21 -0700 | [diff] [blame] | 206 | # TODO(milleral): crbug.com/364273 |
| 207 | # Label doesn't really mean label in this context. We're putting things into |
| 208 | # DEPENDENCIES that really aren't DEPENDENCIES, and we should probably stop |
| 209 | # doing that. |
| 210 | def is_for_special_action(label): |
| 211 | """ |
| 212 | If any special task handles the label specially, then we're using the label |
| 213 | to communicate that we want an action, and not as an actual dependency that |
| 214 | the test has. |
| 215 | |
| 216 | @param label: A string label name. |
| 217 | @return True if any special task handles this label specially, |
| 218 | False if no special task handles this label. |
| 219 | """ |
| 220 | return (Verify.acts_on(label) or |
| 221 | Provision.acts_on(label) or |
| 222 | Cleanup.acts_on(label) or |
| 223 | Repair.acts_on(label)) |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 224 | |
| 225 | |
| 226 | def filter_labels(labels): |
| 227 | """ |
| 228 | Filter a list of labels into two sets: those labels that we know how to |
| 229 | change and those that we don't. For the ones we know how to change, split |
| 230 | them apart into the name of configuration type and its value. |
| 231 | |
| 232 | @param labels: A list of strings of labels. |
| 233 | @returns: A tuple where the first element is a set of unprovisionable |
| 234 | labels, and the second element is a set of the provisionable |
| 235 | labels. |
| 236 | |
| 237 | >>> filter_labels(['bluetooth', 'cros-version:lumpy-release/R28-3993.0.0']) |
| 238 | (set(['bluetooth']), set(['cros-version:lumpy-release/R28-3993.0.0'])) |
| 239 | |
| 240 | """ |
Alex Miller | aa77200 | 2014-04-10 17:51:21 -0700 | [diff] [blame] | 241 | return Provision.partition(labels) |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 242 | |
| 243 | |
| 244 | def split_labels(labels): |
| 245 | """ |
| 246 | Split a list of labels into a dict mapping name to value. All labels must |
| 247 | be provisionable labels, or else a ValueError |
| 248 | |
| 249 | @param labels: list of strings of label names |
| 250 | @returns: A dict of where the key is the configuration name, and the value |
| 251 | is the configuration value. |
| 252 | @raises: ValueError if a label is not a provisionable label. |
| 253 | |
| 254 | >>> split_labels(['cros-version:lumpy-release/R28-3993.0.0']) |
| 255 | {'cros-version': 'lumpy-release/R28-3993.0.0'} |
| 256 | >>> split_labels(['bluetooth']) |
| 257 | Traceback (most recent call last): |
| 258 | ... |
| 259 | ValueError: Unprovisionable label bluetooth |
| 260 | |
| 261 | """ |
| 262 | configurations = dict() |
| 263 | |
| 264 | for label in labels: |
Alex Miller | aa77200 | 2014-04-10 17:51:21 -0700 | [diff] [blame] | 265 | if Provision.acts_on(label): |
Alex Miller | 0516e4c | 2013-06-03 18:07:48 -0700 | [diff] [blame] | 266 | name, value = label.split(':', 1) |
| 267 | configurations[name] = value |
| 268 | else: |
| 269 | raise ValueError('Unprovisionable label %s' % label) |
| 270 | |
| 271 | return configurations |
| 272 | |
| 273 | |
Alex Miller | 2229c9c | 2013-08-27 15:20:39 -0700 | [diff] [blame] | 274 | def join(provision_type, provision_value): |
| 275 | """ |
| 276 | Combine the provision type and value into the label name. |
| 277 | |
| 278 | @param provision_type: One of the constants that are the label prefixes. |
| 279 | @param provision_value: A string of the value for this provision type. |
| 280 | @returns: A string that is the label name for this (type, value) pair. |
| 281 | |
| 282 | >>> join(CROS_VERSION_PREFIX, 'lumpy-release/R27-3773.0.0') |
| 283 | 'cros-version:lumpy-release/R27-3773.0.0' |
| 284 | |
| 285 | """ |
| 286 | return '%s:%s' % (provision_type, provision_value) |
| 287 | |
| 288 | |
Alex Miller | 667b5f2 | 2014-02-28 15:33:39 -0800 | [diff] [blame] | 289 | class SpecialTaskActionException(Exception): |
| 290 | """ |
| 291 | Exception raised when a special task fails to successfully run a test that |
| 292 | is required. |
| 293 | |
| 294 | This is also a literally meaningless exception. It's always just discarded. |
| 295 | """ |
| 296 | |
| 297 | |
| 298 | def run_special_task_actions(job, host, labels, task): |
| 299 | """ |
| 300 | Iterate through all `label`s and run any tests on `host` that `task` has |
| 301 | corresponding to the passed in labels. |
| 302 | |
| 303 | Emits status lines for each run test, and INFO lines for each skipped label. |
| 304 | |
| 305 | @param job: A job object from a control file. |
| 306 | @param host: The host to run actions on. |
| 307 | @param labels: The list of job labels to work on. |
| 308 | @param task: An instance of _SpecialTaskAction. |
| 309 | @returns: None |
| 310 | @raises: SpecialTaskActionException if a test fails. |
| 311 | |
| 312 | """ |
| 313 | capabilities, configuration = filter_labels(labels) |
| 314 | |
| 315 | for label in capabilities: |
| 316 | if task.acts_on(label): |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 317 | action_item = task.test_for(label) |
| 318 | success = action_item.execute(job=job, host=host) |
Alex Miller | 667b5f2 | 2014-02-28 15:33:39 -0800 | [diff] [blame] | 319 | if not success: |
| 320 | raise SpecialTaskActionException() |
| 321 | else: |
| 322 | job.record('INFO', None, task.name, |
| 323 | "Can't %s label '%s'." % (task.name, label)) |
| 324 | |
| 325 | for name, value in split_labels(configuration).items(): |
| 326 | if task.acts_on(name): |
Fang Deng | af30e7c | 2014-11-15 13:57:03 -0800 | [diff] [blame] | 327 | action_item = task.test_for(name) |
| 328 | success = action_item.execute(job=job, host=host, value=value) |
Alex Miller | 667b5f2 | 2014-02-28 15:33:39 -0800 | [diff] [blame] | 329 | if not success: |
| 330 | raise SpecialTaskActionException() |
| 331 | else: |
| 332 | job.record('INFO', None, task.name, |
| 333 | "Can't %s label '%s:%s'." % (task.name, name, value)) |