blob: 532c6ef91e0b85f497fc0ed9a1c659185f055109 [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
Chris Masonef8b53062012-05-08 22:14:18 -070011from autotest_lib.client.common_lib import error, global_config
Chris Masone859fdec2012-01-30 08:38:09 -080012from 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
Chris Masonef8b53062012-05-08 22:14:18 -070016# Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py.
Chris Masone859fdec2012-01-30 08:38:09 -080017
18
19class ControlFileEmpty(Exception):
20 """Raised when the control file exists on the server, but can't be read."""
21 pass
22
23
24def _rpc_utils():
Chris Masoneb1451a02012-03-12 10:21:14 -070025 """Returns the rpc_utils module. MUST be mocked for unit tests.
26
27 rpc_utils initializes django, which we can't do in unit tests.
28 This layer of indirection allows us to only load that module if we're
29 not running unit tests.
30
31 @return: autotest_lib.frontend.afe.rpc_utils
32 """
33 from autotest_lib.frontend.afe import rpc_utils
Chris Masone859fdec2012-01-30 08:38:09 -080034 return rpc_utils
35
36
Chris Masone62579122012-03-08 15:18:43 -080037def canonicalize_suite_name(suite_name):
38 return 'test_suites/control.%s' % suite_name
39
40
Chris Masoneaa10f8e2012-05-15 13:34:21 -070041def formatted_now():
42 return datetime.datetime.now().strftime(dynamic_suite.TIME_FMT)
43
44
Chris Masone62579122012-03-08 15:18:43 -080045def create_suite_job(suite_name, board, build, pool, check_hosts=True):
Chris Masone859fdec2012-01-30 08:38:09 -080046 """
47 Create a job to run a test suite on the given device with the given image.
48
49 When the timeout specified in the control file is reached, the
50 job is guaranteed to have completed and results will be available.
51
Scott Zawalski65650172012-02-16 11:48:26 -050052 @param suite_name: the test suite to run, e.g. 'bvt'.
Chris Masone859fdec2012-01-30 08:38:09 -080053 @param board: the kind of device to run the tests on.
54 @param build: unique name by which to refer to the image from now on.
Scott Zawalski65650172012-02-16 11:48:26 -050055 @param pool: Specify the pool of machines to use for scheduling
56 purposes.
Chris Masone62579122012-03-08 15:18:43 -080057 @param check_hosts: require appropriate live hosts to exist in the lab.
Chris Masone859fdec2012-01-30 08:38:09 -080058
Chris Masone62579122012-03-08 15:18:43 -080059 @raises ControlFileNotFound if a unique suite control file doesn't exist.
60 @raises NoControlFileList if we can't list the control files at all.
61 @raises StageBuildFailure if the dev server throws 500 while staging build.
62 @raises ControlFileEmpty if the control file exists on the server, but
Chris Masone859fdec2012-01-30 08:38:09 -080063 can't be read.
64
65 @return: the job ID of the suite; -1 on error.
66 """
Scott Zawalski65650172012-02-16 11:48:26 -050067 # All suite names are assumed under test_suites/control.XX.
Chris Masone62579122012-03-08 15:18:43 -080068 suite_name = canonicalize_suite_name(suite_name)
Chris Masonea8066a92012-05-01 16:52:31 -070069
70 timings = {}
Chris Sosa6b288c82012-03-29 15:31:06 -070071 # Ensure components of |build| necessary for installing images are staged
72 # on the dev server. However set synchronous to False to allow other
73 # components to be downloaded in the background.
Chris Masone859fdec2012-01-30 08:38:09 -080074 ds = dev_server.DevServer.create()
Chris Masoneaa10f8e2012-05-15 13:34:21 -070075 timings[dynamic_suite.DOWNLOAD_STARTED_TIME] = formatted_now()
Chris Masonef70650c2012-05-16 08:52:12 -070076 try:
77 ds.trigger_download(build, synchronous=False)
78 except dev_server.DevServerException as e:
79 raise error.StageBuildFailure(e)
Chris Masoneaa10f8e2012-05-15 13:34:21 -070080 timings[dynamic_suite.PAYLOAD_FINISHED_TIME] = formatted_now()
Chris Masone859fdec2012-01-30 08:38:09 -080081
82 getter = control_file_getter.DevServerGetter.create(build, ds)
83 # Get the control file for the suite.
84 control_file_in = getter.get_control_file_contents_by_name(suite_name)
85 if not control_file_in:
Chris Masonef8b53062012-05-08 22:14:18 -070086 raise error.ControlFileEmpty(
87 "Fetching %s returned no data." % suite_name)
Chris Masone859fdec2012-01-30 08:38:09 -080088
89 # prepend build and board to the control file
Scott Zawalski65650172012-02-16 11:48:26 -050090 inject_dict = {'board': board,
91 'build': build,
Chris Masone62579122012-03-08 15:18:43 -080092 'check_hosts': check_hosts,
Scott Zawalski65650172012-02-16 11:48:26 -050093 'pool': pool}
94 control_file = dynamic_suite.inject_vars(inject_dict, control_file_in)
Chris Masone859fdec2012-01-30 08:38:09 -080095
96 return _rpc_utils().create_job_common('%s-%s' % (build, suite_name),
97 priority='Medium',
98 control_type='Server',
99 control_file=control_file,
Chris Masonea8066a92012-05-01 16:52:31 -0700100 hostless=True,
101 keyvals=timings)