blob: 821fcf43eac110582ce2889fbeeeee91160336ea [file] [log] [blame]
Dan Shi4df39252013-03-19 13:19:45 -07001# pylint: disable-msg=C0111
2
Chris Masone859fdec2012-01-30 08:38:09 -08003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7__author__ = 'cmasone@chromium.org (Chris Masone)'
8
9import common
Simran Basi773a86e2015-05-13 19:15:42 -070010import ConfigParser
Chris Masonea8066a92012-05-01 16:52:31 -070011import datetime
Chris Masone859fdec2012-01-30 08:38:09 -080012import logging
Simran Basi71206ef2014-08-13 13:51:18 -070013import os
14import shutil
Aviv Keshetd83ef442013-01-16 16:19:35 -080015
Jakob Juelich82b7d1c2014-09-15 16:10:57 -070016from autotest_lib.frontend.afe import models
Matthew Sartorid96fb9b2015-05-19 18:04:58 -070017from autotest_lib.client.common_lib import control_data
Aviv Keshetd83ef442013-01-16 16:19:35 -080018from autotest_lib.client.common_lib import error
Simran Basi71206ef2014-08-13 13:51:18 -070019from autotest_lib.client.common_lib import global_config
Alex Miller7d658cf2013-09-04 16:00:35 -070020from autotest_lib.client.common_lib import priorities
Dan Shidfea3682014-08-10 23:38:40 -070021from autotest_lib.client.common_lib import time_utils
Chris Masone859fdec2012-01-30 08:38:09 -080022from autotest_lib.client.common_lib.cros import dev_server
Gabe Black1e1c41b2015-02-04 23:55:15 -080023from autotest_lib.client.common_lib.cros.graphite import autotest_stats
Jakob Juelich9fffe4f2014-08-14 18:07:05 -070024from autotest_lib.frontend.afe import rpc_utils
Simran Basib6ec8ae2014-04-23 12:05:08 -070025from autotest_lib.server import utils
Dan Shi36cfd832014-10-10 13:38:51 -070026from autotest_lib.server.cros import provision
Chris Masone44e4d6c2012-08-15 14:25:53 -070027from autotest_lib.server.cros.dynamic_suite import constants
Chris Masoneb4935552012-08-14 12:05:54 -070028from autotest_lib.server.cros.dynamic_suite import control_file_getter
Chris Masone44e4d6c2012-08-15 14:25:53 -070029from autotest_lib.server.cros.dynamic_suite import tools
Dan Shi36cfd832014-10-10 13:38:51 -070030from autotest_lib.server.cros.dynamic_suite.suite import Suite
Simran Basi71206ef2014-08-13 13:51:18 -070031from autotest_lib.server.hosts import moblab_host
Dan Shidfea3682014-08-10 23:38:40 -070032from autotest_lib.site_utils import host_history
Dan Shi193905e2014-07-25 23:33:09 -070033from autotest_lib.site_utils import job_history
Dan Shid7bb4f12015-01-06 10:53:50 -080034from autotest_lib.site_utils import server_manager_utils
Dan Shi6964fa52014-12-18 11:04:27 -080035from autotest_lib.site_utils import stable_version_utils
Simran Basi71206ef2014-08-13 13:51:18 -070036
37
38_CONFIG = global_config.global_config
39MOBLAB_BOTO_LOCATION = '/home/moblab/.boto'
Chris Masone859fdec2012-01-30 08:38:09 -080040
Chris Masonef8b53062012-05-08 22:14:18 -070041# Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py.
Chris Masone859fdec2012-01-30 08:38:09 -080042
43
Chris Masone62579122012-03-08 15:18:43 -080044def canonicalize_suite_name(suite_name):
Dan Shi70647ca2015-07-16 22:52:35 -070045 # Do not change this naming convention without updating
46 # site_utils.parse_job_name.
Chris Masone62579122012-03-08 15:18:43 -080047 return 'test_suites/control.%s' % suite_name
48
49
Chris Masoneaa10f8e2012-05-15 13:34:21 -070050def formatted_now():
Dan Shidfea3682014-08-10 23:38:40 -070051 return datetime.datetime.now().strftime(time_utils.TIME_FMT)
Chris Masoneaa10f8e2012-05-15 13:34:21 -070052
53
Simran Basib6ec8ae2014-04-23 12:05:08 -070054def _get_control_file_contents_by_name(build, ds, suite_name):
Chris Masone8dd27e02012-06-25 15:59:43 -070055 """Return control file contents for |suite_name|.
56
57 Query the dev server at |ds| for the control file |suite_name|, included
58 in |build| for |board|.
59
60 @param build: unique name by which to refer to the image from now on.
Chris Masone8dd27e02012-06-25 15:59:43 -070061 @param ds: a dev_server.DevServer instance to fetch control file with.
62 @param suite_name: canonicalized suite name, e.g. test_suites/control.bvt.
63 @raises ControlFileNotFound if a unique suite control file doesn't exist.
64 @raises NoControlFileList if we can't list the control files at all.
65 @raises ControlFileEmpty if the control file exists on the server, but
66 can't be read.
67
68 @return the contents of the desired control file.
69 """
70 getter = control_file_getter.DevServerGetter.create(build, ds)
Gabe Black1e1c41b2015-02-04 23:55:15 -080071 timer = autotest_stats.Timer('control_files.parse.%s.%s' %
72 (ds.get_server_name(ds.url()
73 ).replace('.', '_'),
74 suite_name.rsplit('.')[-1]))
Chris Masone8dd27e02012-06-25 15:59:43 -070075 # Get the control file for the suite.
76 try:
Prashanth Balasubramanianabe3bb72014-11-20 12:00:37 -080077 with timer:
78 control_file_in = getter.get_control_file_contents_by_name(
79 suite_name)
Chris Masone8dd27e02012-06-25 15:59:43 -070080 except error.CrosDynamicSuiteException as e:
Simran Basib6ec8ae2014-04-23 12:05:08 -070081 raise type(e)("%s while testing %s." % (e, build))
Chris Masone8dd27e02012-06-25 15:59:43 -070082 if not control_file_in:
83 raise error.ControlFileEmpty(
84 "Fetching %s returned no data." % suite_name)
Alex Millera713e252013-03-01 10:45:44 -080085 # Force control files to only contain ascii characters.
86 try:
87 control_file_in.encode('ascii')
88 except UnicodeDecodeError as e:
89 raise error.ControlFileMalformed(str(e))
90
Chris Masone8dd27e02012-06-25 15:59:43 -070091 return control_file_in
92
93
Simran Basib6ec8ae2014-04-23 12:05:08 -070094def _stage_build_artifacts(build):
95 """
96 Ensure components of |build| necessary for installing images are staged.
97
98 @param build image we want to stage.
99
Prashanth B6285f6a2014-05-08 18:01:27 -0700100 @raises StageControlFileFailure: if the dev server throws 500 while staging
101 suite control files.
Simran Basib6ec8ae2014-04-23 12:05:08 -0700102
103 @return: dev_server.ImageServer instance to use with this build.
104 @return: timings dictionary containing staging start/end times.
105 """
106 timings = {}
Prashanth B6285f6a2014-05-08 18:01:27 -0700107 # Ensure components of |build| necessary for installing images are staged
108 # on the dev server. However set synchronous to False to allow other
109 # components to be downloaded in the background.
Simran Basib6ec8ae2014-04-23 12:05:08 -0700110 ds = dev_server.ImageServer.resolve(build)
111 timings[constants.DOWNLOAD_STARTED_TIME] = formatted_now()
Gabe Black1e1c41b2015-02-04 23:55:15 -0800112 timer = autotest_stats.Timer('control_files.stage.%s' % (
113 ds.get_server_name(ds.url()).replace('.', '_')))
Simran Basib6ec8ae2014-04-23 12:05:08 -0700114 try:
Prashanth Balasubramanianabe3bb72014-11-20 12:00:37 -0800115 with timer:
116 ds.stage_artifacts(build, ['test_suites'])
Simran Basib6ec8ae2014-04-23 12:05:08 -0700117 except dev_server.DevServerException as e:
Prashanth B6285f6a2014-05-08 18:01:27 -0700118 raise error.StageControlFileFailure(
Simran Basib6ec8ae2014-04-23 12:05:08 -0700119 "Failed to stage %s: %s" % (build, e))
120 timings[constants.PAYLOAD_FINISHED_TIME] = formatted_now()
121 return (ds, timings)
122
123
MK Ryue301eb72015-06-25 12:51:02 -0700124@rpc_utils.route_rpc_to_master
Simran Basib6ec8ae2014-04-23 12:05:08 -0700125def create_suite_job(name='', board='', build='', pool='', control_file='',
126 check_hosts=True, num=None, file_bugs=False, timeout=24,
127 timeout_mins=None, priority=priorities.Priority.DEFAULT,
Fang Deng058860c2014-05-15 15:41:50 -0700128 suite_args=None, wait_for_results=True, job_retry=False,
Fang Deng443f1952015-01-02 14:51:49 -0800129 max_retries=None, max_runtime_mins=None, suite_min_duts=0,
Dan Shi36cfd832014-10-10 13:38:51 -0700130 offload_failures_only=False, builds={},
Dan Shi059261a2016-02-22 12:06:37 -0800131 test_source_build=None, run_prod_code=False,
132 delay_minutes=0, **kwargs):
Chris Masone859fdec2012-01-30 08:38:09 -0800133 """
134 Create a job to run a test suite on the given device with the given image.
135
136 When the timeout specified in the control file is reached, the
137 job is guaranteed to have completed and results will be available.
138
Simran Basib6ec8ae2014-04-23 12:05:08 -0700139 @param name: The test name if control_file is supplied, otherwise the name
140 of the test suite to run, e.g. 'bvt'.
Chris Masone859fdec2012-01-30 08:38:09 -0800141 @param board: the kind of device to run the tests on.
142 @param build: unique name by which to refer to the image from now on.
Dan Shi36cfd832014-10-10 13:38:51 -0700143 @param builds: the builds to install e.g.
144 {'cros-version:': 'x86-alex-release/R18-1655.0.0',
145 'fw-version:': 'x86-alex-firmware/R36-5771.50.0',
146 'fwro-version:': 'x86-alex-firmware/R36-5771.49.0'}
147 If builds is given a value, it overrides argument build.
148 @param test_source_build: Build that contains the server-side test code.
Scott Zawalski65650172012-02-16 11:48:26 -0500149 @param pool: Specify the pool of machines to use for scheduling
150 purposes.
Chris Masone62579122012-03-08 15:18:43 -0800151 @param check_hosts: require appropriate live hosts to exist in the lab.
Aviv Keshetd83ef442013-01-16 16:19:35 -0800152 @param num: Specify the number of machines to schedule across (integer).
153 Leave unspecified or use None to use default sharding factor.
Alex Millerc577f3e2012-09-27 14:06:07 -0700154 @param file_bugs: File a bug on each test failure in this suite.
Alex Miller139690b2013-09-07 15:35:49 -0700155 @param timeout: The max lifetime of this suite, in hours.
Simran Basi7e605742013-11-12 13:43:36 -0800156 @param timeout_mins: The max lifetime of this suite, in minutes. Takes
157 priority over timeout.
Alex Miller139690b2013-09-07 15:35:49 -0700158 @param priority: Integer denoting priority. Higher is more important.
Aviv Keshet7cd12312013-07-25 10:25:55 -0700159 @param suite_args: Optional arguments which will be parsed by the suite
160 control file. Used by control.test_that_wrapper to
161 determine which tests to run.
Dan Shi95122412013-11-12 16:20:33 -0800162 @param wait_for_results: Set to False to run the suite job without waiting
163 for test jobs to finish. Default is True.
Fang Deng058860c2014-05-15 15:41:50 -0700164 @param job_retry: Set to True to enable job-level retry. Default is False.
Fang Deng443f1952015-01-02 14:51:49 -0800165 @param max_retries: Integer, maximum job retries allowed at suite level.
166 None for no max.
Simran Basi102e3522014-09-11 11:46:10 -0700167 @param max_runtime_mins: Maximum amount of time a job can be running in
168 minutes.
Fang Dengcbc01212014-11-25 16:09:46 -0800169 @param suite_min_duts: Integer. Scheduler will prioritize getting the
170 minimum number of machines for the suite when it is
171 competing with another suite that has a higher
172 priority but already got minimum machines it needs.
Simran Basi1e10e922015-04-16 15:09:56 -0700173 @param offload_failures_only: Only enable gs_offloading for failed jobs.
Simran Basi5ace6f22016-01-06 17:30:44 -0800174 @param run_prod_code: If True, the suite will run the test code that
175 lives in prod aka the test code currently on the
176 lab servers. If False, the control files and test
177 code for this suite run will be retrieved from the
178 build artifacts.
Dan Shi059261a2016-02-22 12:06:37 -0800179 @param delay_minutes: Delay the creation of test jobs for a given number of
180 minutes.
Simran Basib6ec8ae2014-04-23 12:05:08 -0700181 @param kwargs: extra keyword args. NOT USED.
Chris Masone859fdec2012-01-30 08:38:09 -0800182
Chris Masone8dd27e02012-06-25 15:59:43 -0700183 @raises ControlFileNotFound: if a unique suite control file doesn't exist.
184 @raises NoControlFileList: if we can't list the control files at all.
Prashanth B6285f6a2014-05-08 18:01:27 -0700185 @raises StageControlFileFailure: If the dev server throws 500 while
186 staging test_suites.
Chris Masone8dd27e02012-06-25 15:59:43 -0700187 @raises ControlFileEmpty: if the control file exists on the server, but
188 can't be read.
Chris Masone859fdec2012-01-30 08:38:09 -0800189
190 @return: the job ID of the suite; -1 on error.
191 """
Aviv Keshetd83ef442013-01-16 16:19:35 -0800192 if type(num) is not int and num is not None:
Chris Sosa18c70b32013-02-15 14:12:43 -0800193 raise error.SuiteArgumentException('Ill specified num argument %r. '
194 'Must be an integer or None.' % num)
Aviv Keshetd83ef442013-01-16 16:19:35 -0800195 if num == 0:
196 logging.warning("Can't run on 0 hosts; using default.")
197 num = None
Dan Shi36cfd832014-10-10 13:38:51 -0700198
199 # TODO(dshi): crbug.com/496782 Remove argument build and its reference after
200 # R45 falls out of stable channel.
201 if build and not builds:
202 builds = {provision.CROS_VERSION_PREFIX: build}
203 # TODO(dshi): crbug.com/497236 Remove this check after firmware ro provision
204 # is supported in Autotest.
205 if provision.FW_RO_VERSION_PREFIX in builds:
206 raise error.SuiteArgumentException(
207 'Updating RO firmware is not supported yet.')
208 # Default test source build to CrOS build if it's not specified.
209 test_source_build = Suite.get_test_source_build(
210 builds, test_source_build=test_source_build)
211
Simran Basi5ace6f22016-01-06 17:30:44 -0800212 suite_name = canonicalize_suite_name(name)
213 if run_prod_code:
214 ds = dev_server.ImageServer.resolve(build)
215 keyvals = {}
216 getter = control_file_getter.FileSystemGetter(
217 [_CONFIG.get_config_value('SCHEDULER',
218 'drone_installation_directory')])
219 control_file = getter.get_control_file_contents_by_name(suite_name)
220 else:
221 (ds, keyvals) = _stage_build_artifacts(test_source_build)
Fang Dengcbc01212014-11-25 16:09:46 -0800222 keyvals[constants.SUITE_MIN_DUTS_KEY] = suite_min_duts
Chris Masone859fdec2012-01-30 08:38:09 -0800223
Simran Basib6ec8ae2014-04-23 12:05:08 -0700224 if not control_file:
Dan Shi36cfd832014-10-10 13:38:51 -0700225 # No control file was supplied so look it up from the build artifacts.
226 suite_name = canonicalize_suite_name(name)
227 control_file = _get_control_file_contents_by_name(test_source_build,
228 ds, suite_name)
Simran Basi86fe9c92016-02-09 17:58:20 -0800229 # Do not change this naming convention without updating
230 # site_utils.parse_job_name.
231 name = '%s-%s' % (test_source_build, suite_name)
Chris Masone46d0eb12012-07-27 18:56:39 -0700232
Simran Basi7e605742013-11-12 13:43:36 -0800233 timeout_mins = timeout_mins or timeout * 60
Simran Basi102e3522014-09-11 11:46:10 -0700234 max_runtime_mins = max_runtime_mins or timeout * 60
Simran Basi7e605742013-11-12 13:43:36 -0800235
Simran Basib6ec8ae2014-04-23 12:05:08 -0700236 if not board:
Dan Shid215dbe2015-06-18 16:14:59 -0700237 board = utils.ParseBuildName(builds[provision.CROS_VERSION_PREFIX])[0]
Chris Masone46d0eb12012-07-27 18:56:39 -0700238
Dan Shi36cfd832014-10-10 13:38:51 -0700239 # TODO(dshi): crbug.com/496782 Remove argument build and its reference after
240 # R45 falls out of stable channel.
Simran Basib6ec8ae2014-04-23 12:05:08 -0700241 # Prepend build and board to the control file.
Scott Zawalski65650172012-02-16 11:48:26 -0500242 inject_dict = {'board': board,
Simran Basi5ace6f22016-01-06 17:30:44 -0800243 'build': builds.get(provision.CROS_VERSION_PREFIX),
Dan Shi36cfd832014-10-10 13:38:51 -0700244 'builds': builds,
Chris Masone62579122012-03-08 15:18:43 -0800245 'check_hosts': check_hosts,
Chris Masone46d0eb12012-07-27 18:56:39 -0700246 'pool': pool,
Aviv Keshetd83ef442013-01-16 16:19:35 -0800247 'num': num,
Dan Shib8a99112013-06-18 13:46:10 -0700248 'file_bugs': file_bugs,
Alex Miller139690b2013-09-07 15:35:49 -0700249 'timeout': timeout,
Simran Basi7e605742013-11-12 13:43:36 -0800250 'timeout_mins': timeout_mins,
Alex Miller7d658cf2013-09-04 16:00:35 -0700251 'devserver_url': ds.url(),
Aviv Keshet7cd12312013-07-25 10:25:55 -0700252 'priority': priority,
Dan Shi95122412013-11-12 16:20:33 -0800253 'suite_args' : suite_args,
Fang Deng058860c2014-05-15 15:41:50 -0700254 'wait_for_results': wait_for_results,
Simran Basi102e3522014-09-11 11:46:10 -0700255 'job_retry': job_retry,
Fang Deng443f1952015-01-02 14:51:49 -0800256 'max_retries': max_retries,
Fang Dengcbc01212014-11-25 16:09:46 -0800257 'max_runtime_mins': max_runtime_mins,
Dan Shi36cfd832014-10-10 13:38:51 -0700258 'offload_failures_only': offload_failures_only,
Simran Basi5ace6f22016-01-06 17:30:44 -0800259 'test_source_build': test_source_build,
Dan Shi059261a2016-02-22 12:06:37 -0800260 'run_prod_code': run_prod_code,
261 'delay_minutes': delay_minutes,
Aviv Keshet7cd12312013-07-25 10:25:55 -0700262 }
263
Simran Basib6ec8ae2014-04-23 12:05:08 -0700264 control_file = tools.inject_vars(inject_dict, control_file)
Chris Masone859fdec2012-01-30 08:38:09 -0800265
Jakob Juelich9fffe4f2014-08-14 18:07:05 -0700266 return rpc_utils.create_job_common(name,
Jakob Juelich59cfe542014-09-02 16:37:46 -0700267 priority=priority,
268 timeout_mins=timeout_mins,
269 max_runtime_mins=max_runtime_mins,
270 control_type='Server',
271 control_file=control_file,
272 hostless=True,
Fang Dengcbc01212014-11-25 16:09:46 -0800273 keyvals=keyvals)
Simran Basi71206ef2014-08-13 13:51:18 -0700274
275
276# TODO: hide the following rpcs under is_moblab
277def moblab_only(func):
278 """Ensure moblab specific functions only run on Moblab devices."""
279 def verify(*args, **kwargs):
280 if not utils.is_moblab():
281 raise error.RPCException('RPC: %s can only run on Moblab Systems!',
282 func.__name__)
283 return func(*args, **kwargs)
284 return verify
285
286
287@moblab_only
288def get_config_values():
289 """Returns all config values parsed from global and shadow configs.
290
291 Config values are grouped by sections, and each section is composed of
292 a list of name value pairs.
293 """
294 sections =_CONFIG.get_sections()
295 config_values = {}
296 for section in sections:
297 config_values[section] = _CONFIG.config.items(section)
Jakob Juelich9fffe4f2014-08-14 18:07:05 -0700298 return rpc_utils.prepare_for_serialization(config_values)
Simran Basi71206ef2014-08-13 13:51:18 -0700299
300
301@moblab_only
302def update_config_handler(config_values):
303 """
304 Update config values and override shadow config.
305
306 @param config_values: See get_moblab_settings().
307 """
Simran Basi773a86e2015-05-13 19:15:42 -0700308 original_config = global_config.global_config_class()
309 original_config.set_config_files(shadow_file='')
310 new_shadow = ConfigParser.RawConfigParser()
Simran Basi71206ef2014-08-13 13:51:18 -0700311 for section, config_value_list in config_values.iteritems():
312 for key, value in config_value_list:
Simran Basi773a86e2015-05-13 19:15:42 -0700313 if original_config.get_config_value(section, key,
314 default='',
315 allow_blank=True) != value:
316 if not new_shadow.has_section(section):
317 new_shadow.add_section(section)
318 new_shadow.set(section, key, value)
Simran Basi71206ef2014-08-13 13:51:18 -0700319 if not _CONFIG.shadow_file or not os.path.exists(_CONFIG.shadow_file):
320 raise error.RPCException('Shadow config file does not exist.')
321
322 with open(_CONFIG.shadow_file, 'w') as config_file:
Simran Basi773a86e2015-05-13 19:15:42 -0700323 new_shadow.write(config_file)
Simran Basi71206ef2014-08-13 13:51:18 -0700324 # TODO (sbasi) crbug.com/403916 - Remove the reboot command and
325 # instead restart the services that rely on the config values.
326 os.system('sudo reboot')
327
328
329@moblab_only
330def reset_config_settings():
331 with open(_CONFIG.shadow_file, 'w') as config_file:
Dan Shi36cfd832014-10-10 13:38:51 -0700332 pass
Simran Basi71206ef2014-08-13 13:51:18 -0700333 os.system('sudo reboot')
334
335
336@moblab_only
337def set_boto_key(boto_key):
338 """Update the boto_key file.
339
340 @param boto_key: File name of boto_key uploaded through handle_file_upload.
341 """
342 if not os.path.exists(boto_key):
343 raise error.RPCException('Boto key: %s does not exist!' % boto_key)
344 shutil.copyfile(boto_key, moblab_host.MOBLAB_BOTO_LOCATION)
Dan Shi193905e2014-07-25 23:33:09 -0700345
346
Dan Shiaec99012016-01-07 09:09:16 -0800347@moblab_only
348def set_launch_control_key(launch_control_key):
349 """Update the launch_control_key file.
350
351 @param launch_control_key: File name of launch_control_key uploaded through
352 handle_file_upload.
353 """
354 if not os.path.exists(launch_control_key):
355 raise error.RPCException('Launch Control key: %s does not exist!' %
356 launch_control_key)
357 shutil.copyfile(launch_control_key,
358 moblab_host.MOBLAB_LAUNCH_CONTROL_KEY_LOCATION)
359 # Restart the devserver service.
360 os.system('sudo restart moblab-devserver-init')
361
362
Dan Shi193905e2014-07-25 23:33:09 -0700363def get_job_history(**filter_data):
364 """Get history of the job, including the special tasks executed for the job
365
366 @param filter_data: filter for the call, should at least include
367 {'job_id': [job id]}
368 @returns: JSON string of the job's history, including the information such
369 as the hosts run the job and the special tasks executed before
370 and after the job.
371 """
372 job_id = filter_data['job_id']
373 job_info = job_history.get_job_info(job_id)
Dan Shidfea3682014-08-10 23:38:40 -0700374 return rpc_utils.prepare_for_serialization(job_info.get_history())
375
376
377def get_host_history(start_time, end_time, hosts=None, board=None, pool=None):
378 """Get history of a list of host.
379
380 The return is a JSON string of host history for each host, for example,
381 {'172.22.33.51': [{'status': 'Resetting'
382 'start_time': '2014-08-07 10:02:16',
383 'end_time': '2014-08-07 10:03:16',
384 'log_url': 'http://autotest/reset-546546/debug',
385 'dbg_str': 'Task: Special Task 19441991 (host ...)'},
386 {'status': 'Running'
387 'start_time': '2014-08-07 10:03:18',
388 'end_time': '2014-08-07 10:13:00',
389 'log_url': 'http://autotest/reset-546546/debug',
390 'dbg_str': 'HQE: 15305005, for job: 14995562'}
391 ]
392 }
393 @param start_time: start time to search for history, can be string value or
394 epoch time.
395 @param end_time: end time to search for history, can be string value or
396 epoch time.
397 @param hosts: A list of hosts to search for history. Default is None.
398 @param board: board type of hosts. Default is None.
399 @param pool: pool type of hosts. Default is None.
400 @returns: JSON string of the host history.
401 """
402 return rpc_utils.prepare_for_serialization(
403 host_history.get_history_details(
404 start_time=start_time, end_time=end_time,
405 hosts=hosts, board=board, pool=pool,
406 process_pool_size=4))
Jakob Juelich59cfe542014-09-02 16:37:46 -0700407
408
MK Ryu07a109f2015-07-21 17:44:32 -0700409def shard_heartbeat(shard_hostname, jobs=(), hqes=(), known_job_ids=(),
410 known_host_ids=(), known_host_statuses=()):
Jakob Juelich1b525742014-09-30 13:08:07 -0700411 """Receive updates for job statuses from shards and assign hosts and jobs.
Jakob Juelich59cfe542014-09-02 16:37:46 -0700412
413 @param shard_hostname: Hostname of the calling shard
Jakob Juelicha94efe62014-09-18 16:02:49 -0700414 @param jobs: Jobs in serialized form that should be updated with newer
415 status from a shard.
416 @param hqes: Hostqueueentries in serialized form that should be updated with
417 newer status from a shard. Note that for every hostqueueentry
418 the corresponding job must be in jobs.
Jakob Juelich1b525742014-09-30 13:08:07 -0700419 @param known_job_ids: List of ids of jobs the shard already has.
420 @param known_host_ids: List of ids of hosts the shard already has.
MK Ryu07a109f2015-07-21 17:44:32 -0700421 @param known_host_statuses: List of statuses of hosts the shard already has.
Jakob Juelicha94efe62014-09-18 16:02:49 -0700422
Fang Dengf3705992014-12-16 17:32:18 -0800423 @returns: Serialized representations of hosts, jobs, suite job keyvals
424 and their dependencies to be inserted into a shard's database.
Jakob Juelich59cfe542014-09-02 16:37:46 -0700425 """
Jakob Juelich1b525742014-09-30 13:08:07 -0700426 # The following alternatives to sending host and job ids in every heartbeat
427 # have been considered:
428 # 1. Sending the highest known job and host ids. This would work for jobs:
429 # Newer jobs always have larger ids. Also, if a job is not assigned to a
430 # particular shard during a heartbeat, it never will be assigned to this
431 # shard later.
432 # This is not true for hosts though: A host that is leased won't be sent
433 # to the shard now, but might be sent in a future heartbeat. This means
434 # sometimes hosts should be transfered that have a lower id than the
435 # maximum host id the shard knows.
436 # 2. Send the number of jobs/hosts the shard knows to the master in each
437 # heartbeat. Compare these to the number of records that already have
438 # the shard_id set to this shard. In the normal case, they should match.
439 # In case they don't, resend all entities of that type.
440 # This would work well for hosts, because there aren't that many.
441 # Resending all jobs is quite a big overhead though.
442 # Also, this approach might run into edge cases when entities are
443 # ever deleted.
444 # 3. Mixtures of the above: Use 1 for jobs and 2 for hosts.
445 # Using two different approaches isn't consistent and might cause
446 # confusion. Also the issues with the case of deletions might still
447 # occur.
448 #
449 # The overhead of sending all job and host ids in every heartbeat is low:
450 # At peaks one board has about 1200 created but unfinished jobs.
451 # See the numbers here: http://goo.gl/gQCGWH
452 # Assuming that job id's have 6 digits and that json serialization takes a
453 # comma and a space as overhead, the traffic per id sent is about 8 bytes.
454 # If 5000 ids need to be sent, this means 40 kilobytes of traffic.
455 # A NOT IN query with 5000 ids took about 30ms in tests made.
456 # These numbers seem low enough to outweigh the disadvantages of the
457 # solutions described above.
Gabe Black1e1c41b2015-02-04 23:55:15 -0800458 timer = autotest_stats.Timer('shard_heartbeat')
Jakob Juelich59cfe542014-09-02 16:37:46 -0700459 with timer:
460 shard_obj = rpc_utils.retrieve_shard(shard_hostname=shard_hostname)
Jakob Juelicha94efe62014-09-18 16:02:49 -0700461 rpc_utils.persist_records_sent_from_shard(shard_obj, jobs, hqes)
MK Ryu07a109f2015-07-21 17:44:32 -0700462 assert len(known_host_ids) == len(known_host_statuses)
463 for i in range(len(known_host_ids)):
464 host_model = models.Host.objects.get(pk=known_host_ids[i])
465 if host_model.status != known_host_statuses[i]:
466 host_model.status = known_host_statuses[i]
467 host_model.save()
468
Fang Dengf3705992014-12-16 17:32:18 -0800469 hosts, jobs, suite_keyvals = rpc_utils.find_records_for_shard(
MK Ryu07a109f2015-07-21 17:44:32 -0700470 shard_obj, known_job_ids=known_job_ids,
471 known_host_ids=known_host_ids)
Jakob Juelich59cfe542014-09-02 16:37:46 -0700472 return {
473 'hosts': [host.serialize() for host in hosts],
474 'jobs': [job.serialize() for job in jobs],
Fang Dengf3705992014-12-16 17:32:18 -0800475 'suite_keyvals': [kv.serialize() for kv in suite_keyvals],
Jakob Juelich59cfe542014-09-02 16:37:46 -0700476 }
Jakob Juelich82b7d1c2014-09-15 16:10:57 -0700477
478
479def get_shards(**filter_data):
480 """Return a list of all shards.
481
482 @returns A sequence of nested dictionaries of shard information.
483 """
484 shards = models.Shard.query_objects(filter_data)
485 serialized_shards = rpc_utils.prepare_rows_as_nested_dicts(shards, ())
486 for serialized, shard in zip(serialized_shards, shards):
487 serialized['labels'] = [label.name for label in shard.labels.all()]
488
489 return serialized_shards
490
491
MK Ryu5dfcc892015-07-16 15:34:04 -0700492def add_shard(hostname, labels):
Jakob Juelich82b7d1c2014-09-15 16:10:57 -0700493 """Add a shard and start running jobs on it.
494
495 @param hostname: The hostname of the shard to be added; needs to be unique.
MK Ryu5dfcc892015-07-16 15:34:04 -0700496 @param labels: Board labels separated by a comma. Jobs of one of the labels
497 will be assigned to the shard.
Jakob Juelich82b7d1c2014-09-15 16:10:57 -0700498
Jakob Juelich8b110ee2014-09-15 16:13:42 -0700499 @raises error.RPCException: If label provided doesn't start with `board:`
500 @raises model_logic.ValidationError: If a shard with the given hostname
Jakob Juelich82b7d1c2014-09-15 16:10:57 -0700501 already exists.
Jakob Juelich8b110ee2014-09-15 16:13:42 -0700502 @raises models.Label.DoesNotExist: If the label specified doesn't exist.
Jakob Juelich82b7d1c2014-09-15 16:10:57 -0700503 """
MK Ryu5dfcc892015-07-16 15:34:04 -0700504 labels = labels.split(',')
505 label_models = []
506 for label in labels:
507 if not label.startswith('board:'):
508 raise error.RPCException('Sharding only supports for `board:.*` '
509 'labels.')
510 # Fetch label first, so shard isn't created when label doesn't exist.
511 label_models.append(models.Label.smart_get(label))
Jakob Juelich8b110ee2014-09-15 16:13:42 -0700512
Jakob Juelich82b7d1c2014-09-15 16:10:57 -0700513 shard = models.Shard.add_object(hostname=hostname)
MK Ryu5dfcc892015-07-16 15:34:04 -0700514 for label in label_models:
515 shard.labels.add(label)
Jakob Juelich82b7d1c2014-09-15 16:10:57 -0700516 return shard.id
517
518
519def delete_shard(hostname):
520 """Delete a shard and reclaim all resources from it.
521
522 This claims back all assigned hosts from the shard. To ensure all DUTs are
523 in a sane state, a Repair task is scheduled for them. This reboots the DUTs
524 and therefore clears all running processes that might be left.
525
526 The shard_id of jobs of that shard will be set to None.
527
528 The status of jobs that haven't been reported to be finished yet, will be
529 lost. The master scheduler will pick up the jobs and execute them.
530
531 @param hostname: Hostname of the shard to delete.
532 """
533 shard = rpc_utils.retrieve_shard(shard_hostname=hostname)
534
535 # TODO(beeps): Power off shard
536
537 # For ChromeOS hosts, repair reboots the DUT.
538 # Repair will excalate through multiple repair steps and will verify the
539 # success after each of them. Anyway, it will always run at least the first
540 # one, which includes a reboot.
541 # After a reboot we can be sure no processes from prior tests that were run
542 # by a shard are still running on the DUT.
543 # Important: Don't just set the status to Repair Failed, as that would run
544 # Verify first, before doing any repair measures. Verify would probably
545 # succeed, so this wouldn't change anything on the DUT.
546 for host in models.Host.objects.filter(shard=shard):
547 models.SpecialTask.objects.create(
548 task=models.SpecialTask.Task.REPAIR,
549 host=host,
550 requested_by=models.User.current_user())
551 models.Host.objects.filter(shard=shard).update(shard=None)
552
553 models.Job.objects.filter(shard=shard).update(shard=None)
554
555 shard.labels.clear()
556
557 shard.delete()
Dan Shi6964fa52014-12-18 11:04:27 -0800558
559
MK Ryua34e3b12015-08-21 16:20:47 -0700560def get_servers(hostname=None, role=None, status=None):
Dan Shid7bb4f12015-01-06 10:53:50 -0800561 """Get a list of servers with matching role and status.
562
MK Ryua34e3b12015-08-21 16:20:47 -0700563 @param hostname: FQDN of the server.
Dan Shid7bb4f12015-01-06 10:53:50 -0800564 @param role: Name of the server role, e.g., drone, scheduler. Default to
565 None to match any role.
566 @param status: Status of the server, e.g., primary, backup, repair_required.
567 Default to None to match any server status.
568
569 @raises error.RPCException: If server database is not used.
570 @return: A list of server names for servers with matching role and status.
571 """
572 if not server_manager_utils.use_server_db():
573 raise error.RPCException('Server database is not enabled. Please try '
574 'retrieve servers from global config.')
MK Ryua34e3b12015-08-21 16:20:47 -0700575 servers = server_manager_utils.get_servers(hostname=hostname, role=role,
Dan Shid7bb4f12015-01-06 10:53:50 -0800576 status=status)
577 return [s.get_details() for s in servers]
578
579
MK Ryufbb002c2015-06-08 14:13:16 -0700580@rpc_utils.route_rpc_to_master
Simran Basibeb2bb22016-02-03 15:25:48 -0800581def get_stable_version(board=stable_version_utils.DEFAULT, android=False):
Dan Shi6964fa52014-12-18 11:04:27 -0800582 """Get stable version for the given board.
583
584 @param board: Name of the board.
Simran Basibeb2bb22016-02-03 15:25:48 -0800585 @param android: If True, the given board is an Android-based device. If
586 False, assume its a Chrome OS-based device.
587
Dan Shi6964fa52014-12-18 11:04:27 -0800588 @return: Stable version of the given board. Return global configure value
589 of CROS.stable_cros_version if stable_versinos table does not have
590 entry of board DEFAULT.
591 """
Simran Basibeb2bb22016-02-03 15:25:48 -0800592 return stable_version_utils.get(board=board, android=android)
Dan Shi25e1fd42014-12-19 14:36:42 -0800593
594
MK Ryufbb002c2015-06-08 14:13:16 -0700595@rpc_utils.route_rpc_to_master
Dan Shi25e1fd42014-12-19 14:36:42 -0800596def get_all_stable_versions():
597 """Get stable versions for all boards.
598
599 @return: A dictionary of board:version.
600 """
601 return stable_version_utils.get_all()
602
603
MK Ryufbb002c2015-06-08 14:13:16 -0700604@rpc_utils.route_rpc_to_master
Dan Shi25e1fd42014-12-19 14:36:42 -0800605def set_stable_version(version, board=stable_version_utils.DEFAULT):
606 """Modify stable version for the given board.
607
608 @param version: The new value of stable version for given board.
609 @param board: Name of the board, default to value `DEFAULT`.
610 """
611 stable_version_utils.set(version=version, board=board)
612
613
MK Ryufbb002c2015-06-08 14:13:16 -0700614@rpc_utils.route_rpc_to_master
Dan Shi25e1fd42014-12-19 14:36:42 -0800615def delete_stable_version(board):
616 """Modify stable version for the given board.
617
618 Delete a stable version entry in afe_stable_versions table for a given
619 board, so default stable version will be used.
620
621 @param board: Name of the board.
622 """
623 stable_version_utils.delete(board=board)
Matthew Sartorid96fb9b2015-05-19 18:04:58 -0700624
625
626def get_tests_by_build(build):
627 """Get the tests that are available for the specified build.
628
629 @param build: unique name by which to refer to the image.
630
631 @return: A sorted list of all tests that are in the build specified.
632 """
633 # Stage the test artifacts.
634 try:
635 ds = dev_server.ImageServer.resolve(build)
636 build = ds.translate(build)
637 except dev_server.DevServerException as e:
638 raise ValueError('Could not resolve build %s: %s' % (build, e))
639
640 try:
641 ds.stage_artifacts(build, ['test_suites'])
642 except dev_server.DevServerException as e:
643 raise error.StageControlFileFailure(
644 'Failed to stage %s: %s' % (build, e))
645
646 # Collect the control files specified in this build
647 cfile_getter = control_file_getter.DevServerGetter.create(build, ds)
648 control_file_list = cfile_getter.get_control_file_list()
649
650 test_objects = []
651 _id = 0
652 for control_file_path in control_file_list:
653 # Read and parse the control file
654 control_file = cfile_getter.get_control_file_contents(
655 control_file_path)
656 control_obj = control_data.parse_control_string(control_file)
657
658 # Extract the values needed for the AFE from the control_obj.
659 # The keys list represents attributes in the control_obj that
660 # are required by the AFE
661 keys = ['author', 'doc', 'name', 'time', 'test_type', 'experimental',
662 'test_category', 'test_class', 'dependencies', 'run_verify',
663 'sync_count', 'job_retries', 'retries', 'path']
664
665 test_object = {}
666 for key in keys:
667 test_object[key] = getattr(control_obj, key) if hasattr(
668 control_obj, key) else ''
669
670 # Unfortunately, the AFE expects different key-names for certain
671 # values, these must be corrected to avoid the risk of tests
672 # being omitted by the AFE.
673 # The 'id' is an additional value used in the AFE.
Matthew Sartori10438092015-06-24 14:30:18 -0700674 # The control_data parsing does not reference 'run_reset', but it
675 # is also used in the AFE and defaults to True.
Matthew Sartorid96fb9b2015-05-19 18:04:58 -0700676 test_object['id'] = _id
Matthew Sartori10438092015-06-24 14:30:18 -0700677 test_object['run_reset'] = True
Matthew Sartorid96fb9b2015-05-19 18:04:58 -0700678 test_object['description'] = test_object.get('doc', '')
679 test_object['test_time'] = test_object.get('time', 0)
680 test_object['test_retry'] = test_object.get('retries', 0)
681
682 # Fix the test name to be consistent with the current presentation
683 # of test names in the AFE.
684 testpath, subname = os.path.split(control_file_path)
685 testname = os.path.basename(testpath)
686 subname = subname.split('.')[1:]
687 if subname:
688 testname = '%s:%s' % (testname, ':'.join(subname))
689
690 test_object['name'] = testname
691
Matthew Sartori10438092015-06-24 14:30:18 -0700692 # Correct the test path as parse_control_string sets an empty string.
693 test_object['path'] = control_file_path
694
Matthew Sartorid96fb9b2015-05-19 18:04:58 -0700695 _id += 1
696 test_objects.append(test_object)
697
Matthew Sartori10438092015-06-24 14:30:18 -0700698 test_objects = sorted(test_objects, key=lambda x: x.get('name'))
Matthew Sartorid96fb9b2015-05-19 18:04:58 -0700699 return rpc_utils.prepare_for_serialization(test_objects)