def test_new_plugin_instance(self, mock_mgr): mock_epa = mock.Mock() mock_epa.name = 'a' mock_epb = mock.Mock() mock_epb.name = 'b' mock_mgr().names.return_value = ['a', 'b'] mock_mgr().map = lambda f: [f(ep) for ep in [mock_epa, mock_epb]] plugins = runtime.NamespacedPlugins('_test_ns_') plugins.new_plugin_instance('a', 'c', 'd', karg='kval') plugins.new_plugin_instance('b') mock_epa.plugin.assert_called_once_with('c', 'd', karg='kval') mock_epb.plugin.assert_called_once_with()
def test_get_plugin_class(self, mock_mgr): mock_epa = mock.Mock() mock_epa.name = 'a' mock_epa.plugin = 'A' mock_epb = mock.Mock() mock_epb.name = 'b' mock_epb.plugin = 'B' mock_mgr().names.return_value = ['a', 'b'] mock_mgr().map = lambda f: [f(ep) for ep in [mock_epa, mock_epb]] plugins = runtime.NamespacedPlugins('_test_ns_') self.assertEqual('A', plugins.get_plugin_class('a')) self.assertEqual('B', plugins.get_plugin_class('b'))
def load_checks(): checks = [] ns_plugin = runtime.NamespacedPlugins(CHECKS_ENTRYPOINTS) # TODO(slaweq): stop using private attribute of runtime.NamespacedPlugins # class when it will provide some better way to access extensions for module_name, module in ns_plugin._extensions.items(): try: project_checks_class = module.entry_point.load() project_checks = project_checks_class().get_checks() if project_checks: checks += project_checks except Exception as e: LOG.exception( "Checks class %(entrypoint)s failed to load. " "Error: %(err)s", { 'entrypoint': module_name, 'err': e }) continue return tuple(checks)
def test_init_reload_no_plugins(self, mock_mgr, mock_log): mock_mgr().names.return_value = [] plugins = runtime.NamespacedPlugins('_test_ns_') mock_log.debug.assert_called_once() mock_mgr().map.assert_not_called() self.assertDictEqual({}, plugins._extensions)
def test_init_reload(self, mock_mgr): plugins = runtime.NamespacedPlugins('_test_ns_') mock_mgr.assert_called_with( '_test_ns_', mock.ANY, invoke_on_load=False) mock_mgr().map.assert_called_with(plugins._add_extension)
# not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from neutron_lib.utils import runtime NEUTRON_OBJECT_NAMESPACE = 'neutron.objects' _REGISTRY = runtime.NamespacedPlugins(NEUTRON_OBJECT_NAMESPACE) def load_class(object_class_name): """Return the versioned object for the given class name. :param object_class_name: The class name of the versioned object to get. :returns: A reference to the class for the said object_class_name. """ return _REGISTRY.get_plugin_class(object_class_name) def new_instance(object_class_name, *inst_args, **inst_kwargs): """Create a new instance of a versioned object.