]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
backport of: https://github.com/ceph/ceph/pull/54307
authorRedouane Kachach <rkachach@redhat.com>
Mon, 6 Nov 2023 15:56:01 +0000 (16:56 +0100)
committerRedouane Kachach <rkachach@redhat.com>
Mon, 6 Nov 2023 15:56:01 +0000 (16:56 +0100)
Signed-off-by: Redouane Kachach <rkachach@redhat.com>
src/pybind/mgr/dashboard/controllers/prometheus.py
src/pybind/mgr/dashboard/tests/test_prometheus.py

index 7222b14f7b5c5b18b5555e3b7c503877df5edace..b639d88262739a70027a409bf0c34404a2ddd8f2 100644 (file)
@@ -61,20 +61,24 @@ class PrometheusRESTController(RESTController):
         user = None
         password = None
         cert_file = None
-        secure_monitoring_stack = bool(mgr.get_module_option_ex('cephadm',
-                                                                'secure_monitoring_stack',
-                                                                'false'))
-        if secure_monitoring_stack:
-            cmd = {'prefix': f'orch {module_name} get-credentials'}
-            ret, out, _ = mgr.mon_command(cmd)
-            if ret == 0 and out is not None:
-                access_info = json.loads(out)
-                user = access_info['user']
-                password = access_info['password']
-                certificate = access_info['certificate']
-                cert_file = tempfile.NamedTemporaryFile(delete=False)
-                cert_file.write(certificate.encode('utf-8'))
-                cert_file.flush()
+
+        orch_backend = mgr.get_module_option_ex('orchestrator', 'orchestrator')
+        if orch_backend == 'cephadm':
+            secure_monitoring_stack = mgr.get_module_option_ex('cephadm',
+                                                               'secure_monitoring_stack',
+                                                               False)
+            if secure_monitoring_stack:
+                cmd = {'prefix': f'orch {module_name} get-credentials'}
+                ret, out, _ = mgr.mon_command(cmd)
+                if ret == 0 and out is not None:
+                    access_info = json.loads(out)
+                    user = access_info['user']
+                    password = access_info['password']
+                    certificate = access_info['certificate']
+                    cert_file = tempfile.NamedTemporaryFile(delete=False)
+                    cert_file.write(certificate.encode('utf-8'))
+                    cert_file.flush()
+
         return user, password, cert_file
 
     def _get_api_url(self, host):
index 21c4a0b10e9ce4ebec709a8d185895d0e5c82118..10aa8669ec0ee84dd7bb0eae0b61a9d3828d51c9 100644 (file)
@@ -26,28 +26,53 @@ class PrometheusControllerTest(ControllerTestCase):
         mgr.get_module_option.side_effect = settings.get
         cls.setup_controllers([Prometheus, PrometheusNotifications, PrometheusReceiver])
 
-    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c: False)
-    def test_rules(self):
-        with patch('requests.request') as mock_request:
-            self._get('/api/prometheus/rules')
-            mock_request.assert_called_with('GET', self.prometheus_host_api + '/rules',
-                                            json=None, params={}, verify=True, auth=None)
-
-    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c: False)
+    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", return_value='cephadm')
+    @patch("dashboard.controllers.prometheus.mgr.mon_command", return_value=(1, {}, None))
+    @patch('requests.request')
+    def test_rules_cephadm(self, mock_request, mock_mon_command, mock_get_module_option_ex):
+        # in this test we use:
+        # in the first call to get_module_option_ex we return 'cephadm' as backend
+        # in the second call we return 'True' for 'secure_monitoring_stack' option
+        mock_get_module_option_ex.side_effect = lambda module, key, default=None: 'cephadm' \
+            if module == 'orchestrator' else True
+        self._get('/api/prometheus/rules')
+        mock_request.assert_called_with('GET',
+                                        self.prometheus_host_api + '/rules',
+                                        json=None, params={},
+                                        verify=True, auth=None)
+        assert mock_mon_command.called
+
+    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", return_value='cephadm')
+    @patch("dashboard.controllers.prometheus.mgr.mon_command", return_value=(1, {}, None))
+    @patch('requests.request')
+    def test_rules_rook(self, mock_request, mock_mon_command, mock_get_module_option_ex):
+        # in this test we use:
+        # in the first call to get_module_option_ex we return 'rook' as backend
+        mock_get_module_option_ex.side_effect = lambda module, key, default=None: 'rook' \
+            if module == 'orchestrator' else None
+        self._get('/api/prometheus/rules')
+        mock_request.assert_called_with('GET',
+                                        self.prometheus_host_api + '/rules',
+                                        json=None,
+                                        params={},
+                                        verify=True, auth=None)
+        assert not mock_mon_command.called
+
+    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c=None: None)
     def test_list(self):
         with patch('requests.request') as mock_request:
             self._get('/api/prometheus')
             mock_request.assert_called_with('GET', self.alert_host_api + '/alerts',
                                             json=None, params={}, verify=True, auth=None)
 
-    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c: False)
+    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c=None: None)
     def test_get_silences(self):
         with patch('requests.request') as mock_request:
             self._get('/api/prometheus/silences')
             mock_request.assert_called_with('GET', self.alert_host_api + '/silences',
                                             json=None, params={}, verify=True, auth=None)
 
-    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c: False)
+    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c=None: None)
     def test_add_silence(self):
         with patch('requests.request') as mock_request:
             self._post('/api/prometheus/silence', {'id': 'new-silence'})
@@ -55,7 +80,7 @@ class PrometheusControllerTest(ControllerTestCase):
                                             params=None, json={'id': 'new-silence'},
                                             verify=True, auth=None)
 
-    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c: False)
+    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c=None: None)
     def test_update_silence(self):
         with patch('requests.request') as mock_request:
             self._post('/api/prometheus/silence', {'id': 'update-silence'})
@@ -63,7 +88,7 @@ class PrometheusControllerTest(ControllerTestCase):
                                             params=None, json={'id': 'update-silence'},
                                             verify=True, auth=None)
 
-    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c: False)
+    @patch("dashboard.controllers.prometheus.mgr.get_module_option_ex", lambda a, b, c=None: None)
     def test_expire_silence(self):
         with patch('requests.request') as mock_request:
             self._delete('/api/prometheus/silence/0')