blob: fc7de1fae0b50753d02f7d5f0dbe376b363e64c7 [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
Chris Masone44e4d6c2012-08-15 14:25:53 -070016from autotest_lib.server.cros.dynamic_suite import constants
Chris Masoneb4935552012-08-14 12:05:54 -070017from autotest_lib.server.cros.dynamic_suite import control_file_getter
Chris Masoneb4935552012-08-14 12:05:54 -070018from autotest_lib.server.cros.dynamic_suite import job_status
Chris Masone44e4d6c2012-08-15 14:25:53 -070019from autotest_lib.server.cros.dynamic_suite import tools
Chris Masone859fdec2012-01-30 08:38:09 -080020
21
Chris Masonef8b53062012-05-08 22:14:18 -070022# Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py.
Chris Masone859fdec2012-01-30 08:38:09 -080023
24
Chris Masone859fdec2012-01-30 08:38:09 -080025def _rpc_utils():
Chris Masoneb1451a02012-03-12 10:21:14 -070026 """Returns the rpc_utils module. MUST be mocked for unit tests.
27
28 rpc_utils initializes django, which we can't do in unit tests.
29 This layer of indirection allows us to only load that module if we're
30 not running unit tests.
31
32 @return: autotest_lib.frontend.afe.rpc_utils
33 """
34 from autotest_lib.frontend.afe import rpc_utils
Chris Masone859fdec2012-01-30 08:38:09 -080035 return rpc_utils
36
37
Chris Masone62579122012-03-08 15:18:43 -080038def canonicalize_suite_name(suite_name):
39 return 'test_suites/control.%s' % suite_name
40
41
Chris Masoneaa10f8e2012-05-15 13:34:21 -070042def formatted_now():
Chris Masone8d6e6412012-06-28 11:20:56 -070043 return datetime.datetime.now().strftime(job_status.TIME_FMT)
Chris Masoneaa10f8e2012-05-15 13:34:21 -070044
45
Chris Masone8dd27e02012-06-25 15:59:43 -070046def get_control_file_contents_by_name(build, board, ds, suite_name):
47 """Return control file contents for |suite_name|.
48
49 Query the dev server at |ds| for the control file |suite_name|, included
50 in |build| for |board|.
51
52 @param build: unique name by which to refer to the image from now on.
53 @param board: the kind of device to run the tests on.
54 @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:
68 raise type(e)("%s while testing %s for %s." % (e, build, board))
69 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
Chris Masone46d0eb12012-07-27 18:56:39 -070081def create_suite_job(suite_name, board, build, pool, check_hosts=True,
Simran Basi7e605742013-11-12 13:43:36 -080082 num=None, file_bugs=False, timeout=24, timeout_mins=None,
Aviv Keshet7cd12312013-07-25 10:25:55 -070083 priority=priorities.Priority.DEFAULT,
84 suite_args=None):
Chris Masone859fdec2012-01-30 08:38:09 -080085 """
86 Create a job to run a test suite on the given device with the given image.
87
88 When the timeout specified in the control file is reached, the
89 job is guaranteed to have completed and results will be available.
90
Scott Zawalski65650172012-02-16 11:48:26 -050091 @param suite_name: the test suite to run, e.g. 'bvt'.
Chris Masone859fdec2012-01-30 08:38:09 -080092 @param board: the kind of device to run the tests on.
93 @param build: unique name by which to refer to the image from now on.
Scott Zawalski65650172012-02-16 11:48:26 -050094 @param pool: Specify the pool of machines to use for scheduling
95 purposes.
Chris Masone62579122012-03-08 15:18:43 -080096 @param check_hosts: require appropriate live hosts to exist in the lab.
Aviv Keshetd83ef442013-01-16 16:19:35 -080097 @param num: Specify the number of machines to schedule across (integer).
98 Leave unspecified or use None to use default sharding factor.
Alex Millerc577f3e2012-09-27 14:06:07 -070099 @param file_bugs: File a bug on each test failure in this suite.
Alex Miller139690b2013-09-07 15:35:49 -0700100 @param timeout: The max lifetime of this suite, in hours.
Simran Basi7e605742013-11-12 13:43:36 -0800101 @param timeout_mins: The max lifetime of this suite, in minutes. Takes
102 priority over timeout.
Alex Miller139690b2013-09-07 15:35:49 -0700103 @param priority: Integer denoting priority. Higher is more important.
Aviv Keshet7cd12312013-07-25 10:25:55 -0700104 @param suite_args: Optional arguments which will be parsed by the suite
105 control file. Used by control.test_that_wrapper to
106 determine which tests to run.
Chris Masone859fdec2012-01-30 08:38:09 -0800107
Chris Masone8dd27e02012-06-25 15:59:43 -0700108 @raises ControlFileNotFound: if a unique suite control file doesn't exist.
109 @raises NoControlFileList: if we can't list the control files at all.
110 @raises StageBuildFailure: if the dev server throws 500 while staging build.
111 @raises ControlFileEmpty: if the control file exists on the server, but
112 can't be read.
Chris Masone859fdec2012-01-30 08:38:09 -0800113
114 @return: the job ID of the suite; -1 on error.
115 """
Scott Zawalski65650172012-02-16 11:48:26 -0500116 # All suite names are assumed under test_suites/control.XX.
Chris Masone62579122012-03-08 15:18:43 -0800117 suite_name = canonicalize_suite_name(suite_name)
Aviv Keshetd83ef442013-01-16 16:19:35 -0800118 if type(num) is not int and num is not None:
Chris Sosa18c70b32013-02-15 14:12:43 -0800119 raise error.SuiteArgumentException('Ill specified num argument %r. '
120 'Must be an integer or None.' % num)
Aviv Keshetd83ef442013-01-16 16:19:35 -0800121 if num == 0:
122 logging.warning("Can't run on 0 hosts; using default.")
123 num = None
Chris Masonea8066a92012-05-01 16:52:31 -0700124
125 timings = {}
Chris Sosa6b288c82012-03-29 15:31:06 -0700126 # Ensure components of |build| necessary for installing images are staged
127 # on the dev server. However set synchronous to False to allow other
128 # components to be downloaded in the background.
Chris Sosaaccb5ce2012-08-30 17:29:15 -0700129 ds = dev_server.ImageServer.resolve(build)
Chris Masone44e4d6c2012-08-15 14:25:53 -0700130 timings[constants.DOWNLOAD_STARTED_TIME] = formatted_now()
Chris Masonef70650c2012-05-16 08:52:12 -0700131 try:
Alex Millerf0c75222013-10-23 10:36:13 -0700132 ds.stage_artifacts(build, ['test_suites'])
Chris Masonef70650c2012-05-16 08:52:12 -0700133 except dev_server.DevServerException as e:
Chris Masone8dd27e02012-06-25 15:59:43 -0700134 raise error.StageBuildFailure(
135 "Failed to stage %s for %s: %s" % (build, board, e))
Chris Masone44e4d6c2012-08-15 14:25:53 -0700136 timings[constants.PAYLOAD_FINISHED_TIME] = formatted_now()
Chris Masone859fdec2012-01-30 08:38:09 -0800137
Chris Masone8dd27e02012-06-25 15:59:43 -0700138 control_file_in = get_control_file_contents_by_name(build, board, ds,
139 suite_name)
Chris Masone46d0eb12012-07-27 18:56:39 -0700140
Simran Basi7e605742013-11-12 13:43:36 -0800141 timeout_mins = timeout_mins or timeout * 60
142
Chris Masone46d0eb12012-07-27 18:56:39 -0700143
Chris Masone859fdec2012-01-30 08:38:09 -0800144 # prepend build and board to the control file
Scott Zawalski65650172012-02-16 11:48:26 -0500145 inject_dict = {'board': board,
146 'build': build,
Chris Masone62579122012-03-08 15:18:43 -0800147 'check_hosts': check_hosts,
Chris Masone46d0eb12012-07-27 18:56:39 -0700148 'pool': pool,
Aviv Keshetd83ef442013-01-16 16:19:35 -0800149 'num': num,
Dan Shib8a99112013-06-18 13:46:10 -0700150 'file_bugs': file_bugs,
Alex Miller139690b2013-09-07 15:35:49 -0700151 'timeout': timeout,
Simran Basi7e605742013-11-12 13:43:36 -0800152 'timeout_mins': timeout_mins,
Alex Miller7d658cf2013-09-04 16:00:35 -0700153 'devserver_url': ds.url(),
Aviv Keshet7cd12312013-07-25 10:25:55 -0700154 'priority': priority,
155 'suite_args' : suite_args
156 }
157
Chris Masone44e4d6c2012-08-15 14:25:53 -0700158 control_file = tools.inject_vars(inject_dict, control_file_in)
Chris Masone859fdec2012-01-30 08:38:09 -0800159
160 return _rpc_utils().create_job_common('%s-%s' % (build, suite_name),
Alex Miller7d658cf2013-09-04 16:00:35 -0700161 priority=priority,
Simran Basi7e605742013-11-12 13:43:36 -0800162 timeout_mins=timeout_mins,
Alex Miller139690b2013-09-07 15:35:49 -0700163 max_runtime_mins=timeout*60,
Chris Masone859fdec2012-01-30 08:38:09 -0800164 control_type='Server',
165 control_file=control_file,
Chris Masonea8066a92012-05-01 16:52:31 -0700166 hostless=True,
167 keyvals=timings)