-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_tests.py
executable file
·244 lines (198 loc) · 7.94 KB
/
run_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/python
"""
Test harness creating a test suite, and running it.
@author: Roy Nielsen
@note Initial working model: 1/15/2015
"""
#--- Native python libraries
import os
import re
import sys
import traceback
import unittest
from optparse import OptionParser, SUPPRESS_HELP, OptionValueError, Option
testdir = "./tests"
from lib.loggers import CyLogger
from lib.loggers import LogPriority as lp
###############################################################################
class TestResultsErrors(BaseException):
"""
Meant for being thrown when an action/class being run/instanciated is not
applicable for the running operating system.
@author: Roy Nielsen
"""
def __init__(self, *args, **kwargs):
BaseException.__init__(self, *args, **kwargs)
###########################################################################
class BuildAndRunSuite(object):
def __init__(self, logger):
"""
"""
if logger:
self.logger = logger
else:
self.logger = CyLogger()
self.module_version = '20160224.032043.009191'
self.prefix = []
##############################################
def setPrefix(self, prefix=[]):
"""
Setter for the prefix variable...
"""
if prefix and isinstance(prefix, list):
self.prefix = prefix
else:
self.prefix=["test_", "Test_"]
##############################################
def get_all_tests(self, prefix=[]):
"""
Collect all available tests using the test prefix(s)
@author: Roy Nielsen
"""
test_list = []
if not self.modules:
allfiles = os.listdir(testdir)
for check_file in allfiles:
test_name = str(check_file).split(".")[0]
pycfile = os.path.join("./tests/", test_name + ".pyc")
if os.path.exists(pycfile):
os.unlink(pycfile)
for item in self.prefix:
if re.match("^%s.+\.py$"%item, check_file):
print "Loading test: " + str(check_file)
test_list.append(os.path.join("./tests/", check_file))
print str(test_list)
return test_list
##############################################
def run_suite(self, modules=[]):
"""
Gather all the tests from this module in a test suite.
@author: Roy Nielsen
"""
self.test_dir_name = testdir.split("/")[1]
self.modules = modules
#####
# Initialize the test suite
self.test_suite = unittest.TestSuite()
#####
# Generate the test list
if self.modules and isinstance(self.modules, list):
test_list = self.modules
else:
test_list = self.get_all_tests(prefix)
#####
# Import each of the tests and add them to the suite
for check_file in test_list:
self.logger.log(lp.DEBUG, str(check_file))
test_name = str(check_file).split("/")[-1]
test_name = str(test_name).split(".")[0]
self.logger.log(lp.DEBUG, "test_name: " + str(test_name))
test_name_import_path = ".".join([self.test_dir_name, test_name])
self.logger.log(lp.DEBUG, "test_name_import_path: " +
str(test_name_import_path))
try:
################################################
# Test class needs to be named the same as the
# filename for this to work.
# import the file named in "test_name" variable
module_to_run = __import__(test_name_import_path,
fromlist=test_name, level=-1)
# getattr(x, 'foobar') is equivalent to x.foobar
test_to_run = getattr(module_to_run, test_name)
# Add the test class to the test suite
self.test_suite.addTest(unittest.makeSuite(test_to_run))
except AttributeError, err:
pass
#####
# calll the run_action to execute the test suite
self.run_action()
##############################################
def run_action(self):
"""
Run the Suite.
"""
runner = unittest.TextTestRunner()
runner.run(self.test_suite)
###############################################################################
# Get all of the possible options passed in to OptionParser that are passed
# in with the -m or --modules flag
class ModulesOption(Option):
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(
self, action, dest, opt, value, values, parser)
###############################################################################
if __name__ == "__main__":
"""
Executes if this file is run.
"""
VERSION="0.4.0"
description = "Generic test runner."
parser = OptionParser(option_class=ModulesOption,
usage='usage: %prog [OPTIONS]',
version='%s' % (VERSION),
description=description)
parser.add_option("-a", "--all-automatable", action="store_true", dest="all",
default=False, help="Run all tests except interactive tests.")
parser.add_option("-v", "--verbose", action="store_true",
dest="verbose", default=False, \
help="Print status messages")
parser.add_option("-d", "--debug", action="store_true", dest="debug",
default=False, help="Print debug messages")
parser.add_option('-p', '--prefix', action="extend", type="string",
dest='prefix', default=[],
help="Collect tests with these prefixes")
parser.add_option('-m', '--modules', action="extend", type="string",
dest='modules', default=[], help="Name of the test" +
" to run. May indicate multiple tests to run," +
" if this switch is used multiple times, each with" +
" different test name")
parser.add_option("-s", "--skip", action="store_true",
dest="skip_syslog", default=False,
help="Skip syslog logging so we don't fill up the logs." + \
"This will leave an incremental log by default in " + \
"/tmp/<uid>.stonixtest.<log number>, where log number" +\
" is the order of the last ten stonixtest runs")
if len(sys.argv) == 1:
parser.parse_args(['--help'])
options, __ = parser.parse_args()
#####
# Options processing
#####
# ... processing modules ...
if options.all:
modules = None
elif options.modules:
modules = options.modules
else:
modules = None
#####
# ... processing logging options...
if options.verbose:
level = 20
elif options.debug:
level = 10
else:
level = 30
logger = CyLogger(level=level)
logger.initializeLogs(filename="ramdiskTestLog")
logger.log(lp.DEBUG, "Modules: " + str(modules))
#####
# ... processing test prefixes
if options.prefix:
prefix = options.prefix
else:
prefix = ["test_", "Test_"]
if os.geteuid != 0:
print("\n\nNote - Some tests will fail if not run with superuser privilege.")
raw_input("Press any key to continue...\n\n")
bars = BuildAndRunSuite(logger)
bars.setPrefix(prefix)
bars.run_suite(modules)