2011-12-22 01:36:26 +00:00
|
|
|
#!/usr/bin/env python
|
2012-09-29 11:10:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-12-22 01:36:26 +00:00
|
|
|
'''
|
|
|
|
Discover all instances of unittest.TestCase in this directory.
|
|
|
|
'''
|
2012-02-12 08:24:20 +00:00
|
|
|
# Import python libs
|
2014-02-03 11:07:11 +00:00
|
|
|
from __future__ import print_function
|
2012-02-12 08:24:20 +00:00
|
|
|
import os
|
2012-09-03 16:42:36 +00:00
|
|
|
import resource
|
2012-09-30 09:40:06 +00:00
|
|
|
import tempfile
|
2012-08-11 00:28:49 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
2013-11-27 12:54:13 +00:00
|
|
|
from integration import TestDaemon, TMP # pylint: disable=W0403
|
2013-06-24 22:53:59 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2013-06-25 12:18:11 +00:00
|
|
|
from salttesting.parser import PNUM, print_header
|
|
|
|
from salttesting.parser.cover import SaltCoverageTestingParser
|
2012-05-05 22:27:16 +00:00
|
|
|
|
2012-02-12 23:03:31 +00:00
|
|
|
TEST_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
|
2013-06-02 08:07:39 +00:00
|
|
|
SALT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
2013-02-02 18:07:07 +00:00
|
|
|
XML_OUTPUT_DIR = os.environ.get(
|
2013-02-02 18:27:59 +00:00
|
|
|
'SALT_XML_TEST_REPORTS_DIR',
|
2013-02-02 18:07:07 +00:00
|
|
|
os.path.join(TMP, 'xml-test-reports')
|
|
|
|
)
|
2013-06-24 18:37:07 +00:00
|
|
|
HTML_OUTPUT_DIR = os.environ.get(
|
|
|
|
'SALT_HTML_TEST_REPORTS_DIR',
|
|
|
|
os.path.join(TMP, 'html-test-reports')
|
|
|
|
)
|
2012-10-02 12:04:22 +00:00
|
|
|
|
2013-06-02 08:07:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
if SALT_ROOT:
|
|
|
|
os.chdir(SALT_ROOT)
|
2013-06-03 17:45:44 +00:00
|
|
|
except OSError as err:
|
2014-02-03 11:07:11 +00:00
|
|
|
print('Failed to change directory to salt\'s source: {0}'.format(err))
|
2013-06-02 08:07:39 +00:00
|
|
|
|
2012-11-06 16:02:36 +00:00
|
|
|
REQUIRED_OPEN_FILES = 3072
|
2012-04-04 16:58:11 +00:00
|
|
|
|
2012-02-12 08:24:20 +00:00
|
|
|
|
2013-06-25 12:18:11 +00:00
|
|
|
class SaltTestsuiteParser(SaltCoverageTestingParser):
|
2013-11-08 20:20:03 +00:00
|
|
|
support_docker_execution = True
|
2013-08-06 21:03:42 +00:00
|
|
|
support_destructive_tests_selection = True
|
2013-11-08 20:20:03 +00:00
|
|
|
source_code_basedir = SALT_ROOT
|
2013-01-30 20:10:45 +00:00
|
|
|
|
2013-06-24 18:37:07 +00:00
|
|
|
def setup_additional_options(self):
|
|
|
|
self.add_option(
|
|
|
|
'--sysinfo',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Print some system information.'
|
2013-01-30 20:42:30 +00:00
|
|
|
)
|
2012-08-25 13:21:19 +00:00
|
|
|
|
2013-06-24 18:37:07 +00:00
|
|
|
self.test_selection_group.add_option(
|
|
|
|
'-m',
|
|
|
|
'--module',
|
|
|
|
'--module-tests',
|
|
|
|
dest='module',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run tests for modules'
|
|
|
|
)
|
|
|
|
self.test_selection_group.add_option(
|
|
|
|
'-S',
|
|
|
|
'--state',
|
|
|
|
'--state-tests',
|
|
|
|
dest='state',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run tests for states'
|
|
|
|
)
|
|
|
|
self.test_selection_group.add_option(
|
|
|
|
'-c',
|
|
|
|
'--client',
|
|
|
|
'--client-tests',
|
|
|
|
dest='client',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run tests for client'
|
|
|
|
)
|
|
|
|
self.test_selection_group.add_option(
|
|
|
|
'-s',
|
|
|
|
'--shell',
|
|
|
|
dest='shell',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run shell tests'
|
|
|
|
)
|
|
|
|
self.test_selection_group.add_option(
|
|
|
|
'-r',
|
|
|
|
'--runner',
|
|
|
|
dest='runner',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run runner tests'
|
|
|
|
)
|
2013-10-26 17:57:00 +00:00
|
|
|
self.test_selection_group.add_option(
|
|
|
|
'-l',
|
|
|
|
'--loader',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run loader tests'
|
|
|
|
)
|
2013-06-24 18:37:07 +00:00
|
|
|
self.test_selection_group.add_option(
|
|
|
|
'-u',
|
|
|
|
'--unit',
|
|
|
|
'--unit-tests',
|
|
|
|
dest='unit',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run unit tests'
|
|
|
|
)
|
2014-05-10 13:12:04 +00:00
|
|
|
self.test_selection_group.add_option(
|
|
|
|
'--ssh',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Run salt-ssh tests. These tests will spin up a temporary '
|
|
|
|
'SSH server on your machine. In certain environments, this '
|
|
|
|
'may be insecure! Default: $default'
|
|
|
|
)
|
2013-06-24 18:37:07 +00:00
|
|
|
self.output_options_group.add_option(
|
|
|
|
'--no-colors',
|
|
|
|
'--no-colours',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Disable colour printing.'
|
2012-10-02 12:04:22 +00:00
|
|
|
)
|
|
|
|
|
2013-06-24 18:37:07 +00:00
|
|
|
def validate_options(self):
|
2013-06-25 12:24:32 +00:00
|
|
|
if self.options.coverage and any((
|
2013-06-25 12:18:11 +00:00
|
|
|
self.options.module, self.options.client, self.options.shell,
|
|
|
|
self.options.unit, self.options.state, self.options.runner,
|
2013-10-26 17:57:00 +00:00
|
|
|
self.options.loader, self.options.name, os.geteuid() != 0,
|
2013-06-25 12:18:11 +00:00
|
|
|
not self.options.run_destructive)):
|
2013-06-24 18:37:07 +00:00
|
|
|
self.error(
|
2013-06-25 12:18:11 +00:00
|
|
|
'No sense in generating the tests coverage report when '
|
|
|
|
'not running the full test suite, including the '
|
|
|
|
'destructive tests, as \'root\'. It would only produce '
|
|
|
|
'incorrect results.'
|
2012-10-02 12:04:22 +00:00
|
|
|
)
|
2013-06-24 18:37:07 +00:00
|
|
|
|
|
|
|
# Set test suite defaults if no specific suite options are provided
|
|
|
|
if not any((self.options.module, self.options.client,
|
|
|
|
self.options.shell, self.options.unit, self.options.state,
|
2013-10-26 17:57:00 +00:00
|
|
|
self.options.runner, self.options.loader,
|
|
|
|
self.options.name)):
|
2013-06-24 18:37:07 +00:00
|
|
|
self.options.module = True
|
|
|
|
self.options.client = True
|
|
|
|
self.options.shell = True
|
|
|
|
self.options.unit = True
|
|
|
|
self.options.runner = True
|
|
|
|
self.options.state = True
|
2013-10-26 17:57:00 +00:00
|
|
|
self.options.loader = True
|
2013-06-24 18:37:07 +00:00
|
|
|
|
2013-06-25 12:18:11 +00:00
|
|
|
self.start_coverage(
|
|
|
|
branch=True,
|
2013-06-26 20:16:39 +00:00
|
|
|
source=[os.path.join(SALT_ROOT, 'salt')],
|
2013-06-25 12:18:11 +00:00
|
|
|
)
|
2013-06-24 18:37:07 +00:00
|
|
|
|
|
|
|
def run_integration_suite(self, suite_folder, display_name):
|
|
|
|
'''
|
|
|
|
Run an integration test suite
|
|
|
|
'''
|
|
|
|
path = os.path.join(TEST_DIR, 'integration', suite_folder)
|
|
|
|
return self.run_suite(path, display_name)
|
|
|
|
|
|
|
|
def run_integration_tests(self):
|
|
|
|
'''
|
|
|
|
Execute the integration tests suite
|
|
|
|
'''
|
2013-08-21 09:47:18 +00:00
|
|
|
named_tests = []
|
|
|
|
named_unit_test = []
|
2013-11-08 20:20:03 +00:00
|
|
|
|
|
|
|
if self.options.name:
|
|
|
|
for test in self.options.name:
|
|
|
|
if test.startswith('unit.'):
|
|
|
|
named_unit_test.append(test)
|
|
|
|
continue
|
|
|
|
named_tests.append(test)
|
2013-08-21 09:47:18 +00:00
|
|
|
|
|
|
|
if (self.options.unit or named_unit_test) and not \
|
2013-07-04 16:17:48 +00:00
|
|
|
(self.options.runner or
|
|
|
|
self.options.state or
|
|
|
|
self.options.module or
|
2013-08-12 10:46:58 +00:00
|
|
|
self.options.client or
|
2013-10-26 17:57:00 +00:00
|
|
|
self.options.loader or
|
2013-08-21 09:47:18 +00:00
|
|
|
named_tests):
|
2013-07-04 16:17:48 +00:00
|
|
|
# We're either not running any of runner, state, module and client
|
|
|
|
# tests, or, we're only running unittests by passing --unit or by
|
|
|
|
# passing only `unit.<whatever>` to --name.
|
|
|
|
# We don't need the tests daemon running
|
2013-06-24 18:37:07 +00:00
|
|
|
return [True]
|
|
|
|
|
|
|
|
smax_open_files, hmax_open_files = resource.getrlimit(
|
|
|
|
resource.RLIMIT_NOFILE
|
|
|
|
)
|
|
|
|
if smax_open_files < REQUIRED_OPEN_FILES:
|
|
|
|
print(
|
2013-12-11 14:19:08 +00:00
|
|
|
' * Max open files setting is too low({0}) for running the '
|
2013-06-24 18:37:07 +00:00
|
|
|
'tests'.format(smax_open_files)
|
|
|
|
)
|
|
|
|
print(
|
2013-12-11 14:19:08 +00:00
|
|
|
' * Trying to raise the limit to {0}'.format(REQUIRED_OPEN_FILES)
|
2013-06-24 18:37:07 +00:00
|
|
|
)
|
|
|
|
if hmax_open_files < 4096:
|
|
|
|
hmax_open_files = 4096 # Decent default?
|
|
|
|
try:
|
|
|
|
resource.setrlimit(
|
|
|
|
resource.RLIMIT_NOFILE,
|
|
|
|
(REQUIRED_OPEN_FILES, hmax_open_files)
|
|
|
|
)
|
|
|
|
except Exception as err:
|
|
|
|
print(
|
|
|
|
'ERROR: Failed to raise the max open files setting -> '
|
|
|
|
'{0}'.format(err)
|
|
|
|
)
|
|
|
|
print('Please issue the following command on your console:')
|
|
|
|
print(' ulimit -n {0}'.format(REQUIRED_OPEN_FILES))
|
|
|
|
self.exit()
|
|
|
|
finally:
|
2014-01-14 15:25:07 +00:00
|
|
|
print('~' * getattr(self.options, 'output_columns', PNUM))
|
|
|
|
|
|
|
|
try:
|
|
|
|
print_header(
|
2014-02-09 00:21:18 +00:00
|
|
|
' * Setting up Salt daemons to execute tests',
|
2014-01-14 15:25:07 +00:00
|
|
|
top=False, width=getattr(self.options, 'output_columns', PNUM)
|
|
|
|
)
|
|
|
|
except TypeError:
|
2014-02-09 00:21:18 +00:00
|
|
|
print_header(' * Setting up Salt daemons to execute tests', top=False)
|
2013-06-24 18:37:07 +00:00
|
|
|
|
|
|
|
status = []
|
|
|
|
if not any([self.options.client, self.options.module,
|
|
|
|
self.options.runner, self.options.shell,
|
2013-10-26 17:57:00 +00:00
|
|
|
self.options.state, self.options.loader,
|
|
|
|
self.options.name]):
|
2013-06-24 18:37:07 +00:00
|
|
|
return status
|
2013-08-12 10:46:58 +00:00
|
|
|
|
2013-06-24 18:37:07 +00:00
|
|
|
with TestDaemon(self):
|
|
|
|
if self.options.name:
|
|
|
|
for name in self.options.name:
|
2013-08-21 09:47:18 +00:00
|
|
|
if name.startswith('unit.'):
|
|
|
|
continue
|
2013-08-12 10:46:58 +00:00
|
|
|
results = self.run_suite('', name, load_from_name=True)
|
2013-06-24 18:37:07 +00:00
|
|
|
status.append(results)
|
2013-10-26 17:57:00 +00:00
|
|
|
if self.options.loader:
|
|
|
|
status.append(self.run_integration_suite('loader', 'Loader'))
|
2013-06-24 18:37:07 +00:00
|
|
|
if self.options.runner:
|
|
|
|
status.append(self.run_integration_suite('runners', 'Runner'))
|
|
|
|
if self.options.module:
|
|
|
|
status.append(self.run_integration_suite('modules', 'Module'))
|
|
|
|
if self.options.state:
|
|
|
|
status.append(self.run_integration_suite('states', 'State'))
|
|
|
|
if self.options.client:
|
|
|
|
status.append(self.run_integration_suite('client', 'Client'))
|
|
|
|
if self.options.shell:
|
|
|
|
status.append(self.run_integration_suite('shell', 'Shell'))
|
|
|
|
return status
|
2012-10-04 23:52:00 +00:00
|
|
|
|
2013-06-24 18:37:07 +00:00
|
|
|
def run_unit_tests(self):
|
|
|
|
'''
|
|
|
|
Execute the unit tests
|
|
|
|
'''
|
2013-08-21 09:47:18 +00:00
|
|
|
named_unit_test = []
|
2013-11-08 20:20:03 +00:00
|
|
|
if self.options.name:
|
|
|
|
for test in self.options.name:
|
|
|
|
if not test.startswith('unit.'):
|
|
|
|
continue
|
|
|
|
named_unit_test.append(test)
|
2013-08-21 09:47:18 +00:00
|
|
|
|
|
|
|
if not self.options.unit and not named_unit_test:
|
2013-07-04 23:53:41 +00:00
|
|
|
# We are not explicitly running the unit tests and none of the
|
|
|
|
# names passed to --name is a unit test.
|
2013-06-24 18:37:07 +00:00
|
|
|
return [True]
|
2013-07-04 23:53:41 +00:00
|
|
|
|
2013-06-24 18:37:07 +00:00
|
|
|
status = []
|
2013-08-12 10:46:58 +00:00
|
|
|
if self.options.unit:
|
|
|
|
results = self.run_suite(
|
|
|
|
os.path.join(TEST_DIR, 'unit'), 'Unit', '*_test.py'
|
|
|
|
)
|
|
|
|
status.append(results)
|
|
|
|
# We executed ALL unittests, we can skip running unittests by name
|
|
|
|
# bellow
|
|
|
|
return status
|
|
|
|
|
2013-08-21 09:47:18 +00:00
|
|
|
for name in named_unit_test:
|
|
|
|
results = self.run_suite(
|
|
|
|
os.path.join(TEST_DIR, 'unit'), name, load_from_name=True
|
|
|
|
)
|
|
|
|
status.append(results)
|
2013-06-24 18:37:07 +00:00
|
|
|
return status
|
2012-09-30 09:40:06 +00:00
|
|
|
|
2013-06-24 18:37:07 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
'''
|
|
|
|
Parse command line options for running specific tests
|
|
|
|
'''
|
|
|
|
parser = SaltTestsuiteParser(
|
|
|
|
TEST_DIR,
|
|
|
|
xml_output_dir=XML_OUTPUT_DIR,
|
|
|
|
tests_logfile=os.path.join(tempfile.gettempdir(), 'salt-runtests.log')
|
|
|
|
)
|
|
|
|
parser.parse_args()
|
|
|
|
|
|
|
|
overall_status = []
|
|
|
|
status = parser.run_integration_tests()
|
|
|
|
overall_status.extend(status)
|
|
|
|
status = parser.run_unit_tests()
|
|
|
|
overall_status.extend(status)
|
|
|
|
false_count = overall_status.count(False)
|
|
|
|
|
2012-05-05 11:53:39 +00:00
|
|
|
if false_count > 0:
|
2013-06-24 18:37:07 +00:00
|
|
|
parser.finalize(1)
|
|
|
|
parser.finalize(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|