From 3c3c5179586f4ceebd70f0fee7e127f636b1c1af Mon Sep 17 00:00:00 2001 From: Michal Nasiadka Date: Thu, 28 Mar 2024 09:10:33 +0100 Subject: [PATCH] tests: Replace imp with importlib.machinery It's needed for Python3.12 support, because imp has been dropped [1]. Also shlex dropped s=None support [2]. [1]: https://docs.python.org/3/whatsnew/3.12.html [2]: https://github.com/python/cpython/issues/94352 Change-Id: I23f37897ea08ac708f6df485f699122df647e552 --- ansible/module_utils/kolla_podman_worker.py | 3 ++- tests/kolla_container_tests/test_docker_worker.py | 6 +++--- tests/kolla_container_tests/test_podman_worker.py | 6 +++--- tests/kolla_container_tests/test_systemd.py | 5 +++-- tests/test_fernet_rotate_cron_generator.py | 4 ++-- tests/test_kolla_container.py | 6 +++--- tests/test_merge_config.py | 5 +++-- tests/test_merge_yaml.py | 4 ++-- 8 files changed, 21 insertions(+), 18 deletions(-) diff --git a/ansible/module_utils/kolla_podman_worker.py b/ansible/module_utils/kolla_podman_worker.py index dd215174e9..1cba08a127 100644 --- a/ansible/module_utils/kolla_podman_worker.py +++ b/ansible/module_utils/kolla_podman_worker.py @@ -78,7 +78,8 @@ class PodmanWorker(ContainerWorker): ) command = self.params.pop('command', '') - self.params['command'] = shlex.split(command) + if command: + self.params['command'] = shlex.split(command) # we have to transform volumes into mounts because podman-py # functionality is broken diff --git a/tests/kolla_container_tests/test_docker_worker.py b/tests/kolla_container_tests/test_docker_worker.py index 7c7909e6aa..dc3064c08b 100644 --- a/tests/kolla_container_tests/test_docker_worker.py +++ b/tests/kolla_container_tests/test_docker_worker.py @@ -15,7 +15,7 @@ import copy -import imp +from importlib.machinery import SourceFileLoader import os import sys from unittest import mock @@ -32,8 +32,8 @@ kolla_container_file = os.path.join(ansible_dir, 'library', 'kolla_container.py') docker_worker_file = os.path.join(ansible_dir, 'module_utils', 'kolla_docker_worker.py') -kc = imp.load_source('kolla_container', kolla_container_file) -dwm = imp.load_source('kolla_docker_worker', docker_worker_file) +kc = SourceFileLoader('kolla_container', kolla_container_file).load_module() +dwm = SourceFileLoader('kolla_docker_worker', docker_worker_file).load_module() FAKE_DATA = { diff --git a/tests/kolla_container_tests/test_podman_worker.py b/tests/kolla_container_tests/test_podman_worker.py index 6767a91008..e1fd358a84 100644 --- a/tests/kolla_container_tests/test_podman_worker.py +++ b/tests/kolla_container_tests/test_podman_worker.py @@ -14,7 +14,7 @@ import copy -import imp +from importlib.machinery import SourceFileLoader import os import sys import unittest @@ -31,8 +31,8 @@ kolla_container_file = os.path.join(ansible_dir, 'library', 'kolla_container.py') podman_worker_file = os.path.join(ansible_dir, 'module_utils', 'kolla_podman_worker.py') -kc = imp.load_source('kolla_container', kolla_container_file) -pwm = imp.load_source('kolla_podman_worker', podman_worker_file) +kc = SourceFileLoader('kolla_container', kolla_container_file).load_module() +pwm = SourceFileLoader('kolla_podman_worker', podman_worker_file).load_module() FAKE_DATA = { 'params': { diff --git a/tests/kolla_container_tests/test_systemd.py b/tests/kolla_container_tests/test_systemd.py index 9f2b624f06..b1a2b6fdeb 100644 --- a/tests/kolla_container_tests/test_systemd.py +++ b/tests/kolla_container_tests/test_systemd.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import imp +from importlib.machinery import SourceFileLoader import os import sys from unittest import mock @@ -26,7 +26,8 @@ this_dir = os.path.dirname(sys.modules[__name__].__file__) ansible_dir = os.path.join(this_dir, '..', '..', 'ansible') systemd_worker_file = os.path.join(ansible_dir, 'module_utils', 'kolla_systemd_worker.py') -swm = imp.load_source('kolla_systemd_worker', systemd_worker_file) +swm = SourceFileLoader('kolla_systemd_worker', + systemd_worker_file).load_module() class TestSystemd(base.BaseTestCase): diff --git a/tests/test_fernet_rotate_cron_generator.py b/tests/test_fernet_rotate_cron_generator.py index 6e01f12d85..29c91be639 100644 --- a/tests/test_fernet_rotate_cron_generator.py +++ b/tests/test_fernet_rotate_cron_generator.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import imp +from importlib.machinery import SourceFileLoader import os import sys @@ -22,7 +22,7 @@ this_dir = os.path.dirname(sys.modules[__name__].__file__) file_under_test = os.path.join(this_dir, '..', 'ansible', 'roles', 'keystone', 'files', 'fernet_rotate_cron_generator.py') -generator = imp.load_source('generator', file_under_test) +generator = SourceFileLoader('generator', file_under_test).load_module() class FernetRotateCronGeneratorTest(base.BaseTestCase): diff --git a/tests/test_kolla_container.py b/tests/test_kolla_container.py index 0fe257bd00..3c8ee0f093 100644 --- a/tests/test_kolla_container.py +++ b/tests/test_kolla_container.py @@ -15,7 +15,7 @@ # FIXME(yoctozepto): tests do not imitate how ansible would handle module args -import imp +from importlib.machinery import SourceFileLoader import os import sys from unittest import mock @@ -28,8 +28,8 @@ kolla_container_file = os.path.join(ansible_dir, 'library', 'kolla_container.py') docker_worker_file = os.path.join(ansible_dir, 'module_utils', 'kolla_docker_worker.py') -kc = imp.load_source('kolla_container', kolla_container_file) -dwm = imp.load_source('kolla_docker_worker', docker_worker_file) +kc = SourceFileLoader('kolla_container', kolla_container_file).load_module() +dwm = SourceFileLoader('kolla_docker_worker', docker_worker_file).load_module() class ModuleArgsTest(base.BaseTestCase): diff --git a/tests/test_merge_config.py b/tests/test_merge_config.py index 1a4cb3bc44..d64b2ae6b9 100644 --- a/tests/test_merge_config.py +++ b/tests/test_merge_config.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import imp +from importlib.machinery import SourceFileLoader import os from io import StringIO @@ -25,7 +25,8 @@ MERGE_CONFIG_FILE = os.path.join(PROJECT_DIR, 'ansible/action_plugins/merge_configs.py') -merge_configs = imp.load_source('merge_configs', MERGE_CONFIG_FILE) +merge_configs = SourceFileLoader('merge_configs', + MERGE_CONFIG_FILE).load_module() TESTA = '''[DEFAULT] key1 = b diff --git a/tests/test_merge_yaml.py b/tests/test_merge_yaml.py index 5c6baa1820..d6b74ae777 100644 --- a/tests/test_merge_yaml.py +++ b/tests/test_merge_yaml.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import imp +from importlib.machinery import SourceFileLoader import os from ansible.errors import AnsibleModuleError @@ -22,7 +22,7 @@ from oslotest import base PROJECT_DIR = os.path.abspath(os.path.join(os. path.dirname(__file__), '../')) MERGE_YAML_FILE = os.path.join(PROJECT_DIR, 'ansible/action_plugins/merge_yaml.py') -merge_yaml = imp.load_source('merge_yaml', MERGE_YAML_FILE) +merge_yaml = SourceFileLoader('merge_yaml', MERGE_YAML_FILE).load_module() class MergeYamlConfigTest(base.BaseTestCase):