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 |
Chris Masone | a8066a9 | 2012-05-01 16:52:31 -0700 | [diff] [blame] | 8 | import datetime |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 9 | import logging |
| 10 | import sys |
Chris Masone | f8b5306 | 2012-05-08 22:14:18 -0700 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import error, global_config |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 12 | from autotest_lib.client.common_lib.cros import dev_server |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 13 | from autotest_lib.server.cros import control_file_getter, dynamic_suite |
| 14 | |
| 15 | |
Chris Masone | f8b5306 | 2012-05-08 22:14:18 -0700 | [diff] [blame] | 16 | # Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 17 | |
| 18 | |
| 19 | class ControlFileEmpty(Exception): |
| 20 | """Raised when the control file exists on the server, but can't be read.""" |
| 21 | pass |
| 22 | |
| 23 | |
| 24 | def _rpc_utils(): |
Chris Masone | b1451a0 | 2012-03-12 10:21:14 -0700 | [diff] [blame] | 25 | """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 Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 34 | return rpc_utils |
| 35 | |
| 36 | |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 37 | def canonicalize_suite_name(suite_name): |
| 38 | return 'test_suites/control.%s' % suite_name |
| 39 | |
| 40 | |
Chris Masone | aa10f8e | 2012-05-15 13:34:21 -0700 | [diff] [blame] | 41 | def formatted_now(): |
| 42 | return datetime.datetime.now().strftime(dynamic_suite.TIME_FMT) |
| 43 | |
| 44 | |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame^] | 45 | def get_control_file_contents_by_name(build, board, ds, suite_name): |
| 46 | """Return control file contents for |suite_name|. |
| 47 | |
| 48 | Query the dev server at |ds| for the control file |suite_name|, included |
| 49 | in |build| for |board|. |
| 50 | |
| 51 | @param build: unique name by which to refer to the image from now on. |
| 52 | @param board: the kind of device to run the tests on. |
| 53 | @param ds: a dev_server.DevServer instance to fetch control file with. |
| 54 | @param suite_name: canonicalized suite name, e.g. test_suites/control.bvt. |
| 55 | @raises ControlFileNotFound if a unique suite control file doesn't exist. |
| 56 | @raises NoControlFileList if we can't list the control files at all. |
| 57 | @raises ControlFileEmpty if the control file exists on the server, but |
| 58 | can't be read. |
| 59 | |
| 60 | @return the contents of the desired control file. |
| 61 | """ |
| 62 | getter = control_file_getter.DevServerGetter.create(build, ds) |
| 63 | # Get the control file for the suite. |
| 64 | try: |
| 65 | control_file_in = getter.get_control_file_contents_by_name(suite_name) |
| 66 | except error.CrosDynamicSuiteException as e: |
| 67 | raise type(e)("%s while testing %s for %s." % (e, build, board)) |
| 68 | if not control_file_in: |
| 69 | raise error.ControlFileEmpty( |
| 70 | "Fetching %s returned no data." % suite_name) |
| 71 | return control_file_in |
| 72 | |
| 73 | |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 74 | def create_suite_job(suite_name, board, build, pool, check_hosts=True): |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 75 | """ |
| 76 | Create a job to run a test suite on the given device with the given image. |
| 77 | |
| 78 | When the timeout specified in the control file is reached, the |
| 79 | job is guaranteed to have completed and results will be available. |
| 80 | |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 81 | @param suite_name: the test suite to run, e.g. 'bvt'. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 82 | @param board: the kind of device to run the tests on. |
| 83 | @param build: unique name by which to refer to the image from now on. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 84 | @param pool: Specify the pool of machines to use for scheduling |
| 85 | purposes. |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 86 | @param check_hosts: require appropriate live hosts to exist in the lab. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 87 | |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame^] | 88 | @raises ControlFileNotFound: if a unique suite control file doesn't exist. |
| 89 | @raises NoControlFileList: if we can't list the control files at all. |
| 90 | @raises StageBuildFailure: if the dev server throws 500 while staging build. |
| 91 | @raises ControlFileEmpty: if the control file exists on the server, but |
| 92 | can't be read. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 93 | |
| 94 | @return: the job ID of the suite; -1 on error. |
| 95 | """ |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 96 | # All suite names are assumed under test_suites/control.XX. |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 97 | suite_name = canonicalize_suite_name(suite_name) |
Chris Masone | a8066a9 | 2012-05-01 16:52:31 -0700 | [diff] [blame] | 98 | |
| 99 | timings = {} |
Chris Sosa | 6b288c8 | 2012-03-29 15:31:06 -0700 | [diff] [blame] | 100 | # Ensure components of |build| necessary for installing images are staged |
| 101 | # on the dev server. However set synchronous to False to allow other |
| 102 | # components to be downloaded in the background. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 103 | ds = dev_server.DevServer.create() |
Chris Masone | aa10f8e | 2012-05-15 13:34:21 -0700 | [diff] [blame] | 104 | timings[dynamic_suite.DOWNLOAD_STARTED_TIME] = formatted_now() |
Chris Masone | f70650c | 2012-05-16 08:52:12 -0700 | [diff] [blame] | 105 | try: |
| 106 | ds.trigger_download(build, synchronous=False) |
| 107 | except dev_server.DevServerException as e: |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame^] | 108 | raise error.StageBuildFailure( |
| 109 | "Failed to stage %s for %s: %s" % (build, board, e)) |
Chris Masone | aa10f8e | 2012-05-15 13:34:21 -0700 | [diff] [blame] | 110 | timings[dynamic_suite.PAYLOAD_FINISHED_TIME] = formatted_now() |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 111 | |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame^] | 112 | control_file_in = get_control_file_contents_by_name(build, board, ds, |
| 113 | suite_name) |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 114 | # prepend build and board to the control file |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 115 | inject_dict = {'board': board, |
| 116 | 'build': build, |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 117 | 'check_hosts': check_hosts, |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 118 | 'pool': pool} |
| 119 | control_file = dynamic_suite.inject_vars(inject_dict, control_file_in) |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 120 | |
| 121 | return _rpc_utils().create_job_common('%s-%s' % (build, suite_name), |
| 122 | priority='Medium', |
| 123 | control_type='Server', |
| 124 | control_file=control_file, |
Chris Masone | a8066a9 | 2012-05-01 16:52:31 -0700 | [diff] [blame] | 125 | hostless=True, |
| 126 | keyvals=timings) |