blob: 6e105043bc69232c322fb38fcb2e3f11e9014ee4 [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
10import sys
11from autotest_lib.client.common_lib import global_config
12from autotest_lib.client.common_lib.cros import dev_server
Chris Masone859fdec2012-01-30 08:38:09 -080013from autotest_lib.server.cros import control_file_getter, dynamic_suite
14
15
16class StageBuildFailure(Exception):
17 """Raised when the dev server throws 500 while staging a build."""
18 pass
19
20
21class ControlFileEmpty(Exception):
22 """Raised when the control file exists on the server, but can't be read."""
23 pass
24
25
26def _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
43def create_suite_job(suite_name, board, build, pool, check_hosts=True):
Chris Masone859fdec2012-01-30 08:38:09 -080044 """
45 Create a job to run a test suite on the given device with the given image.
46
47 When the timeout specified in the control file is reached, the
48 job is guaranteed to have completed and results will be available.
49
Scott Zawalski65650172012-02-16 11:48:26 -050050 @param suite_name: the test suite to run, e.g. 'bvt'.
Chris Masone859fdec2012-01-30 08:38:09 -080051 @param board: the kind of device to run the tests on.
52 @param build: unique name by which to refer to the image from now on.
Scott Zawalski65650172012-02-16 11:48:26 -050053 @param pool: Specify the pool of machines to use for scheduling
54 purposes.
Chris Masone62579122012-03-08 15:18:43 -080055 @param check_hosts: require appropriate live hosts to exist in the lab.
Chris Masone859fdec2012-01-30 08:38:09 -080056
Chris Masone62579122012-03-08 15:18:43 -080057 @raises ControlFileNotFound if a unique suite control file doesn't exist.
58 @raises NoControlFileList if we can't list the control files at all.
59 @raises StageBuildFailure if the dev server throws 500 while staging build.
60 @raises ControlFileEmpty if the control file exists on the server, but
Chris Masone859fdec2012-01-30 08:38:09 -080061 can't be read.
62
63 @return: the job ID of the suite; -1 on error.
64 """
Scott Zawalski65650172012-02-16 11:48:26 -050065 # All suite names are assumed under test_suites/control.XX.
Chris Masone62579122012-03-08 15:18:43 -080066 suite_name = canonicalize_suite_name(suite_name)
Chris Masonea8066a92012-05-01 16:52:31 -070067
68 timings = {}
69 time_fmt = '%Y-%m-%d %H:%M:%S'
Chris Sosa6b288c82012-03-29 15:31:06 -070070 # Ensure components of |build| necessary for installing images are staged
71 # on the dev server. However set synchronous to False to allow other
72 # components to be downloaded in the background.
Chris Masone859fdec2012-01-30 08:38:09 -080073 ds = dev_server.DevServer.create()
Chris Masonea8066a92012-05-01 16:52:31 -070074 timings['download_started_time'] = datetime.datetime.now().strftime(
75 time_fmt)
Chris Sosa6b288c82012-03-29 15:31:06 -070076 if not ds.trigger_download(build, synchronous=False):
Chris Masone859fdec2012-01-30 08:38:09 -080077 raise StageBuildFailure("Server error while staging " + build)
Chris Masonea8066a92012-05-01 16:52:31 -070078 timings['payload_finished_time'] = datetime.datetime.now().strftime(
79 time_fmt)
Chris Masone859fdec2012-01-30 08:38:09 -080080
81 getter = control_file_getter.DevServerGetter.create(build, ds)
82 # Get the control file for the suite.
83 control_file_in = getter.get_control_file_contents_by_name(suite_name)
84 if not control_file_in:
85 raise ControlFileEmpty("Fetching %s returned no data." % suite_name)
86
87 # prepend build and board to the control file
Scott Zawalski65650172012-02-16 11:48:26 -050088 inject_dict = {'board': board,
89 'build': build,
Chris Masone62579122012-03-08 15:18:43 -080090 'check_hosts': check_hosts,
Scott Zawalski65650172012-02-16 11:48:26 -050091 'pool': pool}
92 control_file = dynamic_suite.inject_vars(inject_dict, control_file_in)
Chris Masone859fdec2012-01-30 08:38:09 -080093
94 return _rpc_utils().create_job_common('%s-%s' % (build, suite_name),
95 priority='Medium',
96 control_type='Server',
97 control_file=control_file,
Chris Masonea8066a92012-05-01 16:52:31 -070098 hostless=True,
99 keyvals=timings)