raise ValueError if json decode fails when getting the container ip

This commit is contained in:
Florian Apolloner
2025-09-15 20:02:31 +02:00
parent 0d190016d3
commit b306cac5c1
2 changed files with 14 additions and 1 deletions
+4 -1
View File
@@ -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
@@ -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)