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 | |
Prathmesh Prabhu | 20ea106 | 2017-07-06 10:23:42 -0700 | [diff] [blame] | 5 | """Utility classes used by 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 | |
| 11 | |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 12 | import logging, os, Queue |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 13 | from autotest_lib.client.common_lib import error, utils |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 14 | from autotest_lib.server import autotest, hosts, host_attributes |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 15 | |
| 16 | |
| 17 | class test_item(object): |
| 18 | """Adds machine verification logic to the basic test tuple. |
| 19 | |
| 20 | 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] | 21 | extended form ('testname', {args}, {'include': [], 'exclude': [], |
| 22 | 'attributes': []}) where include and exclude are lists of host attribute |
| 23 | labels and attributes is a list of strings. A machine must have all the |
| 24 | labels in include and must not have any of the labels in exclude to be valid |
| 25 | for the test. Attributes strings can include reboot_before, reboot_after, |
| 26 | and server_job. |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 27 | """ |
| 28 | |
Dale Curtis | e3c4349 | 2011-07-13 10:54:45 -0700 | [diff] [blame] | 29 | def __init__(self, test_name, test_args, test_attribs=None): |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 30 | """Creates an instance of test_item. |
| 31 | |
| 32 | Args: |
| 33 | test_name: string, name of test to execute. |
| 34 | test_args: dictionary, arguments to pass into test. |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 35 | test_attribs: Dictionary of test attributes. Valid keys are: |
| 36 | include - labels a machine must have to run a test. |
| 37 | exclude - labels preventing a machine from running a test. |
| 38 | attributes - reboot before/after test, run test as server job. |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 39 | """ |
| 40 | self.test_name = test_name |
| 41 | self.test_args = test_args |
Nirnimesh | 717b092 | 2011-11-09 12:03:48 -0800 | [diff] [blame] | 42 | self.tagged_test_name = test_name |
| 43 | if test_args.get('tag'): |
| 44 | self.tagged_test_name = test_name + '.' + test_args.get('tag') |
Paul Pendlebury | cfc0d6a | 2011-06-10 08:31:02 -0700 | [diff] [blame] | 45 | |
Dale Curtis | e3c4349 | 2011-07-13 10:54:45 -0700 | [diff] [blame] | 46 | if test_attribs is None: |
| 47 | test_attribs = {} |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 48 | self.inc_set = set(test_attribs.get('include', [])) |
| 49 | self.exc_set = set(test_attribs.get('exclude', [])) |
| 50 | self.attributes = test_attribs.get('attributes', []) |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 51 | |
| 52 | def __str__(self): |
| 53 | """Return an info string of this test.""" |
| 54 | params = ['%s=%s' % (k, v) for k, v in self.test_args.items()] |
| 55 | msg = '%s(%s)' % (self.test_name, params) |
Paul Pendlebury | cfc0d6a | 2011-06-10 08:31:02 -0700 | [diff] [blame] | 56 | if self.inc_set: |
| 57 | msg += ' include=%s' % [s for s in self.inc_set] |
| 58 | if self.exc_set: |
| 59 | msg += ' exclude=%s' % [s for s in self.exc_set] |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 60 | if self.attributes: |
| 61 | msg += ' attributes=%s' % self.attributes |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 62 | return msg |
| 63 | |
| 64 | def validate(self, machine_attributes): |
| 65 | """Check if this test can run on machine with machine_attributes. |
| 66 | |
| 67 | If the test has include attributes, a candidate machine must have all |
| 68 | the attributes to be valid. |
| 69 | |
| 70 | If the test has exclude attributes, a candidate machine cannot have any |
| 71 | of the attributes to be valid. |
| 72 | |
| 73 | Args: |
| 74 | machine_attributes: set, True attributes of candidate machine. |
| 75 | |
| 76 | Returns: |
| 77 | True/False if the machine is valid for this test. |
| 78 | """ |
| 79 | if self.inc_set is not None: |
Paul Pendlebury | cfc0d6a | 2011-06-10 08:31:02 -0700 | [diff] [blame] | 80 | if not self.inc_set <= machine_attributes: |
| 81 | return False |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 82 | if self.exc_set is not None: |
Paul Pendlebury | cfc0d6a | 2011-06-10 08:31:02 -0700 | [diff] [blame] | 83 | if self.exc_set & machine_attributes: |
| 84 | return False |
Paul Pendlebury | f807c18 | 2011-04-05 11:24:34 -0700 | [diff] [blame] | 85 | return True |
| 86 | |
Paul Pendlebury | 7739b43 | 2011-06-17 09:31:57 -0700 | [diff] [blame] | 87 | def run_test(self, client_at, work_dir='.', server_job=None): |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 88 | """Runs the test on the client using autotest. |
| 89 | |
| 90 | Args: |
| 91 | client_at: Autotest instance for this host. |
| 92 | work_dir: Directory to use for results and log files. |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 93 | server_job: Server_Job instance to use to runs server tests. |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 94 | """ |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 95 | if 'reboot_before' in self.attributes: |
Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 96 | client_at.host.reboot() |
| 97 | |
Paul Pendlebury | 92a9074 | 2011-05-25 11:54:34 -0700 | [diff] [blame] | 98 | try: |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 99 | if 'server_job' in self.attributes: |
Paul Pendlebury | 7739b43 | 2011-06-17 09:31:57 -0700 | [diff] [blame] | 100 | if 'host' in self.test_args: |
| 101 | self.test_args['host'] = client_at.host |
| 102 | if server_job is not None: |
| 103 | logging.info('Running Server_Job=%s', self.test_name) |
| 104 | server_job.run_test(self.test_name, **self.test_args) |
| 105 | else: |
| 106 | logging.error('No Server_Job instance provided for test ' |
| 107 | '%s.', self.test_name) |
| 108 | else: |
| 109 | client_at.run_test(self.test_name, results_dir=work_dir, |
| 110 | **self.test_args) |
Paul Pendlebury | 92a9074 | 2011-05-25 11:54:34 -0700 | [diff] [blame] | 111 | finally: |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 112 | if 'reboot_after' in self.attributes: |
Paul Pendlebury | 92a9074 | 2011-05-25 11:54:34 -0700 | [diff] [blame] | 113 | client_at.host.reboot() |