attempt to execute file from bash if call() fails
[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 = Config()):
34         self.name = self.__class__.__name__
35         self.path=os.path.abspath(os.path.dirname(sys.argv[0]))
36         self.config = config
37         self.errors = []
38
39
40     def __call__(self, *args, **kwds):
41         """
42         Main entry point for test functions. logs methods
43         """
44
45         #(min_args, max_args, defaults) = self.args()
46         
47         # Check that the right number of arguments were passed in
48         #if len(args) < len(min_args) or len(args) > len(max_args):
49         #    raise Exception#, (len(args), len(min_args), len(max_args))
50
51         module = self.__class__.__module__.replace(".", os.sep)
52         file = self.path + os.sep + module + ".py"
53         try:
54             result = self.call(*args, **kwds)
55         except NameError:
56             command = "%s %s" % (file, " ".join(args))
57             utils.header(command)
58             (stdout, stderr) = utils.popen(command)
59             print "".join(stdout)
60             result = None
61  
62         return result   
63             
64
65     def args(self):
66         """
67         Returns a tuple:
68
69         ((arg1_name, arg2_name, ...),
70          (arg1_name, arg2_name, ..., optional1_name, optional2_name, ...),
71          (None, None, ..., optional1_default, optional2_default, ...))
72
73         That represents the minimum and maximum sets of arguments that
74         this function accepts and the defaults for the optional arguments.
75         """
76
77         # Inspect call. Remove self from the argument list.
78         max_args = self.call.func_code.co_varnames[1:self.call.func_code.co_argcount]
79         defaults = self.call.func_defaults
80         if defaults is None:
81             defaults = ()
82
83         min_args = max_args[0:len(max_args) - len(defaults)]
84         defaults = tuple([None for arg in min_args]) + defaults
85
86         return (min_args, max_args, defaults)