Repository: juju-solutions/charms.reactive

Issue:
0.4.6 breaks charms.openstack (AttributeError: 'NoneType' object has no attribute 'relation_name')
#### Built with charms.reactive 0.4.5: Apr 26 (ok):
https://openstack-ci-reports.ubuntu.com/artifacts/test_charm_pipeline_amulet_full/openstack/charm-tempest/458466/5/16/index.html

#### Built with charms.reactive 0.4.6: Today (fail):
https://openstack-ci-reports.ubuntu.com/artifacts/test_charm_pipeline/openstack/charm-tempest/458466/5/206/index.html

The tempest-0-var-log.tar.bz2  artifact is of interest for both.

Discussion:

Patch:
diff --git a/charms/reactive/relations.py b/charms/reactive/relations.py
index f81b23c..969e7af 100644
--- a/charms/reactive/relations.py
+++ b/charms/reactive/relations.py
@@ -14,7 +14,9 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
+import os
 import sys
+import importlib
 from inspect import isclass
 
 from charmhelpers.core import hookenv
@@ -25,6 +27,7 @@ from charms.reactive.bus import get_state
 from charms.reactive.bus import set_state
 from charms.reactive.bus import remove_state
 from charms.reactive.bus import StateList
+from charms.reactive.bus import _append_path
 
 
 # arbitrary obj instances to use as defaults instead of None
@@ -200,10 +203,14 @@ class RelationBase(object, metaclass=AutoAccessors):
         """
         # The module has already been discovered and imported.
         module = 'relations.{}.{}'.format(interface, role)
-        if module in sys.modules:
-            return cls._find_subclass(sys.modules[module])
-        else:
-            return None
+        if module not in sys.modules:
+            try:
+                _append_path(hookenv.charm_dir())
+                _append_path(os.path.join(hookenv.charm_dir(), 'hooks'))
+                importlib.import_module(module)
+            except ImportError:
+                return None
+        return cls._find_subclass(sys.modules[module])
 
     @classmethod
     def _find_subclass(cls, module):


Test patch:
diff --git a/tests/test_relations.py b/tests/test_relations.py
index 8f84616..48a664d 100644
--- a/tests/test_relations.py
+++ b/tests/test_relations.py
@@ -14,6 +14,7 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
+import os
 import sys
 import mock
 import unittest
@@ -98,8 +99,23 @@ class TestRelationBase(unittest.TestCase):
         self.assertEqual(res.conversations(), ['conv.join'])
 
     @mock.patch.dict('sys.modules')
+    @mock.patch.object(relations.Conversation, 'join')
+    @mock.patch.object(relations, 'hookenv')
+    def test_cold_import(self, hookenv, conv_join):
+        tests_dir = os.path.dirname(__file__)
+        hookenv.charm_dir.return_value = os.path.join(tests_dir, 'data')
+        sys.modules.pop('relations.hyphen-ated.peer', None)
+        hookenv.relation_to_role_and_interface.return_value = ('peer',
+                                                               'hyphen-ated')
+        relations.RelationBase._cache.clear()
+        assert relations.RelationBase.from_name('test') is not None
+
+    @mock.patch.dict('sys.modules')
+    @mock.patch.object(relations, 'hookenv')
     @mock.patch.object(relations.RelationBase, '_find_subclass')
-    def test_find_impl(self, find_subclass):
+    def test_find_impl(self, find_subclass, hookenv):
+        tests_dir = os.path.dirname(__file__)
+        hookenv.charm_dir.return_value = os.path.join(tests_dir, 'data')
         self.assertIsNone(relations.RelationBase._find_impl('role',
                                                             'interface'))
         assert not find_subclass.called