| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 |
# you may not use this file except in compliance with the License. |
| 3 |
# You may obtain a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 10 |
# implied. |
| 11 |
# See the License for the specific language governing permissions and |
| 12 |
# limitations under the License. |
| 13 |
|
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
from stevedore import enabled |
| 17 |
|
| 18 |
from neutron_lib.tests import _base as base |
| 19 |
from neutron_lib.utils import runtime |
| 20 |
|
| 21 |
|
| 22 |
class _DummyDriver: |
| 23 |
driver = mock.sentinel.dummy_driver |
| 24 |
|
| 25 |
|
| 26 |
class TestRunTime(base.BaseTestCase): |
| 27 |
|
| 28 |
@mock.patch.object(runtime, 'LOG') |
| 29 |
def test_load_class_by_alias_or_classname_no_name(self, mock_log): |
| 30 |
self.assertRaises( |
| 31 |
ImportError, |
| 32 |
runtime.load_class_by_alias_or_classname, 'ns', None) |
| 33 |
|
| 34 |
@mock.patch.object(runtime.driver, 'DriverManager', |
| 35 |
return_value=_DummyDriver) |
| 36 |
@mock.patch.object(runtime, 'LOG') |
| 37 |
def test_load_class_by_alias_or_classname_dummy_driver( |
| 38 |
self, mock_log, mock_driver): |
| 39 |
self.assertEqual(_DummyDriver.driver, |
| 40 |
runtime.load_class_by_alias_or_classname('ns', 'n')) |
| 41 |
|
| 42 |
@mock.patch.object(runtime, 'LOG') |
| 43 |
def test_load_class_by_alias_or_classname_bad_classname(self, mock_log): |
| 44 |
self.assertRaises( |
| 45 |
ImportError, |
| 46 |
runtime.load_class_by_alias_or_classname, 'ns', '_NoClass') |
| 47 |
|
| 48 |
@mock.patch.object(runtime.importutils, 'import_class', |
| 49 |
return_value=mock.sentinel.dummy_class) |
| 50 |
@mock.patch.object(runtime, 'LOG') |
| 51 |
def test_load_class_by_alias_or_classname_with_classname( |
| 52 |
self, mock_log, mock_import): |
| 53 |
self.assertEqual( |
| 54 |
mock.sentinel.dummy_class, |
| 55 |
runtime.load_class_by_alias_or_classname('ns', 'n')) |
| 56 |
|
| 57 |
|
| 58 |
class TestNamespacedPlugins(base.BaseTestCase): |
| 59 |
|
| 60 |
@mock.patch.object(enabled, 'EnabledExtensionManager') |
| 61 |
def test_init_reload(self, mock_mgr): |
| 62 |
plugins = runtime.NamespacedPlugins('_test_ns_') |
| 63 |
mock_mgr.assert_called_with( |
| 64 |
'_test_ns_', mock.ANY, invoke_on_load=False) |
| 65 |
mock_mgr().map.assert_called_with(plugins._add_extension) |
| 66 |
|
| 67 |
@mock.patch.object(runtime, 'LOG') |
| 68 |
@mock.patch.object(enabled, 'EnabledExtensionManager') |
| 69 |
def test_init_reload_no_plugins(self, mock_mgr, mock_log): |
| 70 |
mock_mgr().names.return_value = [] |
| 71 |
plugins = runtime.NamespacedPlugins('_test_ns_') |
| 72 |
mock_log.debug.assert_called_once() |
| 73 |
mock_mgr().map.assert_not_called() |
| 74 |
self.assertDictEqual({}, plugins._extensions) |
| 75 |
|
| 76 |
@mock.patch.object(enabled, 'EnabledExtensionManager') |
| 77 |
def test_add_duplicate_names(self, mock_mgr): |
| 78 |
mock_ep = mock.Mock() |
| 79 |
mock_ep.name = 'a' |
| 80 |
mock_mgr().names.return_value = ['a', 'a'] |
| 81 |
# return 2 EPs with the same name |
| 82 |
mock_mgr().map = lambda f: [f(ep) for ep in [mock_ep, mock_ep]] |
| 83 |
self.assertRaises(KeyError, runtime.NamespacedPlugins, '_test_ns_') |
| 84 |
|
| 85 |
@mock.patch.object(enabled, 'EnabledExtensionManager') |
| 86 |
def test_get_plugin_class(self, mock_mgr): |
| 87 |
mock_epa = mock.Mock() |
| 88 |
mock_epa.name = 'a' |
| 89 |
mock_epa.plugin = 'A' |
| 90 |
mock_epb = mock.Mock() |
| 91 |
mock_epb.name = 'b' |
| 92 |
mock_epb.plugin = 'B' |
| 93 |
mock_mgr().names.return_value = ['a', 'b'] |
| 94 |
mock_mgr().map = lambda f: [f(ep) for ep in [mock_epa, mock_epb]] |
| 95 |
|
| 96 |
plugins = runtime.NamespacedPlugins('_test_ns_') |
| 97 |
self.assertEqual('A', plugins.get_plugin_class('a')) |
| 98 |
self.assertEqual('B', plugins.get_plugin_class('b')) |
| 99 |
|
| 100 |
@mock.patch.object(enabled, 'EnabledExtensionManager') |
| 101 |
def test_new_plugin_instance(self, mock_mgr): |
| 102 |
mock_epa = mock.Mock() |
| 103 |
mock_epa.name = 'a' |
| 104 |
mock_epb = mock.Mock() |
| 105 |
mock_epb.name = 'b' |
| 106 |
mock_mgr().names.return_value = ['a', 'b'] |
| 107 |
mock_mgr().map = lambda f: [f(ep) for ep in [mock_epa, mock_epb]] |
| 108 |
|
| 109 |
plugins = runtime.NamespacedPlugins('_test_ns_') |
| 110 |
plugins.new_plugin_instance('a', 'c', 'd', karg='kval') |
| 111 |
plugins.new_plugin_instance('b') |
| 112 |
mock_epa.plugin.assert_called_once_with('c', 'd', karg='kval') |
| 113 |
mock_epb.plugin.assert_called_once_with() |
| 114 |
|
| 115 |
|
| 116 |
class TestListPackageModules(base.BaseTestCase): |
| 117 |
|
| 118 |
def test_list_package_modules(self): |
| 119 |
# mainly just to ensure we can import modules for both PY2/PY3 |
| 120 |
self.assertGreater( |
| 121 |
len(runtime.list_package_modules('neutron_lib.exceptions')), 3) |