Pārlūkot izejas kodu

raise ValueError if json decode fails when getting the container ip

Florian Apolloner 1 mēnesi atpakaļ
vecāks
revīzija
b306cac5c1

+ 4 - 1
borgmatic/hooks/data_source/config.py

@@ -64,7 +64,10 @@ def get_ip_from_container(container):
             logger.debug(f"Could not find container '{container}' with engine '{engine}'")
             continue  # Container does not exist
 
-        network_data = json.loads(output.strip())
+        try:
+            network_data = json.loads(output.strip())
+        except json.JSONDecodeError as e:
+            raise ValueError(f'Could not decode JSON output from {engine}') from e
         main_ip = network_data.get('IPAddress')
         if main_ip:
             return main_ip

+ 10 - 0
tests/unit/hooks/data_source/test_config.py

@@ -91,3 +91,13 @@ def test_get_container_ip_container_no_network():
     with pytest.raises(ValueError) as exc_info:
         config.get_ip_from_container('yolo')
     assert 'Could not determine ip address for container' in str(exc_info.value)
+
+
+def test_get_container_ip_broken_output():
+    flexmock(config.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')
+
+    flexmock(config).should_receive('execute_command_and_capture_output').and_return('abc')
+
+    with pytest.raises(ValueError) as exc_info:
+        config.get_ip_from_container('yolo')
+    assert 'Could not decode JSON output' in str(exc_info.value)