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