mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ec2): support Dedicated Hosts (AllocateHosts, DescribeHosts, ModifyHosts, ReleaseHosts) (#4816)
* feat(ec2): support Dedicated Hosts (AllocateHosts, DescribeHosts, ModifyHosts, ReleaseHosts) terraform's aws_ec2_host failed at create with "Operation AllocateHosts is not supported". Add the four host actions following the Capacity Reservation pattern: region-scoped store, dedicated-host tags through TagSpecification and CreateTags, released hosts kept in DescribeHosts with state "released", and unknown ids as InvalidHostID.NotFound on describe. ModifyHosts and ReleaseHosts answer per host, putting an unknown, released or (on release) occupied host in the unsuccessful set rather than failing the call. RunInstances now honours Placement.HostId and Placement.Tenancy: the host must exist, tenancy defaults to "host", the zone defaults to the host's, and DescribeInstances reports placement.hostId. DescribeHosts lists the instances placed on each host. * fix(ec2): default AllocateHosts AutoPlacement to off and drop the non-AWS host-recovery filter AWS documents AutoPlacement as defaulting to off; the host record defaulted it to on. host-recovery is not a DescribeHosts filter in the EC2 API, so it is no longer matched. * fix(ec2): validate Dedicated Host zone, settings and launch placement, serialise release against launch RunInstances now rejects a host placement whose zone, tenancy or instance type contradicts the host, re-checked under a lock shared with ModifyHosts and ReleaseHosts. AllocateHosts rejects unknown zones; on/off host settings are validated on allocate and modify. The hosts store is final again, braces and the Consumer import follow AGENTS.md, and the RunInstances mock matcher in Ec2RunInstancesPublicIpParamTest gains the two host parameters. * test(ec2): pin exact host placement error codes and the in-lock host re-check * fix(ec2): validate AllocateHosts zone against modelled zones, import classes * style(ec2): fix import order and hostLock indentation, match AWS occupied-host message * style(ec2): put java.util.Arrays after the non-java imports in the dedicated host test * style(ec2): brace the host tag update and host tag lookup conditionals
This commit is contained in:
1 parent
5ad97781ef
commit
a4c952db54
8 files changed
+1011
-106
No files matched your search
+321
@@ -0,0 +1,321 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
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 java.util.Arrays;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
/**
|
||||
* EC2 Dedicated Hosts over the Query protocol, walked through the lifecycle terraform's
|
||||
* {@code aws_ec2_host} drives: AllocateHosts, DescribeHosts until available, ModifyHosts,
|
||||
* ReleaseHosts, then DescribeHosts showing the host released. Each step is read back through
|
||||
* a separate DescribeHosts rather than trusting the mutating call's own response.
|
||||
*
|
||||
* <p>Ordered because the cases walk one host through its lifecycle.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class Ec2DedicatedHostIntegrationTest {
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
|
||||
|
||||
private static String hostId;
|
||||
private static String instanceId;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void allocateReturnsAHostId() {
|
||||
hostId = given()
|
||||
.formParam("Action", "AllocateHosts")
|
||||
.formParam("InstanceType", "m5.large")
|
||||
.formParam("AvailabilityZone", "us-east-1a")
|
||||
.formParam("AutoPlacement", "off")
|
||||
.formParam("Quantity", "1")
|
||||
.formParam("TagSpecification.1.ResourceType", "dedicated-host")
|
||||
.formParam("TagSpecification.1.Tag.1.Key", "Name")
|
||||
.formParam("TagSpecification.1.Tag.1.Value", "dh-test")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("AllocateHostsResponse.hostIdSet.item", startsWith("h-"))
|
||||
.extract().path("AllocateHostsResponse.hostIdSet.item");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void describeShowsTheHostAvailableWithItsPropertiesAndTags() {
|
||||
given()
|
||||
.formParam("Action", "DescribeHosts")
|
||||
.formParam("HostId.1", hostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeHostsResponse.hostSet.item.hostId", equalTo(hostId))
|
||||
.body("DescribeHostsResponse.hostSet.item.state", equalTo("available"))
|
||||
.body("DescribeHostsResponse.hostSet.item.availabilityZone", equalTo("us-east-1a"))
|
||||
.body("DescribeHostsResponse.hostSet.item.autoPlacement", equalTo("off"))
|
||||
.body("DescribeHostsResponse.hostSet.item.hostRecovery", equalTo("off"))
|
||||
.body("DescribeHostsResponse.hostSet.item.ownerId", equalTo("000000000000"))
|
||||
.body("DescribeHostsResponse.hostSet.item.hostProperties.instanceType", equalTo("m5.large"))
|
||||
// Allocated by type, so no family is echoed: terraform would read it as drift.
|
||||
.body("DescribeHostsResponse.hostSet.item.hostProperties.instanceFamily.size()", equalTo(0))
|
||||
.body("DescribeHostsResponse.hostSet.item.tagSet.item.key", equalTo("Name"))
|
||||
.body("DescribeHostsResponse.hostSet.item.tagSet.item.value", equalTo("dh-test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void modifyIsReadBackThroughDescribe() {
|
||||
given()
|
||||
.formParam("Action", "ModifyHosts")
|
||||
.formParam("HostId.1", hostId)
|
||||
.formParam("AutoPlacement", "on")
|
||||
.formParam("HostRecovery", "on")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ModifyHostsResponse.successful.item", equalTo(hostId));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeHosts")
|
||||
.formParam("HostId.1", hostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeHostsResponse.hostSet.item.autoPlacement", equalTo("on"))
|
||||
.body("DescribeHostsResponse.hostSet.item.hostRecovery", equalTo("on"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void runInstancesOnTheHostReportsPlacementAndOccupiesIt() {
|
||||
instanceId = given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-0abcdef1234567891")
|
||||
.formParam("InstanceType", "m5.large")
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.formParam("Placement.HostId", hostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeInstances")
|
||||
.formParam("InstanceId.1", instanceId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.placement.hostId",
|
||||
equalTo(hostId))
|
||||
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.placement.tenancy",
|
||||
equalTo("host"))
|
||||
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.placement.availabilityZone",
|
||||
equalTo("us-east-1a"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeHosts")
|
||||
.formParam("HostId.1", hostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeHostsResponse.hostSet.item.instances.item.instanceId", equalTo(instanceId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void releaseOfAnOccupiedHostIsUnsuccessful() {
|
||||
given()
|
||||
.formParam("Action", "ReleaseHosts")
|
||||
.formParam("HostId.1", hostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ReleaseHostsResponse.unsuccessful.item.resourceId", equalTo(hostId))
|
||||
.body("ReleaseHostsResponse.unsuccessful.item.error.code", equalTo("Client.InvalidHost.Occupied"))
|
||||
.body("ReleaseHostsResponse.unsuccessful.item.error.message",
|
||||
equalTo("Dedicated host '" + hostId + "' cannot be released as it is occupied."));
|
||||
|
||||
given()
|
||||
.formParam("Action", "TerminateInstances")
|
||||
.formParam("InstanceId.1", instanceId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void releaseLeavesTheHostDescribableAsReleased() {
|
||||
given()
|
||||
.formParam("Action", "ReleaseHosts")
|
||||
.formParam("HostId.1", hostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ReleaseHostsResponse.successful.item", equalTo(hostId));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeHosts")
|
||||
.formParam("HostId.1", hostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeHostsResponse.hostSet.item.state", equalTo("released"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void describeOfAnUnknownHostIsNotFound() {
|
||||
given()
|
||||
.formParam("Action", "DescribeHosts")
|
||||
.formParam("HostId.1", "h-00000000000000000")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidHostID.NotFound"))
|
||||
.body("Response.Errors.Error.Message", containsString("h-00000000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void releaseOfAnUnknownOrAlreadyReleasedHostIsUnsuccessfulNotAnError() {
|
||||
given()
|
||||
.formParam("Action", "ReleaseHosts")
|
||||
.formParam("HostId.1", "h-00000000000000000")
|
||||
.formParam("HostId.2", hostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ReleaseHostsResponse.unsuccessful.item.size()", equalTo(2))
|
||||
.body("ReleaseHostsResponse.unsuccessful.item[0].error.code", equalTo("Client.InvalidHostID.NotFound"))
|
||||
.body("ReleaseHostsResponse.unsuccessful.item[1].resourceId", equalTo(hostId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void allocationThatOmitsAutoPlacementDefaultsItToOff() {
|
||||
String defaultedHostId = given()
|
||||
.formParam("Action", "AllocateHosts")
|
||||
.formParam("InstanceFamily", "m5")
|
||||
.formParam("AvailabilityZone", "us-east-1a")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("AllocateHostsResponse.hostIdSet.item");
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeHosts")
|
||||
.formParam("HostId.1", defaultedHostId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeHostsResponse.hostSet.item.autoPlacement", equalTo("off"));
|
||||
}
|
||||
|
||||
private static ValidatableResponse post(String... kv) {
|
||||
RequestSpecification r = given().header("Authorization", AUTH_HEADER);
|
||||
for (int i = 0; i < kv.length; i += 2) {
|
||||
r = r.formParam(kv[i], kv[i + 1]);
|
||||
}
|
||||
return r.when().post("/").then();
|
||||
}
|
||||
|
||||
private static String allocate(String zone) {
|
||||
return post("Action", "AllocateHosts", "InstanceType", "m5.large", "AvailabilityZone", zone)
|
||||
.statusCode(200).extract().path("AllocateHostsResponse.hostIdSet.item");
|
||||
}
|
||||
|
||||
private static int hostCount() {
|
||||
return post("Action", "DescribeHosts").statusCode(200)
|
||||
.extract().xmlPath().getList("DescribeHostsResponse.hostSet.item").size();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void allocateRejectsANonexistentZoneAndStoresNothing() {
|
||||
int before = hostCount();
|
||||
post("Action", "AllocateHosts", "InstanceType", "m5.large", "AvailabilityZone", "us-east-1z")
|
||||
.statusCode(400).body(containsString("InvalidParameterValue"));
|
||||
org.junit.jupiter.api.Assertions.assertEquals(before, hostCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void invalidHostSettingsAreRejectedOnAllocateAndModifyAndNotStored() {
|
||||
post("Action", "AllocateHosts", "InstanceType", "m5.large", "AvailabilityZone", "us-east-1a",
|
||||
"AutoPlacement", "banana").statusCode(400).body(containsString("InvalidParameterValue"));
|
||||
String id = allocate("us-east-1a");
|
||||
post("Action", "ModifyHosts", "HostId.1", id, "HostRecovery", "banana")
|
||||
.statusCode(400).body(containsString("InvalidParameterValue"));
|
||||
post("Action", "DescribeHosts", "HostId.1", id).statusCode(200)
|
||||
.body("DescribeHostsResponse.hostSet.item.hostRecovery", equalTo("off"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void launchPlacementThatContradictsTheHostIsRejected() {
|
||||
String id = allocate("us-east-1a");
|
||||
String subnetInB = post("Action", "DescribeSubnets", "Filter.1.Name", "availability-zone",
|
||||
"Filter.1.Value.1", "us-east-1b").statusCode(200)
|
||||
.extract().xmlPath().getList("DescribeSubnetsResponse.subnetSet.item.subnetId").get(0).toString();
|
||||
String[] base = {"Action", "RunInstances", "ImageId", "ami-0abcdef1234567891", "MinCount", "1",
|
||||
"MaxCount", "1", "Placement.HostId", id};
|
||||
post(concat(base, "InstanceType", "m5.large", "SubnetId", subnetInB))
|
||||
.statusCode(400).body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
|
||||
post(concat(base, "InstanceType", "m5.large", "Placement.Tenancy", "default"))
|
||||
.statusCode(400).body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
|
||||
post(concat(base, "InstanceType", "c5.large"))
|
||||
.statusCode(400).body("Response.Errors.Error.Code", equalTo("InvalidParameter"));
|
||||
post("Action", "DescribeHosts", "HostId.1", id).statusCode(200)
|
||||
.body("DescribeHostsResponse.hostSet.item.instances.item.size()", equalTo(0));
|
||||
}
|
||||
|
||||
private static String[] concat(String[] a, String... b) {
|
||||
String[] out = Arrays.copyOf(a, a.length + b.length);
|
||||
System.arraycopy(b, 0, out, a.length, b.length);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -60,7 +60,8 @@ class Ec2RunInstancesPublicIpParamTest {
|
||||
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),
|
||||
nullable(LaunchTemplateData.MetadataOptions.class), nullable(String.class), nullable(String.class), eq(false)))
|
||||
nullable(LaunchTemplateData.MetadataOptions.class), nullable(String.class), nullable(String.class), eq(false),
|
||||
nullable(String.class), nullable(String.class)))
|
||||
.thenReturn(new Reservation());
|
||||
|
||||
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
|
||||
@@ -73,7 +74,8 @@ class Ec2RunInstancesPublicIpParamTest {
|
||||
anyInt(), anyInt(), nullable(String.class), anyList(), nullable(String.class),
|
||||
nullable(String.class), anyList(), nullable(String.class), nullable(String.class),
|
||||
associatePublicIp.capture(), nullable(String.class), anyInt(), nullable(String.class),
|
||||
nullable(LaunchTemplateData.MetadataOptions.class), nullable(String.class), nullable(String.class), eq(false));
|
||||
nullable(LaunchTemplateData.MetadataOptions.class), nullable(String.class), nullable(String.class), eq(false),
|
||||
nullable(String.class), nullable(String.class));
|
||||
if (expected == null) {
|
||||
assertNull(associatePublicIp.getValue(),
|
||||
"an absent override must stay null so the subnet default decides");
|
||||
|
||||
@@ -4898,4 +4898,52 @@ class Ec2ServiceTest {
|
||||
// Provider remains untouched
|
||||
assertEquals(1, nodeInstance.getTags().size());
|
||||
}
|
||||
|
||||
// The host is released after runInstances' unlocked availability check and before its instances
|
||||
// are stored. The instance-type catalog lookup sits between the two, so it stands in for the
|
||||
// concurrent ReleaseHosts. Only the re-check under hostLock can still reject the launch.
|
||||
@Test
|
||||
void launchOntoAHostReleasedAfterTheUnlockedCheckIsRejected() {
|
||||
Ec2Service[] holder = new Ec2Service[1];
|
||||
String[] hostId = new String[1];
|
||||
boolean[] released = {false};
|
||||
Ec2InstanceTypeCatalog releasingCatalog = new Ec2InstanceTypeCatalog() {
|
||||
@Override
|
||||
public Optional<CatalogInstanceType> find(String instanceType) {
|
||||
if (hostId[0] != null && !released[0]) {
|
||||
released[0] = true;
|
||||
holder[0].releaseHost("us-east-1", hostId[0]);
|
||||
}
|
||||
return super.find(instanceType);
|
||||
}
|
||||
};
|
||||
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), new AmiImageResolver(imageCatalog), imageCatalog,
|
||||
releasingCatalog, new InMemoryStorageFactory());
|
||||
holder[0] = service;
|
||||
hostId[0] = service.allocateHosts("us-east-1", "us-east-1a", "m5.large", null, 1,
|
||||
null, null, null, null, null).get(0).getHostId();
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.runInstances("us-east-1",
|
||||
"ami-ubuntu2404-amd64", "m5.large", 1, 1, null, List.of(), null, null, List.of(), null, null,
|
||||
null, null, 0, null, null, null, null, false, hostId[0], null));
|
||||
|
||||
assertTrue(released[0], "the hook must have released the host between the two checks");
|
||||
assertEquals("InvalidHostID.NotFound", e.getErrorCode());
|
||||
assertTrue(service.hostInstances("us-east-1", hostId[0]).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void allocateHostsAcceptsModelledZoneWithNoSubnet() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
service.describeSubnets("us-east-1", List.of(), Map.of()).stream()
|
||||
.filter(sn -> "us-east-1c".equals(sn.getAvailabilityZone()))
|
||||
.forEach(sn -> service.deleteSubnet("us-east-1", sn.getSubnetId()));
|
||||
|
||||
assertEquals(1, service.allocateHosts("us-east-1", "us-east-1c", "m5.large", null, 1,
|
||||
null, null, null, null, null).size());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user