Merge "Prohibit the importation of a list of libraries"
authorRoss Brattain <ross.b.brattain@intel.com>
Fri, 2 Mar 2018 19:29:11 +0000 (19:29 +0000)
committerGerrit Code Review <gerrit@opnfv.org>
Fri, 2 Mar 2018 19:29:11 +0000 (19:29 +0000)
yardstick/__init__.py
yardstick/cmd/cli.py
yardstick/common/exceptions.py
yardstick/common/import_tools.py [new file with mode: 0644]
yardstick/tests/unit/common/banned_modules/__init__.py [new file with mode: 0644]
yardstick/tests/unit/common/banned_modules/banned_module.py [new file with mode: 0644]
yardstick/tests/unit/common/banned_modules/importing_module.py [new file with mode: 0644]
yardstick/tests/unit/common/test_import_tools.py [new file with mode: 0644]

index f95b0a9..9eb5db9 100644 (file)
@@ -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
index 67ce114..0bc7c16 100644 (file)
@@ -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",
index 68f9995..9946bf1 100644 (file)
@@ -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 (file)
index 0000000..bf8dc7e
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
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 (file)
index 0000000..f57f939
--- /dev/null
@@ -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 (file)
index 0000000..fc2542f
--- /dev/null
@@ -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 (file)
index 0000000..c8fe5fd
--- /dev/null
@@ -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]