From 71979e9b46e21dc3d3cfc6f06f4a84c9b4c7ce78 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 17 Oct 2020 01:10:24 +0800 Subject: [PATCH] pybind/mgr/dashboard: use setUpClass for initializeing class instead of relying on __init__(), use setUpClass() to initialize class for testing. it turns out in pytest > 4, __init__() is called for the test class but the attributes of the instantiated class is in turn overriden. so we have to use setUpClass to do this job. Signed-off-by: Kefu Chai --- src/pybind/mgr/dashboard/tests/__init__.py | 17 +++++++++++++++-- .../mgr/dashboard/tests/test_api_auditing.py | 8 +------- src/pybind/mgr/dashboard/tests/test_settings.py | 12 ++++++++++++ src/pybind/mgr/dashboard/tests/test_tools.py | 8 ++------ 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/pybind/mgr/dashboard/tests/__init__.py b/src/pybind/mgr/dashboard/tests/__init__.py index 722f7d86e77..1e44f4ad4f7 100644 --- a/src/pybind/mgr/dashboard/tests/__init__.py +++ b/src/pybind/mgr/dashboard/tests/__init__.py @@ -18,6 +18,7 @@ from ..controllers import generate_controller_routes, json_error_page from ..plugins import PLUGIN_MANAGER, debug, feature_toggles # noqa from ..services.auth import AuthManagerTool from ..services.exception import dashboard_exception_handler +from ..tools import RequestLoggingTool PLUGIN_MANAGER.hook.init() PLUGIN_MANAGER.hook.register_commands() @@ -124,7 +125,11 @@ class ControllerTestCase(helper.CPWebCase): cherrypy.tree.mount(None, config={ base_url: {'request.dispatch': mapper}}) - def __init__(self, *args, **kwargs): + _request_logging = False + + @classmethod + def setUpClass(cls): + super().setUpClass() cherrypy.tools.authenticate = AuthManagerTool() cherrypy.tools.dashboard_exception_handler = HandlerWrapperTool(dashboard_exception_handler, priority=31) @@ -134,7 +139,15 @@ class ControllerTestCase(helper.CPWebCase): 'tools.json_in.force': False }) PLUGIN_MANAGER.hook.configure_cherrypy(config=cherrypy.config) - super(ControllerTestCase, self).__init__(*args, **kwargs) + + if cls._request_logging: + cherrypy.tools.request_logging = RequestLoggingTool() + cherrypy.config.update({'tools.request_logging.on': True}) + + @classmethod + def tearDownClass(cls): + if cls._request_logging: + cherrypy.config.update({'tools.request_logging.on': False}) def _request(self, url, method, data=None, headers=None): if not data: diff --git a/src/pybind/mgr/dashboard/tests/test_api_auditing.py b/src/pybind/mgr/dashboard/tests/test_api_auditing.py index 04314b27e0c..b2e83379efc 100644 --- a/src/pybind/mgr/dashboard/tests/test_api_auditing.py +++ b/src/pybind/mgr/dashboard/tests/test_api_auditing.py @@ -4,8 +4,6 @@ from __future__ import absolute_import import json import re -import cherrypy - try: import mock except ImportError: @@ -13,7 +11,6 @@ except ImportError: from .. import mgr from ..controllers import Controller, RESTController -from ..tools import RequestLoggingTool from . import ControllerTestCase, KVStoreMockMixin # pylint: disable=no-name-in-module @@ -35,10 +32,7 @@ class FooResource(RESTController): class ApiAuditingTest(ControllerTestCase, KVStoreMockMixin): - def __init__(self, *args, **kwargs): - cherrypy.tools.request_logging = RequestLoggingTool() - cherrypy.config.update({'tools.request_logging.on': True}) - super(ApiAuditingTest, self).__init__(*args, **kwargs) + _request_logging = True @classmethod def setup_server(cls): diff --git a/src/pybind/mgr/dashboard/tests/test_settings.py b/src/pybind/mgr/dashboard/tests/test_settings.py index 1fdd8f96e5a..ed648f4c5a1 100644 --- a/src/pybind/mgr/dashboard/tests/test_settings.py +++ b/src/pybind/mgr/dashboard/tests/test_settings.py @@ -104,7 +104,19 @@ class SettingsControllerTest(ControllerTestCase, KVStoreMockMixin): SettingsController._cp_config['tools.authenticate.on'] = False cls.setup_controllers([SettingsController]) + @classmethod + def setUpClass(cls): + super().setUpClass() + # pylint: disable=protected-access + settings.Options.GRAFANA_API_HOST = ('localhost', str) + settings.Options.GRAFANA_ENABLED = (False, bool) + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + def setUp(self): + super().setUp() self.mock_kv_store() def test_settings_list(self): diff --git a/src/pybind/mgr/dashboard/tests/test_tools.py b/src/pybind/mgr/dashboard/tests/test_tools.py index 4f9b17c0327..dd260ffaf5d 100644 --- a/src/pybind/mgr/dashboard/tests/test_tools.py +++ b/src/pybind/mgr/dashboard/tests/test_tools.py @@ -13,8 +13,7 @@ except ImportError: from ..controllers import ApiController, BaseController, Controller, Proxy, RESTController from ..services.exception import handle_rados_error -from ..tools import RequestLoggingTool, dict_contains_path, dict_get, \ - json_str_to_object, partial_dict +from ..tools import dict_contains_path, dict_get, json_str_to_object, partial_dict from . import ControllerTestCase # pylint: disable=no-name-in-module @@ -150,10 +149,7 @@ class RESTControllerTest(ControllerTestCase): class RequestLoggingToolTest(ControllerTestCase): - def __init__(self, *args, **kwargs): - cherrypy.tools.request_logging = RequestLoggingTool() - cherrypy.config.update({'tools.request_logging.on': True}) - super(RequestLoggingToolTest, self).__init__(*args, **kwargs) + _request_logging = True @classmethod def setup_server(cls): -- 2.39.5