From fb92b9ba2a2454ebc891260d01e43ce2035b6047 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Mon, 11 Mar 2019 15:45:40 +0000 Subject: [PATCH] mgr/dashboard: tests: update tests that mock mons kv store Signed-off-by: Ricardo Dias --- .../dashboard/tests/test_access_control.py | 24 +++---------- .../mgr/dashboard/tests/test_api_auditing.py | 19 ++--------- .../dashboard/tests/test_feature_toggles.py | 14 ++++---- .../mgr/dashboard/tests/test_ganesha.py | 16 ++------- .../mgr/dashboard/tests/test_settings.py | 34 +++---------------- src/pybind/mgr/dashboard/tests/test_sso.py | 24 ++----------- 6 files changed, 24 insertions(+), 107 deletions(-) diff --git a/src/pybind/mgr/dashboard/tests/test_access_control.py b/src/pybind/mgr/dashboard/tests/test_access_control.py index e4fb7ea916a..5061e9f43f6 100644 --- a/src/pybind/mgr/dashboard/tests/test_access_control.py +++ b/src/pybind/mgr/dashboard/tests/test_access_control.py @@ -7,7 +7,8 @@ import json import time import unittest -from . import CmdException, exec_dashboard_cmd +from . import CmdException +from .helper import CLICommandTestMixin from .. import mgr from ..security import Scope, Permission from ..services.access_control import load_access_control_db, \ @@ -15,34 +16,17 @@ from ..services.access_control import load_access_control_db, \ SYSTEM_ROLES -class AccessControlTest(unittest.TestCase): - CONFIG_KEY_DICT = {} - - @classmethod - def mock_set_module_option(cls, attr, val): - cls.CONFIG_KEY_DICT[attr] = val - - @classmethod - def mock_get_module_option(cls, attr, default=None): - return cls.CONFIG_KEY_DICT.get(attr, default) +class AccessControlTest(unittest.TestCase, CLICommandTestMixin): @classmethod def setUpClass(cls): - mgr.set_module_option.side_effect = cls.mock_set_module_option - mgr.get_module_option.side_effect = cls.mock_get_module_option - # kludge below - mgr.set_store.side_effect = cls.mock_set_module_option - mgr.get_store.side_effect = cls.mock_get_module_option + cls.mock_kv_store() mgr.ACCESS_CONTROL_DB = None def setUp(self): self.CONFIG_KEY_DICT.clear() load_access_control_db() - @classmethod - def exec_cmd(cls, cmd, **kwargs): - return exec_dashboard_cmd(None, cmd, **kwargs) - def load_persistent_db(self): config_key = AccessControlDB.accessdb_config_key() self.assertIn(config_key, self.CONFIG_KEY_DICT) diff --git a/src/pybind/mgr/dashboard/tests/test_api_auditing.py b/src/pybind/mgr/dashboard/tests/test_api_auditing.py index ed5362d9a3d..7021d9a5941 100644 --- a/src/pybind/mgr/dashboard/tests/test_api_auditing.py +++ b/src/pybind/mgr/dashboard/tests/test_api_auditing.py @@ -6,7 +6,7 @@ import json import cherrypy import mock -from .helper import ControllerTestCase +from .helper import ControllerTestCase, KVStoreMockMixin from ..controllers import RESTController, Controller from ..tools import RequestLoggingTool from .. import mgr @@ -28,32 +28,19 @@ class FooResource(RESTController): pass -class ApiAuditingTest(ControllerTestCase): - settings = {} +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) - @classmethod - def mock_set_module_option(cls, key, val): - cls.settings[key] = val - - @classmethod - def mock_get_module_option(cls, key, default=None): - return cls.settings.get(key, default) - - @classmethod - def setUpClass(cls): - mgr.get_module_option.side_effect = cls.mock_get_module_option - mgr.set_module_option.side_effect = cls.mock_set_module_option - @classmethod def setup_server(cls): cls.setup_controllers([FooResource]) def setUp(self): + self.mock_kv_store() mgr.cluster_log = mock.Mock() mgr.set_module_option('AUDIT_API_ENABLED', True) mgr.set_module_option('AUDIT_API_LOG_PAYLOAD', True) diff --git a/src/pybind/mgr/dashboard/tests/test_feature_toggles.py b/src/pybind/mgr/dashboard/tests/test_feature_toggles.py index d959b82f370..4d659dc39e0 100644 --- a/src/pybind/mgr/dashboard/tests/test_feature_toggles.py +++ b/src/pybind/mgr/dashboard/tests/test_feature_toggles.py @@ -4,21 +4,19 @@ from __future__ import absolute_import import unittest from mock import Mock, patch +from .helper import KVStoreMockMixin from ..plugins.feature_toggles import FeatureToggles, Features -class SettingsTest(unittest.TestCase): - CONFIG = { - 'url_prefix': '', - } - +class SettingsTest(unittest.TestCase, KVStoreMockMixin): @classmethod def setUpClass(cls): + cls.mock_kv_store() + cls.CONFIG_KEY_DICT['url_prefix'] = '' + # Mock MODULE_OPTIONS from .. import mgr cls.mgr = mgr - cls.mgr.get_module_option.side_effect = cls.CONFIG.__getitem__ - cls. mgr.set_module_option.side_effect = cls.CONFIG.__setitem__ # Populate real endpoint map from ..controllers import load_controllers @@ -26,7 +24,7 @@ class SettingsTest(unittest.TestCase): # Initialize FeatureToggles plugin cls.plugin = FeatureToggles() - cls.CONFIG.update( + cls.CONFIG_KEY_DICT.update( {k['name']: k['default'] for k in cls.plugin.get_options()}) cls.plugin.setup() diff --git a/src/pybind/mgr/dashboard/tests/test_ganesha.py b/src/pybind/mgr/dashboard/tests/test_ganesha.py index 5e5ee374c1d..4c9711c5e08 100644 --- a/src/pybind/mgr/dashboard/tests/test_ganesha.py +++ b/src/pybind/mgr/dashboard/tests/test_ganesha.py @@ -5,13 +5,14 @@ import unittest from mock import MagicMock, Mock +from .helper import KVStoreMockMixin from .. import mgr from ..settings import Settings from ..services import ganesha from ..services.ganesha import GaneshaConf, Export, GaneshaConfParser -class GaneshaConfTest(unittest.TestCase): +class GaneshaConfTest(unittest.TestCase, KVStoreMockMixin): export_1 = """ EXPORT { Export_ID=1; @@ -103,19 +104,8 @@ EXPORT def _ioctx_list_objects_mock(self): return [obj for _, obj in self.temp_store.items()] - CONFIG_KEY_DICT = {} - - @classmethod - def mock_set_module_option(cls, attr, val): - cls.CONFIG_KEY_DICT[attr] = val - - @classmethod - def mock_get_module_option(cls, attr, default): - return cls.CONFIG_KEY_DICT.get(attr, default) - def setUp(self): - mgr.set_module_option.side_effect = self.mock_set_module_option - mgr.get_module_option.side_effect = self.mock_get_module_option + self.mock_kv_store() Settings.GANESHA_CLUSTERS_RADOS_POOL_NAMESPACE = "ganesha/ns" diff --git a/src/pybind/mgr/dashboard/tests/test_settings.py b/src/pybind/mgr/dashboard/tests/test_settings.py index 3965bde9541..a76779355cb 100644 --- a/src/pybind/mgr/dashboard/tests/test_settings.py +++ b/src/pybind/mgr/dashboard/tests/test_settings.py @@ -3,16 +3,14 @@ from __future__ import absolute_import import errno import unittest -from .. import mgr +from .helper import KVStoreMockMixin from .. import settings from ..controllers.settings import Settings as SettingsController from ..settings import Settings, handle_option_command from .helper import ControllerTestCase -class SettingsTest(unittest.TestCase): - CONFIG_KEY_DICT = {} - +class SettingsTest(unittest.TestCase, KVStoreMockMixin): @classmethod def setUpClass(cls): # pylint: disable=protected-access @@ -21,18 +19,8 @@ class SettingsTest(unittest.TestCase): settings.Options.GRAFANA_ENABLED = (False, bool) settings._OPTIONS_COMMAND_MAP = settings._options_command_map() - @classmethod - def mock_set_module_option(cls, attr, val): - cls.CONFIG_KEY_DICT[attr] = val - - @classmethod - def mock_get_module_option(cls, attr, default): - return cls.CONFIG_KEY_DICT.get(attr, default) - def setUp(self): - self.CONFIG_KEY_DICT.clear() - mgr.set_module_option.side_effect = self.mock_set_module_option - mgr.get_module_option.side_effect = self.mock_get_module_option + self.mock_kv_store() if Settings.GRAFANA_API_HOST != 'localhost': Settings.GRAFANA_API_HOST = 'localhost' if Settings.GRAFANA_API_PORT != 3000: @@ -108,9 +96,7 @@ class SettingsTest(unittest.TestCase): "type object 'Options' has no attribute 'NON_EXISTENT_OPTION'") -class SettingsControllerTest(ControllerTestCase): - config_values = {} - +class SettingsControllerTest(ControllerTestCase, KVStoreMockMixin): @classmethod def setup_server(cls): # pylint: disable=protected-access @@ -118,18 +104,8 @@ class SettingsControllerTest(ControllerTestCase): SettingsController._cp_config['tools.authenticate.on'] = False cls.setup_controllers([SettingsController]) - @classmethod - def mock_set_module_option(cls, attr, val): - cls.config_values[attr] = val - - @classmethod - def mock_get_module_option(cls, attr, default): - return cls.config_values.get(attr, default) - def setUp(self): - self.config_values.clear() - mgr.set_module_option.side_effect = self.mock_set_module_option - mgr.get_module_option.side_effect = self.mock_get_module_option + self.mock_kv_store() def test_settings_list(self): self._get('/api/settings') diff --git a/src/pybind/mgr/dashboard/tests/test_sso.py b/src/pybind/mgr/dashboard/tests/test_sso.py index 583da425610..ee2720a556a 100644 --- a/src/pybind/mgr/dashboard/tests/test_sso.py +++ b/src/pybind/mgr/dashboard/tests/test_sso.py @@ -6,21 +6,11 @@ import errno import unittest from . import CmdException, exec_dashboard_cmd -from .. import mgr +from .helper import CLICommandTestMixin from ..services.sso import handle_sso_command, load_sso_db -class AccessControlTest(unittest.TestCase): - CONFIG_KEY_DICT = {} - - @classmethod - def mock_set_module_option(cls, attr, val): - cls.CONFIG_KEY_DICT[attr] = val - - @classmethod - def mock_get_module_option(cls, attr, default): - return cls.CONFIG_KEY_DICT.get(attr, default) - +class AccessControlTest(unittest.TestCase, CLICommandTestMixin): IDP_METADATA = ''' ''' - @classmethod - def setUpClass(cls): - mgr.set_module_option.side_effect = cls.mock_set_module_option - mgr.get_module_option.side_effect = cls.mock_get_module_option - # kludge below - mgr.set_store.side_effect = cls.mock_set_module_option - mgr.get_store.side_effect = cls.mock_get_module_option - def setUp(self): - self.CONFIG_KEY_DICT.clear() + self.mock_kv_store() load_sso_db() @classmethod -- 2.39.5