Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 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 | |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 5 | """Utility classes used by site_server_job.distribute_across_machines(). |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 6 | |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 7 | test_item: extends the basic test tuple to add include/exclude attributes and |
| 8 | pre/post actions. |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 9 | |
| 10 | machine_worker: is a thread that manages running tests on a host. It |
| 11 | verifies test are valid for a host using the test attributes from test_item |
| 12 | and the host attributes from host_attributes. |
| 13 | """ |
| 14 | |
| 15 | |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 16 | import logging, os, Queue |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import error, utils |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 18 | from autotest_lib.server import autotest, hosts, host_attributes |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | class test_item(object): |
| 22 | """Adds machine verification logic to the basic test tuple. |
| 23 | |
| 24 | Tests can either be tuples of the existing form ('testName', {args}) or the |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 25 | extended form ('testname', {args}, {'include': [], 'exclude': [], |
| 26 | 'attributes': []}) where include and exclude are lists of host attribute |
| 27 | labels and attributes is a list of strings. A machine must have all the |
| 28 | labels in include and must not have any of the labels in exclude to be valid |
| 29 | for the test. Attributes strings can include reboot_before, reboot_after, |
| 30 | and server_job. |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 31 | """ |
| 32 | |
Dale Curtis | e3c4349 | 2011-07-13 10:54:45 -0700 | [diff] [blame] | 33 | def __init__(self, test_name, test_args, test_attribs=None): |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 34 | """Creates an instance of test_item. |
| 35 | |
| 36 | Args: |
| 37 | test_name: string, name of test to execute. |
| 38 | test_args: dictionary, arguments to pass into test. |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 39 | test_attribs: Dictionary of test attributes. Valid keys are: |
| 40 | include - labels a machine must have to run a test. |
| 41 | exclude - labels preventing a machine from running a test. |
| 42 | attributes - reboot before/after test, run test as server job. |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 43 | """ |
| 44 | self.test_name = test_name |
| 45 | self.test_args = test_args |
Paul Pendlebury | cfc0d6a | 2011-06-10 08:31:02 -0700 | [diff] [blame] | 46 | |
Dale Curtis | e3c4349 | 2011-07-13 10:54:45 -0700 | [diff] [blame] | 47 | if test_attribs is None: |
| 48 | test_attribs = {} |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 49 | self.inc_set = set(test_attribs.get('include', [])) |
| 50 | self.exc_set = set(test_attribs.get('exclude', [])) |
| 51 | self.attributes = test_attribs.get('attributes', []) |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 52 | |
| 53 | def __str__(self): |
| 54 | """Return an info string of this test.""" |
| 55 | params = ['%s=%s' % (k, v) for k, v in self.test_args.items()] |
| 56 | msg = '%s(%s)' % (self.test_name, params) |
Paul Pendlebury | cfc0d6a | 2011-06-10 08:31:02 -0700 | [diff] [blame] | 57 | if self.inc_set: |
| 58 | msg += ' include=%s' % [s for s in self.inc_set] |
| 59 | if self.exc_set: |
| 60 | msg += ' exclude=%s' % [s for s in self.exc_set] |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 61 | if self.attributes: |
| 62 | msg += ' attributes=%s' % self.attributes |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 63 | return msg |
| 64 | |
| 65 | def validate(self, machine_attributes): |
| 66 | """Check if this test can run on machine with machine_attributes. |
| 67 | |
| 68 | If the test has include attributes, a candidate machine must have all |
| 69 | the attributes to be valid. |
| 70 | |
| 71 | If the test has exclude attributes, a candidate machine cannot have any |
| 72 | of the attributes to be valid. |
| 73 | |
| 74 | Args: |
| 75 | machine_attributes: set, True attributes of candidate machine. |
| 76 | |
| 77 | Returns: |
| 78 | True/False if the machine is valid for this test. |
| 79 | """ |
| 80 | if self.inc_set is not None: |
Paul Pendlebury | cfc0d6a | 2011-06-10 08:31:02 -0700 | [diff] [blame] | 81 | if not self.inc_set <= machine_attributes: |
| 82 | return False |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 83 | if self.exc_set is not None: |
Paul Pendlebury | cfc0d6a | 2011-06-10 08:31:02 -0700 | [diff] [blame] | 84 | if self.exc_set & machine_attributes: |
| 85 | return False |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 86 | return True |
| 87 | |
Paul Pendlebury | 7739b43 | 2011-06-17 09:31:57 -0700 | [diff] [blame] | 88 | def run_test(self, client_at, work_dir='.', server_job=None): |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 89 | """Runs the test on the client using autotest. |
| 90 | |
| 91 | Args: |
| 92 | client_at: Autotest instance for this host. |
| 93 | work_dir: Directory to use for results and log files. |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 94 | server_job: Server_Job instance to use to runs server tests. |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 95 | """ |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 96 | if 'reboot_before' in self.attributes: |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 97 | client_at.host.reboot() |
| 98 | |
Paul Pendlebury | 92a9074 | 2011-05-25 11:54:34 -0700 | [diff] [blame] | 99 | try: |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 100 | if 'server_job' in self.attributes: |
Paul Pendlebury | 7739b43 | 2011-06-17 09:31:57 -0700 | [diff] [blame] | 101 | if 'host' in self.test_args: |
| 102 | self.test_args['host'] = client_at.host |
| 103 | if server_job is not None: |
| 104 | logging.info('Running Server_Job=%s', self.test_name) |
| 105 | server_job.run_test(self.test_name, **self.test_args) |
| 106 | else: |
| 107 | logging.error('No Server_Job instance provided for test ' |
| 108 | '%s.', self.test_name) |
| 109 | else: |
| 110 | client_at.run_test(self.test_name, results_dir=work_dir, |
| 111 | **self.test_args) |
Paul Pendlebury | 92a9074 | 2011-05-25 11:54:34 -0700 | [diff] [blame] | 112 | finally: |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 113 | if 'reboot_after' in self.attributes: |
Paul Pendlebury | 92a9074 | 2011-05-25 11:54:34 -0700 | [diff] [blame] | 114 | client_at.host.reboot() |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 115 | |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 116 | |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 117 | class machine_worker(object): |
| 118 | """Worker that runs tests on a remote host machine.""" |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 119 | |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 120 | def __init__(self, server_job, machine, work_dir, test_queue, queue_lock, |
| 121 | continuous_parsing=False): |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 122 | """Creates an instance of machine_worker to run tests on a remote host. |
| 123 | |
| 124 | Retrieves that host attributes for this machine and creates the set of |
| 125 | True attributes to validate against test include/exclude attributes. |
| 126 | |
| 127 | Creates a directory to hold the log files for tests run and writes the |
| 128 | hostname and tko parser version into keyvals file. |
| 129 | |
| 130 | Args: |
Paul Pendlebury | 1f6f3e7 | 2011-04-13 11:16:44 -0700 | [diff] [blame] | 131 | server_job: run tests for this server_job. |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 132 | machine: name of remote host. |
| 133 | work_dir: directory server job is using. |
| 134 | test_queue: queue of tests. |
| 135 | queue_lock: lock protecting test_queue. |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 136 | continuous_parsing: bool, enable continuous parsing. |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 137 | """ |
Paul Pendlebury | 1f6f3e7 | 2011-04-13 11:16:44 -0700 | [diff] [blame] | 138 | self._server_job = server_job |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 139 | self._test_queue = test_queue |
| 140 | self._test_queue_lock = queue_lock |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 141 | self._continuous_parsing = continuous_parsing |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 142 | self._tests_run = 0 |
| 143 | self._machine = machine |
| 144 | self._host = hosts.create_host(self._machine) |
| 145 | self._client_at = autotest.Autotest(self._host) |
| 146 | client_attributes = host_attributes.host_attributes(machine) |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 147 | self.attribute_set = set(client_attributes.get_attributes()) |
Paul Pendlebury | 3dd080e | 2011-05-06 12:30:32 -0700 | [diff] [blame] | 148 | self._results_dir = work_dir |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 149 | if not os.path.exists(self._results_dir): |
| 150 | os.makedirs(self._results_dir) |
| 151 | machine_data = {'hostname': self._machine, |
| 152 | 'status_version': str(1)} |
| 153 | utils.write_keyval(self._results_dir, machine_data) |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 154 | |
| 155 | def __str__(self): |
| 156 | attributes = [a for a in self.attribute_set] |
| 157 | return '%s attributes=%s' % (self._machine, attributes) |
| 158 | |
| 159 | def get_test(self): |
| 160 | """Return a test from the queue to run on this host. |
| 161 | |
| 162 | The test queue can be non-empty, but still not contain a test that is |
| 163 | valid for this machine. This function will take exclusive access to |
| 164 | the queue via _test_queue_lock and repeatedly pop tests off the queue |
| 165 | until finding a valid test or depleting the queue. In either case if |
| 166 | invalid tests have been popped from the queue, they are pushed back |
| 167 | onto the queue before returning. |
| 168 | |
| 169 | Returns: |
| 170 | test_item, or None if no more tests exist for this machine. |
| 171 | """ |
| 172 | good_test = None |
| 173 | skipped_tests = [] |
| 174 | |
| 175 | with self._test_queue_lock: |
| 176 | while True: |
| 177 | try: |
| 178 | canidate_test = self._test_queue.get_nowait() |
| 179 | # Check if test is valid for this machine. |
| 180 | if canidate_test.validate(self.attribute_set): |
| 181 | good_test = canidate_test |
| 182 | break |
| 183 | skipped_tests.append(canidate_test) |
| 184 | |
| 185 | except Queue.Empty: |
| 186 | break |
| 187 | |
| 188 | # Return any skipped tests to the queue. |
| 189 | for st in skipped_tests: |
| 190 | self._test_queue.put(st) |
| 191 | |
| 192 | return good_test |
| 193 | |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 194 | def run(self): |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 195 | """Executes tests on the host machine. |
Paul Pendlebury | 1f6f3e7 | 2011-04-13 11:16:44 -0700 | [diff] [blame] | 196 | |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 197 | If continuous parsing was requested, start the parser before running |
| 198 | tests. |
Paul Pendlebury | 1f6f3e7 | 2011-04-13 11:16:44 -0700 | [diff] [blame] | 199 | """ |
Dale Curtis | 1ddf9e5 | 2011-08-05 13:10:58 -0700 | [diff] [blame^] | 200 | # Modify self.resultdir so that it points to the results directory for |
| 201 | # the machine we're working on. Required so that server jobs will write |
| 202 | # to the proper location. |
| 203 | self._server_job.machines = [self._machine] |
| 204 | self._server_job.push_execution_context(self._machine) |
| 205 | os.chdir(self.resultdir) |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 206 | if self._continuous_parsing: |
| 207 | self._server_job._parse_job += "/" + self._machine |
| 208 | self._server_job._using_parser = True |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 209 | self._server_job.init_parser() |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 210 | |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 211 | while True: |
| 212 | active_test = self.get_test() |
| 213 | if active_test is None: |
| 214 | break |
| 215 | |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 216 | logging.info('%s running %s', self._machine, active_test) |
| 217 | try: |
Paul Pendlebury | 7739b43 | 2011-06-17 09:31:57 -0700 | [diff] [blame] | 218 | active_test.run_test(self._client_at, self._results_dir, |
| 219 | self._server_job) |
Paul Pendlebury | 92a9074 | 2011-05-25 11:54:34 -0700 | [diff] [blame] | 220 | except error.AutoservError: |
| 221 | logging.exception('Autoserv error running "%s".', active_test) |
| 222 | except error.AutotestError: |
| 223 | logging.exception('Autotest error running "%s".', active_test) |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 224 | except Exception: |
| 225 | logging.exception('Exception running test "%s".', active_test) |
| 226 | raise |
| 227 | finally: |
| 228 | self._test_queue.task_done() |
| 229 | self._tests_run += 1 |
| 230 | |
Paul Pendlebury | e4afc77 | 2011-04-26 11:20:15 -0700 | [diff] [blame] | 231 | if self._continuous_parsing: |
| 232 | self._server_job.cleanup_parser() |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 233 | logging.info('%s completed %d tests.', self._machine, self._tests_run) |