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
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
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)
'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:
import re
import json
-import cherrypy
+
try:
import mock
except ImportError:
from . import ControllerTestCase, KVStoreMockMixin # pylint: disable=no-name-in-module
from ..controllers import RESTController, Controller
-from ..tools import RequestLoggingTool
from .. import mgr
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):
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):
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
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):