[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

*libvirt-test-api.py: initialize proxy module only once *casecfgcheck.py: use proxy object rather than initialize it by itself *proxy.py: make get_func_call_dict more flexible --- casecfgcheck.py | 5 +---- libvirt-test-api.py | 12 +++++------- proxy.py | 22 ++++++++++++++-------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/casecfgcheck.py b/casecfgcheck.py index 40d7c6e..9a4f8e6 100644 --- a/casecfgcheck.py +++ b/casecfgcheck.py @@ -18,13 +18,10 @@ import proxy class CaseCfgCheck(object): """validate the options in testcase config file""" - def __init__(self, unique_testcases, activities_list): - self.unique_testcases = unique_testcases - + def __init__(self, proxy_obj, activities_list): # XXX to check the first testcase list in activities_list self.activity = activities_list[0] - proxy_obj = proxy.Proxy(self.unique_testcases) self.case_params = proxy_obj.get_params_variables() def check(self): diff --git a/libvirt-test-api.py b/libvirt-test-api.py index 385b52d..7b38aaa 100644 --- a/libvirt-test-api.py +++ b/libvirt-test-api.py @@ -112,20 +112,18 @@ class Main(object): unique_testcases = filterobj.unique_testcases() + # __import__ TESTCASE.py once for duplicate testcase names + proxy_obj = proxy.Proxy(unique_testcases) + # check the options to each testcase in case config file - casechk = CaseCfgCheck(unique_testcases, activities_list) + casechk = CaseCfgCheck(proxy_obj, activities_list) if casechk.check(): return 1 # get a list of unique testcase # with 'clean' flag appended to its previous testcase unique_testcase_keys = filterobj.unique_testcase_cleansuffix() - - # call and initilize proxy component to - # get a list of reference of testcases - proxy_obj = proxy.Proxy(unique_testcase_keys) - - cases_func_ref_dict = proxy_obj.get_func_call_dict() + cases_func_ref_dict = proxy_obj.get_func_call_dict(unique_testcase_keys) # create a null list, then, initilize generator to # get the callable testcase function diff --git a/proxy.py b/proxy.py index bc82a84..49a0420 100644 --- a/proxy.py +++ b/proxy.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# libvirt-test-API is copyright 2010 Red Hat, Inc. +# libvirt-test-API is copyright 2010, 2012 Red Hat, Inc. # # libvirt-test-API is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by @@ -37,12 +37,13 @@ class Proxy(object): casename = elements[1] casemod_ref = self.get_call_dict(module, casename) - self.testcase_ref_dict[testcase_name] = casemod_ref + modcase = module + ':' + casename + self.testcase_ref_dict[modcase] = casemod_ref - def get_func_call_dict(self): - """Return running function reference dictionary """ + def get_func_call_dict(self, unique_testcase_keys): + """get reference to functions defined in testcase file """ func_dict = {} - for testcase_name in self.testcases_names: + for testcase_name in unique_testcase_keys: # Get module, casename elements = testcase_name.split(':') module = elements[0] @@ -55,16 +56,21 @@ class Proxy(object): flag = elements[2] func = casename + flag - casemod_ref = self.testcase_ref_dict[testcase_name] + # use modcase key to get the reference to corresponding + # testcase module + modcase = module + ':' + casename + casemod_ref = self.testcase_ref_dict[modcase] var_func_names = dir(casemod_ref) - key = module + ':' + casename + ':' + func + key = modcase + ':' + func + # check if the expected function is present in + # the list of string name from dir() if func in var_func_names: func_ref = getattr(casemod_ref, func) func_dict[key] = func_ref else: raise exception.TestCaseError("function %s not found in %s" % \ - (func, testcase_name)) + (func, modcase)) return func_dict def get_clearfunc_call_dict(self): -- 1.7.7.5

On 04/16/2012 08:15 AM, Guannan Ren wrote:
*libvirt-test-api.py: initialize proxy module only once *casecfgcheck.py: use proxy object rather than initialize it by itself *proxy.py: make get_func_call_dict more flexible --- casecfgcheck.py | 5 +---- libvirt-test-api.py | 12 +++++------- proxy.py | 22 ++++++++++++++-------- 3 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/casecfgcheck.py b/casecfgcheck.py index 40d7c6e..9a4f8e6 100644 --- a/casecfgcheck.py +++ b/casecfgcheck.py @@ -18,13 +18,10 @@ import proxy
class CaseCfgCheck(object): """validate the options in testcase config file""" - def __init__(self, unique_testcases, activities_list): - self.unique_testcases = unique_testcases - + def __init__(self, proxy_obj, activities_list): # XXX to check the first testcase list in activities_list self.activity = activities_list[0]
- proxy_obj = proxy.Proxy(self.unique_testcases) self.case_params = proxy_obj.get_params_variables()
def check(self): diff --git a/libvirt-test-api.py b/libvirt-test-api.py index 385b52d..7b38aaa 100644 --- a/libvirt-test-api.py +++ b/libvirt-test-api.py @@ -112,20 +112,18 @@ class Main(object):
unique_testcases = filterobj.unique_testcases()
+ # __import__ TESTCASE.py once for duplicate testcase names + proxy_obj = proxy.Proxy(unique_testcases) + # check the options to each testcase in case config file - casechk = CaseCfgCheck(unique_testcases, activities_list) + casechk = CaseCfgCheck(proxy_obj, activities_list) if casechk.check(): return 1
# get a list of unique testcase # with 'clean' flag appended to its previous testcase unique_testcase_keys = filterobj.unique_testcase_cleansuffix() - - # call and initilize proxy component to - # get a list of reference of testcases - proxy_obj = proxy.Proxy(unique_testcase_keys) - - cases_func_ref_dict = proxy_obj.get_func_call_dict() + cases_func_ref_dict = proxy_obj.get_func_call_dict(unique_testcase_keys)
# create a null list, then, initilize generator to # get the callable testcase function diff --git a/proxy.py b/proxy.py index bc82a84..49a0420 100644 --- a/proxy.py +++ b/proxy.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# libvirt-test-API is copyright 2010 Red Hat, Inc. +# libvirt-test-API is copyright 2010, 2012 Red Hat, Inc. # # libvirt-test-API is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by @@ -37,12 +37,13 @@ class Proxy(object): casename = elements[1]
casemod_ref = self.get_call_dict(module, casename) - self.testcase_ref_dict[testcase_name] = casemod_ref + modcase = module + ':' + casename + self.testcase_ref_dict[modcase] = casemod_ref
- def get_func_call_dict(self): - """Return running function reference dictionary """ + def get_func_call_dict(self, unique_testcase_keys): + """get reference to functions defined in testcase file """ func_dict = {} - for testcase_name in self.testcases_names: + for testcase_name in unique_testcase_keys: # Get module, casename elements = testcase_name.split(':') module = elements[0] @@ -55,16 +56,21 @@ class Proxy(object): flag = elements[2] func = casename + flag
- casemod_ref = self.testcase_ref_dict[testcase_name] + # use modcase key to get the reference to corresponding + # testcase module + modcase = module + ':' + casename + casemod_ref = self.testcase_ref_dict[modcase] var_func_names = dir(casemod_ref)
- key = module + ':' + casename + ':' + func + key = modcase + ':' + func + # check if the expected function is present in + # the list of string name from dir() if func in var_func_names: func_ref = getattr(casemod_ref, func) func_dict[key] = func_ref else: raise exception.TestCaseError("function %s not found in %s" % \ - (func, testcase_name)) + (func, modcase)) return func_dict
def get_clearfunc_call_dict(self):
ACK both. Martin P.S.: This is very useful, I don't have to type it manually =)

On 04/16/2012 03:54 PM, Martin Kletzander wrote:
On 04/16/2012 08:15 AM, Guannan Ren wrote:
*libvirt-test-api.py: initialize proxy module only once *casecfgcheck.py: use proxy object rather than initialize it by itself *proxy.py: make get_func_call_dict more flexible --- casecfgcheck.py | 5 +---- libvirt-test-api.py | 12 +++++------- proxy.py | 22 ++++++++++++++-------- 3 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/casecfgcheck.py b/casecfgcheck.py index 40d7c6e..9a4f8e6 100644 --- a/casecfgcheck.py +++ b/casecfgcheck.py @@ -18,13 +18,10 @@ import proxy
class CaseCfgCheck(object): """validate the options in testcase config file""" - def __init__(self, unique_testcases, activities_list): - self.unique_testcases = unique_testcases - + def __init__(self, proxy_obj, activities_list): # XXX to check the first testcase list in activities_list self.activity = activities_list[0]
- proxy_obj = proxy.Proxy(self.unique_testcases) self.case_params = proxy_obj.get_params_variables()
def check(self): diff --git a/libvirt-test-api.py b/libvirt-test-api.py index 385b52d..7b38aaa 100644 --- a/libvirt-test-api.py +++ b/libvirt-test-api.py @@ -112,20 +112,18 @@ class Main(object):
unique_testcases = filterobj.unique_testcases()
+ # __import__ TESTCASE.py once for duplicate testcase names + proxy_obj = proxy.Proxy(unique_testcases) + # check the options to each testcase in case config file - casechk = CaseCfgCheck(unique_testcases, activities_list) + casechk = CaseCfgCheck(proxy_obj, activities_list) if casechk.check(): return 1
# get a list of unique testcase # with 'clean' flag appended to its previous testcase unique_testcase_keys = filterobj.unique_testcase_cleansuffix() - - # call and initilize proxy component to - # get a list of reference of testcases - proxy_obj = proxy.Proxy(unique_testcase_keys) - - cases_func_ref_dict = proxy_obj.get_func_call_dict() + cases_func_ref_dict = proxy_obj.get_func_call_dict(unique_testcase_keys)
# create a null list, then, initilize generator to # get the callable testcase function diff --git a/proxy.py b/proxy.py index bc82a84..49a0420 100644 --- a/proxy.py +++ b/proxy.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# libvirt-test-API is copyright 2010 Red Hat, Inc. +# libvirt-test-API is copyright 2010, 2012 Red Hat, Inc. # # libvirt-test-API is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by @@ -37,12 +37,13 @@ class Proxy(object): casename = elements[1]
casemod_ref = self.get_call_dict(module, casename) - self.testcase_ref_dict[testcase_name] = casemod_ref + modcase = module + ':' + casename + self.testcase_ref_dict[modcase] = casemod_ref
- def get_func_call_dict(self): - """Return running function reference dictionary """ + def get_func_call_dict(self, unique_testcase_keys): + """get reference to functions defined in testcase file """ func_dict = {} - for testcase_name in self.testcases_names: + for testcase_name in unique_testcase_keys: # Get module, casename elements = testcase_name.split(':') module = elements[0] @@ -55,16 +56,21 @@ class Proxy(object): flag = elements[2] func = casename + flag
- casemod_ref = self.testcase_ref_dict[testcase_name] + # use modcase key to get the reference to corresponding + # testcase module + modcase = module + ':' + casename + casemod_ref = self.testcase_ref_dict[modcase] var_func_names = dir(casemod_ref)
- key = module + ':' + casename + ':' + func + key = modcase + ':' + func + # check if the expected function is present in + # the list of string name from dir() if func in var_func_names: func_ref = getattr(casemod_ref, func) func_dict[key] = func_ref else: raise exception.TestCaseError("function %s not found in %s" % \ - (func, testcase_name)) + (func, modcase)) return func_dict
def get_clearfunc_call_dict(self):
ACK both.
Martin
P.S.: This is very useful, I don't have to type it manually =)
Thanks and pushed. Guannan Ren
participants (2)
-
Guannan Ren
-
Martin Kletzander