Use importlib to take place of imp module

The imp module is deprecated[1] since version 3.4, use importlib to
instead

[1]: https://docs.python.org/3/library/imp.html

Change-Id: Id301c0c6ffacea81e4771219beb6cbf8462c1dc4
This commit is contained in:
gugug 2020-08-30 01:04:44 +08:00
parent 4557c983ca
commit 9ed29ce4e0
2 changed files with 22 additions and 4 deletions

View File

@ -13,19 +13,28 @@
# under the License.
import copy
import imp
from importlib import util as imp_util
import os
import unittest
from ansible.errors import AnsibleActionFail
def load_module(name, path):
module_spec = imp_util.spec_from_file_location(
name, path
)
module = imp_util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
return module
# Import method lifted from kolla_ansible's test_merge_config.py
PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))
PLUGIN_FILE = os.path.join(PROJECT_DIR,
'ansible/action_plugins/tenks_update_state.py')
tus = imp.load_source('tenks_update_state', PLUGIN_FILE)
tus = load_module('tenks_update_state', PLUGIN_FILE)
class TestTenksUpdateState(unittest.TestCase):

View File

@ -13,7 +13,7 @@
# under the License.
import copy
import imp
from importlib import util as imp_util
import json
import os
import random
@ -25,13 +25,22 @@ from tests.utils import ModuleTestCase, set_module_args, AnsibleExitJson, \
AnsibleFailJson
def load_module(name, path):
module_spec = imp_util.spec_from_file_location(
name, path
)
module = imp_util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
return module
# Import method lifted from kolla_ansible's test_merge_config.py
PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))
PLUGIN_FILE = os.path.join(PROJECT_DIR,
'ansible/roles/wait-for-resources/library'
'/wait_for_resources.py')
wait_for = imp.load_source('wait_for_resources', PLUGIN_FILE)
wait_for = load_module('wait_for_resources', PLUGIN_FILE)
meets_criteria = wait_for.meets_criteria
get_providers = wait_for.get_providers