diff --git a/.github/workflows/compatibility.yml b/.github/workflows/compatibility.yml index d1f2095a24..9fd3673869 100644 --- a/.github/workflows/compatibility.yml +++ b/.github/workflows/compatibility.yml @@ -437,3 +437,123 @@ jobs: # where the server log matters most. if: failure() || cancelled() run: docker logs floci + + native-ec2-containers: + name: native arm64 / ec2-containers + needs: build-native + runs-on: ubuntu-24.04-arm + timeout-minutes: 20 + # The suites above run EC2 in mock mode, so no native test reached the Docker calls that only + # container-backed EC2 makes: building the security group helper image, committing an instance + # as an AMI and removing that image, and the VPC networks. An instance under security group + # enforcement lives in its helper's network rather than on a VPC network, so Floci runs twice, + # with enforcement on and then off. At the end it is killed and restarted, and must remove the + # VPC network the tests leave behind. + steps: + - name: Download native floci image + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + name: floci-native-image-arm64 + path: /tmp + + - name: Load native floci image + run: gunzip -c /tmp/floci-native-image.tar.gz | docker load + + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + sparse-checkout: | + compatibility-tests + .github/ci + + - uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0 + + - name: Build test image + uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0 + with: + context: compatibility-tests/sdk-test-java + load: true + tags: compat-sdk-test-java + cache-from: type=gha,scope=sdk-test-java + + - name: Write helper scripts + run: | + mkdir -p test-results + docker network create compat-net + cat > start-floci.sh <<'SH' + docker run -d --name floci --network compat-net \ + -p 4566:4566 \ + -v /var/run/docker.sock:/var/run/docker.sock \ + --group-add "$(stat -c '%g' /var/run/docker.sock)" \ + -e FLOCI_BASE_URL=http://floci:4566 \ + -e FLOCI_SERVICES_DOCKER_NETWORK=compat-net \ + -e FLOCI_HOSTNAME=floci \ + "$@" floci:test-native + SH + cat > wait-floci.sh <<'SH' + health=starting + for _ in $(seq 1 60); do + health=$(docker inspect -f '{{.State.Health.Status}}' floci) + if [ "$health" = healthy ]; then + exit 0 + fi + if [ "$health" = unhealthy ]; then + break + fi + sleep 1 + done + echo "::error::floci did not report healthy (last status: $health)" + docker inspect -f '{{json .State.Health}}' floci + docker logs --tail 50 floci + exit 1 + SH + cat > run-tests.sh <<'SH' + docker run --rm --network compat-net \ + --dns "$(docker inspect -f '{{(index .NetworkSettings.Networks "compat-net").IPAddress}}' floci)" \ + -e FLOCI_ENDPOINT=http://floci:4566 \ + -e FLOCI_EC2_CONTAINERS="$1" \ + -v "$(pwd)/test-results/$1:/results" \ + --entrypoint bash \ + compat-sdk-test-java -c 'status=0; mvn test -B -Dtest=Ec2ContainerTest -Dorg.slf4j.simpleLogger.defaultLogLevel=warn || status=$?; cp target/surefire-reports/TEST-*.xml /results/ 2>/dev/null || true; exit $status' + SH + + - name: Run tests with security group enforcement + id: tests + run: | + bash start-floci.sh -e FLOCI_NETWORK_SECURITY_GROUP_ENFORCEMENT_ENABLED=true + bash wait-floci.sh + bash run-tests.sh security-groups + + - name: Run tests on VPC networks + run: | + docker rm -f floci + bash start-floci.sh + bash wait-floci.sh + bash run-tests.sh vpc-networks + + - name: Restart floci and check the orphaned VPC network is removed + run: | + # Killed, not stopped, so the instance container stays attached to its VPC network. + docker kill floci + docker start floci + bash wait-floci.sh + # EC2 reconciles its networks when it first starts, which is on its first request. + curl -s -o /dev/null -X POST http://localhost:4566/ \ + -H 'Authorization: AWS4-HMAC-SHA256 Credential=test/20260101/us-east-1/ec2/aws4_request, SignedHeaders=host, Signature=0' \ + -d 'Action=DescribeVpcs&Version=2016-11-15' + docker logs floci 2>&1 | grep 'Reconciled orphaned VPC network' + left=$(docker network ls -q --filter label=floci_component=ec2-vpc) + if [ -n "$left" ]; then + echo "::error::VPC networks survived the restart: $left" + exit 1 + fi + + - name: Generate test summary + if: always() && steps.tests.outcome != 'skipped' + uses: test-summary/action@37b508cfee6d4d080eedd00b5bb240a6a784a6a5 # v2 + with: + paths: test-results/*/*.xml + + - name: Dump floci logs + if: failure() || cancelled() + run: docker logs floci diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2ContainerTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2ContainerTest.java new file mode 100644 index 0000000000..53cff49ece --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2ContainerTest.java @@ -0,0 +1,148 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.ec2.Ec2Client; +import software.amazon.awssdk.services.ec2.model.CreateImageRequest; +import software.amazon.awssdk.services.ec2.model.CreateSubnetRequest; +import software.amazon.awssdk.services.ec2.model.CreateVpcRequest; +import software.amazon.awssdk.services.ec2.model.DeleteSubnetRequest; +import software.amazon.awssdk.services.ec2.model.DeleteVpcRequest; +import software.amazon.awssdk.services.ec2.model.DeregisterImageRequest; +import software.amazon.awssdk.services.ec2.model.DescribeImagesRequest; +import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest; +import software.amazon.awssdk.services.ec2.model.Image; +import software.amazon.awssdk.services.ec2.model.Instance; +import software.amazon.awssdk.services.ec2.model.InstanceStateName; +import software.amazon.awssdk.services.ec2.model.InstanceType; +import software.amazon.awssdk.services.ec2.model.RunInstancesRequest; +import software.amazon.awssdk.services.ec2.model.TerminateInstancesRequest; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +/** + * EC2 instances backed by real containers. Floci runs EC2 in mock mode for the regular native + * suites, so this class only runs where the workflow starts Floci with container-backed EC2 and + * sets FLOCI_EC2_CONTAINERS. An instance under security group enforcement lives in its firewall + * helper's network, never on a VPC network, so the workflow runs the class once per mode: + * {@code security-groups} with enforcement on, {@code vpc-networks} with it off. + */ +@DisplayName("EC2 instances backed by containers") +class Ec2ContainerTest { + + // The CI job runs on arm64, where an arm64 guest needs no emulation. + private static final String AMI = "ami-amazonlinux2023-arm64"; + private static final String MODE = System.getenv("FLOCI_EC2_CONTAINERS"); + + private static Ec2Client ec2; + + @BeforeAll + static void setup() { + assumeTrue(MODE != null, "Skipping container-backed EC2 test: FLOCI_EC2_CONTAINERS is not set"); + ec2 = TestFixtures.ec2Client(); + } + + @AfterAll + static void cleanup() { + if (ec2 != null) { + ec2.close(); + } + } + + @Test + @DisplayName("An instance under security group enforcement runs and is captured as an AMI") + void instanceUnderSecurityGroupEnforcementIsCaptured() throws InterruptedException { + assumeTrue("security-groups".equals(MODE), "Runs with security group enforcement on"); + String instanceId = runInstance(null); + awaitState(instanceId, InstanceStateName.RUNNING); + + String imageId = ec2.createImage(CreateImageRequest.builder() + .instanceId(instanceId).name("container-test-" + System.currentTimeMillis()).build()).imageId(); + assertThat(awaitImage(imageId).stateAsString()).isEqualTo("available"); + ec2.deregisterImage(DeregisterImageRequest.builder().imageId(imageId).build()); + assertThat(ec2.describeImages(DescribeImagesRequest.builder().imageIds(imageId).build()).images()).isEmpty(); + + ec2.terminateInstances(TerminateInstancesRequest.builder().instanceIds(instanceId).build()); + awaitState(instanceId, InstanceStateName.TERMINATED); + } + + @Test + @DisplayName("An instance in a VPC gets an address from its subnet and is torn down with its VPC") + void instanceInAVpcIsTornDownWithItsVpc() throws InterruptedException { + assumeTrue("vpc-networks".equals(MODE), "Runs with security group enforcement off"); + String vpcId = ec2.createVpc(CreateVpcRequest.builder().cidrBlock("10.87.0.0/16").build()).vpc().vpcId(); + String subnetId = ec2.createSubnet(CreateSubnetRequest.builder() + .vpcId(vpcId).cidrBlock("10.87.1.0/24").build()).subnet().subnetId(); + String instanceId = runInstance(subnetId); + + Instance running = awaitState(instanceId, InstanceStateName.RUNNING); + assertThat(running.privateIpAddress()).startsWith("10.87.1."); + + ec2.terminateInstances(TerminateInstancesRequest.builder().instanceIds(instanceId).build()); + awaitState(instanceId, InstanceStateName.TERMINATED); + ec2.deleteSubnet(DeleteSubnetRequest.builder().subnetId(subnetId).build()); + ec2.deleteVpc(DeleteVpcRequest.builder().vpcId(vpcId).build()); + } + + /** + * Leaves an instance running in its own VPC on purpose. The workflow then kills and restarts + * Floci, and the restarted Floci must disconnect the instance and remove the orphaned network. + */ + @Test + @DisplayName("An instance is left running in a VPC for the restart check") + void instanceLeftRunningForTheRestartCheck() throws InterruptedException { + assumeTrue("vpc-networks".equals(MODE), "Runs with security group enforcement off"); + String vpcId = ec2.createVpc(CreateVpcRequest.builder().cidrBlock("10.88.0.0/16").build()).vpc().vpcId(); + String subnetId = ec2.createSubnet(CreateSubnetRequest.builder() + .vpcId(vpcId).cidrBlock("10.88.1.0/24").build()).subnet().subnetId(); + + Instance running = awaitState(runInstance(subnetId), InstanceStateName.RUNNING); + + assertThat(running.privateIpAddress()).startsWith("10.88.1."); + } + + private static String runInstance(String subnetId) { + return ec2.runInstances(RunInstancesRequest.builder() + .imageId(AMI) + .instanceType(InstanceType.T4_G_MICRO) + .subnetId(subnetId) + .minCount(1) + .maxCount(1) + .build()).instances().get(0).instanceId(); + } + + private static Instance awaitState(String instanceId, InstanceStateName target) throws InterruptedException { + Instance instance = null; + for (int i = 0; i < 180; i++) { + instance = ec2.describeInstances(DescribeInstancesRequest.builder().instanceIds(instanceId).build()) + .reservations().get(0).instances().get(0); + if (instance.state().name() == target) { + return instance; + } + if (instance.state().name() == InstanceStateName.TERMINATED) { + break; + } + Thread.sleep(1000); + } + throw new AssertionError("Instance " + instanceId + " stayed " + instance.state().name() + + " instead of reaching " + target); + } + + private static Image awaitImage(String imageId) throws InterruptedException { + Image image = null; + for (int i = 0; i < 180; i++) { + List images = ec2.describeImages(DescribeImagesRequest.builder().imageIds(imageId).build()).images(); + image = images.isEmpty() ? null : images.get(0); + if (image != null && !"pending".equals(image.stateAsString())) { + return image; + } + Thread.sleep(1000); + } + throw new AssertionError("Image " + imageId + " stayed pending"); + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcsRunningTaskTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcsRunningTaskTest.java new file mode 100644 index 0000000000..145c7cd1fa --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcsRunningTaskTest.java @@ -0,0 +1,280 @@ +package com.floci.test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsRequest; +import software.amazon.awssdk.services.cloudwatchlogs.model.FilteredLogEvent; +import software.amazon.awssdk.services.ecs.EcsClient; +import software.amazon.awssdk.services.ecs.model.ContainerDefinition; +import software.amazon.awssdk.services.ecs.model.CreateClusterRequest; +import software.amazon.awssdk.services.ecs.model.DeleteClusterRequest; +import software.amazon.awssdk.services.ecs.model.ExecuteCommandRequest; +import software.amazon.awssdk.services.ecs.model.LaunchType; +import software.amazon.awssdk.services.ecs.model.NetworkMode; +import software.amazon.awssdk.services.ecs.model.RegisterTaskDefinitionRequest; +import software.amazon.awssdk.services.ecs.model.RunTaskRequest; +import software.amazon.awssdk.services.ecs.model.Session; +import software.amazon.awssdk.services.ecs.model.StopTaskRequest; + +import java.io.ByteArrayOutputStream; +import java.net.URI; +import java.net.http.WebSocket; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +@DisplayName("ECS running task: metadata stats and ECS Exec") +class EcsRunningTaskTest { + + private static final String STATS_MARKER = "STATS:"; + + private static EcsClient ecs; + private static CloudWatchLogsClient logs; + private static String clusterName; + private static String family; + private static String taskArn; + + @BeforeAll + static void setup() { + assumeTrue(TestFixtures.isLambdaDispatchAvailable(), + "Skipping ECS running task test: Docker dispatch not available in this environment"); + ecs = TestFixtures.ecsClient(); + logs = TestFixtures.cloudWatchLogsClient(); + String suffix = String.valueOf(System.currentTimeMillis() % 100000); + clusterName = "stats-cluster-" + suffix; + family = "stats-task-" + suffix; + ecs.createCluster(CreateClusterRequest.builder().clusterName(clusterName).build()); + ecs.registerTaskDefinition(RegisterTaskDefinitionRequest.builder() + .family(family) + .networkMode(NetworkMode.BRIDGE) + .containerDefinitions(ContainerDefinition.builder() + .name("main") + .image("busybox:latest") + .command("sh", "-c", "sleep 2; echo " + STATS_MARKER + + "$(wget -qO- $ECS_CONTAINER_METADATA_URI_V4/stats); sleep 120") + .essential(true) + .memory(64) + .build()) + .build()); + taskArn = ecs.runTask(RunTaskRequest.builder() + .cluster(clusterName) + .taskDefinition(family) + .launchType(LaunchType.FARGATE) + .enableExecuteCommand(true) + .count(1) + .build()).tasks().get(0).taskArn(); + } + + @AfterAll + static void cleanup() { + if (ecs == null) { + return; + } + if (taskArn != null) { + try { + ecs.stopTask(StopTaskRequest.builder().cluster(clusterName).task(taskArn).build()); + } catch (Exception ignored) { + // The task may already have stopped. Cleanup must not hide the test's own result. + } + } + try { + ecs.deleteCluster(DeleteClusterRequest.builder().cluster(clusterName).build()); + } catch (Exception ignored) { + // Cleanup must not hide the test's own result. + } + ecs.close(); + logs.close(); + } + + @Test + @DisplayName("GET ${ECS_CONTAINER_METADATA_URI_V4}/stats returns Docker's stats for the container") + void containerReadsItsOwnStats() throws Exception { + String stats = awaitStatsLine(); + + JsonNode node = new ObjectMapper().readTree(stats); + assertThat(node.path("cpu_stats").path("cpu_usage").path("total_usage").asLong()).isPositive(); + assertThat(node.path("memory_stats").path("usage").asLong()).isPositive(); + } + + @Test + @DisplayName("ExecuteCommand - a size message resizes the session's terminal") + void execSessionResizesTheTerminal() throws Exception { + Session session = ecs.executeCommand(ExecuteCommandRequest.builder() + .cluster(clusterName) + .task(taskArn) + .container("main") + .interactive(true) + .command("/bin/sh") + .build()).session(); + + SessionClient client = new SessionClient(session); + try { + client.open(); + assertThat(client.next().payloadType).isEqualTo(SessionClient.PAYLOAD_HANDSHAKE_REQUEST); + client.send(SessionClient.PAYLOAD_HANDSHAKE_RESPONSE, "{\"ClientVersion\":\"1.2.0.0\"," + + "\"ProcessedClientActions\":[{\"ActionType\":\"SessionType\",\"ActionStatus\":1}],\"Errors\":\"\"}"); + assertThat(client.readUntil("# ", 30)).contains("# "); + client.send(SessionClient.PAYLOAD_SIZE, "{\"cols\":100,\"rows\":40}"); + + // The resize reaches Docker asynchronously, so ask again until the new size shows. + String output = ""; + for (int attempt = 0; attempt < 5 && !output.contains("40 100"); attempt++) { + client.send(SessionClient.PAYLOAD_OUTPUT, "stty size\n"); + output = client.readUntil("40 100", 2); + } + assertThat(output).contains("40 100"); + } finally { + client.close(); + } + } + + private static String awaitStatsLine() throws InterruptedException { + String logGroup = "/ecs/" + family; + long deadline = System.currentTimeMillis() + 60_000; + while (System.currentTimeMillis() < deadline) { + try { + List events = logs.filterLogEvents(FilterLogEventsRequest.builder() + .logGroupName(logGroup) + .build()).events(); + for (FilteredLogEvent event : events) { + String message = event.message().strip(); + if (message.startsWith(STATS_MARKER)) { + return message.substring(STATS_MARKER.length()); + } + } + } catch (Exception ignored) { + // The log group does not exist until the container writes its first line. + } + Thread.sleep(1000); + } + throw new AssertionError("No stats line in " + logGroup + " within 60s"); + } + + /** The client half of the Session Manager data channel that session-manager-plugin speaks. */ + private static final class SessionClient implements WebSocket.Listener { + + static final int PAYLOAD_OUTPUT = 1; + static final int PAYLOAD_SIZE = 3; + static final int PAYLOAD_HANDSHAKE_REQUEST = 5; + static final int PAYLOAD_HANDSHAKE_RESPONSE = 6; + + private static final String INPUT_STREAM_DATA = "input_stream_data"; + private static final String OUTPUT_STREAM_DATA = "output_stream_data"; + private static final int PAYLOAD_OFFSET = 120; + + private final Session session; + private final BlockingQueue received = new LinkedBlockingQueue<>(); + private final ByteArrayOutputStream partial = new ByteArrayOutputStream(); + private WebSocket webSocket; + private long sequence; + + SessionClient(Session session) { + this.session = session; + } + + void open() throws Exception { + webSocket = TestFixtures.emulatorHttpClient().newWebSocketBuilder() + .buildAsync(URI.create(session.streamUrl()), this) + .get(20, TimeUnit.SECONDS); + webSocket.sendText("{\"MessageSchemaVersion\":\"1.0\",\"RequestId\":\"" + UUID.randomUUID() + + "\",\"TokenValue\":\"" + session.tokenValue() + "\",\"ClientId\":\"" + UUID.randomUUID() + "\"}", + true).get(10, TimeUnit.SECONDS); + } + + Frame next() throws InterruptedException { + while (true) { + Frame frame = received.poll(30, TimeUnit.SECONDS); + assertThat(frame).as("a frame from the agent").isNotNull(); + if (OUTPUT_STREAM_DATA.equals(frame.messageType)) { + return frame; + } + } + } + + void send(int payloadType, String payload) throws Exception { + byte[] body = payload.getBytes(StandardCharsets.UTF_8); + byte[] type = new byte[32]; + Arrays.fill(type, (byte) ' '); + byte[] name = INPUT_STREAM_DATA.getBytes(StandardCharsets.UTF_8); + System.arraycopy(name, 0, type, 0, name.length); + UUID id = UUID.randomUUID(); + ByteBuffer buffer = ByteBuffer.allocate(PAYLOAD_OFFSET + body.length); + buffer.putInt(116); + buffer.put(type); + buffer.putInt(1); + buffer.putLong(System.currentTimeMillis()); + buffer.putLong(sequence++); + buffer.putLong(0L); + buffer.putLong(id.getLeastSignificantBits()); + buffer.putLong(id.getMostSignificantBits()); + buffer.put(MessageDigest.getInstance("SHA-256").digest(body)); + buffer.putInt(payloadType); + buffer.putInt(body.length); + buffer.put(body); + webSocket.sendBinary(ByteBuffer.wrap(buffer.array()), true).get(10, TimeUnit.SECONDS); + } + + String readUntil(String expected, int seconds) throws InterruptedException { + StringBuilder output = new StringBuilder(); + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(seconds); + while (System.nanoTime() < deadline && !output.toString().contains(expected)) { + Frame frame = received.poll(1, TimeUnit.SECONDS); + if (frame != null && OUTPUT_STREAM_DATA.equals(frame.messageType) + && frame.payloadType == PAYLOAD_OUTPUT) { + output.append(new String(frame.payload, StandardCharsets.UTF_8)); + } + } + return output.toString(); + } + + void close() { + if (webSocket != null) { + webSocket.abort(); + } + } + + @Override + public CompletionStage onBinary(WebSocket socket, ByteBuffer data, boolean last) { + byte[] chunk = new byte[data.remaining()]; + data.get(chunk); + partial.writeBytes(chunk); + if (last) { + received.add(Frame.decode(partial.toByteArray())); + partial.reset(); + } + socket.request(1); + return null; + } + + private record Frame(String messageType, int payloadType, byte[] payload) { + + static Frame decode(byte[] bytes) { + ByteBuffer buffer = ByteBuffer.wrap(bytes); + buffer.position(4); + byte[] type = new byte[32]; + buffer.get(type); + buffer.position(112); + int payloadType = buffer.getInt(); + int length = buffer.getInt(); + byte[] payload = Arrays.copyOfRange(bytes, PAYLOAD_OFFSET, + Math.min(bytes.length, PAYLOAD_OFFSET + length)); + return new Frame(new String(type, StandardCharsets.UTF_8).trim(), payloadType, payload); + } + } + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaCodeVolumeTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaCodeVolumeTest.java new file mode 100644 index 0000000000..98a53ecf26 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaCodeVolumeTest.java @@ -0,0 +1,83 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.services.lambda.LambdaClient; +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.Runtime; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +@DisplayName("Lambda code large enough to be served from a code volume") +class LambdaCodeVolumeTest { + + private static final String FUNCTION_NAME = TestFixtures.uniqueName("code-volume-fn"); + + private static LambdaClient lambda; + + @BeforeAll + static void setup() { + assumeTrue(TestFixtures.isLambdaDispatchAvailable(), + "Skipping code volume test: Docker dispatch not available in this environment"); + lambda = TestFixtures.lambdaClient(); + } + + @AfterAll + static void cleanup() { + if (lambda == null) { + return; + } + try { + lambda.deleteFunction(DeleteFunctionRequest.builder().functionName(FUNCTION_NAME).build()); + } catch (Exception ignored) { + // The function does not exist if the test failed before creating it. Cleanup must not hide the test's own result. + } + lambda.close(); + } + + @Test + @DisplayName("Invoke - a function with 33 MB of unzipped code runs") + void functionWithLargeCodeRuns() throws IOException { + lambda.createFunction(CreateFunctionRequest.builder() + .functionName(FUNCTION_NAME) + .runtime(Runtime.NODEJS20_X) + .role("arn:aws:iam::000000000000:role/lambda-role") + .handler("index.handler") + .timeout(60) + .code(FunctionCode.builder().zipFile(SdkBytes.fromByteArray(largeCodeZip())).build()) + .build()); + + InvokeResponse response = lambda.invoke(InvokeRequest.builder().functionName(FUNCTION_NAME).build()); + + assertThat(response.functionError()).isNull(); + assertThat(response.payload().asUtf8String()).isEqualTo("{\"ok\":true}"); + } + + /** Zeros compress to almost nothing, so the zip stays small while the unzipped code passes 32 MB. */ + private static byte[] largeCodeZip() throws IOException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (ZipOutputStream zip = new ZipOutputStream(bytes)) { + zip.putNextEntry(new ZipEntry("index.js")); + zip.write("exports.handler = async () => ({ ok: true });".getBytes(StandardCharsets.UTF_8)); + zip.closeEntry(); + zip.putNextEntry(new ZipEntry("padding.bin")); + zip.write(new byte[33 * 1024 * 1024]); + zip.closeEntry(); + } + return bytes.toByteArray(); + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/TimestreamInfluxDbTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/TimestreamInfluxDbTest.java index aa7543c141..28d000c9b8 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/TimestreamInfluxDbTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/TimestreamInfluxDbTest.java @@ -15,6 +15,7 @@ import software.amazon.awssdk.services.timestreaminfluxdb.model.ListDbInstancesR import software.amazon.awssdk.services.timestreaminfluxdb.model.ListTagsForResourceRequest; import software.amazon.awssdk.services.timestreaminfluxdb.model.LogLevel; import software.amazon.awssdk.services.timestreaminfluxdb.model.Parameters; +import software.amazon.awssdk.services.timestreaminfluxdb.model.RebootDbInstanceRequest; import software.amazon.awssdk.services.timestreaminfluxdb.model.ResourceNotFoundException; import software.amazon.awssdk.services.timestreaminfluxdb.model.TagResourceRequest; import software.amazon.awssdk.services.timestreaminfluxdb.model.ValidationException; @@ -67,6 +68,11 @@ class TimestreamInfluxDbTest { .resourceArn(created.arn()).build()).tags(); assertEquals("compat", tags.get("env")); assertEquals("metrics", tags.get("team")); + + awaitStatus(client, created.id(), "AVAILABLE"); + assertEquals("REBOOTING", client.rebootDbInstance(RebootDbInstanceRequest.builder() + .identifier(created.id()).build()).statusAsString()); + awaitStatus(client, created.id(), "AVAILABLE"); } finally { client.deleteDbInstance(DeleteDbInstanceRequest.builder().identifier(created.id()).build()); } @@ -83,4 +89,21 @@ class TimestreamInfluxDbTest { .build())); } } + + private static void awaitStatus(TimestreamInfluxDbClient client, String id, String expected) { + String status = ""; + long deadline = System.currentTimeMillis() + 180_000; + while (System.currentTimeMillis() < deadline && !expected.equals(status)) { + status = client.getDbInstance(GetDbInstanceRequest.builder().identifier(id).build()).statusAsString(); + if (!expected.equals(status)) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(e); + } + } + } + assertEquals(expected, status); + } }