mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lambdaweb): serve endpoint domains from a Node.js environment per revision (#5760)
* feat(lambdaweb): serve endpoint domains from a Node.js environment per revision A request whose Host is an endpoint's domainName goes over HTTP/1.1 to a container that Floci starts from the revision's S3 code on the first request: public.ecr.aws/lambda/nodejs:24 with the code in /var/task, index.js or AWS_LAMBDA_NODEJS_ENTRYPOINT as the entry file, the node flags and the environment variables AWS sets, ten seconds to listen, SIGTERM and one second when idle, and a fresh environment after the process exits between requests. The forwarded request carries the headers AWS adds and loses the ones AWS drops. The response carries lambda-web-request-id and lambda-web-logstream and streams as the app writes it. Two revisions in revisionWeights split the traffic by weight. The platform pages for an unsupported method, an unknown domain, HTTP/1.0, a server that does not listen, a timeout, a crash and a disabled endpoint are the AWS pages. Part of #5699 * fix(lambdaweb): index endpoint domains and claim only the minted host shapes A request looked its domain up by scanning every stored function across accounts and iterating live endpoint maps outside the function lock. The service now keeps a domain index, filled from storage at start and kept by the create and delete paths, and copies the function under its lock. The data plane claims only hosts shaped like the domains Floci mints, in any case and with any port, so a dotted S3 bucket with a lambda-web label still reaches S3. * fix(lambdaweb): reserve environments against the idle reaper and track exits acquire now reserves the environment under the same lock the idle reaper takes, so a request cannot start on a container the reaper is about to stop. Docker faults in the reaper and in discard are contained so one error does not end the periodic run or skip the removal. The exit watcher records the exit time and arms itself again when its stream drops, and a request that found a dead process is replayed only when that recorded exit came first, never because the container is gone. The unpacked code directory is removed on every path. The container gets the execution role's session credentials when the role exists, and the function account's credentials otherwise, as the Lambda launcher does. * fix(lambdaweb): bound the whole exchange by timeoutSeconds and forward chunked bodies timeoutSeconds is a deadline on the whole request, as recorded against AWS: a response that has not started by then is the 504 page, one that is still streaming is cut, and a slow upload gets its 504 once the body is in, since the edge takes the body before it answers. A body without a Content-Length is sent chunked instead of carrying the client's Transfer-Encoding header. A failure of the upload stream ends the exchange and releases the environment once. * test(compat): probe the unknown Lambda Web domain in the shape Floci mints The data plane claims only hosts shaped like the domains it mints, so the unknown-domain check needs a 16 character id to reach it.
This commit is contained in:
1 parent
8362d0817d
commit
6e01216f88
15 files changed
+1670
-6
No files matched your search
+234
@@ -0,0 +1,234 @@
|
||||
package io.github.hectorvent.floci.services.lambdaweb;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Requests to an endpoint domain reach a Node.js server that Floci starts from the revision's S3
|
||||
* code, with the headers AWS adds and drops, the recorded error pages and a fresh environment after
|
||||
* a crash.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class LambdaWebDataPlaneDockerIntegrationTest {
|
||||
|
||||
private static final String BASE = "/2025-03-07";
|
||||
private static final String BUCKET = "web-code-docker";
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/web";
|
||||
private static final String REQUEST_ID = "[A-Z2-7]{26}";
|
||||
private static final String APP = """
|
||||
const http = require('node:http');
|
||||
const [host, port] = process.env.AWS_LAMBDA_HTTP_ENDPOINT.split(':');
|
||||
http.createServer((req, res) => {
|
||||
if (req.url.startsWith('/slow')) { setTimeout(() => res.end('late'), 8000); return; }
|
||||
if (req.url.startsWith('/stream')) {
|
||||
res.writeHead(200, {'content-type': 'text/plain'});
|
||||
let i = 0;
|
||||
const timer = setInterval(() => {
|
||||
res.write('chunk ' + (i++) + '\\n');
|
||||
if (i >= 10) { clearInterval(timer); res.end('done\\n'); }
|
||||
}, 500);
|
||||
return;
|
||||
}
|
||||
if (req.url.startsWith('/status/')) {
|
||||
res.writeHead(Number(req.url.split('/')[2]), {'x-app': 'yes'}); res.end('status body'); return;
|
||||
}
|
||||
if (req.url.startsWith('/crash')) { setTimeout(() => process.exit(1), 50); return; }
|
||||
let body = '';
|
||||
req.on('data', (chunk) => { body += chunk; });
|
||||
req.on('end', () => {
|
||||
res.writeHead(200, {'content-type': 'application/json', 'connection': 'keep-alive', 'trailer': 'x-t'});
|
||||
res.end(JSON.stringify({method: req.method, url: req.url, headers: req.headers, bodyLength: body.length,
|
||||
env: {endpoint: process.env.AWS_LAMBDA_HTTP_ENDPOINT, init: process.env.AWS_LAMBDA_INITIALIZATION_TYPE,
|
||||
exec: process.env.AWS_EXECUTION_ENV, account: process.env.AWS_ACCOUNT_ID,
|
||||
region: process.env.AWS_REGION, custom: process.env.CUSTOM}}));
|
||||
});
|
||||
}).listen(Number(port), host);
|
||||
""";
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
S3Service s3Service;
|
||||
|
||||
@Inject
|
||||
LambdaWebEnvironmentManager environments;
|
||||
|
||||
private String functionName;
|
||||
|
||||
@BeforeEach
|
||||
void requireDocker() {
|
||||
Assumptions.assumeTrue(isDockerAvailable(), "Docker must be available for the Lambda Web data plane");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void stopEnvironments() {
|
||||
environments.stopManagedContainers();
|
||||
if (functionName != null) {
|
||||
given().when().delete(BASE + "/web-functions/" + functionName);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(180)
|
||||
void requestsReachTheServerWithTheHeadersAwsAddsAndDrops() throws Exception {
|
||||
String domain = createFunction(30);
|
||||
|
||||
Response echo = given().header("Host", domain)
|
||||
.header("x-real-ip", "1.2.3.4")
|
||||
.header("x-http-method-override", "PUT")
|
||||
.header("lambda-web-request-id", "fake")
|
||||
.header("x-forwarded-for", "9.9.9.9")
|
||||
.header("x-custom", "1")
|
||||
.when().get("/a/b?x=1");
|
||||
echo.then().statusCode(200)
|
||||
.header("content-type", "application/json")
|
||||
.header("lambda-web-request-id", matchesPattern(REQUEST_ID))
|
||||
.header("lambda-web-logstream", matchesPattern("\\d{4}/\\d{2}/\\d{2}/" + functionName + "\\[rev-[a-z0-9]{5}\\][0-9a-f]{32}"))
|
||||
.header("trailer", nullValue())
|
||||
.body("method", equalTo("GET"))
|
||||
.body("url", equalTo("/a/b?x=1"))
|
||||
.body("headers.host", equalTo(domain))
|
||||
.body("headers.x-custom", equalTo("1"))
|
||||
.body("headers.x-real-ip", nullValue())
|
||||
.body("headers.x-http-method-override", nullValue())
|
||||
.body("headers.lambda-web-endpoint-name", equalTo("dev"))
|
||||
.body("headers.x-forwarded-proto", equalTo("http"))
|
||||
.body("headers.x-forwarded-port", equalTo("4566"))
|
||||
.body("headers.x-forwarded-for", startsWith("9.9.9.9, "))
|
||||
.body("headers.connection", equalTo("close"))
|
||||
.body("env.endpoint", equalTo("0.0.0.0:3000"))
|
||||
.body("env.init", equalTo("native-http"))
|
||||
.body("env.exec", equalTo("AWS_Lambda_nodejs24.x-lite"))
|
||||
.body("env.account", equalTo("000000000000"))
|
||||
.body("env.region", equalTo("us-east-1"))
|
||||
.body("env.custom", equalTo("yes"));
|
||||
assertEquals(echo.header("lambda-web-request-id"), echo.path("headers.lambda-web-request-id"),
|
||||
"the request id the app saw is the one in the response");
|
||||
|
||||
given().header("Host", domain).body("hello body").when().post("/")
|
||||
.then().statusCode(200).body("bodyLength", equalTo(10));
|
||||
given().header("Host", domain).when().get("/status/418")
|
||||
.then().statusCode(418).header("x-app", "yes").body(equalTo("status body"));
|
||||
|
||||
// A body without a Content-Length reaches the app chunked, as on AWS.
|
||||
String chunked = raw("POST /upload HTTP/1.1\r\nHost: " + domain + "\r\nTransfer-Encoding: chunked\r\n"
|
||||
+ "Connection: close\r\n\r\n11\r\nchunked body here\r\n0\r\n\r\n");
|
||||
assertTrue(chunked.startsWith("HTTP/1.1 200 "), chunked);
|
||||
assertTrue(chunked.contains("\"bodyLength\":17"), chunked);
|
||||
assertTrue(chunked.contains("\"transfer-encoding\":\"chunked\""), chunked);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(180)
|
||||
void aTimeoutAndACrashAnswerWithThePlatformPagesAndTheNextRequestGetsAFreshEnvironment() throws Exception {
|
||||
String domain = createFunction(3);
|
||||
|
||||
Response first = given().header("Host", domain).when().get("/status/200");
|
||||
first.then().statusCode(200);
|
||||
String logStream = first.header("lambda-web-logstream");
|
||||
|
||||
Response timeout = given().header("Host", domain).when().get("/slow");
|
||||
timeout.then().statusCode(504)
|
||||
.header("lambda-web-error-type", "Runtime.RequestTimeout")
|
||||
.body(equalTo(LambdaWebErrorPages.html(LambdaWebErrorPages.REQUEST_TIMEOUT,
|
||||
timeout.header("lambda-web-request-id"))));
|
||||
|
||||
// A response that is still streaming at the deadline is cut, as on AWS.
|
||||
String streamed = raw("GET /stream HTTP/1.1\r\nHost: " + domain + "\r\nConnection: close\r\n\r\n");
|
||||
assertTrue(streamed.startsWith("HTTP/1.1 200 "), streamed);
|
||||
assertTrue(streamed.contains("chunk 0"), streamed);
|
||||
assertFalse(streamed.contains("done"), streamed);
|
||||
|
||||
Response crash = given().header("Host", domain).when().get("/crash");
|
||||
crash.then().statusCode(503)
|
||||
.header("lambda-web-error-type", "Runtime.OriginRequestError")
|
||||
.body(equalTo(LambdaWebErrorPages.html(LambdaWebErrorPages.ORIGIN_REQUEST_ERROR,
|
||||
crash.header("lambda-web-request-id"))));
|
||||
|
||||
Response after = given().header("Host", domain).when().get("/status/200");
|
||||
after.then().statusCode(200).header("lambda-web-logstream", not(equalTo(logStream)));
|
||||
assertNotEquals(logStream, after.header("lambda-web-logstream"), "a new environment serves the next request");
|
||||
}
|
||||
|
||||
private String createFunction(int timeoutSeconds) throws IOException {
|
||||
functionName = "dp-docker-" + Long.toString(System.nanoTime(), 36);
|
||||
s3Service.createBucket(BUCKET, "us-east-1");
|
||||
s3Service.putObject(BUCKET, functionName + ".zip", zip(APP), "application/zip", Map.of());
|
||||
return given().contentType("application/json")
|
||||
.body("""
|
||||
{"functionName": "%s",
|
||||
"revisionConfig": {
|
||||
"buildConfig": {"codeConfig": {"s3Object": {"bucket": "%s", "key": "%s.zip"}},
|
||||
"runtimeConfig": {"runtime": "nodejs24.x"}},
|
||||
"serviceConfig": {"executionRoleArn": "%s", "timeoutSeconds": %d,
|
||||
"environmentVariables": {"CUSTOM": "yes"}}},
|
||||
"endpointConfig": {"endpointName": "dev", "endpointType": "HomeRegion",
|
||||
"authType": "ApplicationManaged", "autoDeploymentMode": "LatestRevision"}}
|
||||
""".formatted(functionName, BUCKET, functionName, ROLE, timeoutSeconds))
|
||||
.when().put(BASE + "/web-functions")
|
||||
.then().statusCode(202)
|
||||
.extract().path("endpoint.domainName");
|
||||
}
|
||||
|
||||
/** Sends one raw HTTP/1.1 request with {@code Connection: close} and reads until the server closes. */
|
||||
private static String raw(String request) throws IOException {
|
||||
try (Socket socket = new Socket("localhost", RestAssured.port)) {
|
||||
socket.setSoTimeout(30_000);
|
||||
OutputStream out = socket.getOutputStream();
|
||||
out.write(request.getBytes(StandardCharsets.US_ASCII));
|
||||
out.flush();
|
||||
InputStream in = socket.getInputStream();
|
||||
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] zip(String indexJs) throws IOException {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(bytes)) {
|
||||
zip.putNextEntry(new ZipEntry("index.js"));
|
||||
zip.write(indexJs.getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
|
||||
private boolean isDockerAvailable() {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package io.github.hectorvent.floci.services.lambdaweb;
|
||||
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** The platform answers that need no execution environment, so no Docker. */
|
||||
@QuarkusTest
|
||||
class LambdaWebDataPlaneIntegrationTest {
|
||||
|
||||
private static final String BASE = "/2025-03-07";
|
||||
private static final String BUCKET = "web-code-data-plane";
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/web";
|
||||
private static final String UNKNOWN_HOST = "abcdefghijklmnop.001.lambda-web.us-east-1.localhost:4566";
|
||||
private static final String REQUEST_ID = "[A-Z2-7]{26}";
|
||||
|
||||
@Inject
|
||||
S3Service s3Service;
|
||||
|
||||
@Test
|
||||
void anUnknownEndpointDomainIsNotFound() {
|
||||
Response response = given().header("Host", UNKNOWN_HOST).when().get("/some/path?q=1");
|
||||
response.then().statusCode(404)
|
||||
.header("content-type", "text/html")
|
||||
.header("connection", "keep-alive")
|
||||
.header("lambda-web-error-type", "Platform.InvalidEndpoint")
|
||||
.header("lambda-web-request-id", matchesPattern(REQUEST_ID))
|
||||
.header("content-length", "225");
|
||||
String requestId = response.header("lambda-web-request-id");
|
||||
assertEquals(LambdaWebErrorPages.html(LambdaWebErrorPages.INVALID_ENDPOINT, requestId), response.asString());
|
||||
|
||||
// A domain the control plane minted but with another case or port is another domain.
|
||||
given().header("Host", UNKNOWN_HOST.toUpperCase()).when().get("/")
|
||||
.then().statusCode(404).header("lambda-web-error-type", "Platform.InvalidEndpoint");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDisabledEndpointAnswersBeforeAnyEnvironmentStarts() {
|
||||
String functionName = "dp-disabled-" + Long.toString(System.nanoTime(), 36);
|
||||
String domain = createFunction(functionName);
|
||||
given().contentType("application/json").body("{\"throttleConfig\": {\"rateLimit\": 0}}")
|
||||
.when().patch(BASE + "/web-functions/" + functionName + "/endpoints/dev")
|
||||
.then().statusCode(202);
|
||||
|
||||
Response response = given().header("Host", domain).when().get("/");
|
||||
response.then().statusCode(503)
|
||||
.header("lambda-web-error-type", "Platform.EndpointDisabled")
|
||||
.header("content-length", "274")
|
||||
.body(equalTo(LambdaWebErrorPages.html(LambdaWebErrorPages.ENDPOINT_DISABLED,
|
||||
response.header("lambda-web-request-id"))));
|
||||
|
||||
given().when().delete(BASE + "/web-functions/" + functionName).then().statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpOneDotZeroIsRefused() throws IOException {
|
||||
String response = raw("GET / HTTP/1.0\r\nHost: " + UNKNOWN_HOST + "\r\n\r\n");
|
||||
assertTrue(response.startsWith("HTTP/1.0 505 HTTP Version Not Supported\r\n"), response);
|
||||
assertTrue(response.contains("\r\nlambda-web-error-type: Platform.UnsupportedHttpVersion\r\n"), response);
|
||||
assertTrue(response.contains("\r\nconnection: close\r\n"), response);
|
||||
assertTrue(response.contains("<p>The provided HTTP version is not supported. Please use HTTP/1.1.</p>"), response);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsupportedMethodIsRefusedByTheEdge() throws IOException {
|
||||
String response = raw("TRACE / HTTP/1.1\r\nHost: " + UNKNOWN_HOST + "\r\n\r\n");
|
||||
assertTrue(response.startsWith("HTTP/1.1 400 Bad Request\r\n"), response);
|
||||
assertTrue(response.contains("\r\nLambda-Web-Error-Type: Platform.MalformedRequest\r\n"), response);
|
||||
assertTrue(response.contains("\r\nContent-Type: text/html; charset=utf-8\r\n"), response);
|
||||
assertTrue(response.contains("\r\nConnection: close\r\n"), response);
|
||||
assertTrue(response.endsWith(LambdaWebErrorPages.MALFORMED_REQUEST_HTML), response);
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherHostsStillReachTheControlPlane() {
|
||||
given().when().get(BASE + "/web-account-settings").then().statusCode(200);
|
||||
}
|
||||
|
||||
private String createFunction(String functionName) {
|
||||
s3Service.createBucket(BUCKET, "us-east-1");
|
||||
s3Service.putObject(BUCKET, "app.zip", "zip".getBytes(StandardCharsets.UTF_8), "application/zip", Map.of());
|
||||
return given().contentType("application/json")
|
||||
.body("""
|
||||
{"functionName": "%s",
|
||||
"revisionConfig": {
|
||||
"buildConfig": {"codeConfig": {"s3Object": {"bucket": "%s", "key": "app.zip"}},
|
||||
"runtimeConfig": {"runtime": "nodejs24.x"}},
|
||||
"serviceConfig": {"executionRoleArn": "%s"}},
|
||||
"endpointConfig": {"endpointName": "dev", "endpointType": "HomeRegion",
|
||||
"authType": "ApplicationManaged", "autoDeploymentMode": "LatestRevision"}}
|
||||
""".formatted(functionName, BUCKET, ROLE))
|
||||
.when().put(BASE + "/web-functions")
|
||||
.then().statusCode(202)
|
||||
.extract().path("endpoint.domainName");
|
||||
}
|
||||
|
||||
private static String raw(String request) throws IOException {
|
||||
try (Socket socket = new Socket("localhost", RestAssured.port)) {
|
||||
socket.setSoTimeout(10_000);
|
||||
OutputStream out = socket.getOutputStream();
|
||||
out.write(request.getBytes(StandardCharsets.US_ASCII));
|
||||
out.flush();
|
||||
InputStream in = socket.getInputStream();
|
||||
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package io.github.hectorvent.floci.services.lambdaweb;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class LambdaWebDataPlaneTest {
|
||||
|
||||
@Test
|
||||
void onlyTheMintedDomainShapesAreClaimed() {
|
||||
assertTrue(LambdaWebDataPlane.isLambdaWebHost("abcdefghijklmnop.001.lambda-web.us-east-1.localhost:4566"));
|
||||
assertTrue(LambdaWebDataPlane.isLambdaWebHost("abcdefghijklmnop.001.lambda-web.us-east-1.localhost"));
|
||||
assertTrue(LambdaWebDataPlane.isLambdaWebHost("abcdefghijklmnop.lambda-web.global.localhost:4566"));
|
||||
// A near miss is the AWS 404, so the case and the port are claimed too.
|
||||
assertTrue(LambdaWebDataPlane.isLambdaWebHost("ABCDEFGHIJKLMNOP.001.LAMBDA-WEB.US-EAST-1.LOCALHOST:4566"));
|
||||
// A dotted S3 bucket or another service's host is not.
|
||||
assertFalse(LambdaWebDataPlane.isLambdaWebHost("my.lambda-web.archive.localhost:4566"));
|
||||
assertFalse(LambdaWebDataPlane.isLambdaWebHost("abcdefghijklmnop.lambda-url.us-east-1.localhost:4566"));
|
||||
assertFalse(LambdaWebDataPlane.isLambdaWebHost("localhost:4566"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestIdsAre26CharactersOfBase32() {
|
||||
String id = LambdaWebDataPlane.newRequestId();
|
||||
assertTrue(id.matches("[A-Z2-7]{26}"), id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void thePortComesFromTheHostOrTheScheme() {
|
||||
assertEquals(4566, LambdaWebDataPlane.port("x.001.lambda-web.us-east-1.localhost:4566", "http"));
|
||||
assertEquals(80, LambdaWebDataPlane.port("x.001.lambda-web.us-east-1.localhost", "http"));
|
||||
assertEquals(443, LambdaWebDataPlane.port("x.001.lambda-web.us-east-1.floci.example", "https"));
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package io.github.hectorvent.floci.services.lambdaweb;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** The page bodies must be byte for byte what AWS sends, so their recorded content lengths pin them. */
|
||||
class LambdaWebErrorPagesTest {
|
||||
|
||||
private static final String REQUEST_ID = "WLYFST5BARDELKBHCQDDIBYUPM";
|
||||
|
||||
@Test
|
||||
void pagesHaveTheRecordedContentLengths() {
|
||||
assertEquals(225, length(LambdaWebErrorPages.INVALID_ENDPOINT));
|
||||
assertEquals(264, length(LambdaWebErrorPages.UNSUPPORTED_HTTP_VERSION));
|
||||
assertEquals(274, length(LambdaWebErrorPages.ENDPOINT_DISABLED));
|
||||
assertEquals(249, length(LambdaWebErrorPages.REQUEST_TIMEOUT));
|
||||
assertEquals(236, length(LambdaWebErrorPages.ORIGIN_REQUEST_ERROR));
|
||||
assertEquals(228, length(LambdaWebErrorPages.TIMEOUT_LISTEN_ERROR));
|
||||
assertEquals(228, length(LambdaWebErrorPages.INIT_CODE_ERROR));
|
||||
assertEquals(228, length(LambdaWebErrorPages.RUNTIME_EXIT));
|
||||
assertEquals(180, LambdaWebErrorPages.MALFORMED_REQUEST_HTML.getBytes(StandardCharsets.UTF_8).length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pageNamesTheStatusAndTheRequestId() {
|
||||
String html = LambdaWebErrorPages.html(LambdaWebErrorPages.INVALID_ENDPOINT, REQUEST_ID);
|
||||
assertTrue(html.startsWith("<!DOCTYPE html>\n<html>\n<head>\n <title>HTTP Error 404</title>\n"), html);
|
||||
assertTrue(html.contains(" <h1>404 Not Found</h1>\n <p>The requested endpoint could not be found.</p>\n"), html);
|
||||
assertTrue(html.endsWith(" <p>Request ID: " + REQUEST_ID + "</p>\n</body>\n</html>"), html);
|
||||
}
|
||||
|
||||
@Test
|
||||
void edgeDateHasATwoDigitYear() {
|
||||
assertEquals("Thu, 15 Oct 26 23:32:32 GMT", LambdaWebErrorPages.edgeDate(Instant.parse("2026-10-15T23:32:32Z")));
|
||||
}
|
||||
|
||||
private static int length(LambdaWebErrorPages.Page page) {
|
||||
return LambdaWebErrorPages.html(page, REQUEST_ID).getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
}
|
||||
@@ -529,9 +529,18 @@ class LambdaWebServiceTest {
|
||||
ObjectNode update = mapper.createObjectNode().put("description", "updated");
|
||||
first.updateEndpoint(REGION, "durable", "dev", update);
|
||||
|
||||
String domain = first.getEndpoint(REGION, "durable", "dev").domainName;
|
||||
assertEquals("dev", first.locate(domain).orElseThrow().endpoint().endpointName);
|
||||
|
||||
LambdaWebService second = service(new FileStorageFactory(storageDir));
|
||||
WebFunction restored = second.getFunction(REGION, "durable");
|
||||
assertEquals(created.functionArn, restored.functionArn);
|
||||
LambdaWebService.WebFunctionTarget target = second.locate(domain).orElseThrow();
|
||||
assertEquals(ACCOUNT_ID, target.accountId());
|
||||
assertEquals(REGION, target.region());
|
||||
assertEquals("durable", target.function().functionName);
|
||||
assertEquals("dev", target.endpoint().endpointName);
|
||||
assertTrue(second.locate("nope.001.lambda-web.us-east-1.localhost:4566").isEmpty());
|
||||
assertEquals(2, restored.revisions.size());
|
||||
WebFunctionEndpoint endpoint = second.getEndpoint(REGION, "durable", "dev");
|
||||
assertEquals("updated", endpoint.description);
|
||||
@@ -541,6 +550,8 @@ class LambdaWebServiceTest {
|
||||
assertEquals(endpoint.revisionWeights.get(0).revisionId,
|
||||
second.listRevisions(REGION, "durable", mapper.createObjectNode()).items().get(0).revisionId,
|
||||
"the LatestRevision endpoint moved to the newer revision and that move was written back");
|
||||
second.deleteEndpoint(REGION, "durable", "dev");
|
||||
assertTrue(second.locate(domain).isEmpty(), "a deleted endpoint's domain is gone");
|
||||
}
|
||||
|
||||
private static final class MemoryStorageFactory extends StorageFactory {
|
||||
|
||||
Reference in new issue
Block a user