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