From ea5dd0120602358a57aad950322c5f1c19ad8fb1 Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Tue, 19 Nov 2019 11:07:27 +0100 Subject: [PATCH] bus: Allow overriding discover search path Change-Id: I0fb9ce0f6166fea48dec18b295319c6d4e001567 --- charms_openstack/bus.py | 12 ++++++++---- unit_tests/test_charms_openstack_bus.py | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/charms_openstack/bus.py b/charms_openstack/bus.py index c452522..1b646fd 100644 --- a/charms_openstack/bus.py +++ b/charms_openstack/bus.py @@ -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) diff --git a/unit_tests/test_charms_openstack_bus.py b/unit_tests/test_charms_openstack_bus.py index 59ae460..4306f01 100644 --- a/unit_tests/test_charms_openstack_bus.py +++ b/unit_tests/test_charms_openstack_bus.py @@ -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