Remove import of 'imp' module

Use importlib.util module instead of imp, since it's
being deprecated:

  DeprecationWarning: the imp module is deprecated in favour of
  importlib; see the module's documentation for alternative uses

Had to change test setup to call super() first to get around a
config issue I was seeing locally, causing the entire class of
tests to fail:

  oslo_config.cfg.NoSuchOptError: no such option api_extensions_path
  in group [DEFAULT]

Closes-bug: #1981077

Change-Id: Ic171028a661c3f9f83f6758a57aaeab4450aa907
This commit is contained in:
Brian Haley 2022-05-18 21:38:06 -04:00
parent 8ab5ee1d17
commit 357ee21d12
3 changed files with 23 additions and 6 deletions

View File

@ -14,7 +14,7 @@
# under the License.
import collections
import imp
from importlib import util as imp_util
import os
from keystoneauth1 import loading as ks_loading
@ -451,7 +451,9 @@ class ExtensionManager(object):
mod_name, file_ext = os.path.splitext(os.path.split(f)[-1])
ext_path = os.path.join(path, f)
if file_ext.lower() == '.py' and not mod_name.startswith('_'):
mod = imp.load_source(mod_name, ext_path)
spec = imp_util.spec_from_file_location(mod_name, ext_path)
mod = imp_util.module_from_spec(spec)
spec.loader.exec_module(mod)
ext_name = mod_name.capitalize()
new_ext_class = getattr(mod, ext_name, None)
if not new_ext_class:

View File

@ -102,8 +102,8 @@ class FakePluginWithExtension(service_base.ServicePluginBase):
class ExtensionPathTest(base.BaseTestCase):
def setUp(self):
self.base_path = extensions.get_extensions_path()
super(ExtensionPathTest, self).setUp()
self.base_path = extensions.get_extensions_path()
def test_get_extensions_path_with_plugins(self):
cfg.CONF.set_override('api_extensions_path',

View File

@ -134,6 +134,21 @@ class TestMaintenancePlugin(test_securitygroup.SecurityGroupTestPlugin,
sgag_def.ALIAS]
# Needed to extend resources for revision number tests, this is the
# least invasive way
class TestExtensionManager(extensions.PluginAwareExtensionManager):
def get_resources(self):
resources = super(TestExtensionManager, self).get_resources()
sg_ext_mgr = test_securitygroup.SecurityGroupTestExtensionManager
sg_resources = sg_ext_mgr.get_resources(self)
sg_resources_collection_names = [
res.collection for res in sg_resources]
resources = [r for r in resources
if r.collection not in sg_resources_collection_names]
return resources + sg_resources
class TestRevisionNumberMaintenance(test_securitygroup.SecurityGroupsTestCase,
test_l3.L3NatTestCaseMixin):
@ -141,13 +156,13 @@ class TestRevisionNumberMaintenance(test_securitygroup.SecurityGroupsTestCase,
service_plugins = {
'router':
'neutron.tests.unit.extensions.test_l3.TestL3NatServicePlugin'}
super(TestRevisionNumberMaintenance, self).setUp(
plugin=PLUGIN_CLASS, service_plugins=service_plugins)
l3_plugin = test_l3.TestL3NatServicePlugin()
sec_plugin = test_securitygroup.SecurityGroupTestPlugin()
ext_mgr = extensions.PluginAwareExtensionManager(
ext_mgr = TestExtensionManager(
EXTENSIONS_PATH, {'router': l3_plugin, 'sec': sec_plugin}
)
super(TestRevisionNumberMaintenance, self).setUp(
plugin=PLUGIN_CLASS, service_plugins=service_plugins)
app = config.load_paste_app('extensions_test_app')
self.ext_api = extensions.ExtensionMiddleware(app, ext_mgr=ext_mgr)
self.session = db_api.get_writer_session()