changes code to pass config in sender class

- This change refactor base sender to accept
  the config option in its constructor.
- The use of singleton config in the sender classes
  has been replaced with the use of self._config
- This change updates the unit tests to reflect
  the new constructor paramaters.

Change-Id: I8458145044d76b61e51462674c8fbfc84fd02b0a
Related-bug: #1746097
This commit is contained in:
Deirdre O'Connor 2018-04-10 16:29:16 +01:00 committed by sean mooney
parent ce744c83eb
commit 10e929deb1
9 changed files with 29 additions and 17 deletions

View File

@ -30,7 +30,7 @@ class Notifier(object):
def __init__(self, meters, config):
"""Initialize Notifier."""
self._meters = meters
self._sender = aodh_sender.Sender()
self._sender = aodh_sender.Sender(config)
self._config = config
def notify(self, vl, data):

View File

@ -39,12 +39,12 @@ HTTP_NOT_FOUND = 404
class Sender(common_sender.Sender):
"""Sends the JSON serialized data to Aodh."""
def __init__(self):
def __init__(self, config):
"""Create the Sender instance.
The configuration must be initialized before the object is created.
"""
super(Sender, self).__init__()
super(Sender, self).__init__(config)
self._alarm_ids = {}

View File

@ -50,7 +50,7 @@ class Sender(object):
HTTP_UNAUTHORIZED = requests.codes['UNAUTHORIZED']
HTTP_NOT_FOUND = requests.codes['NOT_FOUND']
def __init__(self):
def __init__(self, config):
"""Create the Sender instance
The configuration must be initialized before the object is created.
@ -60,6 +60,7 @@ class Sender(object):
self._auth_token = None
self._auth_lock = threading.Lock()
self._failed_auth = False
self._config = config
def _on_authenticated(self):
"""Defines an action to be taken after auth_token acquired.
@ -96,12 +97,11 @@ class Sender(object):
try:
# create a keystone client if it doesn't exist
if self._keystone is None:
cfg = Config.instance()
self._keystone = ClientV3(
auth_url=cfg.OS_AUTH_URL,
username=cfg.OS_USERNAME,
password=cfg.OS_PASSWORD,
tenant_name=cfg.OS_TENANT_NAME
auth_url=self._config.OS_AUTH_URL,
username=self._config.OS_USERNAME,
password=self._config.OS_PASSWORD,
tenant_name=self._config.OS_TENANT_NAME
)
# store the authentication token
self._auth_token = self._keystone.auth_token

View File

@ -35,7 +35,7 @@ class Sender(common_sender.Sender):
The cofinguration must be initialized before the object is created.
"""
super(Sender, self).__init__()
super(Sender, self).__init__(config)
self._meter_ids = {}
self._config = config

View File

@ -75,11 +75,12 @@ class MockedCollectd(object):
"""Get notification severity."""
def mock_config(**kwargs):
def mock_config(OS_AUTH_URL=None, OS_USERNAME=None, OS_PASSWORD=None, **kwargs):
"""Return collectd module with collectd logging hooks."""
return mock.patch(
__name__ + '.' + MockedConfig.__name__, specs=True,
**kwargs)
OS_AUTH_URL=OS_AUTH_URL, OS_USERNAME=OS_USERNAME,
OS_PASSWORD=OS_PASSWORD, **kwargs)
def config_module(

View File

@ -22,6 +22,7 @@ import requests
import unittest
from collectd_openstack.aodh import sender as aodh_sender
from collectd_openstack.common.settings import Config
from collections import OrderedDict
from collectd_openstack.common.meters import base
@ -56,7 +57,8 @@ class TestSender(unittest.TestCase):
def setUp(self):
super(TestSender, self).setUp()
self.sender = aodh_sender.Sender()
self._config = Config._decorated()
self.sender = aodh_sender.Sender(self._config)
@mock.patch.object(aodh_sender.Sender, "_get_remote_alarm_id",
autospec=True)

View File

@ -22,6 +22,7 @@ import requests
import unittest
from collectd_openstack.common import sender as common_sender
from collectd_openstack.common.settings import Config
class TestSender(unittest.TestCase):
@ -29,7 +30,8 @@ class TestSender(unittest.TestCase):
def setUp(self):
super(TestSender, self).setUp()
self.sender = common_sender.Sender()
self._config = Config._decorated()
self.sender = common_sender.Sender(self._config)
self.sender._url_base = \
"http://my-gnocchi-endpoint/v1/action"

View File

@ -70,17 +70,22 @@ class MockedCollectd(object):
"Gets a dataset."
def mock_config(BATCH_SIZE=1, **kwargs):
def mock_config(BATCH_SIZE=1, OS_AUTH_URL=None, OS_USERNAME=None,
OS_PASSWORD=None, **kwargs):
"Returns collectd module with collectd logging hooks."
return mock.patch(
__name__ + '.' + MockedConfig.__name__, specs=True,
BATCH_SIZE=BATCH_SIZE, **kwargs)
BATCH_SIZE=BATCH_SIZE, OS_AUTH_URL=OS_AUTH_URL, OS_USERNAME=OS_USERNAME,
OS_PASSWORD=OS_PASSWORD, **kwargs)
class MockedConfig(object):
"Mocked config class."
BATCH_SIZE = 1
OS_AUTH_URL = ''
OS_USERNAME = 'test'
OS_PASSWORD = 'test'
def mock_value(

View File

@ -21,6 +21,7 @@ import mock
import requests
import unittest
from collectd_openstack.common.settings import Config
from collectd_openstack.gnocchi import sender as gnocchi_sender
@ -32,7 +33,8 @@ class TestGnocchiSender(unittest.TestCase):
"""Test the sender class."""
def setUp(self):
self.sender = gnocchi_sender.Sender(config=MockedConfig)
self._config = Config._decorated()
self.sender = gnocchi_sender.Sender(self._config)
self.sender._url_base = \
"http://my-endpoint/v1/metric/%s/measures"