mirror of
https://github.com/floci-io/floci.git
synced 2026-11-25 14:57:54 -05:00
test(compat): gate Lambda dispatch tests on container availability (#387)
Tests that invoke Lambda with REQUEST_RESPONSE depend on Docker-in-Docker for container execution. When DinD is unavailable in CI, these tests time out instead of skipping. Add a shared memoized probe (TestFixtures.isLambdaDispatchAvailable) that creates a throwaway Lambda, invokes it with a 30s timeout, and caches the result per JVM. Tests that need real dispatch now skip cleanly via Assumptions.assumeTrue. Java: ApiGatewayV2ExecuteTest, LambdaLongPathTest, S3NotificationsTest Go: lambda_test.go Invoke subtest
This commit is contained in:
5 files changed
+129
-8
No files matched your search
@@ -37,8 +37,22 @@ import software.amazon.awssdk.services.scheduler.SchedulerClient;
|
||||
import software.amazon.awssdk.services.appconfig.AppConfigClient;
|
||||
import software.amazon.awssdk.services.appconfigdata.AppConfigDataClient;
|
||||
|
||||
import software.amazon.awssdk.core.SdkBytes;
|
||||
import software.amazon.awssdk.services.lambda.model.CreateFunctionRequest;
|
||||
import software.amazon.awssdk.services.lambda.model.DeleteFunctionRequest;
|
||||
import software.amazon.awssdk.services.lambda.model.FunctionCode;
|
||||
import software.amazon.awssdk.services.lambda.model.InvokeRequest;
|
||||
import software.amazon.awssdk.services.lambda.model.InvokeResponse;
|
||||
import software.amazon.awssdk.services.lambda.model.InvocationType;
|
||||
import software.amazon.awssdk.services.lambda.model.Runtime;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.UUID;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* Shared test utilities and AWS client factories.
|
||||
@@ -97,6 +111,80 @@ public final class TestFixtures {
|
||||
return ENDPOINT.getHost();
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// Lambda dispatch availability probe
|
||||
// ============================================
|
||||
|
||||
private static volatile Boolean lambdaDispatchAvailable;
|
||||
|
||||
/**
|
||||
* Checks whether Lambda REQUEST_RESPONSE invocation works in the current
|
||||
* environment. Creates a minimal no-op function, invokes it, and tears it
|
||||
* down. The result is memoized so it runs at most once per JVM.
|
||||
*
|
||||
* Thread-safe: uses double-checked locking so parallel test classes don't
|
||||
* race the probe.
|
||||
*
|
||||
* Returns false on any transport-level failure (timeout, connection refused,
|
||||
* SDK timeout) so that tests can skip cleanly when Docker-in-Docker is
|
||||
* unavailable in CI.
|
||||
*/
|
||||
public static boolean isLambdaDispatchAvailable() {
|
||||
if (lambdaDispatchAvailable != null) {
|
||||
return lambdaDispatchAvailable;
|
||||
}
|
||||
synchronized (TestFixtures.class) {
|
||||
if (lambdaDispatchAvailable != null) {
|
||||
return lambdaDispatchAvailable;
|
||||
}
|
||||
String probeFn = uniqueName("probe-lambda-dispatch");
|
||||
LambdaClient probe = lambdaClient();
|
||||
try {
|
||||
probe.createFunction(CreateFunctionRequest.builder()
|
||||
.functionName(probeFn)
|
||||
.runtime(Runtime.NODEJS20_X)
|
||||
.role("arn:aws:iam::000000000000:role/lambda-role")
|
||||
.handler("index.handler")
|
||||
.code(FunctionCode.builder()
|
||||
.zipFile(SdkBytes.fromByteArray(probeZip()))
|
||||
.build())
|
||||
.build());
|
||||
InvokeResponse response = probe.invoke(InvokeRequest.builder()
|
||||
.functionName(probeFn)
|
||||
.invocationType(InvocationType.REQUEST_RESPONSE)
|
||||
.payload(SdkBytes.fromUtf8String("{}"))
|
||||
.overrideConfiguration(c -> c.apiCallTimeout(Duration.ofSeconds(30)))
|
||||
.build());
|
||||
lambdaDispatchAvailable = response.statusCode() == 200;
|
||||
} catch (Exception e) {
|
||||
lambdaDispatchAvailable = false;
|
||||
} finally {
|
||||
try {
|
||||
probe.deleteFunction(DeleteFunctionRequest.builder()
|
||||
.functionName(probeFn).build());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
probe.close();
|
||||
}
|
||||
return lambdaDispatchAvailable;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] probeZip() {
|
||||
String code = "exports.handler = async () => ({ statusCode: 200 });";
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||
zos.putNextEntry(new ZipEntry("index.js"));
|
||||
zos.write(code.getBytes(StandardCharsets.UTF_8));
|
||||
zos.closeEntry();
|
||||
}
|
||||
return baos.toByteArray();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to build probe ZIP", e);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// AWS Client Factories
|
||||
// ============================================
|
||||
|
||||
+29
-7
@@ -151,6 +151,30 @@ class ApiGatewayV2ExecuteTest {
|
||||
} catch (ConnectException e) {
|
||||
Assumptions.assumeTrue(false, "Floci endpoint is not reachable at " + TestFixtures.endpoint());
|
||||
}
|
||||
|
||||
// Probe result is memoized in TestFixtures; skip warmup if dispatch is unavailable
|
||||
if (!TestFixtures.isLambdaDispatchAvailable()) {
|
||||
lambdaDispatchAvailable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Warm the API GW → Lambda dispatch path and verify the response carries
|
||||
// the expected Lambda proxy envelope (statusCode in body). The direct
|
||||
// probe above only tests raw invoke; APIGW integration may behave differently.
|
||||
try {
|
||||
HttpResponse<String> warmup = http.send(HttpRequest.newBuilder()
|
||||
.uri(URI.create(baseUrl + "/echo/warmup"))
|
||||
.timeout(Duration.ofSeconds(30))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString("{}"))
|
||||
.build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
JsonNode warmupBody = JSON.readTree(warmup.body());
|
||||
lambdaDispatchAvailable = warmup.statusCode() == 200
|
||||
&& warmupBody.path("statusCode").asInt() == 200;
|
||||
} catch (Exception e) {
|
||||
lambdaDispatchAvailable = false;
|
||||
}
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
@@ -174,6 +198,9 @@ class ApiGatewayV2ExecuteTest {
|
||||
@Test
|
||||
@Order(1)
|
||||
void dispatchesHttpApiRouteToLambdaWithV2EventShape() throws Exception {
|
||||
Assumptions.assumeTrue(lambdaDispatchAvailable,
|
||||
"Lambda dispatch unavailable in this environment");
|
||||
|
||||
HttpResponse<String> response = http.send(HttpRequest.newBuilder()
|
||||
.uri(URI.create(baseUrl + "/echo/child/path?color=blue"))
|
||||
.timeout(Duration.ofSeconds(10))
|
||||
@@ -183,14 +210,9 @@ class ApiGatewayV2ExecuteTest {
|
||||
.build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
// Lambda dispatch may not work in CI (no Docker-in-Docker for containers).
|
||||
// Gate this and downstream tests on successful dispatch.
|
||||
JsonNode body = JSON.readTree(response.body());
|
||||
lambdaDispatchAvailable = response.statusCode() == 200
|
||||
&& body.path("statusCode").asInt() == 200;
|
||||
Assumptions.assumeTrue(lambdaDispatchAvailable,
|
||||
"Lambda dispatch unavailable (HTTP " + response.statusCode()
|
||||
+ ", body statusCode " + body.path("statusCode").asInt() + ")");
|
||||
assertThat(response.statusCode()).isEqualTo(200);
|
||||
assertThat(body.path("statusCode").asInt()).isEqualTo(200);
|
||||
|
||||
JsonNode event = JSON.readTree(body.path("body").asText());
|
||||
|
||||
|
||||
@@ -66,6 +66,9 @@ class LambdaLongPathTest {
|
||||
@Test
|
||||
@Order(2)
|
||||
void longPathFileIsAccessibleAtRuntime() throws Exception {
|
||||
Assumptions.assumeTrue(TestFixtures.isLambdaDispatchAvailable(),
|
||||
"Lambda REQUEST_RESPONSE dispatch unavailable in this environment");
|
||||
|
||||
InvokeResponse response = lambda.invoke(InvokeRequest.builder()
|
||||
.functionName(FUNCTION_NAME)
|
||||
.invocationType(InvocationType.REQUEST_RESPONSE)
|
||||
|
||||
+4
@@ -1,6 +1,7 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -206,6 +207,9 @@ class S3NotificationsTest {
|
||||
|
||||
@Test
|
||||
void lambdaNotificationInvokesFunctionForMatchingObject() throws InterruptedException {
|
||||
Assumptions.assumeTrue(TestFixtures.isLambdaDispatchAvailable(),
|
||||
"Lambda REQUEST_RESPONSE dispatch unavailable in this environment");
|
||||
|
||||
String key = "incoming/report.csv";
|
||||
String expectedMessage = "[s3-notification] received " + bucketName + "/" + key;
|
||||
|
||||
|
||||
Reference in new issue
Block a user