feat(lambda): implement reactive S3-to-Lambda sync for hot-reloading (#509)

* feat(lambda): implement reactive S3-to-Lambda sync for hot-reloading
* fix: clean unused hotreload properties
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-17 19:04:47 -05:00
1 parent 9b5f364e47
commit 114fade129
9 files changed
+318 -7

No files matched your search

@@ -0,0 +1,101 @@
package io.github.hectorvent.floci.services.lambda;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.io.ByteArrayOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
/**
* Verifies that updating a ZIP file in S3 automatically patches
* running Lambda containers linked to that bucket/key.
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class LambdaReactiveSyncIntegrationTest {
private static final String BUCKET = "reactive-sync-bucket";
private static final String KEY = "function.zip";
private static final String FN = "reactive-sync-fn";
private static byte[] makeZip(String body) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
zos.putNextEntry(new ZipEntry("index.js"));
zos.write(("exports.handler = async (e) => ({ body: '" + body + "' });").getBytes());
zos.closeEntry();
}
return baos.toByteArray();
}
@Test
@Order(1)
void setupAndInitialInvoke() throws Exception {
// 1. Create bucket and upload V1
given().when().put("/" + BUCKET).then().statusCode(200);
given().body(makeZip("v1")).when().put("/" + BUCKET + "/" + KEY).then().statusCode(200);
// 2. Create function
given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"Runtime": "nodejs20.x",
"Role": "arn:aws:iam::000000000000:role/role",
"Handler": "index.handler",
"Timeout": 30,
"Code": {
"S3Bucket": "%s",
"S3Key": "%s"
}
}
""".formatted(FN, BUCKET, KEY))
.when()
.post("/2015-03-31/functions")
.then()
.statusCode(201);
// 3. First invoke (starts container)
given()
.body("{}")
.when()
.post("/2015-03-31/functions/" + FN + "/invocations")
.then()
.statusCode(200)
.body(containsString("v1"));
}
@Test
@Order(2)
void uploadV2AndVerifyAutoSync() throws Exception {
// 1. Upload V2 to same bucket/key
given().body(makeZip("v2")).when().put("/" + BUCKET + "/" + KEY).then().statusCode(200);
// 2. Wait a bit for async event processing and Docker copy
Thread.sleep(5000);
// 3. Invoke again. Should see V2 without calling UpdateFunctionCode
given()
.body("{}")
.when()
.post("/2015-03-31/functions/" + FN + "/invocations")
.then()
.statusCode(200)
.body(containsString("v2"));
}
@Test
@Order(3)
void cleanup() {
given().when().delete("/2015-03-31/functions/" + FN).then().statusCode(204);
}
}
@@ -0,0 +1,100 @@
package io.github.hectorvent.floci.services.lambda.launcher;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer;
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServerFactory;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Bind;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class ContainerLauncherTest {
@Mock ContainerLifecycleManager lifecycleManager;
@Mock ContainerLogStreamer logStreamer;
@Mock ImageResolver imageResolver;
@Mock RuntimeApiServerFactory runtimeApiServerFactory;
@Mock DockerHostResolver dockerHostResolver;
@Mock EmulatorConfig config;
@Mock EcrRegistryManager ecrRegistryManager;
@Mock RuntimeApiServer runtimeApiServer;
@Mock DockerClient dockerClient;
@TempDir
Path tempDir;
ContainerLauncher launcher;
ContainerBuilder containerBuilder;
@BeforeEach
void setUp() {
// Setup config mocks
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class);
EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);
when(config.services()).thenReturn(services);
when(services.lambda()).thenReturn(lambda);
when(lambda.dockerNetwork()).thenReturn(Optional.empty());
when(config.docker()).thenReturn(docker);
when(docker.logMaxSize()).thenReturn("10m");
when(docker.logMaxFile()).thenReturn("3");
containerBuilder = new ContainerBuilder(config, dockerHostResolver);
launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver,
runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager);
when(runtimeApiServerFactory.create()).thenReturn(runtimeApiServer);
when(runtimeApiServer.getPort()).thenReturn(9000);
when(dockerHostResolver.resolve()).thenReturn("127.0.0.1");
ContainerLifecycleManager.ContainerInfo info = new ContainerLifecycleManager.ContainerInfo("c1", Map.of());
when(lifecycleManager.createAndStart(any())).thenReturn(info);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
}
@Test
void launchFunctionCopiesCode() throws Exception {
Path codePath = Files.createDirectory(tempDir.resolve("code"));
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("standard-fn");
fn.setRuntime("nodejs20.x");
fn.setHandler("index.handler");
fn.setCodeLocalPath(codePath.toString());
launcher.launch(fn);
ArgumentCaptor<ContainerSpec> specCaptor = ArgumentCaptor.forClass(ContainerSpec.class);
verify(lifecycleManager).createAndStart(specCaptor.capture());
ContainerSpec spec = specCaptor.getValue();
assertTrue(spec.binds().isEmpty(), "Function should NOT have bind mounts");
// Function should attempt to copy code
verify(lifecycleManager, atLeastOnce()).getDockerClient();
}
}