blob: 3a4fd16b17da323b8b1614bb42d7de537bcfffe7 [file] [log] [blame]
Alex Miller0516e4c2013-06-03 18:07:48 -07001# 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
Allen Li606673f2017-02-21 18:41:13 -08005import collections
Richard Barnette6c2b70a2017-01-26 13:40:51 -08006import re
Dan Shi7279a5a2016-04-07 11:04:28 -07007import sys
Allen Liaaabda82017-02-21 18:27:38 -08008import warnings
Alex Miller0516e4c2013-06-03 18:07:48 -07009
10import common
Fang Dengaf30e7c2014-11-15 13:57:03 -080011from autotest_lib.server.cros import provision_actionables as actionables
Allen Li89711f72017-02-21 18:21:52 -080012from autotest_lib.utils import labellib
13from autotest_lib.utils.labellib import Key
Alex Miller0516e4c2013-06-03 18:07:48 -070014
15
16### Constants for label prefixes
Allen Li89711f72017-02-21 18:21:52 -080017CROS_VERSION_PREFIX = Key.CROS_VERSION
18ANDROID_BUILD_VERSION_PREFIX = Key.ANDROID_BUILD_VERSION
19TESTBED_BUILD_VERSION_PREFIX = Key.TESTBED_VERSION
20FW_RW_VERSION_PREFIX = Key.FIRMWARE_RW_VERSION
21FW_RO_VERSION_PREFIX = Key.FIRMWARE_RO_VERSION
Alex Miller0516e4c2013-06-03 18:07:48 -070022
Richard Barnette6c2b70a2017-01-26 13:40:51 -080023_ANDROID_BUILD_REGEX = r'.+/.+/P?([0-9]+|LATEST)'
24_ANDROID_TESTBED_BUILD_REGEX = _ANDROID_BUILD_REGEX + '(,|(#[0-9]+))'
25
Dan Shie44f9c02016-02-18 13:25:05 -080026# Special label to skip provision and run reset instead.
27SKIP_PROVISION = 'skip_provision'
28
Chris Sosae92399e2015-04-24 11:32:59 -070029# Default number of provisions attempts to try if we believe the devserver is
30# flaky.
31FLAKY_DEVSERVER_ATTEMPTS = 2
32
Alex Miller0516e4c2013-06-03 18:07:48 -070033
Allen Li606673f2017-02-21 18:41:13 -080034_Action = collections.namedtuple('_Action', 'name, value')
35
36
37def _get_label_action(str_label):
38 """Get action represented by the label.
39
40 This is used for determine actions to perform based on labels, for
41 example for provisioning or repair.
42
43 @param str_label: label string
44 @returns: _Action instance
45 """
46 try:
47 keyval_label = labellib.parse_keyval_label(str_label)
48 except ValueError:
49 return _Action(str_label, None)
50 else:
51 return _Action(keyval_label.key, keyval_label.value)
52
53
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +000054### Helpers to convert value to label
Richard Barnette6c2b70a2017-01-26 13:40:51 -080055def get_version_label_prefix(image):
56 """
57 Determine a version label prefix from a given image name.
58
59 Parses `image` to determine what kind of image it refers
60 to, and returns the corresponding version label prefix.
61
62 Known version label prefixes are:
63 * `CROS_VERSION_PREFIX` for Chrome OS version strings.
64 These images have names like `cave-release/R57-9030.0.0`.
65 * `ANDROID_BUILD_VERSION_PREFIX` for Android build versions
66 These images have names like
67 `git_mnc-release/shamu-userdebug/2457013`.
68 * `TESTBED_BUILD_VERSION_PREFIX` for Android testbed version
69 specifications. These are either comma separated lists of
70 Android versions, or an Android version with a suffix like
71 '#2', indicating two devices running the given build.
72
73 @param image: The image name to be parsed.
74 @returns: A string that is the prefix of version labels for the type
75 of image identified by `image`.
76
77 """
78 if re.match(_ANDROID_TESTBED_BUILD_REGEX, image, re.I):
79 return TESTBED_BUILD_VERSION_PREFIX
80 elif re.match(_ANDROID_BUILD_REGEX, image, re.I):
81 return ANDROID_BUILD_VERSION_PREFIX
82 else:
83 return CROS_VERSION_PREFIX
84
85
86def image_version_to_label(image):
87 """
88 Return a version label appropriate to the given image name.
89
90 The type of version label is as determined described for
91 `get_version_label_prefix()`, meaning the label will identify a
92 CrOS, Android, or Testbed version.
93
94 @param image: The image name to be parsed.
95 @returns: A string that is the appropriate label name.
96
97 """
98 return get_version_label_prefix(image) + ':' + image
99
100
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000101def fwro_version_to_label(image):
Dan Shi9cb0eec2014-06-03 09:04:50 -0700102 """
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000103 Returns the proper label name for a RO firmware build of |image|.
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800104
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000105 @param image: A string of the form 'lumpy-release/R28-3993.0.0'
106 @returns: A string that is the appropriate label name.
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800107
108 """
Allen Liaaabda82017-02-21 18:27:38 -0800109 warnings.warn('fwro_version_to_label is deprecated', stacklevel=2)
110 keyval_label = labellib.KeyvalLabel(Key.FIRMWARE_RO_VERSION, image)
111 return labellib.format_keyval_label(keyval_label)
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800112
113
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000114def fwrw_version_to_label(image):
115 """
116 Returns the proper label name for a RW firmware build of |image|.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700117
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000118 @param image: A string of the form 'lumpy-release/R28-3993.0.0'
119 @returns: A string that is the appropriate label name.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700120
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000121 """
Allen Liaaabda82017-02-21 18:27:38 -0800122 warnings.warn('fwrw_version_to_label is deprecated', stacklevel=2)
123 keyval_label = labellib.KeyvalLabel(Key.FIRMWARE_RW_VERSION, image)
124 return labellib.format_keyval_label(keyval_label)
Dan Shi9cb0eec2014-06-03 09:04:50 -0700125
126
Alex Miller1968edf2014-02-27 18:11:36 -0800127class _SpecialTaskAction(object):
Alex Miller0516e4c2013-06-03 18:07:48 -0700128 """
Alex Miller1968edf2014-02-27 18:11:36 -0800129 Base class to give a template for mapping labels to tests.
Alex Miller0516e4c2013-06-03 18:07:48 -0700130 """
Alex Miller1968edf2014-02-27 18:11:36 -0800131
Dan Shi7279a5a2016-04-07 11:04:28 -0700132 # A dictionary mapping labels to test names.
133 _actions = {}
Alex Miller0516e4c2013-06-03 18:07:48 -0700134
Dan Shi7279a5a2016-04-07 11:04:28 -0700135 # The name of this special task to be used in output.
136 name = None;
Alex Miller0516e4c2013-06-03 18:07:48 -0700137
Dan Shi7279a5a2016-04-07 11:04:28 -0700138 # Some special tasks require to run before others, e.g., ChromeOS image
139 # needs to be updated before firmware provision. List `_priorities` defines
140 # the order of each label prefix. An element with a smaller index has higher
141 # priority. Not listed ones have the lowest priority.
142 # This property should be overriden in subclass to define its own priorities
143 # across available label prefixes.
144 _priorities = []
Alex Miller1968edf2014-02-27 18:11:36 -0800145
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000146
Alex Miller1968edf2014-02-27 18:11:36 -0800147 @classmethod
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000148 def acts_on(cls, label):
Alex Miller1968edf2014-02-27 18:11:36 -0800149 """
150 Returns True if the label is a label that we recognize as something we
151 know how to act on, given our _actions.
152
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000153 @param label: The label as a string.
Alex Miller1968edf2014-02-27 18:11:36 -0800154 @returns: True if there exists a test to run for this label.
Alex Miller1968edf2014-02-27 18:11:36 -0800155 """
Allen Li95e930a2017-02-21 18:31:18 -0800156 action = _get_label_action(label)
157 return action.name in cls._actions
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000158
Alex Miller1968edf2014-02-27 18:11:36 -0800159
160 @classmethod
Allen Li21154582017-02-22 18:00:42 -0800161 def run_task_actions(cls, job, host, labels):
162 """
163 Run task actions on host that correspond to the labels.
164
165 Emits status lines for each run test, and INFO lines for each
166 skipped label.
167
168 @param job: A job object from a control file.
169 @param host: The host to run actions on.
170 @param labels: The list of job labels to work on.
171 @raises: SpecialTaskActionException if a test fails.
172 """
173 unactionable, actionable = cls.partition(labels)
174
175 for label in unactionable:
176 job.record('INFO', None, cls.name,
177 "Can't %s label '%s'." % (cls.name, label))
178
179 # Sort the configuration labels based on `cls._priorities`.
180 sorted_actionable = cls.sort_actionable_labels(actionable)
181 for name, value in sorted_actionable:
182 action_item = cls.action_for(name)
183 success = action_item.execute(job=job, host=host, value=value)
184 if not success:
185 raise SpecialTaskActionException()
186
187
188 @classmethod
Allen Liceb24432017-04-04 14:49:11 -0700189 def action_for(cls, name):
Alex Miller1968edf2014-02-27 18:11:36 -0800190 """
Allen Liceb24432017-04-04 14:49:11 -0700191 Returns the action associated with the given (string) name.
Alex Miller1968edf2014-02-27 18:11:36 -0800192
Allen Liceb24432017-04-04 14:49:11 -0700193 @param name: The name associated with the action requested.
194 @returns: The requested Actionable.
Alex Miller1968edf2014-02-27 18:11:36 -0800195 @raises KeyError: If the name was not recognized as one we care about.
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000196
Alex Miller1968edf2014-02-27 18:11:36 -0800197 """
Allen Liceb24432017-04-04 14:49:11 -0700198 return cls._actions[name]
Alex Miller1968edf2014-02-27 18:11:36 -0800199
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000200
Alex Milleraa772002014-04-10 17:51:21 -0700201 @classmethod
202 def partition(cls, labels):
203 """
204 Filter a list of labels into two sets: those labels that we know how to
205 act on and those that we don't know how to act on.
206
207 @param labels: A list of strings of labels.
208 @returns: A tuple where the first element is a set of unactionable
209 labels, and the second element is a set of the actionable
210 labels.
211 """
Allen Li5b623122017-02-22 17:42:02 -0800212 unactionable = set()
213 actionable = set()
Alex Milleraa772002014-04-10 17:51:21 -0700214
215 for label in labels:
Dan Shie44f9c02016-02-18 13:25:05 -0800216 if label == SKIP_PROVISION:
217 # skip_provision is neither actionable or a capability label.
218 # It doesn't need any handling.
219 continue
220 elif cls.acts_on(label):
Allen Li5b623122017-02-22 17:42:02 -0800221 actionable.add(label)
Alex Milleraa772002014-04-10 17:51:21 -0700222 else:
Allen Li5b623122017-02-22 17:42:02 -0800223 unactionable.add(label)
Alex Milleraa772002014-04-10 17:51:21 -0700224
Allen Li5b623122017-02-22 17:42:02 -0800225 return unactionable, actionable
Alex Milleraa772002014-04-10 17:51:21 -0700226
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000227
Dan Shi7279a5a2016-04-07 11:04:28 -0700228 @classmethod
Allen Li5b623122017-02-22 17:42:02 -0800229 def sort_actionable_labels(cls, labels):
Dan Shi7279a5a2016-04-07 11:04:28 -0700230 """
231 Sort configurations based on the priority defined in cls._priorities.
232
Allen Li5b623122017-02-22 17:42:02 -0800233 @param labels: A list of actionable labels.
Dan Shi7279a5a2016-04-07 11:04:28 -0700234
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000235 @return: A sorted list of tuple of (label_prefix, value), the tuples are
236 sorted based on the label_prefix's index in cls._priorities.
Allen Lifda3e232016-10-17 14:54:12 -0700237 """
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000238 # Split a list of labels into a dict mapping name to value. All labels
239 # must be provisionable labels, or else a ValueError
240 # For example, label 'cros-version:lumpy-release/R28-3993.0.0' is split
241 # to {'cros-version': 'lumpy-release/R28-3993.0.0'}
242 split_configurations = dict()
Allen Li5b623122017-02-22 17:42:02 -0800243 for label in labels:
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000244 name, _, value = label.partition(':')
245 split_configurations[name] = value
Allen Lifda3e232016-10-17 14:54:12 -0700246
Allen Libaa4ec92017-02-22 17:52:30 -0800247 def sort_key(config):
248 if config[0] in cls._priorities:
249 return cls._priorities.index(config[0])
250 else:
251 return sys.maxint
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000252 return sorted(split_configurations.items(), key=sort_key)
Dan Shi7279a5a2016-04-07 11:04:28 -0700253
254
Alex Miller1968edf2014-02-27 18:11:36 -0800255class Verify(_SpecialTaskAction):
Alex Miller0516e4c2013-06-03 18:07:48 -0700256 """
Alex Miller1968edf2014-02-27 18:11:36 -0800257 Tests to verify that the DUT is in a sane, known good state that we can run
258 tests on. Failure to verify leads to running Repair.
Alex Miller0516e4c2013-06-03 18:07:48 -0700259 """
Alex Miller1968edf2014-02-27 18:11:36 -0800260
261 _actions = {
Fang Dengaf30e7c2014-11-15 13:57:03 -0800262 'modem_repair': actionables.TestActionable('cellular_StaleModemReboot'),
Don Garrett6f7e8002015-07-23 22:45:37 +0000263 # TODO(crbug.com/404421): set rpm action to power_RPMTest after the RPM
264 # is stable in lab (destiny). The power_RPMTest failure led to reset job
265 # failure and that left dut in Repair Failed. Since the test will fail
266 # anyway due to the destiny lab issue, and test retry will retry the
267 # test in another DUT.
268 # This change temporarily disable the RPM check in reset job.
269 # Another way to do this is to remove rpm dependency from tests' control
270 # file. That will involve changes on multiple control files. This one
271 # line change here is a simple temporary fix.
272 'rpm': actionables.TestActionable('dummy_PassServer'),
Alex Miller1968edf2014-02-27 18:11:36 -0800273 }
274
275 name = 'verify'
276
277
278class Provision(_SpecialTaskAction):
279 """
280 Provisioning runs to change the configuration of the DUT from one state to
281 another. It will only be run on verified DUTs.
282 """
283
Dan Shi7279a5a2016-04-07 11:04:28 -0700284 # ChromeOS update must happen before firmware install, so the dut has the
285 # correct ChromeOS version label when firmware install occurs. The ChromeOS
286 # version label is used for firmware update to stage desired ChromeOS image
287 # on to the servo USB stick.
288 _priorities = [CROS_VERSION_PREFIX,
289 FW_RO_VERSION_PREFIX,
290 FW_RW_VERSION_PREFIX]
291
Alex Miller1968edf2014-02-27 18:11:36 -0800292 # TODO(milleral): http://crbug.com/249555
293 # Create some way to discover and register provisioning tests so that we
294 # don't need to hand-maintain a list of all of them.
295 _actions = {
Fang Deng9d548742015-02-03 11:35:02 -0800296 CROS_VERSION_PREFIX: actionables.TestActionable(
297 'provision_AutoUpdate',
298 extra_kwargs={'disable_sysinfo': False,
299 'disable_before_test_sysinfo': False,
300 'disable_before_iteration_sysinfo': True,
301 'disable_after_test_sysinfo': True,
302 'disable_after_iteration_sysinfo': True}),
Tom Wai-Hong Tam9a237612016-01-08 03:41:46 +0800303 FW_RO_VERSION_PREFIX: actionables.TestActionable(
Fang Dengaf30e7c2014-11-15 13:57:03 -0800304 'provision_FirmwareUpdate'),
Tom Wai-Hong Tam9a237612016-01-08 03:41:46 +0800305 FW_RW_VERSION_PREFIX: actionables.TestActionable(
306 'provision_FirmwareUpdate',
Dan Shi61e407c2016-04-08 14:21:07 -0700307 extra_kwargs={'rw_only': True,
308 'tag': 'rw_only'}),
Simran Basi5ace6f22016-01-06 17:30:44 -0800309 ANDROID_BUILD_VERSION_PREFIX : actionables.TestActionable(
310 'provision_AndroidUpdate'),
Simran Basiadf31312016-06-28 14:23:05 -0700311 TESTBED_BUILD_VERSION_PREFIX : actionables.TestActionable(
312 'provision_TestbedUpdate'),
Alex Miller1968edf2014-02-27 18:11:36 -0800313 }
314
315 name = 'provision'
316
317
318class Cleanup(_SpecialTaskAction):
319 """
320 Cleanup runs after a test fails to try and remove artifacts of tests and
321 ensure the DUT will be in a sane state for the next test run.
322 """
323
324 _actions = {
Fang Dengaf30e7c2014-11-15 13:57:03 -0800325 'cleanup-reboot': actionables.RebootActionable(),
Alex Miller1968edf2014-02-27 18:11:36 -0800326 }
327
328 name = 'cleanup'
329
330
331class Repair(_SpecialTaskAction):
332 """
333 Repair runs when one of the other special tasks fails. It should be able
334 to take a component of the DUT that's in an unknown state and restore it to
335 a good state.
336 """
337
338 _actions = {
339 }
340
341 name = 'repair'
342
343
Alex Milleraa772002014-04-10 17:51:21 -0700344# TODO(milleral): crbug.com/364273
345# Label doesn't really mean label in this context. We're putting things into
346# DEPENDENCIES that really aren't DEPENDENCIES, and we should probably stop
347# doing that.
348def is_for_special_action(label):
349 """
350 If any special task handles the label specially, then we're using the label
351 to communicate that we want an action, and not as an actual dependency that
352 the test has.
353
354 @param label: A string label name.
355 @return True if any special task handles this label specially,
356 False if no special task handles this label.
357 """
358 return (Verify.acts_on(label) or
359 Provision.acts_on(label) or
360 Cleanup.acts_on(label) or
Dan Shie44f9c02016-02-18 13:25:05 -0800361 Repair.acts_on(label) or
362 label == SKIP_PROVISION)
Alex Miller0516e4c2013-06-03 18:07:48 -0700363
364
Prathmesh Prabhu2c7471d2016-11-15 20:19:57 +0000365def join(provision_type, provision_value):
366 """
367 Combine the provision type and value into the label name.
368
369 @param provision_type: One of the constants that are the label prefixes.
370 @param provision_value: A string of the value for this provision type.
371 @returns: A string that is the label name for this (type, value) pair.
372
373 >>> join(CROS_VERSION_PREFIX, 'lumpy-release/R27-3773.0.0')
374 'cros-version:lumpy-release/R27-3773.0.0'
375
376 """
377 return '%s:%s' % (provision_type, provision_value)
378
379
Alex Miller667b5f22014-02-28 15:33:39 -0800380class SpecialTaskActionException(Exception):
381 """
382 Exception raised when a special task fails to successfully run a test that
383 is required.
384
385 This is also a literally meaningless exception. It's always just discarded.
386 """
387
388
389def run_special_task_actions(job, host, labels, task):
390 """
391 Iterate through all `label`s and run any tests on `host` that `task` has
392 corresponding to the passed in labels.
393
394 Emits status lines for each run test, and INFO lines for each skipped label.
395
396 @param job: A job object from a control file.
397 @param host: The host to run actions on.
398 @param labels: The list of job labels to work on.
399 @param task: An instance of _SpecialTaskAction.
400 @returns: None
401 @raises: SpecialTaskActionException if a test fails.
402
403 """
Allen Li21154582017-02-22 18:00:42 -0800404 warnings.warn('run_special_task_actions is deprecated')
405 task.run_task_actions(job, host, labels)