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 <[email protected]>

--- autotest/server/autotest.py 2010-07-16 07:47:32.000000000 -0700
+++ autotest/server/autotest.py 2010-07-16 07:47:32.000000000 -0700
@@ -1048,3 +1048,27 @@
 
 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)
--- autotest/server/autotest_unittest.py        2010-07-16 07:47:32.000000000 
-0700
+++ autotest/server/autotest_unittest.py        2010-07-16 07:47:32.000000000 
-0700
@@ -311,6 +311,47 @@
         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()
--- autotest/server/hosts/factory.py    2010-07-16 07:47:32.000000000 -0700
+++ autotest/server/hosts/factory.py    2010-07-16 07:47:32.000000000 -0700
@@ -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 @@
                                   "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)
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to