From b0258314501d8df54bfaaf841be8c8326b018601 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Fri, 19 Jan 2018 11:36:25 +0000 Subject: Replace "oslo_utils.importutils" with standard library "importlib" The current implementation of dynamic library importation is prone to failure [1]: - "sys.modules" is modified manually, which is something not recommended [2]. - When a module is imported is added to "sys.modules"; that means there is no need to manually create an entry in this object. - "importlib" library is part of the standard library and is now available in PY3 and PY2 (backported). This library contains a function called "import_module" to import a module in runtime. [1]https://github.com/opnfv/yardstick/blob/d2c7cc4e9768ed003257a95c92cdb278d516761b/yardstick/common/utils.py#L72-L93 [2]http://justus.science/blog/2015/04/19/sys.modules-is-dangerous.html JIRA: YARDSTICK-949 Change-Id: Ide3b74f98858d06fa275fb6c9b78ceeaa64feed5 Signed-off-by: Rodolfo Alonso Hernandez --- yardstick/tests/unit/common/test_utils.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'yardstick/tests/unit/common/test_utils.py') diff --git a/yardstick/tests/unit/common/test_utils.py b/yardstick/tests/unit/common/test_utils.py index 452b93a56..033bb0243 100644 --- a/yardstick/tests/unit/common/test_utils.py +++ b/yardstick/tests/unit/common/test_utils.py @@ -7,12 +7,9 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -# Unittest for yardstick.common.utils - -from __future__ import absolute_import - from copy import deepcopy import errno +import importlib import ipaddress from itertools import product, chain import mock @@ -60,8 +57,8 @@ class ImportModulesFromPackageTestCase(unittest.TestCase): utils.import_modules_from_package('foo.bar') @mock.patch('yardstick.common.utils.os.walk') - @mock.patch('yardstick.common.utils.importutils') - def test_import_modules_from_package(self, mock_importutils, mock_walk): + @mock.patch.object(importlib, 'import_module') + def test_import_modules_from_package(self, mock_import_module, mock_walk): yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__)) mock_walk.return_value = ([ @@ -69,7 +66,7 @@ class ImportModulesFromPackageTestCase(unittest.TestCase): ]) utils.import_modules_from_package('foo.bar') - mock_importutils.import_module.assert_called_with('bar.baz') + mock_import_module.assert_called_once_with('bar.baz') class GetParaFromYaml(unittest.TestCase): -- cgit 1.2.3-korg