Merge "replace imp module"

This commit is contained in:
Zuul 2020-11-16 05:20:55 +00:00 committed by Gerrit Code Review
commit 3847aab594
1 changed files with 16 additions and 4 deletions

View File

@ -12,11 +12,13 @@
# under the License. # under the License.
import copy import copy
import imp import importlib
import json import json
import logging import logging
import six import six
import sys import sys
from multiprocessing import Lock
from unittest import mock from unittest import mock
from tests import common from tests import common
@ -24,6 +26,15 @@ from tests import common
log = logging.getLogger('test_hook_chef') log = logging.getLogger('test_hook_chef')
def load_module(name, path):
module_spec = importlib.util.spec_from_file_location(
name, path
)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
return module
@mock.patch("os.chdir") @mock.patch("os.chdir")
@mock.patch("os.makedirs") @mock.patch("os.makedirs")
@mock.patch('subprocess.Popen') @mock.patch('subprocess.Popen')
@ -67,11 +78,12 @@ class HookChefTest(common.RunScriptTest):
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
def get_module(self): def get_module(self):
lock = Lock()
try: try:
imp.acquire_lock() lock.acquire()
return imp.load_source("hook_chef", self.hook_path) return load_module("hook_chef", self.hook_path)
finally: finally:
imp.release_lock() lock.release()
def test_hook(self, mock_popen, mock_mkdirs, mock_chdir): def test_hook(self, mock_popen, mock_mkdirs, mock_chdir):
data = copy.deepcopy(self.data) data = copy.deepcopy(self.data)