blob: b0fa276cbe1b4b04f975a303834e8bdbfc926cb3 [file] [log] [blame]
Dan Shia1ecd5c2013-06-06 11:21:31 -07001# 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 Shia1ecd5c2013-06-06 11:21:31 -07005
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -08006import httplib
7import json
Alex Millerdadc2c22013-07-08 15:21:21 -07008import logging
beeps023afc62014-02-04 16:59:22 -08009import random
Alex Millerdadc2c22013-07-08 15:21:21 -070010import re
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080011import time
Paul Drewsbef578d2013-09-24 15:10:36 -070012import urllib2
Alex Millerdadc2c22013-07-08 15:21:21 -070013
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080014import common
beeps023afc62014-02-04 16:59:22 -080015from autotest_lib.client.common_lib import base_utils
16from autotest_lib.client.common_lib import error
17from autotest_lib.client.common_lib import global_config
Dan Shia1ecd5c2013-06-06 11:21:31 -070018from autotest_lib.server.cros.dynamic_suite import constants
Dan Shi7e04fa82013-07-25 15:08:48 -070019from autotest_lib.server.cros.dynamic_suite import job_status
Dan Shia1ecd5c2013-06-06 11:21:31 -070020
21
Alex Millerdadc2c22013-07-08 15:21:21 -070022_SHERIFF_JS = global_config.global_config.get_config_value(
23 'NOTIFICATIONS', 'sheriffs', default='')
Fang Deng3197b392013-06-26 11:42:02 -070024_LAB_SHERIFF_JS = global_config.global_config.get_config_value(
25 'NOTIFICATIONS', 'lab_sheriffs', default='')
Alex Millerdadc2c22013-07-08 15:21:21 -070026_CHROMIUM_BUILD_URL = global_config.global_config.get_config_value(
27 'NOTIFICATIONS', 'chromium_build_url', default='')
28
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080029LAB_GOOD_STATES = ('open', 'throttled')
30
31
J. Richard Barnetteabbe0962013-12-10 18:15:44 -080032class TestLabException(Exception):
33 """Exception raised when the Test Lab blocks a test or suite."""
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080034 pass
35
36
37class ParseBuildNameException(Exception):
38 """Raised when ParseBuildName() cannot parse a build name."""
39 pass
40
41
42def ParseBuildName(name):
43 """Format a build name, given board, type, milestone, and manifest num.
44
Simran Basib7d21162014-05-21 15:26:16 -070045 @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 Barnette3cbd76b2013-11-27 12:11:25 -080047
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 Basib7d21162014-05-21 15:26:16 -070051 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 Barnette3cbd76b2013-11-27 12:11:25 -080054
55 """
Simran Basif8f648e2014-09-09 11:40:03 -070056 match = re.match(r'(trybot-)?(?P<board>[\w-]+)-(?P<type>\w+)/'
57 r'(R(?P<milestone>\d+)-(?P<manifest>[\d.ab-]+)|LATEST)',
58 name)
59 if match and len(match.groups()) >= 5:
Simran Basib7d21162014-05-21 15:26:16 -070060 return (match.group('board'), match.group('type'),
61 match.group('milestone'), match.group('manifest'))
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080062 raise ParseBuildNameException('%s is a malformed build name.' % name)
63
Alex Millerdadc2c22013-07-08 15:21:21 -070064
Dan Shia1ecd5c2013-06-06 11:21:31 -070065def get_label_from_afe(hostname, label_prefix, afe):
66 """Retrieve a host's specific label from the AFE.
67
68 Looks for a host label that has the form <label_prefix>:<value>
69 and returns the "<value>" part of the label. None is returned
70 if there is not a label matching the pattern
71
72 @param hostname: hostname of given DUT.
73 @param label_prefix: prefix of label to be matched, e.g., |board:|
74 @param afe: afe instance.
75 @returns the label that matches the prefix or 'None'
76
77 """
78 labels = afe.get_labels(name__startswith=label_prefix,
79 host__hostname__in=[hostname])
80 if labels and len(labels) == 1:
81 return labels[0].name.split(label_prefix, 1)[1]
82
83
84def get_board_from_afe(hostname, afe):
85 """Retrieve given host's board from its labels in the AFE.
86
87 Looks for a host label of the form "board:<board>", and
88 returns the "<board>" part of the label. `None` is returned
89 if there is not a single, unique label matching the pattern.
90
91 @param hostname: hostname of given DUT.
92 @param afe: afe instance.
93 @returns board from label, or `None`.
94
95 """
96 return get_label_from_afe(hostname, constants.BOARD_PREFIX, afe)
97
98
99def get_build_from_afe(hostname, afe):
100 """Retrieve the current build for given host from the AFE.
101
102 Looks through the host's labels in the AFE to determine its build.
103
104 @param hostname: hostname of given DUT.
105 @param afe: afe instance.
106 @returns The current build or None if it could not find it or if there
107 were multiple build labels assigned to this host.
108
109 """
110 return get_label_from_afe(hostname, constants.VERSION_PREFIX, afe)
111
112
Fang Deng3197b392013-06-26 11:42:02 -0700113def get_sheriffs(lab_only=False):
Alex Millerdadc2c22013-07-08 15:21:21 -0700114 """
115 Polls the javascript file that holds the identity of the sheriff and
116 parses it's output to return a list of chromium sheriff email addresses.
117 The javascript file can contain the ldap of more than one sheriff, eg:
118 document.write('sheriff_one, sheriff_two').
119
Fang Deng3197b392013-06-26 11:42:02 -0700120 @param lab_only: if True, only pulls lab sheriff.
121 @return: A list of chroium.org sheriff email addresses to cc on the bug.
122 An empty list if failed to parse the javascript.
Alex Millerdadc2c22013-07-08 15:21:21 -0700123 """
124 sheriff_ids = []
Fang Deng3197b392013-06-26 11:42:02 -0700125 sheriff_js_list = _LAB_SHERIFF_JS.split(',')
126 if not lab_only:
127 sheriff_js_list.extend(_SHERIFF_JS.split(','))
128
129 for sheriff_js in sheriff_js_list:
Alex Millerdadc2c22013-07-08 15:21:21 -0700130 try:
131 url_content = base_utils.urlopen('%s%s'% (
132 _CHROMIUM_BUILD_URL, sheriff_js)).read()
133 except (ValueError, IOError) as e:
beeps4efdf032013-09-17 11:27:14 -0700134 logging.warning('could not parse sheriff from url %s%s: %s',
135 _CHROMIUM_BUILD_URL, sheriff_js, str(e))
Paul Drewsbef578d2013-09-24 15:10:36 -0700136 except (urllib2.URLError, httplib.HTTPException) as e:
137 logging.warning('unexpected error reading from url "%s%s": %s',
138 _CHROMIUM_BUILD_URL, sheriff_js, str(e))
Alex Millerdadc2c22013-07-08 15:21:21 -0700139 else:
140 ldaps = re.search(r"document.write\('(.*)'\)", url_content)
141 if not ldaps:
beeps4efdf032013-09-17 11:27:14 -0700142 logging.warning('Could not retrieve sheriff ldaps for: %s',
143 url_content)
Alex Millerdadc2c22013-07-08 15:21:21 -0700144 continue
145 sheriff_ids += ['%s@chromium.org' % alias.replace(' ', '')
146 for alias in ldaps.group(1).split(',')]
147 return sheriff_ids
beeps46dadc92013-11-07 14:07:10 -0800148
149
150def remote_wget(source_url, dest_path, ssh_cmd):
151 """wget source_url from localhost to dest_path on remote host using ssh.
152
153 @param source_url: The complete url of the source of the package to send.
154 @param dest_path: The path on the remote host's file system where we would
155 like to store the package.
156 @param ssh_cmd: The ssh command to use in performing the remote wget.
157 """
158 wget_cmd = ("wget -O - %s | %s 'cat >%s'" %
159 (source_url, ssh_cmd, dest_path))
160 base_utils.run(wget_cmd)
161
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800162
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800163_MAX_LAB_STATUS_ATTEMPTS = 5
164def _get_lab_status(status_url):
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800165 """Grabs the current lab status and message.
166
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800167 @returns The JSON object obtained from the given URL.
168
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800169 """
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800170 retry_waittime = 1
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800171 for _ in range(_MAX_LAB_STATUS_ATTEMPTS):
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800172 try:
173 response = urllib2.urlopen(status_url)
174 except IOError as e:
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800175 logging.debug('Error occurred when grabbing the lab status: %s.',
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800176 e)
177 time.sleep(retry_waittime)
178 continue
179 # Check for successful response code.
180 if response.getcode() == 200:
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800181 return json.load(response)
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800182 time.sleep(retry_waittime)
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800183 return None
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800184
185
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800186def _decode_lab_status(lab_status, build):
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800187 """Decode lab status, and report exceptions as needed.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800188
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800189 Take a deserialized JSON object from the lab status page, and
190 interpret it to determine the actual lab status. Raise
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800191 exceptions as required to report when the lab is down.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800192
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800193 @param build: build name that we want to check the status of.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800194
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800195 @raises TestLabException Raised if a request to test for the given
196 status and build should be blocked.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800197 """
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800198 # First check if the lab is up.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800199 if not lab_status['general_state'] in LAB_GOOD_STATES:
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800200 raise TestLabException('Chromium OS Test Lab is closed: '
201 '%s.' % lab_status['message'])
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800202
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800203 # Check if the build we wish to use is disabled.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800204 # Lab messages should be in the format of:
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800205 # Lab is 'status' [regex ...] (comment)
206 # If the build name matches any regex, it will be blocked.
207 build_exceptions = re.search('\[(.*)\]', lab_status['message'])
208 if not build_exceptions:
209 return
210 for build_pattern in build_exceptions.group(1).split():
211 if re.search(build_pattern, build):
212 raise TestLabException('Chromium OS Test Lab is closed: '
213 '%s matches %s.' % (
214 build, build_pattern))
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800215 return
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800216
217
Dan Shi94234cb2014-05-23 20:04:31 -0700218def is_in_lab():
219 """Check if current Autotest instance is in lab
220
221 @return: True if the Autotest instance is in lab.
222 """
223 test_server_name = global_config.global_config.get_config_value(
224 'SERVER', 'hostname')
225 return test_server_name.startswith('cautotest')
226
227
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800228def check_lab_status(build):
229 """Check if the lab status allows us to schedule for a build.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800230
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800231 Checks if the lab is down, or if testing for the requested build
232 should be blocked.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800233
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800234 @param build: Name of the build to be scheduled for testing.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800235
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800236 @raises TestLabException Raised if a request to test for the given
237 status and build should be blocked.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800238
239 """
240 # Ensure we are trying to schedule on the actual lab.
Dan Shi94234cb2014-05-23 20:04:31 -0700241 if not is_in_lab():
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800242 return
243
244 # Download the lab status from its home on the web.
245 status_url = global_config.global_config.get_config_value(
246 'CROS', 'lab_status_url')
247 json_status = _get_lab_status(status_url)
248 if json_status is None:
249 # We go ahead and say the lab is open if we can't get the status.
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700250 logging.warning('Could not get a status from %s', status_url)
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800251 return
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800252 _decode_lab_status(json_status, build)
beeps023afc62014-02-04 16:59:22 -0800253
254
255def lock_host_with_labels(afe, lock_manager, labels):
256 """Lookup and lock one host that matches the list of input labels.
257
258 @param afe: An instance of the afe class, as defined in server.frontend.
259 @param lock_manager: A lock manager capable of locking hosts, eg the
260 one defined in server.cros.host_lock_manager.
261 @param labels: A list of labels to look for on hosts.
262
263 @return: The hostname of a host matching all labels, and locked through the
264 lock_manager. The hostname will be as specified in the database the afe
265 object is associated with, i.e if it exists in afe_hosts with a .cros
266 suffix, the hostname returned will contain a .cros suffix.
267
268 @raises: error.NoEligibleHostException: If no hosts matching the list of
269 input labels are available.
270 @raises: error.TestError: If unable to lock a host matching the labels.
271 """
272 potential_hosts = afe.get_hosts(multiple_labels=labels)
273 if not potential_hosts:
274 raise error.NoEligibleHostException(
275 'No devices found with labels %s.' % labels)
276
277 # This prevents errors where a fault might seem repeatable
278 # because we lock, say, the same packet capturer for each test run.
279 random.shuffle(potential_hosts)
280 for host in potential_hosts:
281 if lock_manager.lock([host.hostname]):
282 logging.info('Locked device %s with labels %s.',
283 host.hostname, labels)
284 return host.hostname
285 else:
286 logging.info('Unable to lock device %s with labels %s.',
287 host.hostname, labels)
288
289 raise error.TestError('Could not lock a device with labels %s' % labels)
Dan Shi7e04fa82013-07-25 15:08:48 -0700290
291
292def get_test_views_from_tko(suite_job_id, tko):
293 """Get test name and result for given suite job ID.
294
295 @param suite_job_id: ID of suite job.
296 @param tko: an instance of TKO as defined in server/frontend.py.
297 @return: A dictionary of test status keyed by test name, e.g.,
298 {'dummy_Fail.Error': 'ERROR', 'dummy_Fail.NAError': 'TEST_NA'}
299 @raise: Exception when there is no test view found.
300
301 """
302 views = tko.run('get_detailed_test_views', afe_job_id=suite_job_id)
303 relevant_views = filter(job_status.view_is_relevant, views)
304 if not relevant_views:
305 raise Exception('Failed to retrieve job results.')
306
307 test_views = {}
308 for view in relevant_views:
309 test_views[view['test_name']] = view['status']
310
311 return test_views