Пример #1
0
    def test_relation_factory(self, relation_to_role_and_interface, charm_dir,
                              rel_mod, log, find_factory, entry_points):
        relation_to_role_and_interface.return_value = ('role', 'interface')
        charm_dir.return_value = 'charm_dir'
        rel_mod.return_value = 'module'
        find_factory.return_value = 'fact'

        class MockRelFactory:
            @classmethod
            def from_name(cls, name):
                if name == 'foo':
                    return mock.Mock()

            load = mock.Mock()

        entry_points.return_value = {
            'charms.reactive.relation_factory': [mock.Mock(load=lambda: MockRelFactory)],
        }
        relations.RelationFactory.discover()
        assert MockRelFactory.load.called

        self.assertEqual(relations.relation_factory('relname'), 'fact')
        relation_to_role_and_interface.assert_called_once_with('relname')
        rel_mod.assert_called_once_with('role', 'interface')
        find_factory.assert_called_once_with('module')
        self.assertIs(relations.RelationFactory.get_factory('foo'), MockRelFactory)
Пример #2
0
    def test_relation_factory(self, relation_to_role_and_interface,
                              charm_dir, rel_mod, log, find_factory):
        relation_to_role_and_interface.return_value = ('role', 'interface')
        charm_dir.return_value = 'charm_dir'
        rel_mod.return_value = 'module'
        find_factory.return_value = 'fact'

        self.assertEqual(relations.relation_factory('relname'), 'fact')
        relation_to_role_and_interface.assert_called_once_with('relname')
        rel_mod.assert_called_once_with('role', 'interface')
        find_factory.assert_called_once_with('module')
Пример #3
0
 def test_relation_factory_import_fail(self, relation_to_role_and_interface,
                                       charm_dir, importlib, log):
     relation_to_role_and_interface.return_value = ('role', 'interface')
     charm_dir.return_value = 'charm_dir'
     importlib.import_module.side_effect = ImportError
     self.assertIsNone(relations.relation_factory('relname'))
     relation_to_role_and_interface.assert_called_once_with('relname')
     self.assertEqual(importlib.import_module.call_args_list, [
         mock.call('reactive.relations.interface.role'),
         mock.call('relations.interface.role'),
     ])
     log.assert_called_once_with(mock.ANY, relations.hookenv.ERROR)
Пример #4
0
    def _startup(cls):
        """
        Create Endpoint instances and manage automatic flags.
        """
        for endpoint_name in sorted(hookenv.relation_types()):
            # populate context based on attached relations
            relf = relation_factory(endpoint_name)
            if not relf or not issubclass(relf, cls):
                continue

            rids = sorted(hookenv.relation_ids(endpoint_name))
            # ensure that relation IDs have the endpoint name prefix, in case
            # juju decides to drop it at some point
            rids = ['{}:{}'.format(endpoint_name, rid) if ':' not in rid
                    else rid for rid in rids]
            endpoint = relf(endpoint_name, rids)
            cls._endpoints[endpoint_name] = endpoint
            endpoint._manage_departed()
            endpoint._manage_flags()
            for relation in endpoint.relations:
                hookenv.atexit(relation._flush_data)