Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/main/java/io/github/guacsec/trustifyda/image/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.github.packageurl.MalformedPackageURLException;
import io.github.guacsec.trustifyda.logging.LoggersFactory;
import io.github.guacsec.trustifyda.tools.Operations;
import io.github.guacsec.trustifyda.utils.Environment;
import java.io.File;
Expand All @@ -35,11 +36,14 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class ImageUtils {

private static final Logger LOG = LoggersFactory.getLogger(ImageUtils.class.getName());

static final String TRUSTIFY_DA_SYFT_CONFIG_PATH = "TRUSTIFY_DA_SYFT_CONFIG_PATH";
static final String TRUSTIFY_DA_SYFT_IMAGE_SOURCE = "TRUSTIFY_DA_SYFT_IMAGE_SOURCE";
static final String TRUSTIFY_DA_IMAGE_PLATFORM = "TRUSTIFY_DA_IMAGE_PLATFORM";
Expand Down Expand Up @@ -281,10 +285,21 @@ static String hostInfo(String engine, String info) {
var exec = Operations.getCustomPathOrElse(engine);
var cmd = new String[] {exec, "info"};

var output = Operations.runProcessGetFullOutput(null, cmd, null);
Operations.ProcessExecOutput output;
try {
output = Operations.runProcessGetFullOutput(null, cmd, null);
} catch (RuntimeException e) {
if (e.getCause() instanceof IOException) {
LOG.warning(
String.format(
"Container engine '%s' not available: %s", exec, e.getCause().getMessage()));
return "";
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
}
throw e;
}
if (output.getOutput().isEmpty()
&& (!output.getError().isEmpty() || output.getExitCode() != 0)) {
throw new RuntimeException(output.getError());
return "";
}

return output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.AdditionalMatchers.aryEq;
Expand Down Expand Up @@ -549,9 +550,29 @@ void test_host_info_no_docker_path() {
isNull(), aryEq(new String[] {"docker", "info"}), isNull()))
.thenReturn(output);

var exception =
var result = ImageUtils.hostInfo("docker", "info");
assertEquals("", result);
Comment thread
ruromero marked this conversation as resolved.
}
}

@Test
void test_host_info_other_runtime_exceptions_propagate() {
try (MockedStatic<Operations> mock = Mockito.mockStatic(Operations.class)) {
RuntimeException expected = new RuntimeException("non-io-error");

mock.when(() -> Operations.getCustomPathOrElse(eq("docker"))).thenReturn("docker");

mock.when(() -> Operations.getExecutable(eq("docker"), any())).thenReturn("docker");

mock.when(
() ->
Operations.runProcessGetFullOutput(
isNull(), aryEq(new String[] {"docker", "info"}), isNull()))
.thenThrow(expected);

RuntimeException thrown =
assertThrows(RuntimeException.class, () -> ImageUtils.hostInfo("docker", "info"));
assertEquals("test-error", exception.getMessage());
assertSame(expected, thrown);
}
}

Expand Down
Loading