From 895fa69e750dd058063e6bf5a56149309dd03d0f Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Mon, 18 Dec 2023 11:11:03 +1100 Subject: [PATCH] python3.12: "fix" unittests These are written to import the python utility into the unittest as a module, which is then tested. Python 3.12 removed "imp" which broke this. The documentation discusses using spec_from_file_location [2] for this task, but one complication is that it only allows files with an extension of .py to load like that ... so add an additional hack so that the importlib loader will read the file. [2] https://docs.python.org/3/library/importlib.html Change-Id: I3c842b3a712ead6d2151b90171f9228caecedd11 --- .../elements/growvols/tests/test_growvols.py | 16 ++++++++++++---- .../tests/test_package_squash.py | 15 +++++++++++---- .../elements/svc-map/tests/test_data_merge.py | 16 ++++++++++++---- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/diskimage_builder/elements/growvols/tests/test_growvols.py b/diskimage_builder/elements/growvols/tests/test_growvols.py index 5817b6def..461df58c1 100644 --- a/diskimage_builder/elements/growvols/tests/test_growvols.py +++ b/diskimage_builder/elements/growvols/tests/test_growvols.py @@ -11,14 +11,22 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -import imp +import importlib import mock import os +import sys from oslotest import base -module_path = (os.path.dirname(os.path.realpath(__file__)) + - '/../static/usr/local/sbin/growvols') -growvols = imp.load_source('growvols', module_path) + +importlib.machinery.SOURCE_SUFFIXES.append('') +file_path = (os.path.dirname(os.path.realpath(__file__)) + + '/../static/usr/local/sbin/growvols') +spec = importlib.util.spec_from_file_location('growvols', file_path) +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) +sys.modules['growvols'] = module +growvols = module +importlib.machinery.SOURCE_SUFFIXES.pop() # output of lsblk -Po kname,pkname,name,label,type,fstype,mountpoint LSBLK = """KNAME="sda" PKNAME="" NAME="sda" LABEL="" TYPE="disk" FSTYPE="" MOUNTPOINT="" diff --git a/diskimage_builder/elements/package-installs/tests/test_package_squash.py b/diskimage_builder/elements/package-installs/tests/test_package_squash.py index 631c987f0..2ea0665ba 100644 --- a/diskimage_builder/elements/package-installs/tests/test_package_squash.py +++ b/diskimage_builder/elements/package-installs/tests/test_package_squash.py @@ -13,16 +13,23 @@ # under the License. import collections import functools -import imp +import importlib import mock import os +import sys from oslotest import base from testtools.matchers import Mismatch -installs_squash_src = (os.path.dirname(os.path.realpath(__file__)) + - '/../bin/package-installs-squash') -installs_squash = imp.load_source('installs_squash', installs_squash_src) +importlib.machinery.SOURCE_SUFFIXES.append('') +file_path = (os.path.dirname(os.path.realpath(__file__)) + + '/../bin/package-installs-squash') +spec = importlib.util.spec_from_file_location('installs_squash', file_path) +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) +sys.modules['installs_squash'] = module +installs_squash = module +importlib.machinery.SOURCE_SUFFIXES.pop() class IsMatchingInstallList(object): diff --git a/diskimage_builder/elements/svc-map/tests/test_data_merge.py b/diskimage_builder/elements/svc-map/tests/test_data_merge.py index 2e016a8c1..a53b9f298 100644 --- a/diskimage_builder/elements/svc-map/tests/test_data_merge.py +++ b/diskimage_builder/elements/svc-map/tests/test_data_merge.py @@ -11,13 +11,21 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -import imp +import importlib import os +import sys from oslotest import base -module_path = (os.path.dirname(os.path.realpath(__file__)) + - '/../extra-data.d/10-merge-svc-map-files') -service_map = imp.load_source('service_map', module_path) + +importlib.machinery.SOURCE_SUFFIXES.append('') +file_path = (os.path.dirname(os.path.realpath(__file__)) + + '/../extra-data.d/10-merge-svc-map-files') +spec = importlib.util.spec_from_file_location('service_map', file_path) +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) +sys.modules['service_map'] = module +service_map = module +importlib.machinery.SOURCE_SUFFIXES.pop() class TestDataMerge(base.BaseTestCase):