From 306c2408c3ea30dc53d5883cebc3550f8cb7ebbc Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Tue, 12 Dec 2017 16:18:28 +0000 Subject: Prohibit the importation of a list of libraries Some modules can't be imported and used in Yardstick. For example, "ansible" has GPLv3 license and can't be used as a library in this project without modifying Yardstick license. JIRA: YARDSTICK-896 Change-Id: I9d6bec0493ac50f1051235cc77e81059dfc3e01c Signed-off-by: Rodolfo Alonso Hernandez --- yardstick/__init__.py | 3 +- yardstick/cmd/cli.py | 7 +++- yardstick/common/exceptions.py | 5 +++ yardstick/common/import_tools.py | 31 +++++++++++++++ .../tests/unit/common/banned_modules/__init__.py | 0 .../unit/common/banned_modules/banned_module.py | 17 ++++++++ .../unit/common/banned_modules/importing_module.py | 21 ++++++++++ yardstick/tests/unit/common/test_import_tools.py | 46 ++++++++++++++++++++++ 8 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 yardstick/common/import_tools.py create mode 100644 yardstick/tests/unit/common/banned_modules/__init__.py create mode 100644 yardstick/tests/unit/common/banned_modules/banned_module.py create mode 100644 yardstick/tests/unit/common/banned_modules/importing_module.py create mode 100644 yardstick/tests/unit/common/test_import_tools.py diff --git a/yardstick/__init__.py b/yardstick/__init__.py index f95b0a906..9eb5db9b1 100644 --- a/yardstick/__init__.py +++ b/yardstick/__init__.py @@ -14,8 +14,9 @@ import errno # this module must only import other modules that do # not require loggers to be created, so this cannot -# include yardstick.common.utils from yardstick.common import constants +from yardstick.common import exceptions + try: # do not use yardstick.common.utils.makedirs diff --git a/yardstick/cmd/cli.py b/yardstick/cmd/cli.py index 67ce11451..0bc7c1617 100644 --- a/yardstick/cmd/cli.py +++ b/yardstick/cmd/cli.py @@ -28,6 +28,8 @@ from yardstick.cmd.commands import testcase from yardstick.cmd.commands import plugin from yardstick.cmd.commands import env from yardstick.cmd.commands import report +from yardstick.common import import_tools + CONF = cfg.CONF cli_opts = [ @@ -53,7 +55,8 @@ def find_config_files(path_list): return None -class YardstickCLI(): # pragma: no cover +@import_tools.decorator_banned_modules +class YardstickCLI(object): # pragma: no cover """Command-line interface to yardstick""" # Command categories @@ -108,7 +111,7 @@ class YardstickCLI(): # pragma: no cover # register subcommands to parse additional command line arguments def parser(subparsers): - self._add_command_parsers(YardstickCLI.categories, subparsers) + self._add_command_parsers(self.categories, subparsers) category_opt = cfg.SubCommandOpt("category", title="Command categories", diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index e38dd246c..8e6077b04 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -59,6 +59,11 @@ class FunctionNotImplemented(YardstickException): '"%(class_name)" class.') +class YardstickBannedModuleImported(YardstickException): + # pragma: no cover + message = 'Module "%(module)s" cannnot be imported. Reason: "%(reason)s"' + + class HeatTemplateError(YardstickException): """Error in Heat during the stack deployment""" message = ('Error in Heat during the creation of the OpenStack stack ' diff --git a/yardstick/common/import_tools.py b/yardstick/common/import_tools.py new file mode 100644 index 000000000..bf8dc7e07 --- /dev/null +++ b/yardstick/common/import_tools.py @@ -0,0 +1,31 @@ +# Copyright (c) 2018 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT 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 sys + +from yardstick.common import exceptions + + +BANNED_MODULES = {'ansible': 'Module with GPLv3 license'} + + +def decorator_banned_modules(cls): + def _class(*args, **kwargs): + for module in sys.modules: + for banned_module, reason in BANNED_MODULES.items(): + if module.startswith(banned_module): + raise exceptions.YardstickBannedModuleImported( + module=banned_module, reason=reason) + return cls(*args, **kwargs) + return _class diff --git a/yardstick/tests/unit/common/banned_modules/__init__.py b/yardstick/tests/unit/common/banned_modules/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/yardstick/tests/unit/common/banned_modules/banned_module.py b/yardstick/tests/unit/common/banned_modules/banned_module.py new file mode 100644 index 000000000..f57f939d2 --- /dev/null +++ b/yardstick/tests/unit/common/banned_modules/banned_module.py @@ -0,0 +1,17 @@ +# Copyright (c) 2018 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +class BannedClass(object): + pass diff --git a/yardstick/tests/unit/common/banned_modules/importing_module.py b/yardstick/tests/unit/common/banned_modules/importing_module.py new file mode 100644 index 000000000..fc2542f0c --- /dev/null +++ b/yardstick/tests/unit/common/banned_modules/importing_module.py @@ -0,0 +1,21 @@ +# Copyright (c) 2018 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from yardstick.common import import_tools +from yardstick.tests.unit.common.banned_modules import banned_module + + +@import_tools.decorator_banned_modules +class ImportingClass(banned_module.BannedClass): + pass diff --git a/yardstick/tests/unit/common/test_import_tools.py b/yardstick/tests/unit/common/test_import_tools.py new file mode 100644 index 000000000..c8fe5fde0 --- /dev/null +++ b/yardstick/tests/unit/common/test_import_tools.py @@ -0,0 +1,46 @@ +# Copyright (c) 2018 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT 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 unittest + +from yardstick.common import exceptions as y_exc +from yardstick.common import import_tools + + +@import_tools.decorator_banned_modules +class DummyClass(object): + pass + + +class DecoratorBannedModule(unittest.TestCase): + + MODULE = 'yardstick.tests.unit.common.banned_modules.banned_module' + + def test_passt(self): + self.assertIsNotNone(DummyClass()) + + def test_banned_module(self): + import_tools.BANNED_MODULES[self.MODULE] = 'Banned module!!' + from yardstick.tests.unit.common.banned_modules import importing_module + self.addCleanup(self._remove_module) + + with self.assertRaises(y_exc.YardstickBannedModuleImported) as exc: + importing_module.ImportingClass() + + msg = ('Module "%s" cannnot be imported. Reason: "Banned module!!"' + % self.MODULE) + self.assertEqual(msg, str(exc.exception)) + + def _remove_module(self): + del import_tools.BANNED_MODULES[self.MODULE] -- cgit 1.2.3-korg