summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorjadmanski <jadmanski@592f7852-d20e-0410-864c-8624ca9c26a4>2010-07-19 16:41:49 +0000
committerjadmanski <jadmanski@592f7852-d20e-0410-864c-8624ca9c26a4>2010-07-19 16:41:49 +0000
commitf9351552576a0a3d4bf9795ba725aeff4c485e8a (patch)
tree610455c4180f457082aa46946493d08f4d7a0367 /server
parent0130e53cdb8e4c5d68d7bbe55335f8d3a6771d40 (diff)
Add in a run_test mixin for host objects that allows you run a client
test with just host.run_test, instead of having to setup an autotest client directly and generate a control file. Note that since server-client job state is managed automatically, this also supports profilers out of the box. If you do a job.profilers.add before calling host.run_test then the client test will automatically be run with the appropriate profilers. Signed-off-by: John Admanski <jadmanski@google.com> git-svn-id: svn://test.kernel.org/autotest/trunk@4725 592f7852-d20e-0410-864c-8624ca9c26a4
Diffstat (limited to 'server')
-rw-r--r--server/autotest.py24
-rwxr-xr-xserver/autotest_unittest.py41
-rw-r--r--server/hosts/factory.py5
3 files changed, 69 insertions, 1 deletions
diff --git a/server/autotest.py b/server/autotest.py
index a9587d09..5717d6cb 100644
--- a/server/autotest.py
+++ b/server/autotest.py
@@ -1048,3 +1048,27 @@ SiteAutotest = client_utils.import_site_class(
class Autotest(SiteAutotest):
pass
+
+
+class AutotestHostMixin(object):
+ """A generic mixin to add a run_test method to classes, which will allow
+ you to run an autotest client test on a machine directly."""
+
+ # for testing purposes
+ _Autotest = Autotest
+
+ def run_test(self, test_name, **dargs):
+ """Run an autotest client test on the host.
+
+ @param test_name: The name of the client test.
+ @param dargs: Keyword arguments to pass to the test.
+
+ @returns: True if the test passes, False otherwise."""
+ at = self._Autotest()
+ control_file = ('result = job.run_test(%s)\n'
+ 'job.set_state("test_result", result)\n')
+ test_args = [repr(test_name)]
+ test_args += ['%s=%r' % (k, v) for k, v in dargs.iteritems()]
+ control_file %= ', '.join(test_args)
+ at.run(control_file, host=self)
+ return at.job.get_state('test_result', default=False)
diff --git a/server/autotest_unittest.py b/server/autotest_unittest.py
index 90cf2feb..e144540f 100755
--- a/server/autotest_unittest.py
+++ b/server/autotest_unittest.py
@@ -311,6 +311,47 @@ class TestBaseAutotest(unittest.TestCase):
logger._process_line('AUTOTEST_FETCH_PACKAGE:pkgname.tar.bz2:'
'/autotest/dest/:/autotest/fifo3')
+class test_autotest_mixin(unittest.TestCase):
+ def setUp(self):
+ # a dummy Autotest and job class for use in the mixin
+ class stub_autotest(object):
+ class job(object):
+ state_dict = {}
+ def get_state(self, var, default):
+ return self.state_dict.get(var, default)
+ job = job()
+
+ @staticmethod
+ def run(control_file, host=None):
+ self.control_file = control_file
+ self.host = host
+
+ self.mixin = autotest.AutotestHostMixin()
+ self.mixin._Autotest = stub_autotest
+ self.job = self.mixin._Autotest.job
+
+
+ def test_passes(self):
+ self.job.state_dict['test_result'] = True
+ self.assertEqual(True, self.mixin.run_test('sleeptest', seconds=1))
+ self.assert_("job.run_test('sleeptest', seconds=1)\n"
+ in self.control_file)
+ self.assertEqual(self.mixin, self.host)
+
+
+ def test_fails_clean(self):
+ self.job.state_dict['test_result'] = False
+ self.assertEqual(False, self.mixin.run_test('sleeptest', seconds='2'))
+ self.assert_("job.run_test('sleeptest', seconds='2')\n"
+ in self.control_file)
+ self.assertEqual(self.mixin, self.host)
+
+
+ def test_fails_with_exception(self):
+ self.assertEqual(False, self.mixin.run_test('sleeptest'))
+ self.assert_("job.run_test('sleeptest')\n" in self.control_file)
+ self.assertEqual(self.mixin, self.host)
+
if __name__ == "__main__":
unittest.main()
diff --git a/server/hosts/factory.py b/server/hosts/factory.py
index 1eb9b53a..8e5ba663 100644
--- a/server/hosts/factory.py
+++ b/server/hosts/factory.py
@@ -1,5 +1,5 @@
from autotest_lib.client.common_lib import utils, error, global_config
-from autotest_lib.server import utils as server_utils
+from autotest_lib.server import autotest, utils as server_utils
from autotest_lib.server.hosts import site_factory, ssh_host, serial
from autotest_lib.server.hosts import logfile_monitor
@@ -27,6 +27,9 @@ def create_host(
"on autotest's global_config.ini file." %
SSH_ENGINE)
+ # by default mix in run_test support
+ classes.append(autotest.AutotestHostMixin)
+
# if the user really wants to use netconsole, let them
if netconsole:
classes.append(netconsole.NetconsoleHost)