fix(ec2): honor DHCP options dry runs and restore default set (#6448)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-25 09:15:34 -04:00
1 parent ddb94d6c15
commit 4244e64833
5 files changed
+191 -10

No files matched your search

@@ -234,6 +234,136 @@ class Ec2DhcpOptionsIntegrationTest {
}
}
@Test
void dryRunDhcpOptionsWritesDoNotMutateState() {
String domain = "dry-run-" + System.nanoTime() + ".example";
String id = createOptions(domain);
String vpcId = given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", "10.254.0.0/16")
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(200).extract()
.xmlPath().getString("CreateVpcResponse.vpc.vpcId");
try {
given()
.formParam("Action", "CreateDhcpOptions")
.formParam("DhcpConfiguration.1.Key", "domain-name")
.formParam("DhcpConfiguration.1.Value.1", domain)
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(412).body(containsString("DryRunOperation"));
given()
.formParam("Action", "DescribeDhcpOptions")
.formParam("Filter.1.Name", "value")
.formParam("Filter.1.Value.1", domain)
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(200)
.body("DescribeDhcpOptionsResponse.dhcpOptionsSet.item.dhcpOptionsId", equalTo(id));
given()
.formParam("Action", "AssociateDhcpOptions")
.formParam("DhcpOptionsId", id)
.formParam("VpcId", vpcId)
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(412).body(containsString("DryRunOperation"));
given()
.formParam("Action", "DescribeVpcs")
.formParam("VpcId.1", vpcId)
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(200)
.body("DescribeVpcsResponse.vpcSet.item.dhcpOptionsId", equalTo("dopt-default"));
given()
.formParam("Action", "DeleteDhcpOptions")
.formParam("DhcpOptionsId", id)
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(412).body(containsString("DryRunOperation"));
given()
.formParam("Action", "DescribeDhcpOptions")
.formParam("DhcpOptionsId.1", id)
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(200)
.body("DescribeDhcpOptionsResponse.dhcpOptionsSet.item.dhcpOptionsId", equalTo(id));
} finally {
deleteOptions(id);
given()
.formParam("Action", "DeleteVpc")
.formParam("VpcId", vpcId)
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(200);
}
}
@Test
void createVpcRestoresDeletedRegionalDefaultDhcpOptions() {
String isolatedAuth =
"AWS4-HMAC-SHA256 Credential=000000000099/20260908/us-east-1/ec2/aws4_request";
String defaultVpcId = given()
.formParam("Action", "DescribeVpcs")
.formParam("Filter.1.Name", "is-default")
.formParam("Filter.1.Value.1", "true")
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200).extract()
.xmlPath().getString("DescribeVpcsResponse.vpcSet.item.vpcId");
String firstVpcId = given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", "10.255.0.0/16")
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200).extract()
.xmlPath().getString("CreateVpcResponse.vpc.vpcId");
String secondVpcId = null;
try {
for (String vpcId : List.of(defaultVpcId, firstVpcId)) {
given()
.formParam("Action", "AssociateDhcpOptions")
.formParam("DhcpOptionsId", "default")
.formParam("VpcId", vpcId)
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200);
}
given()
.formParam("Action", "DeleteDhcpOptions")
.formParam("DhcpOptionsId", "dopt-default")
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200);
secondVpcId = given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", "10.252.0.0/16")
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200)
.body("CreateVpcResponse.vpc.dhcpOptionsId", equalTo("dopt-default"))
.extract().xmlPath().getString("CreateVpcResponse.vpc.vpcId");
given()
.formParam("Action", "DescribeDhcpOptions")
.formParam("DhcpOptionsId.1", "dopt-default")
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200)
.body("DescribeDhcpOptionsResponse.dhcpOptionsSet.item.dhcpOptionsId", equalTo("dopt-default"));
} finally {
if (secondVpcId != null) {
given()
.formParam("Action", "DeleteVpc")
.formParam("VpcId", secondVpcId)
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200);
}
given()
.formParam("Action", "DeleteVpc")
.formParam("VpcId", firstVpcId)
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200);
given()
.formParam("Action", "AssociateDhcpOptions")
.formParam("DhcpOptionsId", "dopt-default")
.formParam("VpcId", defaultVpcId)
.header("Authorization", isolatedAuth)
.when().post("/").then().statusCode(200);
}
}
@Test
void numberedConfigurationsDoNotSilentlyDiscardLaterEntries() {
String id = given()
@@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.model.IpPermission;
@@ -82,6 +82,46 @@ class Ec2ServicePersistenceTest {
assertEquals("persistent.example", restored.getConfigurations().getFirst().getValues().getFirst());
}
@Test
void startupRestoresDefaultDhcpOptionsReferencedByLegacyVpc(@TempDir Path dir) throws IOException {
Files.writeString(dir.resolve("ec2-vpcs.json"), """
{
"us-east-1::vpc-legacy": {
"vpcId": "vpc-legacy",
"region": "us-east-1",
"ownerId": "000000000000",
"dhcpOptionsId": "dopt-default"
}
}
""");
Ec2Service restarted = newService(dir);
DhcpOptions restored = restarted.describeDhcpOptions(REGION, List.of("dopt-default"), List.of(), 0, null)
.items().getFirst();
assertEquals("dopt-default", restored.getDhcpOptionsId());
}
@Test
void createVpcRestoresDeletedDefaultDhcpOptionsAfterRestart(@TempDir Path dir) {
Ec2Service original = newService(dir);
original.ensureDefaultResources(REGION);
String defaultVpcId = original.describeVpcs(REGION, List.of(), List.of()).stream()
.filter(Vpc::isDefault).findFirst().orElseThrow().getVpcId();
original.associateDhcpOptions(REGION, "default", defaultVpcId);
original.deleteDhcpOptions(REGION, "dopt-default");
Ec2Service restarted = newService(dir);
restarted.ensureDefaultResources(REGION);
assertTrue(restarted.describeDhcpOptions(REGION, List.of(), List.of(), 0, null)
.items().isEmpty());
assertEquals("dopt-default", restarted.createVpc(REGION, "10.78.0.0/16", false).getDhcpOptionsId());
DhcpOptions restored = restarted.describeDhcpOptions(REGION, List.of("dopt-default"), List.of(), 0, null)
.items().getFirst();
assertEquals("dopt-default", restored.getDhcpOptionsId());
assertEquals("default", restarted.requireVpc(REGION, defaultVpcId).getDhcpOptionsId());
}
@Test
void elasticIpMetadataSurvivesRestart(@TempDir Path dir) {
String region = "us-west-2";