feat(ec2): implement instant CreateFleet (#2956)

* feat(ec2): implement instant CreateFleet

* fix(ec2): address CreateFleet review feedback

* fix(ec2): harden CreateFleet placement rollback

* test(ec2): isolate CreateFleet dry-run fixture

* fix(ec2): group CreateFleet response instances

* fix(ec2): report incomplete fleet rollback

* fix(ec2): resolve fleet placement from subnet overrides
This commit is contained in:
luiseterc authored and GitHub committed 2026-09-04 10:06:44 -04:00
1 parent a7be2dd299
commit 1f87f7c482
7 files changed
+695 -2

No files matched your search

@@ -0,0 +1,218 @@
package io.github.hectorvent.floci.services.ec2;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.startsWith;
/**
* Contract coverage for the instant CreateFleet request used by Karpenter's EC2 provider.
*
* <p>The test deliberately exercises both the singular EC2 Query wire location name
* ({@code LaunchTemplateConfig.N}) and the AWS-compatible DryRun exception. No real Docker
* runtime is required because the test profile runs EC2 in mock mode.</p>
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class Ec2CreateFleetIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
private static final String CONTRACT_IMAGE_ID = "ami-create-fleet-contract";
private static String launchTemplateId;
@Test
@Order(1)
void createFleetDryRunReturnsAwsCompatibleExceptionWithoutLaunching() {
launchTemplateId = createLaunchTemplate();
given()
.formParam("Action", "CreateFleet")
.formParam("Type", "instant")
.formParam("DryRun", "true")
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.LaunchTemplateId", launchTemplateId)
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.Version", "1")
.formParam("LaunchTemplateConfig.1.Overrides.1.InstanceType", "t3.micro")
.formParam("LaunchTemplateConfig.1.Overrides.1.ImageId", CONTRACT_IMAGE_ID)
.formParam("TargetCapacitySpecification.TotalTargetCapacity", "1")
.formParam("TargetCapacitySpecification.DefaultTargetCapacityType", "on-demand")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(412)
.body(containsString("<Code>DryRunOperation</Code>"));
given()
.formParam("Action", "DescribeInstances")
.formParam("Filter.1.Name", "image-id")
.formParam("Filter.1.Value.1", CONTRACT_IMAGE_ID)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.size()", equalTo(0));
}
@Test
@Order(2)
void createFleetRejectsConflictingSubnetAndAvailabilityZone() {
given()
.formParam("Action", "CreateFleet")
.formParam("Type", "instant")
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.LaunchTemplateId", launchTemplateId)
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.Version", "1")
.formParam("LaunchTemplateConfig.1.Overrides.1.InstanceType", "t3.micro")
.formParam("LaunchTemplateConfig.1.Overrides.1.ImageId", CONTRACT_IMAGE_ID)
.formParam("LaunchTemplateConfig.1.Overrides.1.SubnetId", "subnet-default-us-east-1-a")
.formParam("LaunchTemplateConfig.1.Overrides.1.AvailabilityZone", "us-east-1b")
.formParam("TargetCapacitySpecification.TotalTargetCapacity", "1")
.formParam("TargetCapacitySpecification.DefaultTargetCapacityType", "on-demand")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("InvalidParameterCombination"));
}
@Test
@Order(3)
void createFleetSubnetOverrideDerivesAvailabilityZoneInsteadOfInheritingTemplateZone() {
String placementLaunchTemplateId = createLaunchTemplateWithAvailabilityZone();
given()
.formParam("Action", "CreateFleet")
.formParam("Type", "instant")
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.LaunchTemplateId",
placementLaunchTemplateId)
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.Version", "1")
.formParam("LaunchTemplateConfig.1.Overrides.1.InstanceType", "t3.micro")
.formParam("LaunchTemplateConfig.1.Overrides.1.ImageId", CONTRACT_IMAGE_ID)
.formParam("LaunchTemplateConfig.1.Overrides.1.SubnetId", "subnet-default-us-east-1-b")
.formParam("TargetCapacitySpecification.TotalTargetCapacity", "1")
.formParam("TargetCapacitySpecification.DefaultTargetCapacityType", "on-demand")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateFleetResponse.fleetInstanceSet.item.availabilityZone", equalTo("us-east-1b"))
.body("CreateFleetResponse.fleetInstanceSet.item.subnetId",
equalTo("subnet-default-us-east-1-b"))
.body("CreateFleetResponse.fleetInstanceSet.item.launchTemplateAndOverrides.overrides.subnetId",
equalTo("subnet-default-us-east-1-b"))
.body("CreateFleetResponse.fleetInstanceSet.item.launchTemplateAndOverrides.overrides.availabilityZone",
equalTo("us-east-1b"));
}
@Test
@Order(4)
void createFleetRollsBackEarlierLaunchesWhenLaterLaunchFails() {
given()
.formParam("Action", "CreateFleet")
.formParam("Type", "instant")
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.LaunchTemplateId", launchTemplateId)
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.Version", "1")
.formParam("LaunchTemplateConfig.1.Overrides.1.InstanceType", "t3.micro")
.formParam("LaunchTemplateConfig.1.Overrides.1.ImageId", "ami-rollback-test")
.formParam("LaunchTemplateConfig.1.Overrides.2.InstanceType", "t3.micro")
.formParam("LaunchTemplateConfig.1.Overrides.2.ImageId", "ami-rollback-test")
.formParam("LaunchTemplateConfig.1.Overrides.2.SubnetId", "subnet-does-not-exist")
.formParam("TargetCapacitySpecification.TotalTargetCapacity", "2")
.formParam("TargetCapacitySpecification.DefaultTargetCapacityType", "on-demand")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("InvalidSubnetID.NotFound"));
given()
.formParam("Action", "DescribeInstances")
.formParam("Filter.1.Name", "image-id")
.formParam("Filter.1.Value.1", "ami-rollback-test")
.formParam("Filter.2.Name", "instance-state-name")
.formParam("Filter.2.Value.1", "running")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.size()", equalTo(0));
}
@Test
@Order(5)
void createFleetLaunchesInstanceAndReturnsFleetShape() {
if (launchTemplateId == null) {
launchTemplateId = createLaunchTemplate();
}
given()
.formParam("Action", "CreateFleet")
.formParam("Type", "instant")
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.LaunchTemplateId", launchTemplateId)
.formParam("LaunchTemplateConfig.1.LaunchTemplateSpecification.Version", "1")
.formParam("LaunchTemplateConfig.1.Overrides.1.InstanceType", "t3.micro")
.formParam("LaunchTemplateConfig.1.Overrides.1.ImageId", CONTRACT_IMAGE_ID)
.formParam("LaunchTemplateConfig.1.Overrides.1.AvailabilityZone", "us-east-1b")
.formParam("TargetCapacitySpecification.TotalTargetCapacity", "2")
.formParam("TargetCapacitySpecification.DefaultTargetCapacityType", "on-demand")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateFleetResponse.fleetId", startsWith("fleet-"))
.body("CreateFleetResponse.fleetInstanceSet.item.size()", equalTo(1))
.body("CreateFleetResponse.fleetInstanceSet.item.instanceIds.item.size()", equalTo(2))
.body("CreateFleetResponse.fleetInstanceSet.item.instanceIds.item", everyItem(startsWith("i-")))
.body("CreateFleetResponse.fleetInstanceSet.item.instanceType", equalTo("t3.micro"))
.body("CreateFleetResponse.fleetInstanceSet.item.availabilityZone", equalTo("us-east-1b"))
.body("CreateFleetResponse.fleetInstanceSet.item.lifecycle", equalTo("on-demand"))
.body("CreateFleetResponse.fleetInstanceSet.item.launchTemplateAndOverrides.launchTemplateSpecification.launchTemplateId",
equalTo(launchTemplateId))
.body("CreateFleetResponse.fleetInstanceSet.item.launchTemplateAndOverrides.overrides.imageId",
equalTo(CONTRACT_IMAGE_ID));
}
private static String createLaunchTemplate() {
return given()
.formParam("Action", "CreateLaunchTemplate")
.formParam("LaunchTemplateName", "create-fleet-contract")
.formParam("LaunchTemplateData.ImageId", CONTRACT_IMAGE_ID)
.formParam("LaunchTemplateData.InstanceType", "t3.micro")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
}
private static String createLaunchTemplateWithAvailabilityZone() {
return given()
.formParam("Action", "CreateLaunchTemplate")
.formParam("LaunchTemplateName", "create-fleet-placement-contract")
.formParam("LaunchTemplateData.ImageId", CONTRACT_IMAGE_ID)
.formParam("LaunchTemplateData.InstanceType", "t3.micro")
.formParam("LaunchTemplateData.Placement.AvailabilityZone", "us-east-1a")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
}
}
@@ -0,0 +1,80 @@
package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
import io.github.hectorvent.floci.services.ec2.model.Reservation;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Ensures CreateFleet rollback keeps attempting every instance when one termination call fails.
*/
class Ec2CreateFleetRollbackTest {
private static final String REGION = "us-east-1";
@Test
void rollbackContinuesAfterOneTerminationFailureAndReportsIncompleteCleanup() {
Ec2Service service = mock(Ec2Service.class);
LaunchTemplateData template = new LaunchTemplateData();
template.setImageId("ami-test");
template.setInstanceType("t3.micro");
when(service.resolveLaunchTemplateData(REGION, "lt-test", null, "1")).thenReturn(template);
when(service.runInstances(anyString(), anyString(), anyString(), anyInt(), anyInt(), nullable(String.class),
anyList(), nullable(String.class), nullable(String.class), anyList(), nullable(String.class),
nullable(String.class), nullable(Boolean.class), nullable(String.class), anyInt(),
nullable(String.class)))
.thenReturn(reservation("i-first"))
.thenReturn(reservation("i-second"))
.thenThrow(new AwsException("InvalidSubnetID.NotFound", "launch failed", 400));
when(service.terminateInstances(REGION, List.of("i-first")))
.thenThrow(new AwsException("InternalError", "cleanup failed", 500));
when(service.terminateInstances(REGION, List.of("i-second"))).thenReturn(List.of());
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class), mock(Ec2IpamService.class));
Response response = handler.handle("CreateFleet", params(), REGION);
assertEquals(400, response.getStatus());
String responseBody = response.getEntity().toString();
assertTrue(responseBody.contains("<Code>InvalidSubnetID.NotFound</Code>"));
assertTrue(responseBody.contains("CreateFleet rollback incomplete for instance(s): i-first"));
verify(service).terminateInstances(REGION, List.of("i-first"));
verify(service).terminateInstances(REGION, List.of("i-second"));
}
private static Reservation reservation(String instanceId) {
Instance instance = new Instance();
instance.setInstanceId(instanceId);
Reservation reservation = new Reservation();
reservation.getInstances().add(instance);
return reservation;
}
private static MultivaluedMap<String, String> params() {
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
params.putSingle("Type", "instant");
params.putSingle("LaunchTemplateConfig.1.LaunchTemplateSpecification.LaunchTemplateId", "lt-test");
params.putSingle("LaunchTemplateConfig.1.LaunchTemplateSpecification.Version", "1");
params.putSingle("LaunchTemplateConfig.1.Overrides.1.InstanceType", "t3.micro");
params.putSingle("TargetCapacitySpecification.TotalTargetCapacity", "3");
return params;
}
}