Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame^] | 1 | # 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 | |
| 7 | import common |
| 8 | import logging |
| 9 | import sys |
| 10 | from autotest_lib.client.common_lib import global_config |
| 11 | from autotest_lib.client.common_lib.cros import dev_server |
| 12 | # rpc_utils initializes django, which we can't do in unit tests. |
| 13 | if '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 |
| 16 | from autotest_lib.server.cros import control_file_getter, dynamic_suite |
| 17 | |
| 18 | |
| 19 | class StageBuildFailure(Exception): |
| 20 | """Raised when the dev server throws 500 while staging a build.""" |
| 21 | pass |
| 22 | |
| 23 | |
| 24 | class ControlFileEmpty(Exception): |
| 25 | """Raised when the control file exists on the server, but can't be read.""" |
| 26 | pass |
| 27 | |
| 28 | |
| 29 | def _rpc_utils(): |
| 30 | """Returns the rpc_utils module. MUST be mocked for unit tests.""" |
| 31 | return rpc_utils |
| 32 | |
| 33 | |
| 34 | def create_suite_job(suite_name, board, build): |
| 35 | """ |
| 36 | Create a job to run a test suite on the given device with the given image. |
| 37 | |
| 38 | When the timeout specified in the control file is reached, the |
| 39 | job is guaranteed to have completed and results will be available. |
| 40 | |
| 41 | @param suite_name: the test suite to run, e.g. 'control.bvt'. |
| 42 | @param board: the kind of device to run the tests on. |
| 43 | @param build: unique name by which to refer to the image from now on. |
| 44 | |
| 45 | @throws ControlFileNotFound if a unique suite control file doesn't exist. |
| 46 | @throws NoControlFileList if we can't list the control files at all. |
| 47 | @throws StageBuildFailure if the dev server throws 500 while staging build. |
| 48 | @throws ControlFileEmpty if the control file exists on the server, but |
| 49 | can't be read. |
| 50 | |
| 51 | @return: the job ID of the suite; -1 on error. |
| 52 | """ |
| 53 | # Ensure |build| is staged is on the dev server. |
| 54 | ds = dev_server.DevServer.create() |
| 55 | if not ds.trigger_download(build): |
| 56 | raise StageBuildFailure("Server error while staging " + build) |
| 57 | |
| 58 | getter = control_file_getter.DevServerGetter.create(build, ds) |
| 59 | # Get the control file for the suite. |
| 60 | control_file_in = getter.get_control_file_contents_by_name(suite_name) |
| 61 | if not control_file_in: |
| 62 | raise ControlFileEmpty("Fetching %s returned no data." % suite_name) |
| 63 | |
| 64 | # prepend build and board to the control file |
| 65 | control_file = dynamic_suite.inject_vars({'board': board, 'build': build}, |
| 66 | control_file_in) |
| 67 | |
| 68 | return _rpc_utils().create_job_common('%s-%s' % (build, suite_name), |
| 69 | priority='Medium', |
| 70 | control_type='Server', |
| 71 | control_file=control_file, |
| 72 | hostless=True) |