2011-12-22 01:36:26 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
'''
|
|
|
|
Discover all instances of unittest.TestCase in this directory.
|
|
|
|
'''
|
2012-02-12 08:24:20 +00:00
|
|
|
# Import python libs
|
2012-05-05 11:53:39 +00:00
|
|
|
import sys
|
2012-02-12 08:24:20 +00:00
|
|
|
import os
|
2012-08-11 00:28:49 +00:00
|
|
|
import logging
|
2012-04-04 17:49:28 +00:00
|
|
|
import optparse
|
2012-09-03 16:42:36 +00:00
|
|
|
import resource
|
2012-08-11 00:28:49 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
2012-02-12 23:03:31 +00:00
|
|
|
import saltunittest
|
2012-02-20 12:18:13 +00:00
|
|
|
from integration import TestDaemon
|
2012-02-12 09:58:15 +00:00
|
|
|
|
2012-05-05 22:27:16 +00:00
|
|
|
try:
|
|
|
|
import xmlrunner
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2012-02-12 23:03:31 +00:00
|
|
|
TEST_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
|
2011-12-22 01:36:26 +00:00
|
|
|
|
2012-08-01 18:07:56 +00:00
|
|
|
PNUM = 70
|
2012-09-03 16:42:36 +00:00
|
|
|
REQUIRED_OPEN_FILES = 2048
|
2012-04-04 16:58:11 +00:00
|
|
|
|
2012-04-04 17:49:28 +00:00
|
|
|
|
2012-05-30 02:56:47 +00:00
|
|
|
def run_suite(opts, path, display_name, suffix='[!_]*.py'):
|
2012-05-30 23:10:48 +00:00
|
|
|
'''
|
|
|
|
Execute a unit test suite
|
|
|
|
'''
|
2012-05-30 02:56:47 +00:00
|
|
|
loader = saltunittest.TestLoader()
|
|
|
|
if opts.name:
|
|
|
|
tests = loader.loadTestsFromName(opts.name)
|
|
|
|
else:
|
|
|
|
tests = loader.discover(path, suffix, TEST_DIR)
|
|
|
|
print('~' * PNUM)
|
|
|
|
print('Starting {0} Tests'.format(display_name))
|
|
|
|
print('~' * PNUM)
|
|
|
|
if opts.xmlout:
|
|
|
|
runner = xmlrunner.XMLTestRunner(output='test-reports').run(tests)
|
|
|
|
else:
|
|
|
|
runner = saltunittest.TextTestRunner(
|
|
|
|
verbosity=opts.verbosity).run(tests)
|
|
|
|
return runner.wasSuccessful()
|
|
|
|
|
|
|
|
|
|
|
|
def run_integration_suite(opts, suite_folder, display_name):
|
2012-05-30 23:10:48 +00:00
|
|
|
'''
|
|
|
|
Run an integration test suite
|
|
|
|
'''
|
2012-05-30 02:56:47 +00:00
|
|
|
path = os.path.join(TEST_DIR, 'integration', suite_folder)
|
|
|
|
return run_suite(opts, path, display_name)
|
|
|
|
|
|
|
|
|
2012-05-05 20:42:47 +00:00
|
|
|
def run_integration_tests(opts):
|
2012-04-04 17:49:28 +00:00
|
|
|
'''
|
|
|
|
Execute the integration tests suite
|
|
|
|
'''
|
2012-09-03 16:42:36 +00:00
|
|
|
smax_open_files, hmax_open_files = resource.getrlimit(resource.RLIMIT_NOFILE)
|
|
|
|
if smax_open_files < REQUIRED_OPEN_FILES:
|
|
|
|
print('~' * PNUM)
|
|
|
|
print('Max open files setting is too low({0}) for running the tests'.format(smax_open_files))
|
|
|
|
print('Trying to raise the limit to {0}'.format(REQUIRED_OPEN_FILES))
|
2012-09-03 18:10:38 +00:00
|
|
|
if hmax_open_files < 4096:
|
|
|
|
hmax_open_files = 4096 # Decent default?
|
2012-09-03 16:42:36 +00:00
|
|
|
try:
|
|
|
|
resource.setrlimit(
|
|
|
|
resource.RLIMIT_NOFILE,
|
2012-09-03 18:10:38 +00:00
|
|
|
(REQUIRED_OPEN_FILES, hmax_open_files)
|
2012-09-03 16:42:36 +00:00
|
|
|
)
|
|
|
|
except Exception, err:
|
2012-09-03 18:10:38 +00:00
|
|
|
print('ERROR: Failed to raise the max open files setting -> {0}'.format(err))
|
2012-09-03 16:42:36 +00:00
|
|
|
print('Please issue the following command on your console:')
|
|
|
|
print(' ulimit -n {0}'.format(REQUIRED_OPEN_FILES))
|
|
|
|
sys.exit(1)
|
|
|
|
finally:
|
|
|
|
print('~' * PNUM)
|
|
|
|
|
2012-04-20 19:13:02 +00:00
|
|
|
print('~' * PNUM)
|
|
|
|
print('Setting up Salt daemons to execute tests')
|
|
|
|
print('~' * PNUM)
|
2012-05-05 11:53:39 +00:00
|
|
|
status = []
|
2012-05-30 02:56:47 +00:00
|
|
|
if not any([opts.client, opts.module, opts.runner,
|
|
|
|
opts.shell, opts.state, opts.name]):
|
|
|
|
return status
|
2012-07-20 06:16:14 +00:00
|
|
|
with TestDaemon(clean=opts.clean):
|
2012-05-30 02:56:47 +00:00
|
|
|
if opts.name:
|
|
|
|
results = run_suite(opts, '', opts.name)
|
|
|
|
status.append(results)
|
2012-05-30 23:10:48 +00:00
|
|
|
if opts.runner:
|
|
|
|
status.append(run_integration_suite(opts, 'runners', 'Runner'))
|
2012-05-05 20:42:47 +00:00
|
|
|
if opts.module:
|
2012-05-30 02:56:47 +00:00
|
|
|
status.append(run_integration_suite(opts, 'modules', 'Module'))
|
2012-05-13 04:27:30 +00:00
|
|
|
if opts.state:
|
2012-05-30 02:56:47 +00:00
|
|
|
status.append(run_integration_suite(opts, 'states', 'State'))
|
2012-05-05 20:42:47 +00:00
|
|
|
if opts.client:
|
2012-05-30 02:56:47 +00:00
|
|
|
status.append(run_integration_suite(opts, 'client', 'Client'))
|
2012-05-05 20:42:47 +00:00
|
|
|
if opts.shell:
|
2012-05-30 02:56:47 +00:00
|
|
|
status.append(run_integration_suite(opts, 'shell', 'Shell'))
|
2012-05-05 11:53:39 +00:00
|
|
|
return status
|
2012-04-04 17:49:28 +00:00
|
|
|
|
|
|
|
|
2012-05-05 20:42:47 +00:00
|
|
|
def run_unit_tests(opts):
|
2012-04-04 17:49:28 +00:00
|
|
|
'''
|
|
|
|
Execute the unit tests
|
|
|
|
'''
|
2012-05-05 20:42:47 +00:00
|
|
|
if not opts.unit:
|
2012-05-05 12:37:39 +00:00
|
|
|
return [True]
|
2012-05-05 11:53:39 +00:00
|
|
|
status = []
|
2012-05-30 02:56:47 +00:00
|
|
|
results = run_suite(
|
|
|
|
opts, os.path.join(TEST_DIR, 'unit'), 'Unit', '*_test.py')
|
|
|
|
status.append(results)
|
2012-05-05 11:53:39 +00:00
|
|
|
return status
|
2012-02-20 12:18:13 +00:00
|
|
|
|
2012-02-12 08:24:20 +00:00
|
|
|
|
2012-04-04 17:49:28 +00:00
|
|
|
def parse_opts():
|
|
|
|
'''
|
|
|
|
Parse command line options for running specific tests
|
|
|
|
'''
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('-m',
|
|
|
|
'--module',
|
|
|
|
'--module-tests',
|
|
|
|
dest='module',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run tests for modules')
|
2012-05-13 02:55:56 +00:00
|
|
|
parser.add_option('-S',
|
|
|
|
'--state',
|
|
|
|
'--state-tests',
|
|
|
|
dest='state',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run tests for states')
|
2012-04-04 17:49:28 +00:00
|
|
|
parser.add_option('-c',
|
|
|
|
'--client',
|
|
|
|
'--client-tests',
|
|
|
|
dest='client',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run tests for client')
|
2012-05-05 20:42:47 +00:00
|
|
|
parser.add_option('-s',
|
|
|
|
'--shell',
|
|
|
|
dest='shell',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run shell tests')
|
2012-05-28 03:01:08 +00:00
|
|
|
parser.add_option('-r',
|
|
|
|
'--runner',
|
|
|
|
dest='runner',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run runner tests')
|
2012-04-04 17:49:28 +00:00
|
|
|
parser.add_option('-u',
|
|
|
|
'--unit',
|
|
|
|
'--unit-tests',
|
|
|
|
dest='unit',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run unit tests')
|
2012-05-05 20:42:47 +00:00
|
|
|
parser.add_option('-v',
|
|
|
|
'--verbose',
|
|
|
|
dest='verbosity',
|
|
|
|
default=1,
|
|
|
|
action='count',
|
|
|
|
help='Verbose test runner output')
|
2012-05-05 22:27:16 +00:00
|
|
|
parser.add_option('-x',
|
|
|
|
'--xml',
|
|
|
|
dest='xmlout',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
2012-05-30 02:56:47 +00:00
|
|
|
help='XML test runner output')
|
|
|
|
parser.add_option('-n',
|
|
|
|
'--name',
|
|
|
|
dest='name',
|
|
|
|
default='',
|
|
|
|
help='Specific test name to run')
|
2012-07-20 06:16:14 +00:00
|
|
|
parser.add_option('--clean',
|
|
|
|
dest='clean',
|
|
|
|
default=True,
|
|
|
|
action='store_true',
|
|
|
|
help=('Clean up test environment before and after '
|
|
|
|
'integration testing (default behaviour)'))
|
|
|
|
parser.add_option('--no-clean',
|
|
|
|
dest='clean',
|
|
|
|
action='store_false',
|
|
|
|
help=('Don\'t clean up test environment before and after '
|
|
|
|
'integration testing (speed up test process)'))
|
2012-05-05 22:27:16 +00:00
|
|
|
|
2012-05-30 02:56:47 +00:00
|
|
|
options, _ = parser.parse_args()
|
2012-08-25 13:21:19 +00:00
|
|
|
|
|
|
|
# Setup minimal logging to stop errors and use if greater verbosity is used
|
|
|
|
if options.verbosity > 2:
|
|
|
|
# -vv
|
|
|
|
level = logging.INFO
|
|
|
|
if options.verbosity > 3:
|
|
|
|
# -vvv
|
|
|
|
level = logging.DEBUG
|
|
|
|
logging.basicConfig(
|
|
|
|
stream=sys.stderr, level=level,
|
|
|
|
format="[%(levelname)-8s][%(name)-10s:%(lineno)-4d] %(message)s"
|
|
|
|
)
|
|
|
|
else:
|
2012-09-20 18:16:20 +00:00
|
|
|
logging.basicConfig(stream=open(os.devnull, 'w'), level=0)
|
2012-08-25 13:21:19 +00:00
|
|
|
|
2012-05-30 02:56:47 +00:00
|
|
|
if not any((options.module, options.client,
|
|
|
|
options.shell, options.unit,
|
|
|
|
options.state, options.runner,
|
|
|
|
options.name)):
|
2012-05-05 20:42:47 +00:00
|
|
|
options.module = True
|
|
|
|
options.client = True
|
|
|
|
options.shell = True
|
|
|
|
options.unit = True
|
2012-05-28 03:01:08 +00:00
|
|
|
options.runner = True
|
2012-06-30 16:40:00 +00:00
|
|
|
options.state = True
|
2012-05-05 20:42:47 +00:00
|
|
|
return options
|
2012-04-04 17:49:28 +00:00
|
|
|
|
|
|
|
|
2012-05-28 03:01:08 +00:00
|
|
|
if __name__ == '__main__':
|
2012-04-04 17:49:28 +00:00
|
|
|
opts = parse_opts()
|
2012-05-05 11:53:39 +00:00
|
|
|
overall_status = []
|
|
|
|
status = run_integration_tests(opts)
|
|
|
|
overall_status.extend(status)
|
|
|
|
status = run_unit_tests(opts)
|
|
|
|
overall_status.extend(status)
|
|
|
|
false_count = overall_status.count(False)
|
|
|
|
if false_count > 0:
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
sys.exit(0)
|