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 |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 10 | import os |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 11 | |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 12 | from autotest_lib.frontend.afe import models |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 13 | from autotest_lib.client.common_lib import control_data |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 14 | from autotest_lib.client.common_lib import error |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import global_config |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 16 | from autotest_lib.client.common_lib import priorities |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import time_utils |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 18 | from autotest_lib.client.common_lib.cros import dev_server |
Aviv Keshet | 14cac44 | 2016-11-20 21:44:11 -0800 | [diff] [blame^] | 19 | # TODO(akeshet): Replace with monarch. |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 20 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
Jakob Juelich | 9fffe4f | 2014-08-14 18:07:05 -0700 | [diff] [blame] | 21 | from autotest_lib.frontend.afe import rpc_utils |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 22 | from autotest_lib.server import utils |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 23 | from autotest_lib.server.cros import provision |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 24 | from autotest_lib.server.cros.dynamic_suite import constants |
Chris Masone | b493555 | 2012-08-14 12:05:54 -0700 | [diff] [blame] | 25 | from autotest_lib.server.cros.dynamic_suite import control_file_getter |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 26 | from autotest_lib.server.cros.dynamic_suite import tools |
xixuan | 0f7755d | 2016-04-18 14:49:12 -0700 | [diff] [blame] | 27 | from autotest_lib.server.cros.dynamic_suite import suite as SuiteBase |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 28 | from autotest_lib.server.cros.dynamic_suite.suite import Suite |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 29 | from autotest_lib.site_utils import host_history |
Dan Shi | 193905e | 2014-07-25 23:33:09 -0700 | [diff] [blame] | 30 | from autotest_lib.site_utils import job_history |
Dan Shi | d7bb4f1 | 2015-01-06 10:53:50 -0800 | [diff] [blame] | 31 | from autotest_lib.site_utils import server_manager_utils |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 32 | from autotest_lib.site_utils import stable_version_utils |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 33 | |
| 34 | |
| 35 | _CONFIG = global_config.global_config |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 36 | |
Chris Masone | f8b5306 | 2012-05-08 22:14:18 -0700 | [diff] [blame] | 37 | # Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 38 | |
| 39 | |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 40 | def canonicalize_suite_name(suite_name): |
xixuan | ba232a3 | 2016-08-25 17:01:59 -0700 | [diff] [blame] | 41 | """Canonicalize the suite's name. |
| 42 | |
| 43 | @param suite_name: the name of the suite. |
| 44 | """ |
Dan Shi | 70647ca | 2015-07-16 22:52:35 -0700 | [diff] [blame] | 45 | # Do not change this naming convention without updating |
| 46 | # site_utils.parse_job_name. |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 47 | return 'test_suites/control.%s' % suite_name |
| 48 | |
| 49 | |
Chris Masone | aa10f8e | 2012-05-15 13:34:21 -0700 | [diff] [blame] | 50 | def formatted_now(): |
xixuan | ba232a3 | 2016-08-25 17:01:59 -0700 | [diff] [blame] | 51 | """Format the current datetime.""" |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 52 | return datetime.datetime.now().strftime(time_utils.TIME_FMT) |
Chris Masone | aa10f8e | 2012-05-15 13:34:21 -0700 | [diff] [blame] | 53 | |
| 54 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 55 | def _get_control_file_contents_by_name(build, ds, suite_name): |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 56 | """Return control file contents for |suite_name|. |
| 57 | |
| 58 | Query the dev server at |ds| for the control file |suite_name|, included |
| 59 | in |build| for |board|. |
| 60 | |
| 61 | @param build: unique name by which to refer to the image from now on. |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 62 | @param ds: a dev_server.DevServer instance to fetch control file with. |
| 63 | @param suite_name: canonicalized suite name, e.g. test_suites/control.bvt. |
| 64 | @raises ControlFileNotFound if a unique suite control file doesn't exist. |
| 65 | @raises NoControlFileList if we can't list the control files at all. |
| 66 | @raises ControlFileEmpty if the control file exists on the server, but |
| 67 | can't be read. |
| 68 | |
| 69 | @return the contents of the desired control file. |
| 70 | """ |
| 71 | getter = control_file_getter.DevServerGetter.create(build, ds) |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 72 | timer = autotest_stats.Timer('control_files.parse.%s.%s' % |
| 73 | (ds.get_server_name(ds.url() |
| 74 | ).replace('.', '_'), |
| 75 | suite_name.rsplit('.')[-1])) |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 76 | # Get the control file for the suite. |
| 77 | try: |
Prashanth Balasubramanian | abe3bb7 | 2014-11-20 12:00:37 -0800 | [diff] [blame] | 78 | with timer: |
| 79 | control_file_in = getter.get_control_file_contents_by_name( |
| 80 | suite_name) |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 81 | except error.CrosDynamicSuiteException as e: |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 82 | raise type(e)("%s while testing %s." % (e, build)) |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 83 | if not control_file_in: |
| 84 | raise error.ControlFileEmpty( |
| 85 | "Fetching %s returned no data." % suite_name) |
Alex Miller | a713e25 | 2013-03-01 10:45:44 -0800 | [diff] [blame] | 86 | # Force control files to only contain ascii characters. |
| 87 | try: |
| 88 | control_file_in.encode('ascii') |
| 89 | except UnicodeDecodeError as e: |
| 90 | raise error.ControlFileMalformed(str(e)) |
| 91 | |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 92 | return control_file_in |
| 93 | |
| 94 | |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 95 | def _stage_build_artifacts(build, hostname=None): |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 96 | """ |
| 97 | Ensure components of |build| necessary for installing images are staged. |
| 98 | |
| 99 | @param build image we want to stage. |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 100 | @param hostname hostname of a dut may run test on. This is to help to locate |
| 101 | a devserver closer to duts if needed. Default is None. |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 102 | |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 103 | @raises StageControlFileFailure: if the dev server throws 500 while staging |
| 104 | suite control files. |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 105 | |
| 106 | @return: dev_server.ImageServer instance to use with this build. |
| 107 | @return: timings dictionary containing staging start/end times. |
| 108 | """ |
| 109 | timings = {} |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 110 | # Ensure components of |build| necessary for installing images are staged |
| 111 | # on the dev server. However set synchronous to False to allow other |
| 112 | # components to be downloaded in the background. |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 113 | ds = dev_server.resolve(build, hostname=hostname) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 114 | timings[constants.DOWNLOAD_STARTED_TIME] = formatted_now() |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 115 | timer = autotest_stats.Timer('control_files.stage.%s' % ( |
| 116 | ds.get_server_name(ds.url()).replace('.', '_'))) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 117 | try: |
Prashanth Balasubramanian | abe3bb7 | 2014-11-20 12:00:37 -0800 | [diff] [blame] | 118 | with timer: |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 119 | ds.stage_artifacts(image=build, artifacts=['test_suites']) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 120 | except dev_server.DevServerException as e: |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 121 | raise error.StageControlFileFailure( |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 122 | "Failed to stage %s: %s" % (build, e)) |
| 123 | timings[constants.PAYLOAD_FINISHED_TIME] = formatted_now() |
| 124 | return (ds, timings) |
| 125 | |
| 126 | |
MK Ryu | e301eb7 | 2015-06-25 12:51:02 -0700 | [diff] [blame] | 127 | @rpc_utils.route_rpc_to_master |
Dan Shi | 5984d78 | 2016-04-05 18:43:51 -0700 | [diff] [blame] | 128 | def create_suite_job(name='', board='', pool='', control_file='', |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 129 | check_hosts=True, num=None, file_bugs=False, timeout=24, |
| 130 | timeout_mins=None, priority=priorities.Priority.DEFAULT, |
Fang Deng | 058860c | 2014-05-15 15:41:50 -0700 | [diff] [blame] | 131 | suite_args=None, wait_for_results=True, job_retry=False, |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 132 | max_retries=None, max_runtime_mins=None, suite_min_duts=0, |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 133 | offload_failures_only=False, builds={}, |
Dan Shi | 059261a | 2016-02-22 12:06:37 -0800 | [diff] [blame] | 134 | test_source_build=None, run_prod_code=False, |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 135 | delay_minutes=0, is_cloning=False, **kwargs): |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 136 | """ |
| 137 | Create a job to run a test suite on the given device with the given image. |
| 138 | |
| 139 | When the timeout specified in the control file is reached, the |
| 140 | job is guaranteed to have completed and results will be available. |
| 141 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 142 | @param name: The test name if control_file is supplied, otherwise the name |
| 143 | of the test suite to run, e.g. 'bvt'. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 144 | @param board: the kind of device to run the tests on. |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 145 | @param builds: the builds to install e.g. |
| 146 | {'cros-version:': 'x86-alex-release/R18-1655.0.0', |
Dan Shi | 5984d78 | 2016-04-05 18:43:51 -0700 | [diff] [blame] | 147 | 'fwrw-version:': 'x86-alex-firmware/R36-5771.50.0', |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 148 | 'fwro-version:': 'x86-alex-firmware/R36-5771.49.0'} |
| 149 | If builds is given a value, it overrides argument build. |
| 150 | @param test_source_build: Build that contains the server-side test code. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 151 | @param pool: Specify the pool of machines to use for scheduling |
| 152 | purposes. |
xixuan | ba232a3 | 2016-08-25 17:01:59 -0700 | [diff] [blame] | 153 | @param control_file: the control file of the job. |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 154 | @param check_hosts: require appropriate live hosts to exist in the lab. |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 155 | @param num: Specify the number of machines to schedule across (integer). |
| 156 | Leave unspecified or use None to use default sharding factor. |
Alex Miller | c577f3e | 2012-09-27 14:06:07 -0700 | [diff] [blame] | 157 | @param file_bugs: File a bug on each test failure in this suite. |
Alex Miller | 139690b | 2013-09-07 15:35:49 -0700 | [diff] [blame] | 158 | @param timeout: The max lifetime of this suite, in hours. |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 159 | @param timeout_mins: The max lifetime of this suite, in minutes. Takes |
| 160 | priority over timeout. |
Alex Miller | 139690b | 2013-09-07 15:35:49 -0700 | [diff] [blame] | 161 | @param priority: Integer denoting priority. Higher is more important. |
Aviv Keshet | 7cd1231 | 2013-07-25 10:25:55 -0700 | [diff] [blame] | 162 | @param suite_args: Optional arguments which will be parsed by the suite |
| 163 | control file. Used by control.test_that_wrapper to |
| 164 | determine which tests to run. |
Dan Shi | 9512241 | 2013-11-12 16:20:33 -0800 | [diff] [blame] | 165 | @param wait_for_results: Set to False to run the suite job without waiting |
| 166 | for test jobs to finish. Default is True. |
Fang Deng | 058860c | 2014-05-15 15:41:50 -0700 | [diff] [blame] | 167 | @param job_retry: Set to True to enable job-level retry. Default is False. |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 168 | @param max_retries: Integer, maximum job retries allowed at suite level. |
| 169 | None for no max. |
Simran Basi | 102e352 | 2014-09-11 11:46:10 -0700 | [diff] [blame] | 170 | @param max_runtime_mins: Maximum amount of time a job can be running in |
| 171 | minutes. |
Fang Deng | cbc0121 | 2014-11-25 16:09:46 -0800 | [diff] [blame] | 172 | @param suite_min_duts: Integer. Scheduler will prioritize getting the |
| 173 | minimum number of machines for the suite when it is |
| 174 | competing with another suite that has a higher |
| 175 | priority but already got minimum machines it needs. |
Simran Basi | 1e10e92 | 2015-04-16 15:09:56 -0700 | [diff] [blame] | 176 | @param offload_failures_only: Only enable gs_offloading for failed jobs. |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 177 | @param run_prod_code: If True, the suite will run the test code that |
| 178 | lives in prod aka the test code currently on the |
| 179 | lab servers. If False, the control files and test |
| 180 | code for this suite run will be retrieved from the |
| 181 | build artifacts. |
Dan Shi | 059261a | 2016-02-22 12:06:37 -0800 | [diff] [blame] | 182 | @param delay_minutes: Delay the creation of test jobs for a given number of |
| 183 | minutes. |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 184 | @param is_cloning: True if creating a cloning job. |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 185 | @param kwargs: extra keyword args. NOT USED. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 186 | |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 187 | @raises ControlFileNotFound: if a unique suite control file doesn't exist. |
| 188 | @raises NoControlFileList: if we can't list the control files at all. |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 189 | @raises StageControlFileFailure: If the dev server throws 500 while |
| 190 | staging test_suites. |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 191 | @raises ControlFileEmpty: if the control file exists on the server, but |
| 192 | can't be read. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 193 | |
| 194 | @return: the job ID of the suite; -1 on error. |
| 195 | """ |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 196 | if type(num) is not int and num is not None: |
Chris Sosa | 18c70b3 | 2013-02-15 14:12:43 -0800 | [diff] [blame] | 197 | raise error.SuiteArgumentException('Ill specified num argument %r. ' |
| 198 | 'Must be an integer or None.' % num) |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 199 | if num == 0: |
| 200 | logging.warning("Can't run on 0 hosts; using default.") |
| 201 | num = None |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 202 | |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 203 | # Default test source build to CrOS build if it's not specified and |
| 204 | # run_prod_code is set to False. |
| 205 | if not run_prod_code: |
| 206 | test_source_build = Suite.get_test_source_build( |
| 207 | builds, test_source_build=test_source_build) |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 208 | |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 209 | # If 'prefer_local_devserver' is True in global setting, and both board |
| 210 | # and pool are specified, pick a dut in the given board and pool, and |
| 211 | # use that to help to pick a devserver in the same subnet of the duts |
| 212 | # to be used to run tests. |
| 213 | if dev_server.PREFER_LOCAL_DEVSERVER and pool and board: |
| 214 | sample_dut = rpc_utils.get_sample_dut(board, pool) |
| 215 | else: |
| 216 | sample_dut = None |
| 217 | |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 218 | suite_name = canonicalize_suite_name(name) |
| 219 | if run_prod_code: |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 220 | ds = dev_server.resolve(test_source_build, hostname=sample_dut) |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 221 | keyvals = {} |
| 222 | getter = control_file_getter.FileSystemGetter( |
| 223 | [_CONFIG.get_config_value('SCHEDULER', |
| 224 | 'drone_installation_directory')]) |
| 225 | control_file = getter.get_control_file_contents_by_name(suite_name) |
| 226 | else: |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 227 | (ds, keyvals) = _stage_build_artifacts( |
| 228 | test_source_build, hostname=sample_dut) |
Fang Deng | cbc0121 | 2014-11-25 16:09:46 -0800 | [diff] [blame] | 229 | keyvals[constants.SUITE_MIN_DUTS_KEY] = suite_min_duts |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 230 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 231 | if not control_file: |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 232 | # No control file was supplied so look it up from the build artifacts. |
| 233 | suite_name = canonicalize_suite_name(name) |
| 234 | control_file = _get_control_file_contents_by_name(test_source_build, |
| 235 | ds, suite_name) |
Simran Basi | 86fe9c9 | 2016-02-09 17:58:20 -0800 | [diff] [blame] | 236 | # Do not change this naming convention without updating |
| 237 | # site_utils.parse_job_name. |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 238 | if not run_prod_code: |
| 239 | name = '%s-%s' % (test_source_build, suite_name) |
| 240 | else: |
| 241 | # If run_prod_code is True, test_source_build is not set, use the |
| 242 | # first build in the builds list for the sutie job name. |
| 243 | name = '%s-%s' % (builds.values()[0], suite_name) |
Chris Masone | 46d0eb1 | 2012-07-27 18:56:39 -0700 | [diff] [blame] | 244 | |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 245 | timeout_mins = timeout_mins or timeout * 60 |
Simran Basi | 102e352 | 2014-09-11 11:46:10 -0700 | [diff] [blame] | 246 | max_runtime_mins = max_runtime_mins or timeout * 60 |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 247 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 248 | if not board: |
Dan Shi | d215dbe | 2015-06-18 16:14:59 -0700 | [diff] [blame] | 249 | board = utils.ParseBuildName(builds[provision.CROS_VERSION_PREFIX])[0] |
Chris Masone | 46d0eb1 | 2012-07-27 18:56:39 -0700 | [diff] [blame] | 250 | |
Dan Shi | 5984d78 | 2016-04-05 18:43:51 -0700 | [diff] [blame] | 251 | # Prepend builds and board to the control file. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 252 | inject_dict = {'board': board, |
Dan Shi | 6dc22d1 | 2016-04-06 22:10:04 -0700 | [diff] [blame] | 253 | # `build` is needed for suites like AU to stage image inside |
| 254 | # suite control file. |
| 255 | 'build': test_source_build, |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 256 | 'builds': builds, |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 257 | 'check_hosts': check_hosts, |
Chris Masone | 46d0eb1 | 2012-07-27 18:56:39 -0700 | [diff] [blame] | 258 | 'pool': pool, |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 259 | 'num': num, |
Dan Shi | b8a9911 | 2013-06-18 13:46:10 -0700 | [diff] [blame] | 260 | 'file_bugs': file_bugs, |
Alex Miller | 139690b | 2013-09-07 15:35:49 -0700 | [diff] [blame] | 261 | 'timeout': timeout, |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 262 | 'timeout_mins': timeout_mins, |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 263 | 'devserver_url': ds.url(), |
Aviv Keshet | 7cd1231 | 2013-07-25 10:25:55 -0700 | [diff] [blame] | 264 | 'priority': priority, |
Dan Shi | 9512241 | 2013-11-12 16:20:33 -0800 | [diff] [blame] | 265 | 'suite_args' : suite_args, |
Fang Deng | 058860c | 2014-05-15 15:41:50 -0700 | [diff] [blame] | 266 | 'wait_for_results': wait_for_results, |
Simran Basi | 102e352 | 2014-09-11 11:46:10 -0700 | [diff] [blame] | 267 | 'job_retry': job_retry, |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 268 | 'max_retries': max_retries, |
Fang Deng | cbc0121 | 2014-11-25 16:09:46 -0800 | [diff] [blame] | 269 | 'max_runtime_mins': max_runtime_mins, |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 270 | 'offload_failures_only': offload_failures_only, |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 271 | 'test_source_build': test_source_build, |
Dan Shi | 059261a | 2016-02-22 12:06:37 -0800 | [diff] [blame] | 272 | 'run_prod_code': run_prod_code, |
| 273 | 'delay_minutes': delay_minutes, |
Aviv Keshet | 7cd1231 | 2013-07-25 10:25:55 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 276 | if is_cloning: |
| 277 | control_file = tools.remove_injection(control_file) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 278 | control_file = tools.inject_vars(inject_dict, control_file) |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 279 | |
Jakob Juelich | 9fffe4f | 2014-08-14 18:07:05 -0700 | [diff] [blame] | 280 | return rpc_utils.create_job_common(name, |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 281 | priority=priority, |
| 282 | timeout_mins=timeout_mins, |
| 283 | max_runtime_mins=max_runtime_mins, |
| 284 | control_type='Server', |
| 285 | control_file=control_file, |
| 286 | hostless=True, |
Fang Deng | cbc0121 | 2014-11-25 16:09:46 -0800 | [diff] [blame] | 287 | keyvals=keyvals) |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 288 | |
| 289 | |
Dan Shi | 193905e | 2014-07-25 23:33:09 -0700 | [diff] [blame] | 290 | def get_job_history(**filter_data): |
| 291 | """Get history of the job, including the special tasks executed for the job |
| 292 | |
| 293 | @param filter_data: filter for the call, should at least include |
| 294 | {'job_id': [job id]} |
| 295 | @returns: JSON string of the job's history, including the information such |
| 296 | as the hosts run the job and the special tasks executed before |
| 297 | and after the job. |
| 298 | """ |
| 299 | job_id = filter_data['job_id'] |
| 300 | job_info = job_history.get_job_info(job_id) |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 301 | return rpc_utils.prepare_for_serialization(job_info.get_history()) |
| 302 | |
| 303 | |
| 304 | def get_host_history(start_time, end_time, hosts=None, board=None, pool=None): |
| 305 | """Get history of a list of host. |
| 306 | |
| 307 | The return is a JSON string of host history for each host, for example, |
| 308 | {'172.22.33.51': [{'status': 'Resetting' |
| 309 | 'start_time': '2014-08-07 10:02:16', |
| 310 | 'end_time': '2014-08-07 10:03:16', |
| 311 | 'log_url': 'http://autotest/reset-546546/debug', |
| 312 | 'dbg_str': 'Task: Special Task 19441991 (host ...)'}, |
| 313 | {'status': 'Running' |
| 314 | 'start_time': '2014-08-07 10:03:18', |
| 315 | 'end_time': '2014-08-07 10:13:00', |
| 316 | 'log_url': 'http://autotest/reset-546546/debug', |
| 317 | 'dbg_str': 'HQE: 15305005, for job: 14995562'} |
| 318 | ] |
| 319 | } |
| 320 | @param start_time: start time to search for history, can be string value or |
| 321 | epoch time. |
| 322 | @param end_time: end time to search for history, can be string value or |
| 323 | epoch time. |
| 324 | @param hosts: A list of hosts to search for history. Default is None. |
| 325 | @param board: board type of hosts. Default is None. |
| 326 | @param pool: pool type of hosts. Default is None. |
| 327 | @returns: JSON string of the host history. |
| 328 | """ |
| 329 | return rpc_utils.prepare_for_serialization( |
| 330 | host_history.get_history_details( |
| 331 | start_time=start_time, end_time=end_time, |
| 332 | hosts=hosts, board=board, pool=pool, |
| 333 | process_pool_size=4)) |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 334 | |
| 335 | |
MK Ryu | 07a109f | 2015-07-21 17:44:32 -0700 | [diff] [blame] | 336 | def shard_heartbeat(shard_hostname, jobs=(), hqes=(), known_job_ids=(), |
| 337 | known_host_ids=(), known_host_statuses=()): |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 338 | """Receive updates for job statuses from shards and assign hosts and jobs. |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 339 | |
| 340 | @param shard_hostname: Hostname of the calling shard |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 341 | @param jobs: Jobs in serialized form that should be updated with newer |
| 342 | status from a shard. |
| 343 | @param hqes: Hostqueueentries in serialized form that should be updated with |
| 344 | newer status from a shard. Note that for every hostqueueentry |
| 345 | the corresponding job must be in jobs. |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 346 | @param known_job_ids: List of ids of jobs the shard already has. |
| 347 | @param known_host_ids: List of ids of hosts the shard already has. |
MK Ryu | 07a109f | 2015-07-21 17:44:32 -0700 | [diff] [blame] | 348 | @param known_host_statuses: List of statuses of hosts the shard already has. |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 349 | |
Fang Deng | f370599 | 2014-12-16 17:32:18 -0800 | [diff] [blame] | 350 | @returns: Serialized representations of hosts, jobs, suite job keyvals |
| 351 | and their dependencies to be inserted into a shard's database. |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 352 | """ |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 353 | # The following alternatives to sending host and job ids in every heartbeat |
| 354 | # have been considered: |
| 355 | # 1. Sending the highest known job and host ids. This would work for jobs: |
| 356 | # Newer jobs always have larger ids. Also, if a job is not assigned to a |
| 357 | # particular shard during a heartbeat, it never will be assigned to this |
| 358 | # shard later. |
| 359 | # This is not true for hosts though: A host that is leased won't be sent |
| 360 | # to the shard now, but might be sent in a future heartbeat. This means |
| 361 | # sometimes hosts should be transfered that have a lower id than the |
| 362 | # maximum host id the shard knows. |
| 363 | # 2. Send the number of jobs/hosts the shard knows to the master in each |
| 364 | # heartbeat. Compare these to the number of records that already have |
| 365 | # the shard_id set to this shard. In the normal case, they should match. |
| 366 | # In case they don't, resend all entities of that type. |
| 367 | # This would work well for hosts, because there aren't that many. |
| 368 | # Resending all jobs is quite a big overhead though. |
| 369 | # Also, this approach might run into edge cases when entities are |
| 370 | # ever deleted. |
| 371 | # 3. Mixtures of the above: Use 1 for jobs and 2 for hosts. |
| 372 | # Using two different approaches isn't consistent and might cause |
| 373 | # confusion. Also the issues with the case of deletions might still |
| 374 | # occur. |
| 375 | # |
| 376 | # The overhead of sending all job and host ids in every heartbeat is low: |
| 377 | # At peaks one board has about 1200 created but unfinished jobs. |
| 378 | # See the numbers here: http://goo.gl/gQCGWH |
| 379 | # Assuming that job id's have 6 digits and that json serialization takes a |
| 380 | # comma and a space as overhead, the traffic per id sent is about 8 bytes. |
| 381 | # If 5000 ids need to be sent, this means 40 kilobytes of traffic. |
| 382 | # A NOT IN query with 5000 ids took about 30ms in tests made. |
| 383 | # These numbers seem low enough to outweigh the disadvantages of the |
| 384 | # solutions described above. |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 385 | timer = autotest_stats.Timer('shard_heartbeat') |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 386 | with timer: |
| 387 | shard_obj = rpc_utils.retrieve_shard(shard_hostname=shard_hostname) |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 388 | rpc_utils.persist_records_sent_from_shard(shard_obj, jobs, hqes) |
MK Ryu | 07a109f | 2015-07-21 17:44:32 -0700 | [diff] [blame] | 389 | assert len(known_host_ids) == len(known_host_statuses) |
| 390 | for i in range(len(known_host_ids)): |
| 391 | host_model = models.Host.objects.get(pk=known_host_ids[i]) |
| 392 | if host_model.status != known_host_statuses[i]: |
| 393 | host_model.status = known_host_statuses[i] |
| 394 | host_model.save() |
| 395 | |
Fang Deng | f370599 | 2014-12-16 17:32:18 -0800 | [diff] [blame] | 396 | hosts, jobs, suite_keyvals = rpc_utils.find_records_for_shard( |
MK Ryu | 07a109f | 2015-07-21 17:44:32 -0700 | [diff] [blame] | 397 | shard_obj, known_job_ids=known_job_ids, |
| 398 | known_host_ids=known_host_ids) |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 399 | return { |
| 400 | 'hosts': [host.serialize() for host in hosts], |
| 401 | 'jobs': [job.serialize() for job in jobs], |
Fang Deng | f370599 | 2014-12-16 17:32:18 -0800 | [diff] [blame] | 402 | 'suite_keyvals': [kv.serialize() for kv in suite_keyvals], |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 403 | } |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 404 | |
| 405 | |
| 406 | def get_shards(**filter_data): |
| 407 | """Return a list of all shards. |
| 408 | |
| 409 | @returns A sequence of nested dictionaries of shard information. |
| 410 | """ |
| 411 | shards = models.Shard.query_objects(filter_data) |
| 412 | serialized_shards = rpc_utils.prepare_rows_as_nested_dicts(shards, ()) |
| 413 | for serialized, shard in zip(serialized_shards, shards): |
| 414 | serialized['labels'] = [label.name for label in shard.labels.all()] |
| 415 | |
| 416 | return serialized_shards |
| 417 | |
| 418 | |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 419 | def _assign_board_to_shard_precheck(labels): |
| 420 | """Verify whether board labels are valid to be added to a given shard. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 421 | |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 422 | First check whether board label is in correct format. Second, check whether |
| 423 | the board label exist. Third, check whether the board has already been |
| 424 | assigned to shard. |
| 425 | |
| 426 | @param labels: Board labels separated by comma. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 427 | |
Jakob Juelich | 8b110ee | 2014-09-15 16:13:42 -0700 | [diff] [blame] | 428 | @raises error.RPCException: If label provided doesn't start with `board:` |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 429 | or board has been added to shard already. |
Jakob Juelich | 8b110ee | 2014-09-15 16:13:42 -0700 | [diff] [blame] | 430 | @raises models.Label.DoesNotExist: If the label specified doesn't exist. |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 431 | |
| 432 | @returns: A list of label models that ready to be added to shard. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 433 | """ |
MK Ryu | 5dfcc89 | 2015-07-16 15:34:04 -0700 | [diff] [blame] | 434 | labels = labels.split(',') |
| 435 | label_models = [] |
| 436 | for label in labels: |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 437 | # Check whether the board label is in correct format. |
MK Ryu | 5dfcc89 | 2015-07-16 15:34:04 -0700 | [diff] [blame] | 438 | if not label.startswith('board:'): |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 439 | raise error.RPCException('Sharding only supports `board:.*` label.') |
| 440 | # Check whether the board label exist. If not, exception will be thrown |
| 441 | # by smart_get function. |
| 442 | label = models.Label.smart_get(label) |
| 443 | label_id = models.Label.list_objects({'name':label})[0].get('id') |
| 444 | # Check whether the board has been sharded already |
| 445 | try: |
| 446 | shard = models.Shard.objects.get(labels=label) |
| 447 | raise error.RPCException( |
| 448 | '%s is already on shard %s' % (label, shard.hostname)) |
| 449 | except models.Shard.DoesNotExist as e: |
| 450 | # board is not on any shard, so it's valid. |
| 451 | label_models.append(label) |
| 452 | return label_models |
Jakob Juelich | 8b110ee | 2014-09-15 16:13:42 -0700 | [diff] [blame] | 453 | |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 454 | |
| 455 | def add_shard(hostname, labels): |
| 456 | """Add a shard and start running jobs on it. |
| 457 | |
| 458 | @param hostname: The hostname of the shard to be added; needs to be unique. |
| 459 | @param labels: Board labels separated by comma. Jobs of one of the labels |
| 460 | will be assigned to the shard. |
| 461 | |
| 462 | @raises error.RPCException: If label provided doesn't start with `board:` or |
| 463 | board has been added to shard already. |
| 464 | @raises model_logic.ValidationError: If a shard with the given hostname |
| 465 | already exist. |
| 466 | @raises models.Label.DoesNotExist: If the label specified doesn't exist. |
| 467 | |
| 468 | @returns: The id of the added shard. |
| 469 | """ |
| 470 | labels = _assign_board_to_shard_precheck(labels) |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 471 | shard = models.Shard.add_object(hostname=hostname) |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 472 | for label in labels: |
| 473 | shard.labels.add(label) |
| 474 | return shard.id |
| 475 | |
| 476 | |
| 477 | def add_board_to_shard(hostname, labels): |
| 478 | """Add boards to a given shard |
| 479 | |
| 480 | @param hostname: The hostname of the shard to be changed. |
| 481 | @param labels: Board labels separated by comma. |
| 482 | |
| 483 | @raises error.RPCException: If label provided doesn't start with `board:` or |
| 484 | board has been added to shard already. |
| 485 | @raises models.Label.DoesNotExist: If the label specified doesn't exist. |
| 486 | |
| 487 | @returns: The id of the changed shard. |
| 488 | """ |
| 489 | labels = _assign_board_to_shard_precheck(labels) |
| 490 | shard = models.Shard.objects.get(hostname=hostname) |
| 491 | for label in labels: |
MK Ryu | 5dfcc89 | 2015-07-16 15:34:04 -0700 | [diff] [blame] | 492 | shard.labels.add(label) |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 493 | return shard.id |
| 494 | |
| 495 | |
| 496 | def delete_shard(hostname): |
| 497 | """Delete a shard and reclaim all resources from it. |
| 498 | |
| 499 | This claims back all assigned hosts from the shard. To ensure all DUTs are |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 500 | in a sane state, a Reboot task with highest priority is scheduled for them. |
| 501 | This reboots the DUTs and then all left tasks continue to run in drone of |
| 502 | the master. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 503 | |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 504 | The procedure for deleting a shard: |
| 505 | * Lock all unlocked hosts on that shard. |
| 506 | * Remove shard information . |
| 507 | * Assign a reboot task with highest priority to these hosts. |
| 508 | * Unlock these hosts, then, the reboot tasks run in front of all other |
| 509 | tasks. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 510 | |
| 511 | The status of jobs that haven't been reported to be finished yet, will be |
| 512 | lost. The master scheduler will pick up the jobs and execute them. |
| 513 | |
| 514 | @param hostname: Hostname of the shard to delete. |
| 515 | """ |
| 516 | shard = rpc_utils.retrieve_shard(shard_hostname=hostname) |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 517 | hostnames_to_lock = [h.hostname for h in |
| 518 | models.Host.objects.filter(shard=shard, locked=False)] |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 519 | |
| 520 | # TODO(beeps): Power off shard |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 521 | # For ChromeOS hosts, a reboot test with the highest priority is added to |
| 522 | # the DUT. After a reboot it should be ganranteed that no processes from |
| 523 | # prior tests that were run by a shard are still running on. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 524 | |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 525 | # Lock all unlocked hosts. |
| 526 | dicts = {'locked': True, 'lock_time': datetime.datetime.now()} |
| 527 | models.Host.objects.filter(hostname__in=hostnames_to_lock).update(**dicts) |
| 528 | |
| 529 | # Remove shard information. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 530 | models.Host.objects.filter(shard=shard).update(shard=None) |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 531 | models.Job.objects.filter(shard=shard).update(shard=None) |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 532 | shard.labels.clear() |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 533 | shard.delete() |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 534 | |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 535 | # Assign a reboot task with highest priority: Super. |
| 536 | t = models.Test.objects.get(name='platform_BootPerfServer:shard') |
| 537 | c = utils.read_file(os.path.join(common.autotest_dir, t.path)) |
| 538 | if hostnames_to_lock: |
| 539 | rpc_utils.create_job_common( |
| 540 | 'reboot_dut_for_shard_deletion', |
| 541 | priority=priorities.Priority.SUPER, |
| 542 | control_type='Server', |
| 543 | control_file=c, hosts=hostnames_to_lock) |
| 544 | |
| 545 | # Unlock these shard-related hosts. |
| 546 | dicts = {'locked': False, 'lock_time': None} |
| 547 | models.Host.objects.filter(hostname__in=hostnames_to_lock).update(**dicts) |
| 548 | |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 549 | |
MK Ryu | a34e3b1 | 2015-08-21 16:20:47 -0700 | [diff] [blame] | 550 | def get_servers(hostname=None, role=None, status=None): |
Dan Shi | d7bb4f1 | 2015-01-06 10:53:50 -0800 | [diff] [blame] | 551 | """Get a list of servers with matching role and status. |
| 552 | |
MK Ryu | a34e3b1 | 2015-08-21 16:20:47 -0700 | [diff] [blame] | 553 | @param hostname: FQDN of the server. |
Dan Shi | d7bb4f1 | 2015-01-06 10:53:50 -0800 | [diff] [blame] | 554 | @param role: Name of the server role, e.g., drone, scheduler. Default to |
| 555 | None to match any role. |
| 556 | @param status: Status of the server, e.g., primary, backup, repair_required. |
| 557 | Default to None to match any server status. |
| 558 | |
| 559 | @raises error.RPCException: If server database is not used. |
| 560 | @return: A list of server names for servers with matching role and status. |
| 561 | """ |
| 562 | if not server_manager_utils.use_server_db(): |
| 563 | raise error.RPCException('Server database is not enabled. Please try ' |
| 564 | 'retrieve servers from global config.') |
MK Ryu | a34e3b1 | 2015-08-21 16:20:47 -0700 | [diff] [blame] | 565 | servers = server_manager_utils.get_servers(hostname=hostname, role=role, |
Dan Shi | d7bb4f1 | 2015-01-06 10:53:50 -0800 | [diff] [blame] | 566 | status=status) |
| 567 | return [s.get_details() for s in servers] |
| 568 | |
| 569 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 570 | @rpc_utils.route_rpc_to_master |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 571 | def get_stable_version(board=stable_version_utils.DEFAULT, android=False): |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 572 | """Get stable version for the given board. |
| 573 | |
| 574 | @param board: Name of the board. |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 575 | @param android: If True, the given board is an Android-based device. If |
| 576 | False, assume its a Chrome OS-based device. |
| 577 | |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 578 | @return: Stable version of the given board. Return global configure value |
| 579 | of CROS.stable_cros_version if stable_versinos table does not have |
| 580 | entry of board DEFAULT. |
| 581 | """ |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 582 | return stable_version_utils.get(board=board, android=android) |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 583 | |
| 584 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 585 | @rpc_utils.route_rpc_to_master |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 586 | def get_all_stable_versions(): |
| 587 | """Get stable versions for all boards. |
| 588 | |
| 589 | @return: A dictionary of board:version. |
| 590 | """ |
| 591 | return stable_version_utils.get_all() |
| 592 | |
| 593 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 594 | @rpc_utils.route_rpc_to_master |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 595 | def set_stable_version(version, board=stable_version_utils.DEFAULT): |
| 596 | """Modify stable version for the given board. |
| 597 | |
| 598 | @param version: The new value of stable version for given board. |
| 599 | @param board: Name of the board, default to value `DEFAULT`. |
| 600 | """ |
| 601 | stable_version_utils.set(version=version, board=board) |
| 602 | |
| 603 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 604 | @rpc_utils.route_rpc_to_master |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 605 | def delete_stable_version(board): |
| 606 | """Modify stable version for the given board. |
| 607 | |
| 608 | Delete a stable version entry in afe_stable_versions table for a given |
| 609 | board, so default stable version will be used. |
| 610 | |
| 611 | @param board: Name of the board. |
| 612 | """ |
| 613 | stable_version_utils.delete(board=board) |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 614 | |
| 615 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 616 | def _initialize_control_file_getter(build): |
| 617 | """Get the remote control file getter. |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 618 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 619 | @param build: unique name by which to refer to a remote build image. |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 620 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 621 | @return: A control file getter object. |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 622 | """ |
| 623 | # Stage the test artifacts. |
| 624 | try: |
| 625 | ds = dev_server.ImageServer.resolve(build) |
| 626 | build = ds.translate(build) |
| 627 | except dev_server.DevServerException as e: |
| 628 | raise ValueError('Could not resolve build %s: %s' % (build, e)) |
| 629 | |
| 630 | try: |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 631 | ds.stage_artifacts(image=build, artifacts=['test_suites']) |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 632 | except dev_server.DevServerException as e: |
| 633 | raise error.StageControlFileFailure( |
| 634 | 'Failed to stage %s: %s' % (build, e)) |
| 635 | |
| 636 | # Collect the control files specified in this build |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 637 | return control_file_getter.DevServerGetter.create(build, ds) |
| 638 | |
| 639 | |
| 640 | def get_tests_by_build(build, ignore_invalid_tests=True): |
| 641 | """Get the tests that are available for the specified build. |
| 642 | |
| 643 | @param build: unique name by which to refer to the image. |
| 644 | @param ignore_invalid_tests: flag on if unparsable tests are ignored. |
| 645 | |
| 646 | @return: A sorted list of all tests that are in the build specified. |
| 647 | """ |
| 648 | # Collect the control files specified in this build |
| 649 | cfile_getter = _initialize_control_file_getter(build) |
xixuan | 0f7755d | 2016-04-18 14:49:12 -0700 | [diff] [blame] | 650 | if SuiteBase.ENABLE_CONTROLS_IN_BATCH: |
| 651 | control_file_info_list = cfile_getter.get_suite_info() |
| 652 | control_file_list = control_file_info_list.keys() |
| 653 | else: |
| 654 | control_file_list = cfile_getter.get_control_file_list() |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 655 | |
| 656 | test_objects = [] |
| 657 | _id = 0 |
| 658 | for control_file_path in control_file_list: |
| 659 | # Read and parse the control file |
xixuan | 0f7755d | 2016-04-18 14:49:12 -0700 | [diff] [blame] | 660 | if SuiteBase.ENABLE_CONTROLS_IN_BATCH: |
| 661 | control_file = control_file_info_list[control_file_path] |
| 662 | else: |
| 663 | control_file = cfile_getter.get_control_file_contents( |
| 664 | control_file_path) |
Michael Tang | 340efe3 | 2016-04-16 12:15:17 -0700 | [diff] [blame] | 665 | try: |
| 666 | control_obj = control_data.parse_control_string(control_file) |
| 667 | except: |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 668 | logging.info('Failed to parse control file: %s', control_file_path) |
Michael Tang | 340efe3 | 2016-04-16 12:15:17 -0700 | [diff] [blame] | 669 | if not ignore_invalid_tests: |
| 670 | raise |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 671 | |
| 672 | # Extract the values needed for the AFE from the control_obj. |
| 673 | # The keys list represents attributes in the control_obj that |
| 674 | # are required by the AFE |
| 675 | keys = ['author', 'doc', 'name', 'time', 'test_type', 'experimental', |
| 676 | 'test_category', 'test_class', 'dependencies', 'run_verify', |
| 677 | 'sync_count', 'job_retries', 'retries', 'path'] |
| 678 | |
| 679 | test_object = {} |
| 680 | for key in keys: |
| 681 | test_object[key] = getattr(control_obj, key) if hasattr( |
| 682 | control_obj, key) else '' |
| 683 | |
| 684 | # Unfortunately, the AFE expects different key-names for certain |
| 685 | # values, these must be corrected to avoid the risk of tests |
| 686 | # being omitted by the AFE. |
| 687 | # The 'id' is an additional value used in the AFE. |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 688 | # The control_data parsing does not reference 'run_reset', but it |
| 689 | # is also used in the AFE and defaults to True. |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 690 | test_object['id'] = _id |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 691 | test_object['run_reset'] = True |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 692 | test_object['description'] = test_object.get('doc', '') |
| 693 | test_object['test_time'] = test_object.get('time', 0) |
| 694 | test_object['test_retry'] = test_object.get('retries', 0) |
| 695 | |
| 696 | # Fix the test name to be consistent with the current presentation |
| 697 | # of test names in the AFE. |
| 698 | testpath, subname = os.path.split(control_file_path) |
| 699 | testname = os.path.basename(testpath) |
| 700 | subname = subname.split('.')[1:] |
| 701 | if subname: |
| 702 | testname = '%s:%s' % (testname, ':'.join(subname)) |
| 703 | |
| 704 | test_object['name'] = testname |
| 705 | |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 706 | # Correct the test path as parse_control_string sets an empty string. |
| 707 | test_object['path'] = control_file_path |
| 708 | |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 709 | _id += 1 |
| 710 | test_objects.append(test_object) |
| 711 | |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 712 | test_objects = sorted(test_objects, key=lambda x: x.get('name')) |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 713 | return rpc_utils.prepare_for_serialization(test_objects) |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 714 | |
| 715 | |
| 716 | def get_test_control_files_by_build(tests, build, ignore_invalid_tests=False): |
| 717 | """Get the test control files that are available for the specified build. |
| 718 | |
| 719 | @param tests A sequence of test objects to run. |
| 720 | @param build: unique name by which to refer to the image. |
| 721 | @param ignore_invalid_tests: flag on if unparsable tests are ignored. |
| 722 | |
| 723 | @return: A sorted list of all tests that are in the build specified. |
| 724 | """ |
| 725 | raw_control_files = [] |
| 726 | # shortcut to avoid staging the image. |
| 727 | if not tests: |
| 728 | return raw_control_files |
| 729 | |
| 730 | cfile_getter = _initialize_control_file_getter(build) |
| 731 | if SuiteBase.ENABLE_CONTROLS_IN_BATCH: |
| 732 | control_file_info_list = cfile_getter.get_suite_info() |
| 733 | |
| 734 | for test in tests: |
| 735 | # Read and parse the control file |
| 736 | if SuiteBase.ENABLE_CONTROLS_IN_BATCH: |
| 737 | control_file = control_file_info_list[test.path] |
| 738 | else: |
| 739 | control_file = cfile_getter.get_control_file_contents( |
| 740 | test.path) |
| 741 | raw_control_files.append(control_file) |
| 742 | return raw_control_files |