blob: 57e9fee428060d77ba4ed2f4a0b5ddc42b1d18cf [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
Chris Masone8d6e6412012-06-28 11:20:56 -070014from autotest_lib.server.cros import job_status
Chris Masone859fdec2012-01-30 08:38:09 -080015
16
Chris Masonef8b53062012-05-08 22:14:18 -070017# Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py.
Chris Masone859fdec2012-01-30 08:38:09 -080018
19
20class ControlFileEmpty(Exception):
21 """Raised when the control file exists on the server, but can't be read."""
22 pass
23
24
25def _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)
72 return control_file_in
73
74
Chris Masone62579122012-03-08 15:18:43 -080075def create_suite_job(suite_name, board, build, pool, check_hosts=True):
Chris Masone859fdec2012-01-30 08:38:09 -080076 """
77 Create a job to run a test suite on the given device with the given image.
78
79 When the timeout specified in the control file is reached, the
80 job is guaranteed to have completed and results will be available.
81
Scott Zawalski65650172012-02-16 11:48:26 -050082 @param suite_name: the test suite to run, e.g. 'bvt'.
Chris Masone859fdec2012-01-30 08:38:09 -080083 @param board: the kind of device to run the tests on.
84 @param build: unique name by which to refer to the image from now on.
Scott Zawalski65650172012-02-16 11:48:26 -050085 @param pool: Specify the pool of machines to use for scheduling
86 purposes.
Chris Masone62579122012-03-08 15:18:43 -080087 @param check_hosts: require appropriate live hosts to exist in the lab.
Chris Masone859fdec2012-01-30 08:38:09 -080088
Chris Masone8dd27e02012-06-25 15:59:43 -070089 @raises ControlFileNotFound: if a unique suite control file doesn't exist.
90 @raises NoControlFileList: if we can't list the control files at all.
91 @raises StageBuildFailure: if the dev server throws 500 while staging build.
92 @raises ControlFileEmpty: if the control file exists on the server, but
93 can't be read.
Chris Masone859fdec2012-01-30 08:38:09 -080094
95 @return: the job ID of the suite; -1 on error.
96 """
Scott Zawalski65650172012-02-16 11:48:26 -050097 # All suite names are assumed under test_suites/control.XX.
Chris Masone62579122012-03-08 15:18:43 -080098 suite_name = canonicalize_suite_name(suite_name)
Chris Masonea8066a92012-05-01 16:52:31 -070099
100 timings = {}
Chris Sosa6b288c82012-03-29 15:31:06 -0700101 # Ensure components of |build| necessary for installing images are staged
102 # on the dev server. However set synchronous to False to allow other
103 # components to be downloaded in the background.
Chris Masone859fdec2012-01-30 08:38:09 -0800104 ds = dev_server.DevServer.create()
Chris Masoneaa10f8e2012-05-15 13:34:21 -0700105 timings[dynamic_suite.DOWNLOAD_STARTED_TIME] = formatted_now()
Chris Masonef70650c2012-05-16 08:52:12 -0700106 try:
107 ds.trigger_download(build, synchronous=False)
108 except dev_server.DevServerException as e:
Chris Masone8dd27e02012-06-25 15:59:43 -0700109 raise error.StageBuildFailure(
110 "Failed to stage %s for %s: %s" % (build, board, e))
Chris Masoneaa10f8e2012-05-15 13:34:21 -0700111 timings[dynamic_suite.PAYLOAD_FINISHED_TIME] = formatted_now()
Chris Masone859fdec2012-01-30 08:38:09 -0800112
Chris Masone8dd27e02012-06-25 15:59:43 -0700113 control_file_in = get_control_file_contents_by_name(build, board, ds,
114 suite_name)
Chris Masone859fdec2012-01-30 08:38:09 -0800115 # prepend build and board to the control file
Scott Zawalski65650172012-02-16 11:48:26 -0500116 inject_dict = {'board': board,
117 'build': build,
Chris Masone62579122012-03-08 15:18:43 -0800118 'check_hosts': check_hosts,
Scott Zawalski65650172012-02-16 11:48:26 -0500119 'pool': pool}
120 control_file = dynamic_suite.inject_vars(inject_dict, control_file_in)
Chris Masone859fdec2012-01-30 08:38:09 -0800121
122 return _rpc_utils().create_job_common('%s-%s' % (build, suite_name),
123 priority='Medium',
124 control_type='Server',
125 control_file=control_file,
Chris Masonea8066a92012-05-01 16:52:31 -0700126 hostless=True,
127 keyvals=timings)