blob: a50763dd489817c76edd3bdeba62be579801ec76 [file] [log] [blame]
Chris Masone859fdec2012-01-30 08:38:09 -08001# Copyright (c) 2012 The Chromium OS 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
5__author__ = 'cmasone@chromium.org (Chris Masone)'
6
7import common
Chris Masonea8066a92012-05-01 16:52:31 -07008import datetime
Chris Masone859fdec2012-01-30 08:38:09 -08009import logging
Aviv Keshetd83ef442013-01-16 16:19:35 -080010
11from autotest_lib.client.common_lib import error
Chris Masone859fdec2012-01-30 08:38:09 -080012from autotest_lib.client.common_lib.cros import dev_server
Chris Masone44e4d6c2012-08-15 14:25:53 -070013from autotest_lib.server.cros.dynamic_suite import constants
Chris Masoneb4935552012-08-14 12:05:54 -070014from autotest_lib.server.cros.dynamic_suite import control_file_getter
Chris Masoneb4935552012-08-14 12:05:54 -070015from autotest_lib.server.cros.dynamic_suite import job_status
Chris Masone44e4d6c2012-08-15 14:25:53 -070016from autotest_lib.server.cros.dynamic_suite import tools
Chris Masone859fdec2012-01-30 08:38:09 -080017
18
Chris Masonef8b53062012-05-08 22:14:18 -070019# Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py.
Chris Masone859fdec2012-01-30 08:38:09 -080020
21
Chris Masone859fdec2012-01-30 08:38:09 -080022def _rpc_utils():
Chris Masoneb1451a02012-03-12 10:21:14 -070023 """Returns the rpc_utils module. MUST be mocked for unit tests.
24
25 rpc_utils initializes django, which we can't do in unit tests.
26 This layer of indirection allows us to only load that module if we're
27 not running unit tests.
28
29 @return: autotest_lib.frontend.afe.rpc_utils
30 """
31 from autotest_lib.frontend.afe import rpc_utils
Chris Masone859fdec2012-01-30 08:38:09 -080032 return rpc_utils
33
34
Chris Masone62579122012-03-08 15:18:43 -080035def canonicalize_suite_name(suite_name):
36 return 'test_suites/control.%s' % suite_name
37
38
Chris Masoneaa10f8e2012-05-15 13:34:21 -070039def formatted_now():
Chris Masone8d6e6412012-06-28 11:20:56 -070040 return datetime.datetime.now().strftime(job_status.TIME_FMT)
Chris Masoneaa10f8e2012-05-15 13:34:21 -070041
42
Chris Masone8dd27e02012-06-25 15:59:43 -070043def get_control_file_contents_by_name(build, board, ds, suite_name):
44 """Return control file contents for |suite_name|.
45
46 Query the dev server at |ds| for the control file |suite_name|, included
47 in |build| for |board|.
48
49 @param build: unique name by which to refer to the image from now on.
50 @param board: the kind of device to run the tests on.
51 @param ds: a dev_server.DevServer instance to fetch control file with.
52 @param suite_name: canonicalized suite name, e.g. test_suites/control.bvt.
53 @raises ControlFileNotFound if a unique suite control file doesn't exist.
54 @raises NoControlFileList if we can't list the control files at all.
55 @raises ControlFileEmpty if the control file exists on the server, but
56 can't be read.
57
58 @return the contents of the desired control file.
59 """
60 getter = control_file_getter.DevServerGetter.create(build, ds)
61 # Get the control file for the suite.
62 try:
63 control_file_in = getter.get_control_file_contents_by_name(suite_name)
64 except error.CrosDynamicSuiteException as e:
65 raise type(e)("%s while testing %s for %s." % (e, build, board))
66 if not control_file_in:
67 raise error.ControlFileEmpty(
68 "Fetching %s returned no data." % suite_name)
69 return control_file_in
70
71
Chris Masone46d0eb12012-07-27 18:56:39 -070072def create_suite_job(suite_name, board, build, pool, check_hosts=True,
Alex Millerc577f3e2012-09-27 14:06:07 -070073 num=None, file_bugs=False):
Chris Masone859fdec2012-01-30 08:38:09 -080074 """
75 Create a job to run a test suite on the given device with the given image.
76
77 When the timeout specified in the control file is reached, the
78 job is guaranteed to have completed and results will be available.
79
Scott Zawalski65650172012-02-16 11:48:26 -050080 @param suite_name: the test suite to run, e.g. 'bvt'.
Chris Masone859fdec2012-01-30 08:38:09 -080081 @param board: the kind of device to run the tests on.
82 @param build: unique name by which to refer to the image from now on.
Scott Zawalski65650172012-02-16 11:48:26 -050083 @param pool: Specify the pool of machines to use for scheduling
84 purposes.
Chris Masone62579122012-03-08 15:18:43 -080085 @param check_hosts: require appropriate live hosts to exist in the lab.
Aviv Keshetd83ef442013-01-16 16:19:35 -080086 @param num: Specify the number of machines to schedule across (integer).
87 Leave unspecified or use None to use default sharding factor.
Alex Millerc577f3e2012-09-27 14:06:07 -070088 @param file_bugs: File a bug on each test failure in this suite.
Chris Masone859fdec2012-01-30 08:38:09 -080089
Chris Masone8dd27e02012-06-25 15:59:43 -070090 @raises ControlFileNotFound: if a unique suite control file doesn't exist.
91 @raises NoControlFileList: if we can't list the control files at all.
92 @raises StageBuildFailure: if the dev server throws 500 while staging build.
93 @raises ControlFileEmpty: if the control file exists on the server, but
94 can't be read.
Chris Masone859fdec2012-01-30 08:38:09 -080095
96 @return: the job ID of the suite; -1 on error.
97 """
Scott Zawalski65650172012-02-16 11:48:26 -050098 # All suite names are assumed under test_suites/control.XX.
Chris Masone62579122012-03-08 15:18:43 -080099 suite_name = canonicalize_suite_name(suite_name)
Aviv Keshetd83ef442013-01-16 16:19:35 -0800100
101 if type(num) is not int and num is not None:
102 raise error.SuiteArgumentException('Ill specified num argument. Must be'
103 ' an integer or None.')
104 if num == 0:
105 logging.warning("Can't run on 0 hosts; using default.")
106 num = None
Chris Masonea8066a92012-05-01 16:52:31 -0700107
108 timings = {}
Chris Sosa6b288c82012-03-29 15:31:06 -0700109 # Ensure components of |build| necessary for installing images are staged
110 # on the dev server. However set synchronous to False to allow other
111 # components to be downloaded in the background.
Chris Sosaaccb5ce2012-08-30 17:29:15 -0700112 ds = dev_server.ImageServer.resolve(build)
Chris Masone44e4d6c2012-08-15 14:25:53 -0700113 timings[constants.DOWNLOAD_STARTED_TIME] = formatted_now()
Chris Masonef70650c2012-05-16 08:52:12 -0700114 try:
115 ds.trigger_download(build, synchronous=False)
116 except dev_server.DevServerException as e:
Chris Masone8dd27e02012-06-25 15:59:43 -0700117 raise error.StageBuildFailure(
118 "Failed to stage %s for %s: %s" % (build, board, e))
Chris Masone44e4d6c2012-08-15 14:25:53 -0700119 timings[constants.PAYLOAD_FINISHED_TIME] = formatted_now()
Chris Masone859fdec2012-01-30 08:38:09 -0800120
Chris Masone8dd27e02012-06-25 15:59:43 -0700121 control_file_in = get_control_file_contents_by_name(build, board, ds,
122 suite_name)
Chris Masone46d0eb12012-07-27 18:56:39 -0700123
124
Chris Masone859fdec2012-01-30 08:38:09 -0800125 # prepend build and board to the control file
Scott Zawalski65650172012-02-16 11:48:26 -0500126 inject_dict = {'board': board,
127 'build': build,
Chris Masone62579122012-03-08 15:18:43 -0800128 'check_hosts': check_hosts,
Chris Masone46d0eb12012-07-27 18:56:39 -0700129 'pool': pool,
Aviv Keshetd83ef442013-01-16 16:19:35 -0800130 'num': num,
Alex Millerc577f3e2012-09-27 14:06:07 -0700131 'file_bugs': file_bugs}
Chris Masone44e4d6c2012-08-15 14:25:53 -0700132 control_file = tools.inject_vars(inject_dict, control_file_in)
Chris Masone859fdec2012-01-30 08:38:09 -0800133
134 return _rpc_utils().create_job_common('%s-%s' % (build, suite_name),
135 priority='Medium',
136 control_type='Server',
137 control_file=control_file,
Chris Masonea8066a92012-05-01 16:52:31 -0700138 hostless=True,
139 keyvals=timings)