Allow overriding values using args in ServicePlugin

Some of the subclasses of ServicePlugin allow overriding the
service_api_url using args. This could be useful for all
plugins so move that code into ServicePlugin.

Also allow overriding of process_names, search_pattern,
overwrite and template_dir.

Remove the code in the ironic and trove plugins that duplicated
this code.

Added a unit test for this code.

Change-Id: Ibd97effa6d5bff0cf621e4bdcbe6f5b781d195f4
This commit is contained in:
Craig Bryant
2015-10-30 12:39:29 -06:00
parent 5486add7a2
commit 032c043727
4 changed files with 104 additions and 28 deletions

View File

@@ -9,18 +9,6 @@ class Ironic(monasca_setup.detection.ServicePlugin):
"""Detect Ironic daemons and setup configuration to monitor them."""
def __init__(self, template_dir, overwrite=True, args=None):
service_api_url = "http://localhost:6385"
if isinstance(args, str):
try:
# Turn 'service_api_url=url' into
# dict {'service_api_url':'url'}
args_dict = dict([item.split('=') for item
in args.split()])
if "service_api_url" in args_dict:
service_api_url = args_dict['service_api_url']
except Exception:
log.exception('Error parsing detection arguments')
service_params = {
'args': args,
@@ -28,7 +16,7 @@ class Ironic(monasca_setup.detection.ServicePlugin):
'overwrite': overwrite,
'service_name': 'Baremetal',
'process_names': ['ironic-api', 'ironic-conductor'],
'service_api_url': service_api_url,
'service_api_url': "http://localhost:6385",
'search_pattern': '.*200 OK.*',
}

View File

@@ -10,26 +10,13 @@ class Trove(monasca_setup.detection.ServicePlugin):
def __init__(self, template_dir, overwrite=True, args=None):
service_api_url = "http://localhost:8779"
if isinstance(args, str):
try:
# Turn 'service_api_url=url' into
# dict {'service_api_url':'url'}
args_dict = dict([item.split('=') for item
in args.split()])
if "service_api_url" in args_dict:
service_api_url = args_dict['service_api_url']
except Exception:
log.exception('Error parsing detection arguments')
service_params = {
'args': args,
'template_dir': template_dir,
'overwrite': overwrite,
'service_name': 'database',
'process_names': ['trove-api', 'trove-taskmanager', 'trove-conductor'],
'service_api_url': service_api_url,
'service_api_url': "http://localhost:8779",
'search_pattern': '.*v1.*'
}

View File

@@ -26,8 +26,32 @@ class ServicePlugin(Plugin):
self.process_names = kwargs['process_names']
self.service_api_url = kwargs.get('service_api_url')
self.search_pattern = kwargs['search_pattern']
overwrite = kwargs['overwrite']
template_dir = kwargs['template_dir'],
if 'args' in kwargs:
args = kwargs['args']
if isinstance(args, str):
try:
# Turn 'service_api_url=url' into
# dict {'service_api_url':'url'}
args_dict = dict([item.split('=') for item
in args.split()])
super(ServicePlugin, self).__init__(kwargs['template_dir'], kwargs['overwrite'], kwargs.get('args'))
# Allow args to override all of these parameters
if 'process_names' in args_dict:
self.process_names = args_dict['process_names'].split(',')
if 'service_api_url' in args_dict:
self.service_api_url = args_dict['service_api_url']
if 'search_pattern' in args_dict:
self.search_pattern = args_dict['search_pattern']
if 'overwrite' in args_dict:
overwrite = args_dict['overwrite']
if 'template_dir' in args_dict:
template_dir = args_dict['template_dir']
except Exception:
log.exception('Error parsing detection arguments')
super(ServicePlugin, self).__init__(template_dir, overwrite, kwargs.get('args'))
def _detect(self):
"""Run detection.

View File

@@ -0,0 +1,77 @@
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.assertEquals(http_instance['url'], url)
self.assertEquals(http_instance['match_pattern'], '.*OK.*')
processes = config['process']['instances']
self.assertEquals(processes[0]['search_string'], ['nose'])
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 += ' 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.assertEquals(http_instance['url'], url)
self.assertEquals(http_instance['match_pattern'], pattern)
processes = config['process']['instances']
self.assertEquals(processes[0]['search_string'], ['tox'])
self.assertEquals(processes[1]['search_string'], ['nose'])
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': 'object-storage',
'process_names': ['nose'],
'service_api_url': url,
'search_pattern': '.*OK.*'
}
super(TestPlugin, self).__init__(service_params)