blob: aa7c3499a79579bdddbdbbbe8ec5dd68a28c701b [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
Chris Masonea8066a92012-05-01 16:52:31 -070010import datetime
Chris Masone859fdec2012-01-30 08:38:09 -080011import logging
Aviv Keshetd83ef442013-01-16 16:19:35 -080012
13from autotest_lib.client.common_lib import error
Alex Miller7d658cf2013-09-04 16:00:35 -070014from autotest_lib.client.common_lib import priorities
Chris Masone859fdec2012-01-30 08:38:09 -080015from autotest_lib.client.common_lib.cros import dev_server
Simran Basib6ec8ae2014-04-23 12:05:08 -070016from autotest_lib.server import utils
Chris Masone44e4d6c2012-08-15 14:25:53 -070017from autotest_lib.server.cros.dynamic_suite import constants
Chris Masoneb4935552012-08-14 12:05:54 -070018from autotest_lib.server.cros.dynamic_suite import control_file_getter
Chris Masoneb4935552012-08-14 12:05:54 -070019from autotest_lib.server.cros.dynamic_suite import job_status
Chris Masone44e4d6c2012-08-15 14:25:53 -070020from autotest_lib.server.cros.dynamic_suite import tools
Chris Masone859fdec2012-01-30 08:38:09 -080021
22
Chris Masonef8b53062012-05-08 22:14:18 -070023# Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py.
Chris Masone859fdec2012-01-30 08:38:09 -080024
25
Chris Masone859fdec2012-01-30 08:38:09 -080026def _rpc_utils():
Chris Masoneb1451a02012-03-12 10:21:14 -070027 """Returns the rpc_utils module. MUST be mocked for unit tests.
28
29 rpc_utils initializes django, which we can't do in unit tests.
30 This layer of indirection allows us to only load that module if we're
31 not running unit tests.
32
33 @return: autotest_lib.frontend.afe.rpc_utils
34 """
35 from autotest_lib.frontend.afe import rpc_utils
Chris Masone859fdec2012-01-30 08:38:09 -080036 return rpc_utils
37
38
Chris Masone62579122012-03-08 15:18:43 -080039def canonicalize_suite_name(suite_name):
40 return 'test_suites/control.%s' % suite_name
41
42
Chris Masoneaa10f8e2012-05-15 13:34:21 -070043def formatted_now():
Chris Masone8d6e6412012-06-28 11:20:56 -070044 return datetime.datetime.now().strftime(job_status.TIME_FMT)
Chris Masoneaa10f8e2012-05-15 13:34:21 -070045
46
Simran Basib6ec8ae2014-04-23 12:05:08 -070047def _get_control_file_contents_by_name(build, ds, suite_name):
Chris Masone8dd27e02012-06-25 15:59:43 -070048 """Return control file contents for |suite_name|.
49
50 Query the dev server at |ds| for the control file |suite_name|, included
51 in |build| for |board|.
52
53 @param build: unique name by which to refer to the image from now on.
Chris Masone8dd27e02012-06-25 15:59:43 -070054 @param ds: a dev_server.DevServer instance to fetch control file with.
55 @param suite_name: canonicalized suite name, e.g. test_suites/control.bvt.
56 @raises ControlFileNotFound if a unique suite control file doesn't exist.
57 @raises NoControlFileList if we can't list the control files at all.
58 @raises ControlFileEmpty if the control file exists on the server, but
59 can't be read.
60
61 @return the contents of the desired control file.
62 """
63 getter = control_file_getter.DevServerGetter.create(build, ds)
64 # Get the control file for the suite.
65 try:
66 control_file_in = getter.get_control_file_contents_by_name(suite_name)
67 except error.CrosDynamicSuiteException as e:
Simran Basib6ec8ae2014-04-23 12:05:08 -070068 raise type(e)("%s while testing %s." % (e, build))
Chris Masone8dd27e02012-06-25 15:59:43 -070069 if not control_file_in:
70 raise error.ControlFileEmpty(
71 "Fetching %s returned no data." % suite_name)
Alex Millera713e252013-03-01 10:45:44 -080072 # Force control files to only contain ascii characters.
73 try:
74 control_file_in.encode('ascii')
75 except UnicodeDecodeError as e:
76 raise error.ControlFileMalformed(str(e))
77
Chris Masone8dd27e02012-06-25 15:59:43 -070078 return control_file_in
79
80
Simran Basib6ec8ae2014-04-23 12:05:08 -070081def _stage_build_artifacts(build):
82 """
83 Ensure components of |build| necessary for installing images are staged.
84
85 @param build image we want to stage.
86
87 @raises StageBuildFailure: if the dev server throws 500 while staging
88 build.
89
90 @return: dev_server.ImageServer instance to use with this build.
91 @return: timings dictionary containing staging start/end times.
92 """
93 timings = {}
94 # Set synchronous to False to allow other components to be downloaded in
95 # the background.
96 ds = dev_server.ImageServer.resolve(build)
97 timings[constants.DOWNLOAD_STARTED_TIME] = formatted_now()
98 try:
99 ds.stage_artifacts(build, ['test_suites'])
100 except dev_server.DevServerException as e:
101 raise error.StageBuildFailure(
102 "Failed to stage %s: %s" % (build, e))
103 timings[constants.PAYLOAD_FINISHED_TIME] = formatted_now()
104 return (ds, timings)
105
106
107def create_suite_job(name='', board='', build='', pool='', control_file='',
108 check_hosts=True, num=None, file_bugs=False, timeout=24,
109 timeout_mins=None, priority=priorities.Priority.DEFAULT,
110 suite_args=None, wait_for_results=True, **kwargs):
Chris Masone859fdec2012-01-30 08:38:09 -0800111 """
112 Create a job to run a test suite on the given device with the given image.
113
114 When the timeout specified in the control file is reached, the
115 job is guaranteed to have completed and results will be available.
116
Simran Basib6ec8ae2014-04-23 12:05:08 -0700117 @param name: The test name if control_file is supplied, otherwise the name
118 of the test suite to run, e.g. 'bvt'.
Chris Masone859fdec2012-01-30 08:38:09 -0800119 @param board: the kind of device to run the tests on.
120 @param build: unique name by which to refer to the image from now on.
Scott Zawalski65650172012-02-16 11:48:26 -0500121 @param pool: Specify the pool of machines to use for scheduling
122 purposes.
Chris Masone62579122012-03-08 15:18:43 -0800123 @param check_hosts: require appropriate live hosts to exist in the lab.
Aviv Keshetd83ef442013-01-16 16:19:35 -0800124 @param num: Specify the number of machines to schedule across (integer).
125 Leave unspecified or use None to use default sharding factor.
Alex Millerc577f3e2012-09-27 14:06:07 -0700126 @param file_bugs: File a bug on each test failure in this suite.
Alex Miller139690b2013-09-07 15:35:49 -0700127 @param timeout: The max lifetime of this suite, in hours.
Simran Basi7e605742013-11-12 13:43:36 -0800128 @param timeout_mins: The max lifetime of this suite, in minutes. Takes
129 priority over timeout.
Alex Miller139690b2013-09-07 15:35:49 -0700130 @param priority: Integer denoting priority. Higher is more important.
Aviv Keshet7cd12312013-07-25 10:25:55 -0700131 @param suite_args: Optional arguments which will be parsed by the suite
132 control file. Used by control.test_that_wrapper to
133 determine which tests to run.
Dan Shi95122412013-11-12 16:20:33 -0800134 @param wait_for_results: Set to False to run the suite job without waiting
135 for test jobs to finish. Default is True.
Simran Basib6ec8ae2014-04-23 12:05:08 -0700136 @param kwargs: extra keyword args. NOT USED.
Chris Masone859fdec2012-01-30 08:38:09 -0800137
Chris Masone8dd27e02012-06-25 15:59:43 -0700138 @raises ControlFileNotFound: if a unique suite control file doesn't exist.
139 @raises NoControlFileList: if we can't list the control files at all.
140 @raises StageBuildFailure: if the dev server throws 500 while staging build.
141 @raises ControlFileEmpty: if the control file exists on the server, but
142 can't be read.
Chris Masone859fdec2012-01-30 08:38:09 -0800143
144 @return: the job ID of the suite; -1 on error.
145 """
Aviv Keshetd83ef442013-01-16 16:19:35 -0800146 if type(num) is not int and num is not None:
Chris Sosa18c70b32013-02-15 14:12:43 -0800147 raise error.SuiteArgumentException('Ill specified num argument %r. '
148 'Must be an integer or None.' % num)
Aviv Keshetd83ef442013-01-16 16:19:35 -0800149 if num == 0:
150 logging.warning("Can't run on 0 hosts; using default.")
151 num = None
Chris Masonea8066a92012-05-01 16:52:31 -0700152
Simran Basib6ec8ae2014-04-23 12:05:08 -0700153 (ds, timings) = _stage_build_artifacts(build)
Chris Masone859fdec2012-01-30 08:38:09 -0800154
Simran Basib6ec8ae2014-04-23 12:05:08 -0700155 if not control_file:
156 # No control file was supplied so look it up from the build artifacts.
157 suite_name = canonicalize_suite_name(name)
158 control_file = _get_control_file_contents_by_name(build, ds, suite_name)
159 name = '%s-%s' % (build, suite_name)
Chris Masone46d0eb12012-07-27 18:56:39 -0700160
Simran Basi7e605742013-11-12 13:43:36 -0800161 timeout_mins = timeout_mins or timeout * 60
162
Simran Basib6ec8ae2014-04-23 12:05:08 -0700163 if not board:
164 board = utils.ParseBuildName(build)[0]
Chris Masone46d0eb12012-07-27 18:56:39 -0700165
Simran Basib6ec8ae2014-04-23 12:05:08 -0700166 # Prepend build and board to the control file.
Scott Zawalski65650172012-02-16 11:48:26 -0500167 inject_dict = {'board': board,
168 'build': build,
Chris Masone62579122012-03-08 15:18:43 -0800169 'check_hosts': check_hosts,
Chris Masone46d0eb12012-07-27 18:56:39 -0700170 'pool': pool,
Aviv Keshetd83ef442013-01-16 16:19:35 -0800171 'num': num,
Dan Shib8a99112013-06-18 13:46:10 -0700172 'file_bugs': file_bugs,
Alex Miller139690b2013-09-07 15:35:49 -0700173 'timeout': timeout,
Simran Basi7e605742013-11-12 13:43:36 -0800174 'timeout_mins': timeout_mins,
Alex Miller7d658cf2013-09-04 16:00:35 -0700175 'devserver_url': ds.url(),
Aviv Keshet7cd12312013-07-25 10:25:55 -0700176 'priority': priority,
Dan Shi95122412013-11-12 16:20:33 -0800177 'suite_args' : suite_args,
178 'wait_for_results': wait_for_results
Aviv Keshet7cd12312013-07-25 10:25:55 -0700179 }
180
Simran Basib6ec8ae2014-04-23 12:05:08 -0700181 control_file = tools.inject_vars(inject_dict, control_file)
Chris Masone859fdec2012-01-30 08:38:09 -0800182
Simran Basib6ec8ae2014-04-23 12:05:08 -0700183 return _rpc_utils().create_job_common(name,
Alex Miller7d658cf2013-09-04 16:00:35 -0700184 priority=priority,
Simran Basi7e605742013-11-12 13:43:36 -0800185 timeout_mins=timeout_mins,
Alex Miller139690b2013-09-07 15:35:49 -0700186 max_runtime_mins=timeout*60,
Chris Masone859fdec2012-01-30 08:38:09 -0800187 control_type='Server',
188 control_file=control_file,
Chris Masonea8066a92012-05-01 16:52:31 -0700189 hostless=True,
190 keyvals=timings)