41188d790b64a2f28e67f455bac31081350a26d6
[tests.git] / qaapi / qa / tests / Test.py
1 import sys, os
2 from types import *
3 from pprint import pprint
4
5 # Add qa root directory to the path to make
6 # imports easier 
7 path = os.path.dirname(os.path.abspath(__file__))
8 path_parts = path.split(os.sep)
9 sys.path.append(os.sep.join(path_parts[:-2]))
10
11 from qa import utils
12 from qa.logger import log
13 from qa.Config import Config
14
15 class Test:
16     """
17     Base class for all QA test functions. At a minimum all tests
18     must define:
19
20     call(arg1, arg2, ...): method body
21     """
22
23     accepts = []
24     returns = bool
25     status = "current" 
26
27     def call(self, *args):
28         """
29         Method body for test functions. Must override.
30         """
31         return True
32
33     def __init__(self, config = None):
34         if config is None: config = Config()
35         self.name = self.__class__.__name__
36         self.path=os.path.abspath(os.path.dirname(sys.argv[0]))
37         self.config = config
38         self.errors = []
39
40
41     def __call__(self, *args, **kwds):
42         """
43         Main entry point for test functions. logs methods
44         """
45
46         #(min_args, max_args, defaults) = self.args()
47         
48         # Check that the right number of arguments were passed in
49         #if len(args) < len(min_args) or len(args) > len(max_args):
50         #    raise Exception#, (len(args), len(min_args), len(max_args))
51
52         module = self.__class__.__module__.replace(".", os.sep)
53         file = self.path + os.sep + module + ".py"
54         try:
55             result = self.call(*args, **kwds)
56         except NameError:
57             command = "%s %s" % (file, " ".join(args))
58             utils.header(command)
59             (stdout, stderr) = utils.popen(command)
60             print "".join(stdout)
61             result = None
62  
63         return result   
64             
65
66     def args(self):
67         """
68         Returns a tuple:
69
70         ((arg1_name, arg2_name, ...),
71          (arg1_name, arg2_name, ..., optional1_name, optional2_name, ...),
72          (None, None, ..., optional1_default, optional2_default, ...))
73
74         That represents the minimum and maximum sets of arguments that
75         this function accepts and the defaults for the optional arguments.
76         """
77
78         # Inspect call. Remove self from the argument list.
79         max_args = self.call.func_code.co_varnames[1:self.call.func_code.co_argcount]
80         defaults = self.call.func_defaults
81         if defaults is None:
82             defaults = ()
83
84         min_args = max_args[0:len(max_args) - len(defaults)]
85         defaults = tuple([None for arg in min_args]) + defaults
86
87         return (min_args, max_args, defaults)