Extend CI for monasca-agent

Following commit does several things:

* changes nose to ostestr
* enables coverage
* adds flake8 for tests
* adds bandit

Bandit note:
Multiple asserts of bandit had to be disabled at this
point because fixing them was not obvious. Several simple
asserts like B110 [try_except_pass] were fixed with

Closes-Bug: #1628740
Change-Id: I640857349008178e8a6f565e31ca2fde26ce8da7
This commit is contained in:
Tomasz Trębski 2016-09-29 10:25:11 +09:00
parent 34b0f185d8
commit 0418111eaf
13 changed files with 87 additions and 22 deletions

4
.gitignore vendored
View File

@ -13,3 +13,7 @@ AUTHORS
ChangeLog
*.egg*
.*.sw*
.coverage
htmlcov/
.testrepository/
cover/

9
.testr.conf Normal file
View File

@ -0,0 +1,9 @@
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-160} \
${PYTHON:-python} -m subunit.run discover -t ./ ${OS_TEST_PATH:-./tests} $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list
group_regex=tests(?:\.|_)([^_]+)

View File

@ -115,7 +115,7 @@ class HAProxy(AgentCheck):
try:
# Try converting to a long, if failure, just leave it
val = float(val)
except Exception:
except Exception: # nosec
pass
data_dict[fields[i]] = val

View File

@ -78,7 +78,7 @@ class Jenkins(AgentCheck):
.find('hudson.plugins.git.Branch') \
.find('name') \
.text
except Exception:
except Exception: # nosec
pass
return d
@ -102,7 +102,7 @@ class Jenkins(AgentCheck):
# If we can't get build metadata, we try the previous one
try:
build_metadata = self._get_build_metadata(dir_name)
except Exception:
except Exception: # nosec
continue
output = {

View File

@ -174,7 +174,7 @@ class Memcache(AgentCheck):
# See https://github.com/DataDog/dd-agent/issues/278 for details.
try:
memcache.Client.debuglog = None
except Exception:
except Exception: # nosec
pass
port = int(instance.get('port', self.DEFAULT_PORT))

View File

@ -58,7 +58,7 @@ class WrapNagios(ServicesCheck):
last_run_path = self.init_config.get('temp_file_path')
# Use a default last_run_file if no temp_file is specified in the YAML
if last_run_path is None:
last_run_path = '/dev/shm/'
last_run_path = '/dev/shm/' # nosec
if last_run_path.endswith('/') is False:
last_run_path += '/'

View File

@ -207,7 +207,7 @@ SELECT relname,
raise ImportError(
"psycopg2 library cannot be imported. Please check the installation instruction on the Datadog Website.")
if host == 'localhost' and password == '':
if host == 'localhost' and password == '': # nosec
# Use ident method
connection = pg.connect("user=%s dbname=%s" % (user, dbname))
elif port != '':

View File

@ -322,6 +322,6 @@ if __name__ == '__main__':
# Try our best to log the error.
try:
log.exception("Uncaught error running the Agent")
except Exception:
except Exception: # nosec
pass
raise

View File

@ -279,8 +279,9 @@ class JMXFetch(object):
os.kill(pid, signal.SIGTERM)
JMXFetch.pid_file.clean()
try:
os.remove(os.path.join(tempfile.gettempdir(), PYTHON_JMX_STATUS_FILE))
except Exception:
os.remove(os.path.join(tempfile.gettempdir(),
PYTHON_JMX_STATUS_FILE))
except Exception: # nosec
pass
log.info("Success")
except Exception:

View File

@ -29,7 +29,7 @@ opt_group = cfg.OptGroup(name='vmware',
OPTS = [
cfg.HostAddressOpt('host_ip',
default='0.0.0.0',
default='0.0.0.0', # nosec
help='IP address of the VMware Vsphere host.'),
cfg.IntOpt('host_port',
default=443,

View File

@ -2,7 +2,9 @@
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
hacking>=0.12.0,!=0.13.0,<0.14 # Apache-2.0
flake8<2.6.0,>=2.5.4 # MIT
nose # LGPL
bandit>=1.1.0 # Apache-2.0
mock>=2.0 # BSD
coverage>=4.0 # Apache-2.0
oslotest>=1.10.0 # Apache-2.0
os-testr>=0.8.0 # Apache-2.0
prometheus_client

View File

@ -6,7 +6,10 @@ import unittest
from monasca_agent.common.keystone import Keystone
port_used = 0
class TestKeystone(unittest.TestCase):
def setUp(self):
global port_used
# Create a server socket so the htto check config gets created
@ -34,14 +37,14 @@ class TestKeystone(unittest.TestCase):
self.assertEqual(http_instance['match_pattern'], '.*OK.*')
processes = config['process']['instances']
self.assertEqual(processes[0]['search_string'], ['nose'])
self.assertEqual(processes[0]['search_string'], ['testr'])
def test_override_values(self):
""" Test overriding values using args works
"""
url = 'http://localhost:{0}/othercheck'.format(port_used)
pattern = 'CHECK.*'
args = 'process_names=tox,nose service_api_url='
args = 'process_names=tox,testr service_api_url='
args += ' service_api_url=' + url
args += ' search_pattern=' + pattern
test_plugin = TestPlugin('.', args=args)
@ -54,7 +57,8 @@ class TestKeystone(unittest.TestCase):
processes = config['process']['instances']
self.assertEqual(processes[0]['search_string'], ['tox'])
self.assertEqual(processes[1]['search_string'], ['nose'])
self.assertEqual(processes[1]['search_string'], ['testr'])
class TestPlugin(monasca_setup.detection.ServicePlugin):
@ -68,8 +72,8 @@ class TestPlugin(monasca_setup.detection.ServicePlugin):
'args': args,
'template_dir': template_dir,
'overwrite': overwrite,
'service_name': 'object-storage',
'process_names': ['nose'],
'service_name': 'os-testr',
'process_names': ['testr'],
'service_api_url': url,
'search_pattern': '.*OK.*'
}

55
tox.ini
View File

@ -1,6 +1,6 @@
[tox]
envlist = py27,pep8
minversion = 2.0
envlist = py27,pep8,cover
minversion = 2.5
skipsdist = True
[testenv]
@ -19,14 +19,59 @@ deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
whitelist_externals = bash
find
rm
install_command = {toxinidir}/tools/tox_install.sh {env:UPPER_CONSTRAINTS_FILE:https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt} {opts} {packages}
commands =
find . -type f -name "*.pyc" -delete
nosetests tests/ --verbose
[testenv:py27]
basepython = python2.7
commands =
{[testenv]commands}
ostestr {posargs}
[testenv:cover]
basepython = python2.7
commands =
{[testenv]commands}
coverage erase
python setup.py test --coverage --testr-args='{posargs}' --coverage-package-name=monasca_agent
coverage report
[testenv:bandit]
commands =
# B101 - asserts used on purpose
# Following rules should be fixed in future
# B602[ subprocess_popen_with_shell_equals_true ] - removed when fixed
# B603[ subprocess_without_shell_equals_true ] - removed when fixed
# B301[ pickle ] - removed when fixed
# B303[ insecure MD5 usage ] - removed when fixed
# B311[ random ] - removed when fixed
# B403[ import_pickle ] - removed when fixed
# B404[ import_subprocess ] - removed when fixed
# B405[ import_xml_etree] - removed when fixed
# B310[ urllib_urlopen ] - removed when fixed
# B320[ xml_bad_tree ] - removed when fixed
# B410[ import_lxml ] - removed when fixed
# B411[ import_xmlrpclib ] - removed when fixed
# B605[ start_process_with_a_shell ] - removed when fixed
# B607[ start_process_with_partial_path ] - removed when fixed
# B608[ hardcoded_sql_expressions ] - removed when fixed
# B501[ request_with_no_cert_validation ] - removed when fixed
# B504[ ssl_with_no_version ] - removed when fixed
bandit -r monasca_agent -n5 -s B101,B602,B603,B301,B303,B311,B403,B404,B405,B310,B320,B410,B411,B501,B504,B605,B607,B608 -x {toxinidir}/tests
[testenv:flake8]
commands =
flake8 monasca_agent
flake8 tests
[testenv:pep8]
basepython = python2.7
commands = flake8
deps =
{[testenv]deps}
commands =
{[testenv:flake8]commands}
{[testenv:bandit]commands}
[testenv:venv]
commands = {posargs}