]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Fix JSONDecodeError when orch get-security-config returns empty string
authorcopilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Tue, 17 Feb 2026 15:06:36 +0000 (15:06 +0000)
committercopilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Tue, 17 Feb 2026 15:06:36 +0000 (15:06 +0000)
Co-authored-by: epuertat <37327689+epuertat@users.noreply.github.com>
src/pybind/mgr/prometheus/module.py
src/pybind/mgr/prometheus/test_module.py

index 26b0da41d6d078c8cb390f048845f61647bff9a2..a8cfe08c12421e284bbcdf4c129e33c9d6f619d5 100644 (file)
@@ -1966,7 +1966,7 @@ class Module(MgrModule, OrchestratorClientMixin):
         cmd = {'prefix': 'orch get-security-config'}
         ret, out, _ = self.mon_command(cmd)
 
-        if ret == 0 and out is not None:
+        if ret == 0 and out:
             try:
                 security_config = json.loads(out)
                 if security_config.get('security_enabled', False):
@@ -2013,7 +2013,7 @@ class Module(MgrModule, OrchestratorClientMixin):
         if ret != 0:
             self.log.error(f'mon command to generate-certificates failed: {err}')
             return
-        elif out is None:
+        elif not out:
             self.log.error('mon command to generate-certificates failed to generate certificates')
             return
 
index 0647cb658c75de174a9ced594f5bd9ab5fd794a1..9f668256612874800b7e7349a18436bbd1e61c5a 100644 (file)
@@ -1,7 +1,8 @@
 from typing import Dict
 from unittest import TestCase
+from unittest.mock import Mock, patch
 
-from prometheus.module import Metric, LabelValues, Number
+from prometheus.module import Metric, LabelValues, Number, Module
 
 
 class MetricGroupTest(TestCase):
@@ -91,3 +92,65 @@ ceph_disk_occupation_display{ceph_daemon="osd.5+osd.6",device="/dev/dm-1",instan
         with self.assertRaises(AssertionError) as cm:
             m.group_by(["foo"], {"bar": "not callable str"})
         self.assertEqual(str(cm.exception), "joins must be callable")
+
+
+class ConfigureTest(TestCase):
+    def test_configure_with_empty_security_config(self):
+        """Test that configure handles empty string from orch get-security-config"""
+        module = Mock(spec=Module)
+        module.log = Mock()
+        
+        # Mock mon_command to return empty string (the problematic case)
+        module.mon_command = Mock(return_value=(0, "", ""))
+        
+        # Mock setup_default_config
+        module.setup_default_config = Mock()
+        
+        # Call the actual configure method with our mocks
+        Module.configure(module, "127.0.0.1", 9283)
+        
+        # Verify that setup_default_config was called (fallback behavior)
+        module.setup_default_config.assert_called_once_with("127.0.0.1", 9283)
+        
+        # Verify no exception was raised and log was not called with exception
+        module.log.exception.assert_not_called()
+    
+    def test_configure_with_none_security_config(self):
+        """Test that configure handles None from orch get-security-config"""
+        module = Mock(spec=Module)
+        module.log = Mock()
+        
+        # Mock mon_command to return None
+        module.mon_command = Mock(return_value=(0, None, ""))
+        
+        # Mock setup_default_config
+        module.setup_default_config = Mock()
+        
+        # Call the actual configure method with our mocks
+        Module.configure(module, "127.0.0.1", 9283)
+        
+        # Verify that setup_default_config was called (fallback behavior)
+        module.setup_default_config.assert_called_once_with("127.0.0.1", 9283)
+        
+        # Verify no exception was raised
+        module.log.exception.assert_not_called()
+    
+    def test_configure_with_valid_security_config_disabled(self):
+        """Test that configure handles valid JSON with security disabled"""
+        module = Mock(spec=Module)
+        module.log = Mock()
+        
+        # Mock mon_command to return valid JSON with security_enabled: false
+        module.mon_command = Mock(return_value=(0, '{"security_enabled": false}', ""))
+        
+        # Mock setup_default_config
+        module.setup_default_config = Mock()
+        
+        # Call the actual configure method with our mocks
+        Module.configure(module, "127.0.0.1", 9283)
+        
+        # Verify that setup_default_config was called (security disabled)
+        module.setup_default_config.assert_called_once_with("127.0.0.1", 9283)
+        
+        # Verify no exception was raised
+        module.log.exception.assert_not_called()