blob: e9bd3989cf44c54feb4e9412a3e3f20c8c270e80 [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
Prathmesh Prabhu20ea1062017-07-06 10:23:42 -07005"""Utility classes used by 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"""
10
11
Paul Pendlebury57593562011-06-15 10:45:49 -070012import logging, os, Queue
Paul Pendleburyf807c182011-04-05 11:24:34 -070013from autotest_lib.client.common_lib import error, utils
Paul Pendlebury57593562011-06-15 10:45:49 -070014from autotest_lib.server import autotest, hosts, host_attributes
Paul Pendleburyf807c182011-04-05 11:24:34 -070015
16
17class 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 Pendlebury57593562011-06-15 10:45:49 -070021 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 Pendleburyf807c182011-04-05 11:24:34 -070027 """
28
Dale Curtise3c43492011-07-13 10:54:45 -070029 def __init__(self, test_name, test_args, test_attribs=None):
Paul Pendleburyf807c182011-04-05 11:24:34 -070030 """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 Pendlebury57593562011-06-15 10:45:49 -070035 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 Pendleburyf807c182011-04-05 11:24:34 -070039 """
40 self.test_name = test_name
41 self.test_args = test_args
Nirnimesh717b0922011-11-09 12:03:48 -080042 self.tagged_test_name = test_name
43 if test_args.get('tag'):
44 self.tagged_test_name = test_name + '.' + test_args.get('tag')
Paul Pendleburycfc0d6a2011-06-10 08:31:02 -070045
Dale Curtise3c43492011-07-13 10:54:45 -070046 if test_attribs is None:
47 test_attribs = {}
Paul Pendlebury57593562011-06-15 10:45:49 -070048 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 Pendleburyf807c182011-04-05 11:24:34 -070051
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 Pendleburycfc0d6a2011-06-10 08:31:02 -070056 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 Pendlebury57593562011-06-15 10:45:49 -070060 if self.attributes:
61 msg += ' attributes=%s' % self.attributes
Paul Pendleburyf807c182011-04-05 11:24:34 -070062 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 Pendleburycfc0d6a2011-06-10 08:31:02 -070080 if not self.inc_set <= machine_attributes:
81 return False
Paul Pendleburyf807c182011-04-05 11:24:34 -070082 if self.exc_set is not None:
Paul Pendleburycfc0d6a2011-06-10 08:31:02 -070083 if self.exc_set & machine_attributes:
84 return False
Paul Pendleburyf807c182011-04-05 11:24:34 -070085 return True
86
Paul Pendlebury7739b432011-06-17 09:31:57 -070087 def run_test(self, client_at, work_dir='.', server_job=None):
Paul Pendlebury7c1fdcf2011-05-04 12:39:15 -070088 """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 Pendlebury57593562011-06-15 10:45:49 -070093 server_job: Server_Job instance to use to runs server tests.
Paul Pendlebury7c1fdcf2011-05-04 12:39:15 -070094 """
Paul Pendlebury57593562011-06-15 10:45:49 -070095 if 'reboot_before' in self.attributes:
Paul Pendlebury7c1fdcf2011-05-04 12:39:15 -070096 client_at.host.reboot()
97
Paul Pendlebury92a90742011-05-25 11:54:34 -070098 try:
Paul Pendlebury57593562011-06-15 10:45:49 -070099 if 'server_job' in self.attributes:
Paul Pendlebury7739b432011-06-17 09:31:57 -0700100 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 Pendlebury92a90742011-05-25 11:54:34 -0700111 finally:
Paul Pendlebury57593562011-06-15 10:45:49 -0700112 if 'reboot_after' in self.attributes:
Paul Pendlebury92a90742011-05-25 11:54:34 -0700113 client_at.host.reboot()