diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index a283395ad2..627bcee3d4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -979,6 +979,27 @@ public class LambdaService { snapshot.setLastModified(System.currentTimeMillis()); snapshot.setRevisionId(UUID.randomUUID().toString()); + // Everything that determines what actually runs. Without these the snapshot describes a + // function with no code: a version-qualified invoke resolves to it, launches a container + // with nothing in it, and hangs to the function timeout instead of failing (#1987). A + // published version is an immutable snapshot of code plus configuration, so it carries the + // code location for every package type, not only Zip. + snapshot.setCodeLocalPath(fn.getCodeLocalPath()); + snapshot.setCodeSha256(fn.getCodeSha256()); + snapshot.setS3Bucket(fn.getS3Bucket()); + snapshot.setS3Key(fn.getS3Key()); + snapshot.setHotReloadHostPath(fn.getHotReloadHostPath()); + snapshot.setImageUri(fn.getImageUri()); + snapshot.setImageConfigCommand(fn.getImageConfigCommand()); + snapshot.setImageConfigEntryPoint(fn.getImageConfigEntryPoint()); + snapshot.setImageConfigWorkingDirectory(fn.getImageConfigWorkingDirectory()); + snapshot.setLayers(fn.getLayers()); + snapshot.setArchitectures(fn.getArchitectures()); + snapshot.setEphemeralStorageSize(fn.getEphemeralStorageSize()); + snapshot.setTracingMode(fn.getTracingMode()); + snapshot.setDeadLetterTargetArn(fn.getDeadLetterTargetArn()); + snapshot.setKmsKeyArn(fn.getKmsKeyArn()); + functionStore.save(region, snapshot); LOG.infov("Published version {0} for function {1}", version, functionName); return snapshot; diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java index 3ad399afd6..d7f66aa32b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java @@ -156,14 +156,24 @@ public class ContainerLauncher { // For Zip functions, verify code exists before allocating any resources. // Hot-reload functions use a bind-mount; the Docker daemon validates the path at start. - if (!fn.isHotReload()) { - if (fn.getCodeLocalPath() != null) { - Path codePath = Path.of(fn.getCodeLocalPath()); - if (!Files.exists(codePath)) { - throw new RuntimeException("Code directory not found for function '" - + fn.getFunctionName() + "': " + fn.getCodeLocalPath() - + " (function may have been deleted or updated)"); - } + // Image functions carry an imageUri rather than a local path. + if (!fn.isHotReload() && !"Image".equals(fn.getPackageType())) { + if (fn.getCodeLocalPath() == null) { + // A null path used to skip validation entirely, which made the one state that + // always means "no code" the one state never checked: the container started + // empty, the runtime logged an ImportModuleError nobody saw, and the caller + // waited out the whole function timeout. Failing here surfaces a dropped code + // field at its cause instead of as a timeout somewhere else (#1987). + throw new RuntimeException("No code location for function '" + + fn.getFunctionName() + "'" + + ("$LATEST".equals(fn.getVersion()) ? "" : " version " + fn.getVersion()) + + " (function has no deployed code)"); + } + Path codePath = Path.of(fn.getCodeLocalPath()); + if (!Files.exists(codePath)) { + throw new RuntimeException("Code directory not found for function '" + + fn.getFunctionName() + "': " + fn.getCodeLocalPath() + + " (function may have been deleted or updated)"); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java index 2e74af061e..67b0b3364b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -349,12 +350,16 @@ class LambdaImageConfigTest { } @Test - void zipFunctionStillUsesHandlerAsCmd() throws Exception { + void zipFunctionStillUsesHandlerAsCmd(@TempDir Path codeDir) throws Exception { LambdaFunction fn = new LambdaFunction(); fn.setFunctionName("zip-fn"); fn.setRuntime("nodejs20.x"); fn.setHandler("index.handler"); fn.setPackageType("Zip"); + // A Zip function needs a code location — launch() now rejects one without it instead + // of starting an empty container. This test covers cmd construction, so any real + // directory will do. + fn.setCodeLocalPath(codeDir.toString()); when(imageResolver.resolve("nodejs20.x")).thenReturn("public.ecr.aws/lambda/nodejs:20"); launcher.launch(fn); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java index c93630ba02..e5bb7a8859 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java @@ -6,6 +6,11 @@ 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.Base64; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.*; @@ -125,4 +130,160 @@ class LambdaVersionIntegrationTest { .then() .statusCode(404); } + + @Test + @Order(7) + void invokeLatest_baselineRunsTheCode() throws Exception { + // Baseline for the case below. Every other test in this file uses a function with no code + // at all, so none of them can invoke; this pins that the zip, runtime and harness are + // sound, which is what makes a failure in the next test attributable to versioning. + // Deliberately a SEPARATE function: invoking $LATEST on the function under test would + // leave a warm container that the version invoke then reuses. + String fnName = "version-invoke-baseline"; + createInvocableFunction(fnName); + + try { + given() + .contentType("application/json") + .body("{}") + .when() + .post(BASE_PATH + "/functions/" + fnName + "/invocations") + .then() + .statusCode(200) + .header("X-Amz-Function-Error", nullValue()) + .body("ok", equalTo(true)); + } finally { + given().delete(BASE_PATH + "/functions/" + fnName); + } + } + + @Test + @Order(8) + void invokePublishedVersion_runsTheVersionsCode() throws Exception { + // A published version is only useful if it can be invoked, and nothing in this file + // exercised that. publishVersion copies 14 fields onto the snapshot but no code location + // (codeLocalPath, s3Bucket/s3Key, imageUri are all dropped), and ContainerLauncher.launch + // only validates the code path when it is non-null — so a null one skips validation, the + // container starts with nothing to run, and the invoke times out rather than throwing. + String fnName = "version-invoke-cold"; + createInvocableFunction(fnName); + + try { + given() + .contentType("application/json") + .body("{\"Description\": \"invocable\"}") + .when() + .post(BASE_PATH + "/functions/" + fnName + "/versions") + .then() + .statusCode(201) + .body("Version", equalTo("1")); + + // The version invoke must be a COLD start to mean anything. $LATEST is never invoked + // on this function: the warm pool is keyed by function name alone (#1988), so a warm + // container from a $LATEST invoke gets handed to a version-qualified one and serves + // $LATEST's code — which makes this assertion pass while the version path is broken. + given() + .contentType("application/json") + .body("{}") + .when() + .post(BASE_PATH + "/functions/" + fnName + ":1/invocations") + .then() + .statusCode(200) + // Body first: the header assertion below fails fast and would otherwise mask what + // the version actually returned. + .body("ok", equalTo(true)) + .header("X-Amz-Function-Error", nullValue()); + } finally { + given().delete(BASE_PATH + "/functions/" + fnName); + } + } + + @Test + @Order(9) + void publishVersion_copiesObservabilityAndEncryptionConfig() { + // A published version is an immutable snapshot of configuration too, not only code. + // TracingConfig, DeadLetterConfig and KMSKeyArn are all surfaced wherever a function's + // configuration is serialized (LambdaController:625-637) but were dropped by the + // hand-enumerated field copy the same way the code location was, before that copy was + // extended to include them. + // + // Verified through ListVersionsByFunction rather than GetFunction/GetFunctionConfiguration + // with a :1 qualifier: those two both resolve through LambdaService.getFunction(region, + // functionName), which hardcodes "$LATEST" in its storage lookup and never reads the + // qualifier at all (LambdaFunctionStore.get(region, functionName) -> get(..., "$LATEST")), + // so they cannot distinguish version 1's stored config from $LATEST's. That is a separate, + // pre-existing gap, tracked apart from this fix. ListVersionsByFunction is genuinely + // version-aware: it serializes each stored version object directly. + String fnName = "version-config-snapshot"; + given() + .contentType("application/json") + .body(String.format(""" + { + "FunctionName": "%s", + "Runtime": "nodejs20.x", + "Role": "arn:aws:iam::000000000000:role/lambda-role", + "Handler": "index.handler", + "TracingConfig": {"Mode": "Active"}, + "DeadLetterConfig": {"TargetArn": "arn:aws:sqs:us-east-1:000000000000:dlq"}, + "KMSKeyArn": "arn:aws:kms:us-east-1:000000000000:key/test-key" + } + """, fnName)) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201); + + try { + given() + .contentType("application/json") + .body("{}") + .when() + .post(BASE_PATH + "/functions/" + fnName + "/versions") + .then() + .statusCode(201) + .body("Version", equalTo("1")); + + given() + .when() + .get(BASE_PATH + "/functions/" + fnName + "/versions") + .then() + .statusCode(200) + .body("Versions.find { it.Version == '1' }.TracingConfig.Mode", equalTo("Active")) + .body("Versions.find { it.Version == '1' }.DeadLetterConfig.TargetArn", + equalTo("arn:aws:sqs:us-east-1:000000000000:dlq")) + .body("Versions.find { it.Version == '1' }.KMSKeyArn", + equalTo("arn:aws:kms:us-east-1:000000000000:key/test-key")); + } finally { + given().delete(BASE_PATH + "/functions/" + fnName); + } + } + + private void createInvocableFunction(String fnName) throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (ZipOutputStream zos = new ZipOutputStream(baos)) { + zos.putNextEntry(new ZipEntry("index.js")); + zos.write("exports.handler = async () => ({ ok: true });".getBytes()); + zos.closeEntry(); + } + String base64Zip = Base64.getEncoder().encodeToString(baos.toByteArray()); + + given() + .contentType("application/json") + .body(""" + { + "FunctionName": "%s", + "Runtime": "nodejs20.x", + "Role": "arn:aws:iam::000000000000:role/lambda-role", + "Handler": "index.handler", + "Timeout": 5, + "Code": { + "ZipFile": "%s" + } + } + """.formatted(fnName, base64Zip)) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201); + } }