Skip to contentSkip to conversation

Turn KVM test subtest loading mechanism into infrastructure

RFC PATCH New Lucas Meneghel Rodrigues 1 participant 1 message Dec 8, 2010 KVM

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Turn KVM test subtest loading mechanism into infrastructure

We found out that any client side test that has to implement a substantial amount of code in the form of sub-tests could benefit from having the KVM test mechanism of loading subtests available. So in order to maximize reuse, turn the mechanism into a class, called subtest_loader. Any client side tests that want to benefit from it would have to inherit and implement some methods if they wish (Some wouldn't have to implement a thing).

I would like to hear your opinions about that approach.

---
 client/bin/test.py      |   93 ++++++++++++++++++++++++++++++++++++
 client/tests/kvm/kvm.py |  120 +++++++++++++++++-----------------------------
 2 files changed, 137 insertions(+), 76 deletions(-)
client/bin/test.py +93 -0
index 33f194c..7be4bb4 100644
@@ -107,6 +107,99 @@ class test(common_test.base_test):
                     logging.warning('Please verify %s for more info', dir)
 
 
+class subtest_loader(test):
+    """
+    Class specialized in loading and running subtests.
+
+    It is used for more complex tests, that encompass a large amount of
+    subtests, such as the KVM tests. If you plan on using this, create
+    a subdir 'tests' inside your test dir, and for each test you want to
+    create, make sure the .py file is called [test_name].py and this
+    file implements at least a function called run_[test_name].
+
+    The function run_once is already implemented, and other tests might want
+    to implement the methods:
+
+    environment_setup()
+    preprocess()
+    postprocess_on_error()
+    postprocess()
+    on_error()
+    """
+    def environment_setup(self):
+        pass
+
+
+    def preprocess(self):
+        pass
+
+
+    def postprocess_on_error(self):
+        pass
+
+
+    def postprocess(self):
+        pass
+
+
+    def on_error(self):
+        pass
+
+
+    def run_once(self, params):
+        self.params = params
+        # Report the parameters we've received and write them as keyvals
+        logging.debug("Test parameters:")
+        keys = self.params.keys()
+        keys.sort()
+        for key in keys:
+            logging.debug("    %s = %s", key, self.params[key])
+            self.write_test_keyval({key: self.params[key]})
+
+        self.environment_setup()
+        test_passed = False
+
+        try:
+            try:
+                try:
+                    # Get the test routine corresponding to the specified
+                    # test type
+                    t_type = params.get("type")
+                    # Verify if we have the correspondent source file for it
+                    subtest_dir = os.path.join(self.bindir, "tests")
+                    module_path = os.path.join(subtest_dir, "%s.py" % t_type)
+                    if not os.path.isfile(module_path):
+                        raise error.TestError("No %s.py test file found" %
+                                              t_type)
+                    # Load the test module
+                    f, p, d = imp.find_module(t_type, [subtest_dir])
+                    test_module = imp.load_module(t_type, f, p, d)
+                    f.close()
+
+                    self.preprocess()
+
+                    # Run the test function
+                    run_func = getattr(test_module, "run_%s" % t_type)
+                    try:
+                        run_func(self, self.params, env)
+                    finally:
+                        kvm_utils.dump_env(env, env_filename)
+                    test_passed = True
+
+                except Exception, e:
+                    logging.error("Test failed: %s: %s",
+                                  e.__class__.__name__, e)
+
+                    self.postprocess_on_error()
+
+            finally:
+                self.postprocess()
+
+        except Exception, e:
+            self.on_error()
+
+
+
 def runtest(job, url, tag, args, dargs):
     common_test.runtest(job, url, tag, args, dargs, locals(), globals(),
                         job.sysinfo.log_before_each_test,
client/tests/kvm/kvm.py +44 -76

144 lines not on this page · raw message

Signature · 1 line
1.7.2.3