]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
pybind/mgr/dashboard: use setUpClass for initializeing class
authorKefu Chai <kchai@redhat.com>
Fri, 16 Oct 2020 17:10:24 +0000 (01:10 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 20 Oct 2020 11:13:12 +0000 (19:13 +0800)
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>
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 722f7d86e773503a967318c67b451d1cda1d33c0..1e44f4ad4f754e9542325c43987bf35d5ea65f1f 100644 (file)
@@ -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:
index 04314b27e0c1e15e6c43c33cea45e69332debaba..b2e83379efc8de3da51b70af3ab58427a59fde23 100644 (file)
@@ -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):
index 1fdd8f96e5aabaa2fedb4d0e461381c6ea3f91f6..ed648f4c5a1c8227308ade7fc77098632b0874c8 100644 (file)
@@ -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):
index 4f9b17c03278a909b5ff84f210b6d9dd18cb0e04..dd260ffaf5d75fd12f88cb4697da8a09cc456867 100644 (file)
@@ -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):