Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(lambda): propagate image inspection failures (#2268)

Mohammad Mehdi Roshani9ebf9feb54bc
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ImageCacheService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ImageCacheService.java
index c1ca757bb4..365e10886b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ImageCacheService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ImageCacheService.java
@@ -124,9 +124,6 @@ public class ImageCacheService {
return true;
} catch (com.github.dockerjava.api.exception.NotFoundException e) {
return false;
- } catch (Exception e) {
- LOG.debugv("Could not check local image presence for {0}: {1}", imageUri, e.getMessage());
- return false;
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ImageCacheServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ImageCacheServiceTest.java
index 1f3edba2b9..d53f674265 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ImageCacheServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ImageCacheServiceTest.java
@@ -1,22 +1,68 @@
package io.github.hectorvent.floci.services.lambda.launcher;
+import com.github.dockerjava.api.DockerClient;
+import com.github.dockerjava.api.command.InspectImageCmd;
+import com.github.dockerjava.api.command.PullImageCmd;
+import com.github.dockerjava.api.command.PullImageResultCallback;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.exception.UnauthorizedException;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import org.junit.jupiter.api.Test;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
+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.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
class ImageCacheServiceTest {
private static final String IMAGE = "public.ecr.aws/docker/library/alpine:latest";
+ @Test
+ void pullsImageWhenInspectionReportsNotFound() throws Exception {
+ DockerClient dockerClient = mock(DockerClient.class);
+ InspectImageCmd inspectImage = mock(InspectImageCmd.class);
+ PullImageCmd pullImage = mock(PullImageCmd.class);
+ PullImageResultCallback callback = mock(PullImageResultCallback.class);
+ when(dockerClient.inspectImageCmd(IMAGE)).thenReturn(inspectImage);
+ when(inspectImage.exec()).thenThrow(new NotFoundException("image not found"));
+ when(dockerClient.pullImageCmd(IMAGE)).thenReturn(pullImage);
+ when(pullImage.withAuthConfig(any())).thenReturn(pullImage);
+ when(pullImage.exec(any(PullImageResultCallback.class))).thenReturn(callback);
+
+ newService(dockerClient).ensureImageExists(IMAGE);
+
+ verify(pullImage).exec(any(PullImageResultCallback.class));
+ verify(callback).awaitCompletion(5, TimeUnit.MINUTES);
+ }
+
+ @Test
+ void propagatesImageInspectionFailureWithoutPulling() {
+ DockerClient dockerClient = mock(DockerClient.class);
+ InspectImageCmd inspectImage = mock(InspectImageCmd.class);
+ DockerClientException failure = new DockerClientException("daemon unavailable");
+ when(dockerClient.inspectImageCmd(IMAGE)).thenReturn(inspectImage);
+ when(inspectImage.exec()).thenThrow(failure);
+
+ DockerClientException thrown = assertThrows(DockerClientException.class,
+ () -> newService(dockerClient).ensureImageExists(IMAGE));
+
+ assertSame(failure, thrown);
+ verify(dockerClient, never()).pullImageCmd(IMAGE);
+ }
+
@Test
void succeedsOnFirstAttempt() throws Exception {
AtomicInteger calls = new AtomicInteger();
@@ -115,4 +161,12 @@ class ImageCacheServiceTest {
}));
assertEquals(1, calls.get());
}
+
+ private static ImageCacheService newService(DockerClient dockerClient) {
+ EmulatorConfig config = mock(EmulatorConfig.class);
+ EmulatorConfig.DockerConfig dockerConfig = mock(EmulatorConfig.DockerConfig.class);
+ when(config.docker()).thenReturn(dockerConfig);
+ when(dockerConfig.registryCredentials()).thenReturn(List.of());
+ return new ImageCacheService(dockerClient, config);
+ }
}