blob: 4e257149bbf4997926069a5326dafa7131a63b18 [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
8import logging
9import sys
10from autotest_lib.client.common_lib import global_config
11from autotest_lib.client.common_lib.cros import dev_server
12# rpc_utils initializes django, which we can't do in unit tests.
13if 'unittest' not in sys.modules.keys():
14 # So, only load that module if we're not running unit tests.
15 from autotest_lib.frontend.afe import rpc_utils
16from autotest_lib.server.cros import control_file_getter, dynamic_suite
17
18
19class StageBuildFailure(Exception):
20 """Raised when the dev server throws 500 while staging a build."""
21 pass
22
23
24class ControlFileEmpty(Exception):
25 """Raised when the control file exists on the server, but can't be read."""
26 pass
27
28
29def _rpc_utils():
30 """Returns the rpc_utils module. MUST be mocked for unit tests."""
31 return rpc_utils
32
33
Chris Masone62579122012-03-08 15:18:43 -080034def canonicalize_suite_name(suite_name):
35 return 'test_suites/control.%s' % suite_name
36
37
38def create_suite_job(suite_name, board, build, pool, check_hosts=True):
Chris Masone859fdec2012-01-30 08:38:09 -080039 """
40 Create a job to run a test suite on the given device with the given image.
41
42 When the timeout specified in the control file is reached, the
43 job is guaranteed to have completed and results will be available.
44
Scott Zawalski65650172012-02-16 11:48:26 -050045 @param suite_name: the test suite to run, e.g. 'bvt'.
Chris Masone859fdec2012-01-30 08:38:09 -080046 @param board: the kind of device to run the tests on.
47 @param build: unique name by which to refer to the image from now on.
Scott Zawalski65650172012-02-16 11:48:26 -050048 @param pool: Specify the pool of machines to use for scheduling
49 purposes.
Chris Masone62579122012-03-08 15:18:43 -080050 @param check_hosts: require appropriate live hosts to exist in the lab.
Chris Masone859fdec2012-01-30 08:38:09 -080051
Chris Masone62579122012-03-08 15:18:43 -080052 @raises ControlFileNotFound if a unique suite control file doesn't exist.
53 @raises NoControlFileList if we can't list the control files at all.
54 @raises StageBuildFailure if the dev server throws 500 while staging build.
55 @raises ControlFileEmpty if the control file exists on the server, but
Chris Masone859fdec2012-01-30 08:38:09 -080056 can't be read.
57
58 @return: the job ID of the suite; -1 on error.
59 """
Scott Zawalski65650172012-02-16 11:48:26 -050060 # All suite names are assumed under test_suites/control.XX.
Chris Masone62579122012-03-08 15:18:43 -080061 suite_name = canonicalize_suite_name(suite_name)
Chris Masone859fdec2012-01-30 08:38:09 -080062 # Ensure |build| is staged is on the dev server.
63 ds = dev_server.DevServer.create()
64 if not ds.trigger_download(build):
65 raise StageBuildFailure("Server error while staging " + build)
66
67 getter = control_file_getter.DevServerGetter.create(build, ds)
68 # Get the control file for the suite.
69 control_file_in = getter.get_control_file_contents_by_name(suite_name)
70 if not control_file_in:
71 raise ControlFileEmpty("Fetching %s returned no data." % suite_name)
72
73 # prepend build and board to the control file
Scott Zawalski65650172012-02-16 11:48:26 -050074 inject_dict = {'board': board,
75 'build': build,
Chris Masone62579122012-03-08 15:18:43 -080076 'check_hosts': check_hosts,
Scott Zawalski65650172012-02-16 11:48:26 -050077 'pool': pool}
78 control_file = dynamic_suite.inject_vars(inject_dict, control_file_in)
Chris Masone859fdec2012-01-30 08:38:09 -080079
80 return _rpc_utils().create_job_common('%s-%s' % (build, suite_name),
81 priority='Medium',
82 control_type='Server',
83 control_file=control_file,
84 hostless=True)