blob: 54b77a35315d7f8d037a139a66f64f82c67d6f78 [file] [log] [blame]
Chris Masone6a0680f2012-03-02 08:40:00 -08001# Copyright (c) 2012 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.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -08004
beepsbff9f9d2013-12-06 11:14:08 -08005import glob
Simran Basi87d7a212012-09-27 10:41:05 -07006import logging
Simran Basiaf9b8e72012-10-12 15:02:36 -07007import os
Fang Deng7c2be102012-08-27 16:20:25 -07008import re
Simran Basiaf9b8e72012-10-12 15:02:36 -07009import signal
Scott Zawalski347a0b82012-03-30 16:39:21 -040010import socket
beepsbff9f9d2013-12-06 11:14:08 -080011import sys
Simran Basiaf9b8e72012-10-12 15:02:36 -070012import time
beeps60aec242013-06-26 14:47:48 -070013import urllib2
Chris Masone6a0680f2012-03-02 08:40:00 -080014
Simran Basiaf9b8e72012-10-12 15:02:36 -070015from autotest_lib.client.common_lib import base_utils, error, global_config
beepsc4fb1472013-05-08 21:49:48 -070016from autotest_lib.client.cros import constants
Simran Basiaf9b8e72012-10-12 15:02:36 -070017
18
19# Keep checking if the pid is alive every second until the timeout (in seconds)
20CHECK_PID_IS_ALIVE_TIMEOUT = 6
21
Simran Basi22aa9fe2012-12-07 16:37:09 -080022_LOCAL_HOST_LIST = ('localhost', '127.0.0.1')
23
Fang Deng3197b392013-06-26 11:42:02 -070024
Chris Masone6a0680f2012-03-02 08:40:00 -080025def ping(host, deadline=None, tries=None, timeout=60):
26 """Attempt to ping |host|.
27
28 Shell out to 'ping' to try to reach |host| for |timeout| seconds.
29 Returns exit code of ping.
30
31 Per 'man ping', if you specify BOTH |deadline| and |tries|, ping only
32 returns 0 if we get responses to |tries| pings within |deadline| seconds.
33
34 Specifying |deadline| or |count| alone should return 0 as long as
35 some packets receive responses.
36
beepsfda8f412013-05-02 19:08:20 -070037 @param host: the host to ping.
Chris Masone6a0680f2012-03-02 08:40:00 -080038 @param deadline: seconds within which |tries| pings must succeed.
39 @param tries: number of pings to send.
40 @param timeout: number of seconds after which to kill 'ping' command.
41 @return exit code of ping command.
42 """
43 args = [host]
44 if deadline:
45 args.append('-w%d' % deadline)
46 if tries:
47 args.append('-c%d' % tries)
48 return base_utils.run('ping', args=args,
49 ignore_status=True, timeout=timeout,
Scott Zawalskiae843542012-03-20 09:51:29 -040050 stdout_tee=base_utils.TEE_TO_LOGS,
51 stderr_tee=base_utils.TEE_TO_LOGS).exit_status
Scott Zawalski347a0b82012-03-30 16:39:21 -040052
53
54def host_is_in_lab_zone(hostname):
55 """Check if the host is in the CROS.dns_zone.
56
57 @param hostname: The hostname to check.
58 @returns True if hostname.dns_zone resolves, otherwise False.
59 """
60 host_parts = hostname.split('.')
61 dns_zone = global_config.global_config.get_config_value('CROS', 'dns_zone',
62 default=None)
63 fqdn = '%s.%s' % (host_parts[0], dns_zone)
64 try:
65 socket.gethostbyname(fqdn)
66 return True
67 except socket.gaierror:
68 return False
Fang Deng7c2be102012-08-27 16:20:25 -070069
70
beepsc4fb1472013-05-08 21:49:48 -070071def get_chrome_version(job_views):
72 """
73 Retrieves the version of the chrome binary associated with a job.
74
75 When a test runs we query the chrome binary for it's version and drop
76 that value into a client keyval. To retrieve the chrome version we get all
77 the views associated with a test from the db, including those of the
78 server and client jobs, and parse the version out of the first test view
79 that has it. If we never ran a single test in the suite the job_views
80 dictionary will not contain a chrome version.
81
82 This method cannot retrieve the chrome version from a dictionary that
83 does not conform to the structure of an autotest tko view.
84
85 @param job_views: a list of a job's result views, as returned by
86 the get_detailed_test_views method in rpc_interface.
87 @return: The chrome version string, or None if one can't be found.
88 """
89
90 # Aborted jobs have no views.
91 if not job_views:
92 return None
93
94 for view in job_views:
95 if (view.get('attributes')
96 and constants.CHROME_VERSION in view['attributes'].keys()):
97
98 return view['attributes'].get(constants.CHROME_VERSION)
99
100 logging.warning('Could not find chrome version for failure.')
101 return None
102
103
Simran Basi85f4c362014-04-08 13:40:57 -0700104def _lsbrelease_search(regex, group_id=0):
105 """Searches /etc/lsb-release for a regex match.
106
107 @param regex: Regex to match.
108 @param group_id: The group in the regex we are searching for.
109 Default is group 0.
110
111 @returns the string in the specified group if there is a match or None if
112 not found.
113
114 @raises IOError if /etc/lsb-release can not be accessed.
115 """
116 with open(constants.LSB_RELEASE) as lsb_release_file:
117 for line in lsb_release_file:
118 m = re.match(regex, line)
119 if m:
120 return m.group(group_id)
121 return None
122
123
Fang Deng7c2be102012-08-27 16:20:25 -0700124def get_current_board():
125 """Return the current board name.
126
127 @return current board name, e.g "lumpy", None on fail.
128 """
Simran Basi85f4c362014-04-08 13:40:57 -0700129 return _lsbrelease_search(r'^CHROMEOS_RELEASE_BOARD=(.+)$', group_id=1)
Simran Basi87d7a212012-09-27 10:41:05 -0700130
131
Simran Basi85f4c362014-04-08 13:40:57 -0700132def is_moblab():
133 """Return if we are running on a Moblab system or not.
134
135 @return the board string if this is a Moblab device or None if it is not.
136 """
137 try:
138 return _lsbrelease_search(r'.*moblab')
139 except IOError as e:
140 logging.error('Unable to determine if this is a moblab system: %s', e)
141
Simran Basi87d7a212012-09-27 10:41:05 -0700142# TODO(petermayo): crosbug.com/31826 Share this with _GsUpload in
143# //chromite.git/buildbot/prebuilt.py somewhere/somehow
144def gs_upload(local_file, remote_file, acl, result_dir=None,
145 transfer_timeout=300, acl_timeout=300):
146 """Upload to GS bucket.
147
148 @param local_file: Local file to upload
149 @param remote_file: Remote location to upload the local_file to.
150 @param acl: name or file used for controlling access to the uploaded
151 file.
152 @param result_dir: Result directory if you want to add tracing to the
153 upload.
beepsfda8f412013-05-02 19:08:20 -0700154 @param transfer_timeout: Timeout for this upload call.
155 @param acl_timeout: Timeout for the acl call needed to confirm that
156 the uploader has permissions to execute the upload.
Simran Basi87d7a212012-09-27 10:41:05 -0700157
158 @raise CmdError: the exit code of the gsutil call was not 0.
159
160 @returns True/False - depending on if the upload succeeded or failed.
161 """
162 # https://developers.google.com/storage/docs/accesscontrol#extension
163 CANNED_ACLS = ['project-private', 'private', 'public-read',
164 'public-read-write', 'authenticated-read',
165 'bucket-owner-read', 'bucket-owner-full-control']
166 _GSUTIL_BIN = 'gsutil'
167 acl_cmd = None
168 if acl in CANNED_ACLS:
169 cmd = '%s cp -a %s %s %s' % (_GSUTIL_BIN, acl, local_file, remote_file)
170 else:
171 # For private uploads we assume that the overlay board is set up
172 # properly and a googlestore_acl.xml is present, if not this script
173 # errors
174 cmd = '%s cp -a private %s %s' % (_GSUTIL_BIN, local_file, remote_file)
175 if not os.path.exists(acl):
176 logging.error('Unable to find ACL File %s.', acl)
177 return False
178 acl_cmd = '%s setacl %s %s' % (_GSUTIL_BIN, acl, remote_file)
179 if not result_dir:
180 base_utils.run(cmd, timeout=transfer_timeout, verbose=True)
181 if acl_cmd:
182 base_utils.run(acl_cmd, timeout=acl_timeout, verbose=True)
183 return True
184 with open(os.path.join(result_dir, 'tracing'), 'w') as ftrace:
185 ftrace.write('Preamble\n')
186 base_utils.run(cmd, timeout=transfer_timeout, verbose=True,
187 stdout_tee=ftrace, stderr_tee=ftrace)
188 if acl_cmd:
189 ftrace.write('\nACL setting\n')
190 # Apply the passed in ACL xml file to the uploaded object.
191 base_utils.run(acl_cmd, timeout=acl_timeout, verbose=True,
192 stdout_tee=ftrace, stderr_tee=ftrace)
193 ftrace.write('Postamble\n')
194 return True
Simran Basiaf9b8e72012-10-12 15:02:36 -0700195
196
Gilad Arnold0ed760c2012-11-05 23:42:53 -0800197def gs_ls(uri_pattern):
198 """Returns a list of URIs that match a given pattern.
199
200 @param uri_pattern: a GS URI pattern, may contain wildcards
201
202 @return A list of URIs matching the given pattern.
203
204 @raise CmdError: the gsutil command failed.
205
206 """
207 gs_cmd = ' '.join(['gsutil', 'ls', uri_pattern])
208 result = base_utils.system_output(gs_cmd).splitlines()
209 return [path.rstrip() for path in result if path]
210
211
Simran Basiaf9b8e72012-10-12 15:02:36 -0700212def nuke_pids(pid_list, signal_queue=[signal.SIGTERM, signal.SIGKILL]):
213 """
214 Given a list of pid's, kill them via an esclating series of signals.
215
216 @param pid_list: List of PID's to kill.
217 @param signal_queue: Queue of signals to send the PID's to terminate them.
218 """
219 for sig in signal_queue:
220 logging.debug('Sending signal %s to the following pids:', sig)
221 for pid in pid_list:
222 logging.debug('Pid %d', pid)
223 try:
224 os.kill(pid, sig)
225 except OSError:
226 # The process may have died from a previous signal before we
227 # could kill it.
228 pass
229 time.sleep(CHECK_PID_IS_ALIVE_TIMEOUT)
230 failed_list = []
231 if signal.SIGKILL in signal_queue:
232 return
233 for pid in pid_list:
234 if base_utils.pid_is_alive(pid):
235 failed_list.append('Could not kill %d for process name: %s.' % pid,
Simran Basi62723202013-01-22 15:24:49 -0800236 base_utils.get_process_name(pid))
Simran Basiaf9b8e72012-10-12 15:02:36 -0700237 if failed_list:
238 raise error.AutoservRunError('Following errors occured: %s' %
239 failed_list, None)
Gilad Arnold0ed760c2012-11-05 23:42:53 -0800240
241
242def externalize_host(host):
243 """Returns an externally accessible host name.
244
245 @param host: a host name or address (string)
246
247 @return An externally visible host name or address
248
249 """
250 return socket.gethostname() if host in _LOCAL_HOST_LIST else host
Simran Basi22aa9fe2012-12-07 16:37:09 -0800251
252
beeps60aec242013-06-26 14:47:48 -0700253def urlopen_socket_timeout(url, data=None, timeout=5):
254 """
255 Wrapper to urllib2.urlopen with a socket timeout.
256
257 This method will convert all socket timeouts to
258 TimeoutExceptions, so we can use it in conjunction
259 with the rpc retry decorator and continue to handle
260 other URLErrors as we see fit.
261
262 @param url: The url to open.
263 @param data: The data to send to the url (eg: the urlencoded dictionary
264 used with a POST call).
265 @param timeout: The timeout for this urlopen call.
266
267 @return: The response of the urlopen call.
268
269 @raises: error.TimeoutException when a socket timeout occurs.
Dan Shi6c00dde2013-07-29 17:47:29 -0700270 urllib2.URLError for errors that not caused by timeout.
271 urllib2.HTTPError for errors like 404 url not found.
beeps60aec242013-06-26 14:47:48 -0700272 """
273 old_timeout = socket.getdefaulttimeout()
274 socket.setdefaulttimeout(timeout)
275 try:
276 return urllib2.urlopen(url, data=data)
277 except urllib2.URLError as e:
278 if type(e.reason) is socket.timeout:
279 raise error.TimeoutException(str(e))
Dan Shi6c00dde2013-07-29 17:47:29 -0700280 raise
beeps60aec242013-06-26 14:47:48 -0700281 finally:
282 socket.setdefaulttimeout(old_timeout)
Luis Lozano40b7d0d2014-01-17 15:12:06 -0800283
284
285def parse_chrome_version(version_string):
286 """
287 Parse a chrome version string and return version and milestone.
288
289 Given a chrome version of the form "W.X.Y.Z", return "W.X.Y.Z" as
290 the version and "W" as the milestone.
291
292 @param version_string: Chrome version string.
293 @return: a tuple (chrome_version, milestone). If the incoming version
294 string is not of the form "W.X.Y.Z", chrome_version will
295 be set to the incoming "version_string" argument and the
296 milestone will be set to the empty string.
297 """
298 match = re.search('(\d+)\.\d+\.\d+\.\d+', version_string)
299 ver = match.group(0) if match else version_string
300 milestone = match.group(1) if match else ''
301 return ver, milestone
beepsbff9f9d2013-12-06 11:14:08 -0800302
303
304def take_screenshot(dest_dir, fname_prefix, format='png'):
mussafd5b8052014-05-06 10:04:54 -0700305 """
306 Take screenshot and save to a new file in the dest_dir.
beepsbff9f9d2013-12-06 11:14:08 -0800307
mussafd5b8052014-05-06 10:04:54 -0700308 @param dest_dir: path, destination directory to save the screenshot.
309 @param fname_prefix: string, prefix for output filename.
310 @param format: string, file format ('png', 'jpg', etc) to use.
beepsbff9f9d2013-12-06 11:14:08 -0800311
mussafd5b8052014-05-06 10:04:54 -0700312 @returns complete path to saved screenshot file.
313
beepsbff9f9d2013-12-06 11:14:08 -0800314 """
315 next_index = len(glob.glob(
316 os.path.join(dest_dir, '%s-*.%s' % (fname_prefix, format))))
317 screenshot_file = os.path.join(
318 dest_dir, '%s-%d.%s' % (fname_prefix, next_index, format))
319 logging.info('Saving screenshot to %s.', screenshot_file)
320
mussafd5b8052014-05-06 10:04:54 -0700321 import_cmd = ('/usr/local/bin/import -window root -depth 8 %s' %
322 screenshot_file)
323
324 _execute_screenshot_capture_command(import_cmd)
325
326 return screenshot_file
327
328
329def take_screen_shot_crop_by_height(fullpath, final_height, x_offset_pixels,
330 y_offset_pixels):
331 """
332 Take a screenshot, crop to final height starting at given (x, y) coordinate.
333
334 Image width will be adjusted to maintain original aspect ratio).
335
336 @param fullpath: path, fullpath of the file that will become the image file.
337 @param final_height: integer, height in pixels of resulting image.
338 @param x_offset_pixels: integer, number of pixels from left margin
339 to begin cropping.
340 @param y_offset_pixels: integer, number of pixels from top margin
341 to begin cropping.
342
343 """
344
345 params = {'height': final_height, 'x_offset': x_offset_pixels,
346 'y_offset': y_offset_pixels, 'path': fullpath}
347
348 import_cmd = ('/usr/local/bin/import -window root -depth 8 -crop '
349 'x%(height)d+%(x_offset)d+%(y_offset)d %(path)s' % params)
350
351 _execute_screenshot_capture_command(import_cmd)
352
353 return fullpath
354
355
356def _execute_screenshot_capture_command(import_cmd_string):
357 """
358 Executes command to capture a screenshot.
359
360 Provides safe execution of command to capture screenshot by wrapping
361 the command around a try-catch construct.
362
363 @param import_cmd_string: string, screenshot capture command.
364
365 """
366
beepsbff9f9d2013-12-06 11:14:08 -0800367 old_exc_type = sys.exc_info()[0]
mussafd5b8052014-05-06 10:04:54 -0700368 full_cmd = ('DISPLAY=:0.0 XAUTHORITY=/home/chronos/.Xauthority %s' %
369 import_cmd_string)
beepsbff9f9d2013-12-06 11:14:08 -0800370 try:
mussafd5b8052014-05-06 10:04:54 -0700371 base_utils.system(full_cmd)
beepsbff9f9d2013-12-06 11:14:08 -0800372 except Exception as err:
373 # Do not raise an exception if the screenshot fails while processing
374 # another exception.
375 if old_exc_type is None:
376 raise
mussafd5b8052014-05-06 10:04:54 -0700377 logging.error(err)