]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: tests: update tests that mock mons kv store
authorRicardo Dias <rdias@suse.com>
Mon, 11 Mar 2019 15:45:40 +0000 (15:45 +0000)
committerRicardo Dias <rdias@suse.com>
Tue, 12 Mar 2019 10:15:57 +0000 (10:15 +0000)
Signed-off-by: Ricardo Dias <rdias@suse.com>
src/pybind/mgr/dashboard/tests/test_access_control.py
src/pybind/mgr/dashboard/tests/test_api_auditing.py
src/pybind/mgr/dashboard/tests/test_feature_toggles.py
src/pybind/mgr/dashboard/tests/test_ganesha.py
src/pybind/mgr/dashboard/tests/test_settings.py
src/pybind/mgr/dashboard/tests/test_sso.py

index e4fb7ea916a8e7ab2a06319661e2407cfdfa9535..5061e9f43f6fa43d9236af677ed3be632ec9623e 100644 (file)
@@ -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)
index ed5362d9a3d1e5bc86af7c9181462cd5824c3eba..7021d9a5941f4e888094a73323b4da6cc9d92695 100644 (file)
@@ -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)
index d959b82f37030c4de224ec8eac1c7b9cd30b4b24..4d659dc39e05b77b17244d33fcc75e77bd3bf666 100644 (file)
@@ -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()
 
index 5e5ee374c1ddd921028e15a89ea5fb2cb83e6868..4c9711c5e086aa8a8de0dc768cd07773f864f82d 100644 (file)
@@ -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"
 
index 3965bde95410ddd2885ddd6d20389d92ecbb4b3d..a76779355cbc024486785fee49d6c128558c6dda 100644 (file)
@@ -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')
index 583da425610fc4299d22bd20878466ae874d38a2..ee2720a556a30096521f906bf8355c1b4ab674b2 100644 (file)
@@ -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 = '''<?xml version="1.0"?>
 <md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"
                      xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
@@ -69,16 +59,8 @@ class AccessControlTest(unittest.TestCase):
   </md:IDPSSODescriptor>
 </md:EntityDescriptor>'''
 
-    @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