]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr/dashboard: use setUpClass for initializeing class 40657/head
authorKefu Chai <kchai@redhat.com>
Fri, 16 Oct 2020 17:10:24 +0000 (01:10 +0800)
committerBrad Hubbard <bhubbard@redhat.com>
Thu, 13 May 2021 03:14:42 +0000 (13:14 +1000)
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 <kchai@redhat.com>
(cherry picked from commit 71979e9b46e21dc3d3cfc6f06f4a84c9b4c7ce78)

 Conflicts:
src/pybind/mgr/dashboard/tests/test_api_auditing.py:
          Differences in import lines
src/pybind/mgr/dashboard/tests/test_tools.py:
          Differences in import lines

src/pybind/mgr/dashboard/tests/__init__.py
src/pybind/mgr/dashboard/tests/test_api_auditing.py
src/pybind/mgr/dashboard/tests/test_settings.py
src/pybind/mgr/dashboard/tests/test_tools.py

index 00bfcda3eabf0b49edd8ef12ce5496a646d0a478..c29cab412d7bff746fe106d35141fb3ef0e043c5 100644 (file)
@@ -19,6 +19,7 @@ from .. import mgr
 from ..controllers import json_error_page, generate_controller_routes
 from ..services.auth import AuthManagerTool
 from ..services.exception import dashboard_exception_handler
+from ..tools import RequestLoggingTool
 
 from ..plugins import PLUGIN_MANAGER
 from ..plugins import feature_toggles, debug  # noqa
@@ -133,7 +134,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)
@@ -143,7 +148,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:
index 023ea7992a1f7a1b2f9b124f842ee63dea1faf2f..abfa3bd73edc82a5c03cf044fce212d6c35c3fc7 100644 (file)
@@ -3,7 +3,7 @@ from __future__ import absolute_import
 
 import re
 import json
-import cherrypy
+
 try:
     import mock
 except ImportError:
@@ -11,7 +11,6 @@ except ImportError:
 
 from . import ControllerTestCase, KVStoreMockMixin  # pylint: disable=no-name-in-module
 from ..controllers import RESTController, Controller
-from ..tools import RequestLoggingTool
 from .. import mgr
 
 
@@ -33,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):
index abe2558cd0cc0e5db96d3afde5a4f72207e10e98..6ef9f8f74e03c04dc9278420f1df6e22c866205f 100644 (file)
@@ -135,7 +135,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):
index 340ff8c41d5e43ec45cfc79c005aae33260b2537..b9d6a43f89e4df7a708b9260666bb7178a06fab5 100644 (file)
@@ -11,11 +11,10 @@ except ImportError:
     from unittest.mock import patch
 
 from . import ControllerTestCase  # pylint: disable=no-name-in-module
+from ..tools import dict_contains_path, dict_get, json_str_to_object, partial_dict
 from ..services.exception import handle_rados_error
 from ..controllers import RESTController, ApiController, Controller, \
                           BaseController, Proxy
-from ..tools import dict_contains_path, json_str_to_object, partial_dict,\
-                    dict_get, RequestLoggingTool
 
 
 # pylint: disable=W0613
@@ -151,10 +150,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):