blob: b1a9d3764cce44de09cde013bf6194d31da7207b [file] [log] [blame]
Paul Pendleburyf807c182011-04-05 11:24:34 -07001# 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 Pendlebury57593562011-06-15 10:45:49 -07005"""Utility classes used by site_server_job.distribute_across_machines().
Paul Pendleburyf807c182011-04-05 11:24:34 -07006
Paul Pendlebury7c1fdcf2011-05-04 12:39:15 -07007test_item: extends the basic test tuple to add include/exclude attributes and
8 pre/post actions.
Paul Pendleburyf807c182011-04-05 11:24:34 -07009
10machine_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 Pendlebury57593562011-06-15 10:45:49 -070016import logging, os, Queue
Paul Pendleburyf807c182011-04-05 11:24:34 -070017from autotest_lib.client.common_lib import error, utils
Paul Pendlebury57593562011-06-15 10:45:49 -070018from autotest_lib.server import autotest, hosts, host_attributes
Paul Pendleburyf807c182011-04-05 11:24:34 -070019
20
21class 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 Pendlebury57593562011-06-15 10:45:49 -070025 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 Pendleburyf807c182011-04-05 11:24:34 -070031 """
32
Dale Curtise3c43492011-07-13 10:54:45 -070033 def __init__(self, test_name, test_args, test_attribs=None):
Paul Pendleburyf807c182011-04-05 11:24:34 -070034 """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 Pendlebury57593562011-06-15 10:45:49 -070039 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 Pendleburyf807c182011-04-05 11:24:34 -070043 """
44 self.test_name = test_name
45 self.test_args = test_args
Paul Pendleburycfc0d6a2011-06-10 08:31:02 -070046
Dale Curtise3c43492011-07-13 10:54:45 -070047 if test_attribs is None:
48 test_attribs = {}
Paul Pendlebury57593562011-06-15 10:45:49 -070049 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 Pendleburyf807c182011-04-05 11:24:34 -070052
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 Pendleburycfc0d6a2011-06-10 08:31:02 -070057 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 Pendlebury57593562011-06-15 10:45:49 -070061 if self.attributes:
62 msg += ' attributes=%s' % self.attributes
Paul Pendleburyf807c182011-04-05 11:24:34 -070063 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 Pendleburycfc0d6a2011-06-10 08:31:02 -070081 if not self.inc_set <= machine_attributes:
82 return False
Paul Pendleburyf807c182011-04-05 11:24:34 -070083 if self.exc_set is not None:
Paul Pendleburycfc0d6a2011-06-10 08:31:02 -070084 if self.exc_set & machine_attributes:
85 return False
Paul Pendleburyf807c182011-04-05 11:24:34 -070086 return True
87
Paul Pendlebury7739b432011-06-17 09:31:57 -070088 def run_test(self, client_at, work_dir='.', server_job=None):
Paul Pendlebury7c1fdcf2011-05-04 12:39:15 -070089 """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 Pendlebury57593562011-06-15 10:45:49 -070094 server_job: Server_Job instance to use to runs server tests.
Paul Pendlebury7c1fdcf2011-05-04 12:39:15 -070095 """
Paul Pendlebury57593562011-06-15 10:45:49 -070096 if 'reboot_before' in self.attributes:
Paul Pendlebury7c1fdcf2011-05-04 12:39:15 -070097 client_at.host.reboot()
98
Paul Pendlebury92a90742011-05-25 11:54:34 -070099 try:
Paul Pendlebury57593562011-06-15 10:45:49 -0700100 if 'server_job' in self.attributes:
Paul Pendlebury7739b432011-06-17 09:31:57 -0700101 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 Pendlebury92a90742011-05-25 11:54:34 -0700112 finally:
Paul Pendlebury57593562011-06-15 10:45:49 -0700113 if 'reboot_after' in self.attributes:
Paul Pendlebury92a90742011-05-25 11:54:34 -0700114 client_at.host.reboot()
Paul Pendlebury7c1fdcf2011-05-04 12:39:15 -0700115
Paul Pendleburyf807c182011-04-05 11:24:34 -0700116
Paul Pendlebury57593562011-06-15 10:45:49 -0700117class machine_worker(object):
118 """Worker that runs tests on a remote host machine."""
Paul Pendleburyf807c182011-04-05 11:24:34 -0700119
Paul Pendleburye4afc772011-04-26 11:20:15 -0700120 def __init__(self, server_job, machine, work_dir, test_queue, queue_lock,
121 continuous_parsing=False):
Paul Pendleburyf807c182011-04-05 11:24:34 -0700122 """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 Pendlebury1f6f3e72011-04-13 11:16:44 -0700131 server_job: run tests for this server_job.
Paul Pendleburyf807c182011-04-05 11:24:34 -0700132 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 Pendleburye4afc772011-04-26 11:20:15 -0700136 continuous_parsing: bool, enable continuous parsing.
Paul Pendleburyf807c182011-04-05 11:24:34 -0700137 """
Paul Pendlebury1f6f3e72011-04-13 11:16:44 -0700138 self._server_job = server_job
Paul Pendleburyf807c182011-04-05 11:24:34 -0700139 self._test_queue = test_queue
140 self._test_queue_lock = queue_lock
Paul Pendleburye4afc772011-04-26 11:20:15 -0700141 self._continuous_parsing = continuous_parsing
Paul Pendleburyf807c182011-04-05 11:24:34 -0700142 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 Pendlebury7c1fdcf2011-05-04 12:39:15 -0700147 self.attribute_set = set(client_attributes.get_attributes())
Paul Pendlebury3dd080e2011-05-06 12:30:32 -0700148 self._results_dir = work_dir
Paul Pendlebury57593562011-06-15 10:45:49 -0700149 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 Pendleburyf807c182011-04-05 11:24:34 -0700154
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 Pendleburye4afc772011-04-26 11:20:15 -0700194 def run(self):
Paul Pendleburye4afc772011-04-26 11:20:15 -0700195 """Executes tests on the host machine.
Paul Pendlebury1f6f3e72011-04-13 11:16:44 -0700196
Paul Pendleburye4afc772011-04-26 11:20:15 -0700197 If continuous parsing was requested, start the parser before running
198 tests.
Paul Pendlebury1f6f3e72011-04-13 11:16:44 -0700199 """
Dale Curtis1ddf9e52011-08-05 13:10:58 -0700200 # 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 Pendleburye4afc772011-04-26 11:20:15 -0700206 if self._continuous_parsing:
207 self._server_job._parse_job += "/" + self._machine
208 self._server_job._using_parser = True
Paul Pendleburye4afc772011-04-26 11:20:15 -0700209 self._server_job.init_parser()
Paul Pendleburyf807c182011-04-05 11:24:34 -0700210
Paul Pendleburyf807c182011-04-05 11:24:34 -0700211 while True:
212 active_test = self.get_test()
213 if active_test is None:
214 break
215
Paul Pendleburyf807c182011-04-05 11:24:34 -0700216 logging.info('%s running %s', self._machine, active_test)
217 try:
Paul Pendlebury7739b432011-06-17 09:31:57 -0700218 active_test.run_test(self._client_at, self._results_dir,
219 self._server_job)
Paul Pendlebury92a90742011-05-25 11:54:34 -0700220 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 Pendleburyf807c182011-04-05 11:24:34 -0700224 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 Pendleburye4afc772011-04-26 11:20:15 -0700231 if self._continuous_parsing:
232 self._server_job.cleanup_parser()
Paul Pendleburyf807c182011-04-05 11:24:34 -0700233 logging.info('%s completed %d tests.', self._machine, self._tests_run)