mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
chore(ci): native CI now fails when the native image breaks a Docker call (#4514)
* chore(ecs): read the task metadata stats from inside a container No compatibility test reached Docker's stats API, so the native image could lose the docker-java stats models without any test failing. * chore(compat): reach the Docker commands no native test used Recording the Docker API calls of the Java suite against the native image showed commands no test reached. Cover the ones a CI run can reach: - ECS Exec resizes the terminal of an interactive session - a Lambda function with 33 MB of unzipped code is served from a code volume, which lists the volumes - Timestream for InfluxDB reboots an instance * chore(ci): run container-backed EC2 against the native image The native suites run EC2 in mock mode, so no native test reached the Docker calls 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. A new job starts the native image with container-backed EC2, once with security group enforcement and once without, since an enforced instance lives in its helper's network rather than on a VPC network. It ends by killing and restarting Floci, which must remove the VPC network the tests leave behind. * chore(compat): trust Floci's test certificate in the ECS Exec session client The native CI job runs Floci with TLS, which makes the ExecuteCommand stream URL wss:// on Floci's test CA. * chore(ci): launch arm64 EC2 guests in the container-backed EC2 job The job runs on an arm64 runner, and the x86_64 AMI starts its guest as linux/amd64. The VPC network instances never reached running there. Maven also logs its test counts now, so a skipped phase shows in the job log. * chore(compat): say why the new tests' cleanup failures are ignored * chore(ci): stop waiting for Floci once it reports unhealthy in the EC2 job The wait kept polling for the full minute after an unhealthy status, unlike the existing wait step in the same workflow.
This commit is contained in:
1 parent
b8cae89a2d
commit
dce7983b15
5 files changed
+654
No files matched your search
@@ -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<Image> 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");
|
||||
}
|
||||
}
|
||||
+280
@@ -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<FilteredLogEvent> 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<Frame> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+83
@@ -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();
|
||||
}
|
||||
}
|
||||
+23
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user