mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(lambda): give published versions their code location (#2012)
* test(lambda): cover invoking a published version LambdaVersionIntegrationTest covered publish, list and delete against a function with no code at all, so nothing ever invoked a version. Adds that case, plus a $LATEST baseline on a separate function so a failure is attributable to versioning rather than to the harness. Currently fails, reproducing #1987: the version invoke returns HTTP 200 with X-Amz-Function-Error: Unhandled and no result body, after the container logs Runtime.ImportModuleError ("Cannot find module 'index'") and the invoke times out at the 5s function timeout. The baseline is a separate function on purpose. Invoking $LATEST on the function under test leaves a warm container that the version-qualified invoke then reuses -- the warm pool is keyed by function name alone (#1988) -- so the version runs $LATEST's code and the assertion passes while the version path is still broken. An earlier draft of this test did exactly that and reported #1987 as fixed. * fix(lambda): give published versions their code publishVersion built the version snapshot by copying fields one at a time and copied no code location at all -- not just codeLocalPath but s3Bucket, s3Key, imageUri and codeSha256. A numeric qualifier resolves straight to that snapshot, so a version-qualified invoke launched a container for a function with no code. It timed out rather than failing because ContainerLauncher.launch only validated the code path when it was non-null, making the one state that always means "no code" the one state never checked. The container came up empty, the runtime logged Runtime.ImportModuleError where nobody saw it, and the caller waited out the whole function timeout before getting a 200 with X-Amz-Function-Error: Unhandled and no result body. Copy the fields that determine what runs -- code location for every package type, plus layers, architectures and ephemeral storage -- and make a missing code location an error for non-hot-reload Zip functions. The guard is what keeps the next dropped field from surfacing as a timeout somewhere else instead of an error at its cause. LambdaImageConfigTest.zipFunctionStillUsesHandlerAsCmd built a bare Zip function with no code path as a fixture shortcut; it now gets a @TempDir. The test covers cmd construction, not code validation. Closes #1987 * fix(lambda): copy remaining config fields and fix @Order collision Two follow-ups from review after the code-location fix: - publishVersion now also copies tracingMode, deadLetterTargetArn and kmsKeyArn onto the version snapshot. These are surfaced by every config-serializing endpoint the same as the code-location fields, and were dropped by the same hand-enumerated field copy. Verified through ListVersionsByFunction rather than GetFunction/ GetFunctionConfiguration with a :1 qualifier: those two resolve through LambdaService.getFunction(region, functionName), which hardcodes "$LATEST" in its storage lookup and never reads the qualifier, so they can't distinguish a version's stored config from $LATEST's — a separate, pre-existing gap, not fixed here. - LambdaVersionIntegrationTest had two methods at @Order(6) after merging main, which independently added a test at the same value. Renumbered to keep execution order deterministic; both tests use independent function fixtures so this was never a functional bug. Verified against a revert of each change: both new/moved assertions fail without their fix and pass with it restored. --------- Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
1 parent
7a2dc53b96
commit
42022fe937
4 files changed
+206
-9
No files matched your search
@@ -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;
|
||||
|
||||
+18
-8
@@ -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)");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
+161
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user