From 9ed29ce4e05d33c73d6f15e1bb7a8fcca803a263 Mon Sep 17 00:00:00 2001 From: gugug Date: Sun, 30 Aug 2020 01:04:44 +0800 Subject: [PATCH] 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 --- tests/test_tenks_update_state.py | 13 +++++++++++-- tests/test_tenks_wait_for_resources.py | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/tests/test_tenks_update_state.py b/tests/test_tenks_update_state.py index 2a973be..e1a15fe 100644 --- a/tests/test_tenks_update_state.py +++ b/tests/test_tenks_update_state.py @@ -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): diff --git a/tests/test_tenks_wait_for_resources.py b/tests/test_tenks_wait_for_resources.py index 27a7dbd..049c60e 100644 --- a/tests/test_tenks_wait_for_resources.py +++ b/tests/test_tenks_wait_for_resources.py @@ -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