Merge "Allow list of tests specified on command line"

This commit is contained in:
Jenkins 2016-01-14 15:48:11 +00:00 committed by Gerrit Code Review
commit b900331db3
5 changed files with 71 additions and 19 deletions

View File

@ -143,11 +143,17 @@ def main():
help=('if omitted default locations are checked. '
'Check documentation for searched paths')
)
parser.add_argument(
group = parser.add_mutually_exclusive_group()
group.add_argument(
'-p', '--profile', dest='profile',
action='store', default=None, type=str,
help='test set profile in config to use (defaults to all tests)'
)
group.add_argument(
'-t', '--tests', dest='tests',
action='store', default=None, type=str,
help='list of test names to run'
)
parser.add_argument(
'-l', '--level', dest='severity', action='count',
default=1, help=('results severity filter. Show only issues of a given'
@ -225,9 +231,11 @@ def main():
log_format = b_conf.get_option('log_format')
_init_logger(debug, log_format=log_format)
profile_name = args.tests.split(',') if args.tests else args.profile
try:
b_mgr = b_manager.BanditManager(b_conf, args.agg_type, args.debug,
profile_name=args.profile,
profile_name=profile_name,
verbose=args.verbose,
ignore_nosec=args.ignore_nosec)
except utils.ProfileNotFound as e:

View File

@ -27,6 +27,8 @@ class Manager(object):
self.plugins = list(self.plugins_mgr)
self.plugin_names = self.plugins_mgr.names()
self.plugins_by_id = {p.plugin._test_id: p for p in self.plugins}
self.plugin_name_to_id = {
p.name: p.plugin._test_id for p in self.plugins}
def load_formatters(self, formatters_namespace):
self.formatters_mgr = extension.ExtensionManager(
@ -46,6 +48,8 @@ class Manager(object):
verify_requirements=False,
)
def get_plugin_id(self, plugin_name):
return self.plugin_name_to_id.get(plugin_name)
# Using entry-points and pkg_resources *can* be expensive. So let's load these
# once, store them on the object, and have a module global object for

View File

@ -65,16 +65,10 @@ class BanditManager():
self.metrics = metrics.Metrics()
# if the profile name was specified, try to find it in the config
if profile_name:
if profile_name in self.b_conf.config['profiles']:
profile = self.b_conf.config['profiles'][profile_name]
logger.debug(
"read in profile '%s': %s",
profile_name, profile
)
else:
raise utils.ProfileNotFound(self.b_conf.config_file,
profile_name)
if isinstance(profile_name, list):
profile = {'include': profile_name}
elif profile_name:
profile = self._get_profile(profile_name)
else:
profile = None
@ -85,6 +79,14 @@ class BanditManager():
self.progress = b_constants.progress_increment
self.scores = []
def _get_profile(self, profile_name):
if profile_name not in self.b_conf.config['profiles']:
raise utils.ProfileNotFound(self.b_conf.config_file, profile_name)
profile = self.b_conf.config['profiles'][profile_name]
logger.debug("read in profile '%s': %s", profile_name, profile)
return profile
def get_issue_list(self,
sev_level=b_constants.LOW,
conf_level=b_constants.LOW):

View File

@ -88,19 +88,23 @@ class BanditTestSet():
# copy of tests dictionary for removing tests from
temp_dict = copy.deepcopy(self.tests)
extmgr = self._get_extension_manager()
# if the include list is empty, we don't have to do anything, if it
# isn't, we need to remove all tests except the ones in the list
if include_list:
for check_type in self.tests:
for test_name in self.tests[check_type]:
if test_name not in include_list:
if ((test_name not in include_list and
extmgr.get_plugin_id(test_name) not in include_list)):
del temp_dict[check_type][test_name]
# remove the items specified in exclude list
if exclude_list:
for check_type in self.tests:
for test_name in self.tests[check_type]:
if test_name in exclude_list:
if ((test_name in exclude_list or
extmgr.get_plugin_id(test_name) in exclude_list)):
del temp_dict[check_type][test_name]
# copy tests back over from temp copy
@ -114,7 +118,7 @@ class BanditTestSet():
return extension_loader.MANAGER
def load_tests(self, filter=None):
'''Loads all tests in the plugins directory into testsdictionary.'''
'''Loads all tests in the plugins directory into tests dictionary.'''
self.tests = dict()
extmgr = self._get_extension_manager()
@ -177,4 +181,7 @@ class BanditTestSet():
@property
def has_tests(self):
return bool(self.tests)
result = False
for check_type in self.tests:
result = result or self.tests[check_type]
return result

View File

@ -88,8 +88,7 @@ class ManagerTests(testtools.TestCase):
self.assertEqual(m.debug, False)
self.assertEqual(m.verbose, False)
self.assertEqual(m.agg_type, 'file')
self.assertTrue(self.manager.has_tests)
self.assertTrue(m.has_tests)
def test_create_manager_with_profile_bad(self):
try:
@ -102,6 +101,39 @@ class ManagerTests(testtools.TestCase):
self.assertTrue(err.startswith(
"Unable to find profile (Bad) in config file:"))
def test_create_manager_with_test_id_list(self):
# make sure we can create a manager
m = manager.BanditManager(config=self.config, agg_type='file',
debug=False, verbose=False,
profile_name=['B108','B501'])
self.assertEqual(m.debug, False)
self.assertEqual(m.verbose, False)
self.assertEqual(m.agg_type, 'file')
self.assertTrue(m.has_tests)
def test_create_manager_with_test_name_list(self):
# make sure we can create a manager
m = manager.BanditManager(config=self.config, agg_type='file',
debug=False, verbose=False,
profile_name=['exec_used','paramiko_calls'])
self.assertEqual(m.debug, False)
self.assertEqual(m.verbose, False)
self.assertEqual(m.agg_type, 'file')
self.assertTrue(m.has_tests)
def test_create_manager_with_invalid_test_list(self):
# make sure we can create a manager
m = manager.BanditManager(config=self.config, agg_type='file',
debug=False, verbose=False,
profile_name=['bogus'])
self.assertEqual(m.debug, False)
self.assertEqual(m.verbose, False)
self.assertEqual(m.agg_type, 'file')
self.assertFalse(m.has_tests)
def test_matches_globlist(self):
self.assertTrue(manager._matches_glob_list('test', ['*tes*']))
self.assertFalse(manager._matches_glob_list('test', ['*fes*']))
@ -127,7 +159,6 @@ class ManagerTests(testtools.TestCase):
self.assertFalse(c)
self.assertFalse(d)
@mock.patch('os.walk')
def test_get_files_from_dir(self, os_walk):
os_walk.return_value = [