monasca-agent/tests/test_service_plugin.py
Tomasz Trębski 0418111eaf 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
2017-03-30 06:33:59 +02:00

82 lines
2.6 KiB
Python

import monasca_setup.detection
import socket
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
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.s.bind(('', 0))
self.s.listen(1)
except socket.error as msg:
raise Exception('Bind failed. Message ' + msg[1])
(host, port_used) = self.s.getsockname()
def tearDown(self):
self.s.close()
def test_no_override_(self):
""" Test setting values with no overrides works as expected
"""
args = None
test_plugin = TestPlugin('.', args=args)
test_plugin._detect()
config = test_plugin.build_config()
http_instance = config['http_check']['instances'][0]
url = 'http://localhost:{0}/healthcheck'.format(port_used)
self.assertEqual(http_instance['url'], url)
self.assertEqual(http_instance['match_pattern'], '.*OK.*')
processes = config['process']['instances']
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,testr service_api_url='
args += ' service_api_url=' + url
args += ' search_pattern=' + pattern
test_plugin = TestPlugin('.', args=args)
test_plugin._detect()
config = test_plugin.build_config()
http_instance = config['http_check']['instances'][0]
self.assertEqual(http_instance['url'], url)
self.assertEqual(http_instance['match_pattern'], pattern)
processes = config['process']['instances']
self.assertEqual(processes[0]['search_string'], ['tox'])
self.assertEqual(processes[1]['search_string'], ['testr'])
class TestPlugin(monasca_setup.detection.ServicePlugin):
"""Test Plugin
"""
def __init__(self, template_dir, overwrite=True, args=None):
url = 'http://localhost:{0}/healthcheck'.format(port_used)
service_params = {
'args': args,
'template_dir': template_dir,
'overwrite': overwrite,
'service_name': 'os-testr',
'process_names': ['testr'],
'service_api_url': url,
'search_pattern': '.*OK.*'
}
super(TestPlugin, self).__init__(service_params)