Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium 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 | |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 5 | |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 6 | import httplib |
| 7 | import json |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 8 | import logging |
beeps | 023afc6 | 2014-02-04 16:59:22 -0800 | [diff] [blame] | 9 | import random |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 10 | import re |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 11 | import time |
Paul Drews | bef578d | 2013-09-24 15:10:36 -0700 | [diff] [blame] | 12 | import urllib2 |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 13 | |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 14 | import common |
beeps | 023afc6 | 2014-02-04 16:59:22 -0800 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import base_utils |
| 16 | from autotest_lib.client.common_lib import error |
| 17 | from autotest_lib.client.common_lib import global_config |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 18 | from autotest_lib.server.cros.dynamic_suite import constants |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 19 | from autotest_lib.server.cros.dynamic_suite import job_status |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 20 | |
| 21 | |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 22 | _SHERIFF_JS = global_config.global_config.get_config_value( |
| 23 | 'NOTIFICATIONS', 'sheriffs', default='') |
Fang Deng | 3197b39 | 2013-06-26 11:42:02 -0700 | [diff] [blame] | 24 | _LAB_SHERIFF_JS = global_config.global_config.get_config_value( |
| 25 | 'NOTIFICATIONS', 'lab_sheriffs', default='') |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 26 | _CHROMIUM_BUILD_URL = global_config.global_config.get_config_value( |
| 27 | 'NOTIFICATIONS', 'chromium_build_url', default='') |
| 28 | |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 29 | LAB_GOOD_STATES = ('open', 'throttled') |
| 30 | |
| 31 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 32 | class TestLabException(Exception): |
| 33 | """Exception raised when the Test Lab blocks a test or suite.""" |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 34 | pass |
| 35 | |
| 36 | |
| 37 | class ParseBuildNameException(Exception): |
| 38 | """Raised when ParseBuildName() cannot parse a build name.""" |
| 39 | pass |
| 40 | |
| 41 | |
| 42 | def ParseBuildName(name): |
| 43 | """Format a build name, given board, type, milestone, and manifest num. |
| 44 | |
Simran Basi | b7d2116 | 2014-05-21 15:26:16 -0700 | [diff] [blame] | 45 | @param name: a build name, e.g. 'x86-alex-release/R20-2015.0.0' or a |
| 46 | relative build name, e.g. 'x86-alex-release/LATEST' |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 47 | |
| 48 | @return board: board the manifest is for, e.g. x86-alex. |
| 49 | @return type: one of 'release', 'factory', or 'firmware' |
| 50 | @return milestone: (numeric) milestone the manifest was associated with. |
Simran Basi | b7d2116 | 2014-05-21 15:26:16 -0700 | [diff] [blame] | 51 | Will be None for relative build names. |
| 52 | @return manifest: manifest number, e.g. '2015.0.0'. |
| 53 | Will be None for relative build names. |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 54 | |
| 55 | """ |
Simran Basi | b7d2116 | 2014-05-21 15:26:16 -0700 | [diff] [blame] | 56 | match = re.match(r'(?P<board>[\w-]+)-(?P<type>\w+)/(R(?P<milestone>\d+)-' |
| 57 | r'(?P<manifest>[\d.ab-]+)|LATEST)', name) |
| 58 | if match and len(match.groups()) == 5: |
| 59 | return (match.group('board'), match.group('type'), |
| 60 | match.group('milestone'), match.group('manifest')) |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 61 | raise ParseBuildNameException('%s is a malformed build name.' % name) |
| 62 | |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 63 | |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 64 | def get_label_from_afe(hostname, label_prefix, afe): |
| 65 | """Retrieve a host's specific label from the AFE. |
| 66 | |
| 67 | Looks for a host label that has the form <label_prefix>:<value> |
| 68 | and returns the "<value>" part of the label. None is returned |
| 69 | if there is not a label matching the pattern |
| 70 | |
| 71 | @param hostname: hostname of given DUT. |
| 72 | @param label_prefix: prefix of label to be matched, e.g., |board:| |
| 73 | @param afe: afe instance. |
| 74 | @returns the label that matches the prefix or 'None' |
| 75 | |
| 76 | """ |
| 77 | labels = afe.get_labels(name__startswith=label_prefix, |
| 78 | host__hostname__in=[hostname]) |
| 79 | if labels and len(labels) == 1: |
| 80 | return labels[0].name.split(label_prefix, 1)[1] |
| 81 | |
| 82 | |
| 83 | def get_board_from_afe(hostname, afe): |
| 84 | """Retrieve given host's board from its labels in the AFE. |
| 85 | |
| 86 | Looks for a host label of the form "board:<board>", and |
| 87 | returns the "<board>" part of the label. `None` is returned |
| 88 | if there is not a single, unique label matching the pattern. |
| 89 | |
| 90 | @param hostname: hostname of given DUT. |
| 91 | @param afe: afe instance. |
| 92 | @returns board from label, or `None`. |
| 93 | |
| 94 | """ |
| 95 | return get_label_from_afe(hostname, constants.BOARD_PREFIX, afe) |
| 96 | |
| 97 | |
| 98 | def get_build_from_afe(hostname, afe): |
| 99 | """Retrieve the current build for given host from the AFE. |
| 100 | |
| 101 | Looks through the host's labels in the AFE to determine its build. |
| 102 | |
| 103 | @param hostname: hostname of given DUT. |
| 104 | @param afe: afe instance. |
| 105 | @returns The current build or None if it could not find it or if there |
| 106 | were multiple build labels assigned to this host. |
| 107 | |
| 108 | """ |
| 109 | return get_label_from_afe(hostname, constants.VERSION_PREFIX, afe) |
| 110 | |
| 111 | |
Fang Deng | 3197b39 | 2013-06-26 11:42:02 -0700 | [diff] [blame] | 112 | def get_sheriffs(lab_only=False): |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 113 | """ |
| 114 | Polls the javascript file that holds the identity of the sheriff and |
| 115 | parses it's output to return a list of chromium sheriff email addresses. |
| 116 | The javascript file can contain the ldap of more than one sheriff, eg: |
| 117 | document.write('sheriff_one, sheriff_two'). |
| 118 | |
Fang Deng | 3197b39 | 2013-06-26 11:42:02 -0700 | [diff] [blame] | 119 | @param lab_only: if True, only pulls lab sheriff. |
| 120 | @return: A list of chroium.org sheriff email addresses to cc on the bug. |
| 121 | An empty list if failed to parse the javascript. |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 122 | """ |
| 123 | sheriff_ids = [] |
Fang Deng | 3197b39 | 2013-06-26 11:42:02 -0700 | [diff] [blame] | 124 | sheriff_js_list = _LAB_SHERIFF_JS.split(',') |
| 125 | if not lab_only: |
| 126 | sheriff_js_list.extend(_SHERIFF_JS.split(',')) |
| 127 | |
| 128 | for sheriff_js in sheriff_js_list: |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 129 | try: |
| 130 | url_content = base_utils.urlopen('%s%s'% ( |
| 131 | _CHROMIUM_BUILD_URL, sheriff_js)).read() |
| 132 | except (ValueError, IOError) as e: |
beeps | 4efdf03 | 2013-09-17 11:27:14 -0700 | [diff] [blame] | 133 | logging.warning('could not parse sheriff from url %s%s: %s', |
| 134 | _CHROMIUM_BUILD_URL, sheriff_js, str(e)) |
Paul Drews | bef578d | 2013-09-24 15:10:36 -0700 | [diff] [blame] | 135 | except (urllib2.URLError, httplib.HTTPException) as e: |
| 136 | logging.warning('unexpected error reading from url "%s%s": %s', |
| 137 | _CHROMIUM_BUILD_URL, sheriff_js, str(e)) |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 138 | else: |
| 139 | ldaps = re.search(r"document.write\('(.*)'\)", url_content) |
| 140 | if not ldaps: |
beeps | 4efdf03 | 2013-09-17 11:27:14 -0700 | [diff] [blame] | 141 | logging.warning('Could not retrieve sheriff ldaps for: %s', |
| 142 | url_content) |
Alex Miller | dadc2c2 | 2013-07-08 15:21:21 -0700 | [diff] [blame] | 143 | continue |
| 144 | sheriff_ids += ['%s@chromium.org' % alias.replace(' ', '') |
| 145 | for alias in ldaps.group(1).split(',')] |
| 146 | return sheriff_ids |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def remote_wget(source_url, dest_path, ssh_cmd): |
| 150 | """wget source_url from localhost to dest_path on remote host using ssh. |
| 151 | |
| 152 | @param source_url: The complete url of the source of the package to send. |
| 153 | @param dest_path: The path on the remote host's file system where we would |
| 154 | like to store the package. |
| 155 | @param ssh_cmd: The ssh command to use in performing the remote wget. |
| 156 | """ |
| 157 | wget_cmd = ("wget -O - %s | %s 'cat >%s'" % |
| 158 | (source_url, ssh_cmd, dest_path)) |
| 159 | base_utils.run(wget_cmd) |
| 160 | |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 161 | |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 162 | _MAX_LAB_STATUS_ATTEMPTS = 5 |
| 163 | def _get_lab_status(status_url): |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 164 | """Grabs the current lab status and message. |
| 165 | |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 166 | @returns The JSON object obtained from the given URL. |
| 167 | |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 168 | """ |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 169 | retry_waittime = 1 |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 170 | for _ in range(_MAX_LAB_STATUS_ATTEMPTS): |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 171 | try: |
| 172 | response = urllib2.urlopen(status_url) |
| 173 | except IOError as e: |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 174 | logging.debug('Error occurred when grabbing the lab status: %s.', |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 175 | e) |
| 176 | time.sleep(retry_waittime) |
| 177 | continue |
| 178 | # Check for successful response code. |
| 179 | if response.getcode() == 200: |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 180 | return json.load(response) |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 181 | time.sleep(retry_waittime) |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 182 | return None |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 183 | |
| 184 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 185 | def _decode_lab_status(lab_status, build): |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 186 | """Decode lab status, and report exceptions as needed. |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 187 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 188 | Take a deserialized JSON object from the lab status page, and |
| 189 | interpret it to determine the actual lab status. Raise |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 190 | exceptions as required to report when the lab is down. |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 191 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 192 | @param build: build name that we want to check the status of. |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 193 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 194 | @raises TestLabException Raised if a request to test for the given |
| 195 | status and build should be blocked. |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 196 | """ |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 197 | # First check if the lab is up. |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 198 | if not lab_status['general_state'] in LAB_GOOD_STATES: |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 199 | raise TestLabException('Chromium OS Test Lab is closed: ' |
| 200 | '%s.' % lab_status['message']) |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 201 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 202 | # Check if the build we wish to use is disabled. |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 203 | # Lab messages should be in the format of: |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 204 | # Lab is 'status' [regex ...] (comment) |
| 205 | # If the build name matches any regex, it will be blocked. |
| 206 | build_exceptions = re.search('\[(.*)\]', lab_status['message']) |
| 207 | if not build_exceptions: |
| 208 | return |
| 209 | for build_pattern in build_exceptions.group(1).split(): |
| 210 | if re.search(build_pattern, build): |
| 211 | raise TestLabException('Chromium OS Test Lab is closed: ' |
| 212 | '%s matches %s.' % ( |
| 213 | build, build_pattern)) |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 214 | return |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 215 | |
| 216 | |
Dan Shi | 94234cb | 2014-05-23 20:04:31 -0700 | [diff] [blame] | 217 | def is_in_lab(): |
| 218 | """Check if current Autotest instance is in lab |
| 219 | |
| 220 | @return: True if the Autotest instance is in lab. |
| 221 | """ |
| 222 | test_server_name = global_config.global_config.get_config_value( |
| 223 | 'SERVER', 'hostname') |
| 224 | return test_server_name.startswith('cautotest') |
| 225 | |
| 226 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 227 | def check_lab_status(build): |
| 228 | """Check if the lab status allows us to schedule for a build. |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 229 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 230 | Checks if the lab is down, or if testing for the requested build |
| 231 | should be blocked. |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 232 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 233 | @param build: Name of the build to be scheduled for testing. |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 234 | |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 235 | @raises TestLabException Raised if a request to test for the given |
| 236 | status and build should be blocked. |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 237 | |
| 238 | """ |
| 239 | # Ensure we are trying to schedule on the actual lab. |
Dan Shi | 94234cb | 2014-05-23 20:04:31 -0700 | [diff] [blame] | 240 | if not is_in_lab(): |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 241 | return |
| 242 | |
| 243 | # Download the lab status from its home on the web. |
| 244 | status_url = global_config.global_config.get_config_value( |
| 245 | 'CROS', 'lab_status_url') |
| 246 | json_status = _get_lab_status(status_url) |
| 247 | if json_status is None: |
| 248 | # We go ahead and say the lab is open if we can't get the status. |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 249 | logging.warning('Could not get a status from %s', status_url) |
J. Richard Barnette | 266da2a | 2013-11-27 15:09:55 -0800 | [diff] [blame] | 250 | return |
J. Richard Barnette | abbe096 | 2013-12-10 18:15:44 -0800 | [diff] [blame] | 251 | _decode_lab_status(json_status, build) |
beeps | 023afc6 | 2014-02-04 16:59:22 -0800 | [diff] [blame] | 252 | |
| 253 | |
| 254 | def lock_host_with_labels(afe, lock_manager, labels): |
| 255 | """Lookup and lock one host that matches the list of input labels. |
| 256 | |
| 257 | @param afe: An instance of the afe class, as defined in server.frontend. |
| 258 | @param lock_manager: A lock manager capable of locking hosts, eg the |
| 259 | one defined in server.cros.host_lock_manager. |
| 260 | @param labels: A list of labels to look for on hosts. |
| 261 | |
| 262 | @return: The hostname of a host matching all labels, and locked through the |
| 263 | lock_manager. The hostname will be as specified in the database the afe |
| 264 | object is associated with, i.e if it exists in afe_hosts with a .cros |
| 265 | suffix, the hostname returned will contain a .cros suffix. |
| 266 | |
| 267 | @raises: error.NoEligibleHostException: If no hosts matching the list of |
| 268 | input labels are available. |
| 269 | @raises: error.TestError: If unable to lock a host matching the labels. |
| 270 | """ |
| 271 | potential_hosts = afe.get_hosts(multiple_labels=labels) |
| 272 | if not potential_hosts: |
| 273 | raise error.NoEligibleHostException( |
| 274 | 'No devices found with labels %s.' % labels) |
| 275 | |
| 276 | # This prevents errors where a fault might seem repeatable |
| 277 | # because we lock, say, the same packet capturer for each test run. |
| 278 | random.shuffle(potential_hosts) |
| 279 | for host in potential_hosts: |
| 280 | if lock_manager.lock([host.hostname]): |
| 281 | logging.info('Locked device %s with labels %s.', |
| 282 | host.hostname, labels) |
| 283 | return host.hostname |
| 284 | else: |
| 285 | logging.info('Unable to lock device %s with labels %s.', |
| 286 | host.hostname, labels) |
| 287 | |
| 288 | raise error.TestError('Could not lock a device with labels %s' % labels) |
Dan Shi | 7e04fa8 | 2013-07-25 15:08:48 -0700 | [diff] [blame] | 289 | |
| 290 | |
| 291 | def get_test_views_from_tko(suite_job_id, tko): |
| 292 | """Get test name and result for given suite job ID. |
| 293 | |
| 294 | @param suite_job_id: ID of suite job. |
| 295 | @param tko: an instance of TKO as defined in server/frontend.py. |
| 296 | @return: A dictionary of test status keyed by test name, e.g., |
| 297 | {'dummy_Fail.Error': 'ERROR', 'dummy_Fail.NAError': 'TEST_NA'} |
| 298 | @raise: Exception when there is no test view found. |
| 299 | |
| 300 | """ |
| 301 | views = tko.run('get_detailed_test_views', afe_job_id=suite_job_id) |
| 302 | relevant_views = filter(job_status.view_is_relevant, views) |
| 303 | if not relevant_views: |
| 304 | raise Exception('Failed to retrieve job results.') |
| 305 | |
| 306 | test_views = {} |
| 307 | for view in relevant_views: |
| 308 | test_views[view['test_name']] = view['status'] |
| 309 | |
| 310 | return test_views |