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
This commit is contained in:
Ian Wienand 2023-12-18 11:11:03 +11:00
parent 72513f6bdf
commit 895fa69e75
No known key found for this signature in database
3 changed files with 35 additions and 12 deletions

View File

@ -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=""

View File

@ -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):

View File

@ -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):