Skip to content

Commit 95be102

Browse files
ruromeroclaude
andcommitted
fix: add logging for IOException catch and test for exception propagation
Address Sourcery review feedback: log a warning when a container engine is not available instead of silently returning empty, and add a test verifying that non-IO RuntimeExceptions still propagate. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3b3c81f commit 95be102

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/main/java/io/github/guacsec/trustifyda/image/ImageUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.fasterxml.jackson.databind.node.ObjectNode;
2525
import com.fasterxml.jackson.databind.node.TextNode;
2626
import com.github.packageurl.MalformedPackageURLException;
27+
import io.github.guacsec.trustifyda.logging.LoggersFactory;
2728
import io.github.guacsec.trustifyda.tools.Operations;
2829
import io.github.guacsec.trustifyda.utils.Environment;
2930
import java.io.File;
@@ -35,11 +36,14 @@
3536
import java.util.Map;
3637
import java.util.Objects;
3738
import java.util.function.Supplier;
39+
import java.util.logging.Logger;
3840
import java.util.stream.Collectors;
3941
import java.util.stream.StreamSupport;
4042

4143
public class ImageUtils {
4244

45+
private static final Logger LOG = LoggersFactory.getLogger(ImageUtils.class.getName());
46+
4347
static final String TRUSTIFY_DA_SYFT_CONFIG_PATH = "TRUSTIFY_DA_SYFT_CONFIG_PATH";
4448
static final String TRUSTIFY_DA_SYFT_IMAGE_SOURCE = "TRUSTIFY_DA_SYFT_IMAGE_SOURCE";
4549
static final String TRUSTIFY_DA_IMAGE_PLATFORM = "TRUSTIFY_DA_IMAGE_PLATFORM";
@@ -286,6 +290,9 @@ static String hostInfo(String engine, String info) {
286290
output = Operations.runProcessGetFullOutput(null, cmd, null);
287291
} catch (RuntimeException e) {
288292
if (e.getCause() instanceof IOException) {
293+
LOG.warning(
294+
String.format(
295+
"Container engine '%s' not available: %s", exec, e.getCause().getMessage()));
289296
return "";
290297
}
291298
throw e;

src/test/java/io/github/guacsec/trustifyda/image/ImageUtilsTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import static org.assertj.core.api.Assertions.assertThat;
2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertNull;
30+
import static org.junit.jupiter.api.Assertions.assertSame;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
3032
import static org.junit.jupiter.api.Assertions.assertTrue;
3133
import static org.mockito.AdditionalMatchers.aryEq;
3234
import static org.mockito.ArgumentMatchers.any;
@@ -553,6 +555,27 @@ void test_host_info_no_docker_path() {
553555
}
554556
}
555557

558+
@Test
559+
void test_host_info_other_runtime_exceptions_propagate() {
560+
try (MockedStatic<Operations> mock = Mockito.mockStatic(Operations.class)) {
561+
RuntimeException expected = new RuntimeException("non-io-error");
562+
563+
mock.when(() -> Operations.getCustomPathOrElse(eq("docker"))).thenReturn("docker");
564+
565+
mock.when(() -> Operations.getExecutable(eq("docker"), any())).thenReturn("docker");
566+
567+
mock.when(
568+
() ->
569+
Operations.runProcessGetFullOutput(
570+
isNull(), aryEq(new String[] {"docker", "info"}), isNull()))
571+
.thenThrow(expected);
572+
573+
RuntimeException thrown =
574+
assertThrows(RuntimeException.class, () -> ImageUtils.hostInfo("docker", "info"));
575+
assertSame(expected, thrown);
576+
}
577+
}
578+
556579
@Test
557580
@SetSystemProperty(key = "TRUSTIFY_DA_DOCKER_PATH", value = mockDockerPath)
558581
@SetSystemProperty(key = "PATH", value = mockPath)

0 commit comments

Comments
 (0)