[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [test-API PATCH 1/2] add a new option -t to print template of testcase config file



    If a given testcase has clean function, the clean flag will be
    used automatically
    # python libvirt-test-api.py -t repos/domain/attach_disk.py \
                                    repos/storage/create_netfs_pool.py \
                                    repos/domain/save.py

    output:
domain:attach_disk
    guestname
        GUESTNAME
    guesttype
        GUESTTYPE
    imagename
        IMAGENAME
    imagesize
        IMAGESIZE
    hdmodel
        HDMODEL

storage:create_netfs_pool
    poolname
        POOLNAME
    sourcename
        SOURCENAME
    sourcepath
        SOURCEPATH
    pooltype
        POOLTYPE
    [targetpath]
        TARGETPATH

domain:save
    guestname
        GUESTNAME
    filepath
        FILEPATH
clean
---
 libvirt-test-api.py |   49 +++++++++++++++++++++++++++++++++++++++++++++++--
 proxy.py            |   17 +++++++++++++++++
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/libvirt-test-api.py b/libvirt-test-api.py
index c171276..385b52d 100644
--- a/libvirt-test-api.py
+++ b/libvirt-test-api.py
@@ -35,6 +35,7 @@ def usage():
     print "Usage: libvirt_test_api.py <OPTIONS> <ARGUS>"
     print "\noptions: -h, --help : Display usage information \
            \n         -c, --casefile: Specify configuration file \
+           \n         -t, --template: Print testcase config file template \
            \n         -f, --logxml: Specify log file with type xml \
            \n         -l, --log-level: 0 or 1 currently \
            \n         -d, --delete-log: Delete log items \
@@ -45,6 +46,7 @@ def usage():
     print "example: \
            \n         python libvirt-test-api.py -l 0|1 -c TEST.CONF    \
            \n         python libvirt-test-api.py -c TEST.CONF -f TEST.XML \
+           \n         python libvirt-test-api.py -t repos/domain/start.py ... \
            \n         python libvirt-test-api.py -m TESTONE.XML TESTTWO.XML \
            \n         python libvirt-test-api.py -d TEST.XML TESTRUNID TESTID \
            \n         python libvirt-test-api.py -d TEST.XML TESTRUNID \
@@ -215,6 +217,41 @@ class Main(object):
             return 1
         return 0
 
+    def print_casefile(self, testcases):
+        """print testcase file template"""
+        modcasename = []
+        for case in testcases:
+            if not os.path.isfile(case) or not case.endswith('.py'):
+                print "testcase %s couldn't be recognized" % case
+                return 1
+
+            paths = case.split('/')
+            modcasename.append(paths[1] + ':' + paths[2][:-3])
+
+        proxy_obj = proxy.Proxy(modcasename)
+        case_params = proxy_obj.get_params_variables()
+
+        string = "# the file is generated automatically, please\n" \
+                 "# make some modifications before the use of it\n" \
+                 "# params in [] are optional to its testcase\n"
+        for key in modcasename:
+            string += "%s\n" % key
+            required_params, optional_params = case_params[key]
+            for p in required_params:
+                string += " " * 4  + p + "\n"
+                string += " " * 8 + p.upper() + "\n"
+            for p in optional_params:
+                string += " " * 4 + "[" + p + "]\n"
+                string += " " * 8 + p.upper() + "\n"
+
+            if proxy_obj.has_clean_function(key):
+                string += "clean\n"
+
+            string += "\n"
+
+        print string
+        return 0
+
     def remove_log(self, testrunid, testid = None):
         """  to remove log item in the log xmlfile """
         log_xml_parser = LogXMLParser(self.logxml)
@@ -274,8 +311,8 @@ if __name__ == "__main__":
     loglevel = 0
 
     try:
-        opts, args = getopt.getopt(sys.argv[1:], "hc:l:dmr",
-                    ["help", "casefile=", "logxml=",
+        opts, args = getopt.getopt(sys.argv[1:], "hc:tl:dmr",
+                    ["help", "casefile=", "template", "logxml=",
                     "delete-log=", "merge=", "rerun="])
     except getopt.GetoptError, err:
         print str(err)
@@ -288,6 +325,14 @@ if __name__ == "__main__":
             sys.exit(0)
         if o == "-c" or o == "--casefile":
             casefile = v
+        if o == "-t" or o == "--template":
+            if len(args) <= 0:
+                usage()
+                sys.exit(1)
+            main = Main('', '', '', '')
+            if main.print_casefile(args):
+                sys.exit(1)
+            sys.exit(0)
         if o == "-f" or o == "--logxml":
             logxml = v
         if o == "-l" or o == "--log-level":
diff --git a/proxy.py b/proxy.py
index fdbffd9..bc82a84 100644
--- a/proxy.py
+++ b/proxy.py
@@ -120,6 +120,23 @@ class Proxy(object):
                       ("required_params or optional_params not found in %s" % testcase_name)
         return case_params
 
+    def has_clean_function(self, testcase_name):
+        """ Return true if the testcase have clean function
+        """
+        if testcase_name not in self.testcases_names:
+            return False
+
+        elements = testcase_name.split(':')
+        casename = elements[1]
+        func = casename + '_clean'
+
+        casemod_ref = self.testcase_ref_dict[testcase_name]
+        var_func_names = dir(casemod_ref)
+
+        if func in var_func_names:
+            return True
+        return False
+
     def get_call_dict(self, module, casename, func = None):
         """ Return testing function reference dictionary """
         case_abs_path = '%s.%s.%s' % ('repos', module, casename)
-- 
1.7.7.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]