bus: Allow overriding discover search path
Change-Id: I0fb9ce0f6166fea48dec18b295319c6d4e001567
This commit is contained in:
@@ -21,7 +21,7 @@ import charmhelpers.core.hookenv as hookenv
|
||||
# Code below is based on charms.reactive.bus
|
||||
|
||||
|
||||
def discover():
|
||||
def discover(search_path=None):
|
||||
"""Discover Openstack handlers based on convention.
|
||||
|
||||
Handlers will be loaded from the following directory and its
|
||||
@@ -30,10 +30,14 @@ def discover():
|
||||
* ``$CHARM_DIR/lib/charm/openstack``
|
||||
|
||||
The Python files will be imported and decorated functions registered.
|
||||
|
||||
:param search_path: Override path to search for handlers.
|
||||
:type search_path: Optional[str]
|
||||
"""
|
||||
search_path = os.path.join(
|
||||
hookenv.charm_dir(), 'lib', 'charm', 'openstack')
|
||||
base_path = os.path.join(hookenv.charm_dir(), 'lib', 'charm')
|
||||
if not search_path:
|
||||
search_path = os.path.join(
|
||||
hookenv.charm_dir(), 'lib', 'charm', 'openstack')
|
||||
base_path = os.path.dirname(search_path)
|
||||
for dirpath, dirnames, filenames in os.walk(search_path):
|
||||
for filename in filenames:
|
||||
filepath = os.path.join(dirpath, filename)
|
||||
|
||||
@@ -44,6 +44,26 @@ class TestBus(unittest.TestCase):
|
||||
'/x/unit-aodh-1/charm/lib/charm/openstack/aodh.py')]
|
||||
_register_handlers_from_file.assert_has_calls(expect_calls)
|
||||
|
||||
@mock.patch.object(bus.os, 'walk')
|
||||
@mock.patch.object(bus, '_register_handlers_from_file')
|
||||
@mock.patch('charmhelpers.core.hookenv.charm_dir')
|
||||
def test_discover_search_path(self, charm_dir,
|
||||
_register_handlers_from_file, walk):
|
||||
os.walk.return_value = [(
|
||||
'/x/unit-aodh-1/charm/lib/charms',
|
||||
['__pycache__'],
|
||||
['__init__.py', 'aodh.py'])]
|
||||
|
||||
bus.discover(search_path='/x/unit-aodh-1/charm/lib/charms')
|
||||
expect_calls = [
|
||||
mock.call(
|
||||
'/x/unit-aodh-1/charm/lib',
|
||||
'/x/unit-aodh-1/charm/lib/charms/__init__.py'),
|
||||
mock.call(
|
||||
'/x/unit-aodh-1/charm/lib',
|
||||
'/x/unit-aodh-1/charm/lib/charms/aodh.py')]
|
||||
_register_handlers_from_file.assert_has_calls(expect_calls)
|
||||
|
||||
@mock.patch.object(bus.importlib, 'import_module')
|
||||
def test_load_module(self, import_module):
|
||||
import_module.side_effect = lambda x: x
|
||||
|
||||
Reference in New Issue
Block a user