• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Utility classes used by server_job.distribute_across_machines().
7
8test_item: extends the basic test tuple to add include/exclude attributes and
9    pre/post actions.
10"""
11
12
13import logging, os, six.moves.queue
14from autotest_lib.client.common_lib import error, utils
15from autotest_lib.server import autotest, hosts, host_attributes
16
17
18class test_item(object):
19    """Adds machine verification logic to the basic test tuple.
20
21    Tests can either be tuples of the existing form ('testName', {args}) or the
22    extended form ('testname', {args}, {'include': [], 'exclude': [],
23    'attributes': []}) where include and exclude are lists of host attribute
24    labels and attributes is a list of strings. A machine must have all the
25    labels in include and must not have any of the labels in exclude to be valid
26    for the test. Attributes strings can include reboot_before, reboot_after,
27    and server_job.
28    """
29
30    def __init__(self, test_name, test_args, test_attribs=None):
31        """Creates an instance of test_item.
32
33        Args:
34            test_name: string, name of test to execute.
35            test_args: dictionary, arguments to pass into test.
36            test_attribs: Dictionary of test attributes. Valid keys are:
37              include - labels a machine must have to run a test.
38              exclude - labels preventing a machine from running a test.
39              attributes - reboot before/after test, run test as server job.
40        """
41        self.test_name = test_name
42        self.test_args = test_args
43        self.tagged_test_name = test_name
44        if test_args.get('tag'):
45            self.tagged_test_name = test_name + '.' + test_args.get('tag')
46
47        if test_attribs is None:
48            test_attribs = {}
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', [])
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)
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]
61        if self.attributes:
62            msg += ' attributes=%s' % self.attributes
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:
81            if not self.inc_set <= machine_attributes:
82                return False
83        if self.exc_set is not None:
84            if self.exc_set & machine_attributes:
85                return False
86        return True
87
88    def run_test(self, client_at, work_dir='.', server_job=None):
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.
94            server_job: Server_Job instance to use to runs server tests.
95        """
96        if 'reboot_before' in self.attributes:
97            client_at.host.reboot()
98
99        try:
100            if 'server_job' in self.attributes:
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)
112        finally:
113            if 'reboot_after' in self.attributes:
114                client_at.host.reboot()
115