fix(ec2): report an unknown subnet id from DescribeSubnets as an error (#4408)

* fix(ec2): report an unknown subnet id from DescribeSubnets as an error

A requested SubnetId that does not exist was dropped from the result instead of
raising. An empty list tells the caller the subnet is gone when the id may
simply be wrong, and a waiter polling for a subnet it just created cannot tell
not-yet from never.

describeVpcs already reads its own requested ids this way, so this is the same
shape applied to subnets. A describe naming no ids still lists the region.

* fix(ec2): raise the unknown subnet id from the query handler, not the service

describeSubnets is shared with ELB, ELBv2, DMS, ElastiCache, RDS and others,
each of which reads an absent id as its own error. ElbV2Service raises
SubnetNotFound and DmsService raises InvalidSubnet, and both are the codes
those APIs owe their callers. Raising InvalidSubnetID.NotFound in the service
replaced them. Move the per-id check to handleDescribeSubnets so only the EC2
query surface changes, and reuse the existing requireSubnet helper.

Assertions now read the XML elements rather than the response text.

* test(ec2): validate the unknown subnet id with SDK clients

AGENTS.md asks for management APIs to be exercised through an SDK client
rather than handcrafted requests alone. Ec2Client.describeSubnets raises
InvalidSubnetID.NotFound for a missing id, on its own and mixed with a good
one, and resolves the known id.

The ELBv2 case pins the regression the query handler placement avoids. With
the check in describeSubnets, CreateLoadBalancer answers a missing subnet
with InvalidSubnetID.NotFound rather than the SubnetNotFound that API owes
its callers, and this test fails.

* test(ec2): delete the VPC even when subnet creation fails

Creating the subnet outside the try meant a failure there left the VPC
behind, which accumulates against persistent storage.
This commit is contained in:
lex00 authored and GitHub committed 2026-09-25 13:53:02 -04:00
1 parent b0ad799c5c
commit 49f253d0ec
3 files changed
+168

No files matched your search

@@ -0,0 +1,89 @@
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.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
/**
* DescribeSubnets reports a subnet id that does not exist as an error.
*
* <p>It used to return an empty list, which tells a caller the subnet is gone when the id may
* simply be wrong, and leaves a waiter polling for a subnet it just created unable to tell "not
* yet" from "never". DescribeVpcs already reads its own ids this way.
*
* <p>Assertions read the XML elements rather than the response text, so a body carrying the right
* words in the wrong shape fails.
*/
@QuarkusTest
class Ec2DescribeSubnetsNotFoundIntegrationTest {
private static final String AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260908/us-east-1/ec2/aws4_request";
private static ValidatableResponse ec2(String action, String... formParams) {
RequestSpecification request = given().header("Authorization", AUTH)
.formParam("Action", action);
for (int i = 0; i < formParams.length; i += 2) {
request = request.formParam(formParams[i], formParams[i + 1]);
}
return request.when().post("/").then();
}
@Test
void anUnknownSubnetIdIsRefused() {
ec2("DescribeSubnets", "SubnetId.1", "subnet-0000000000000dead")
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidSubnetID.NotFound"))
.body("Response.Errors.Error.Message",
equalTo("The subnet ID 'subnet-0000000000000dead' does not exist"));
}
@Test
void aKnownSubnetIdStillReturns() {
String vpcId = ec2("CreateVpc", "CidrBlock", "10.72.0.0/16")
.statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId");
String subnetId = ec2("CreateSubnet", "VpcId", vpcId, "CidrBlock", "10.72.1.0/24")
.statusCode(200).extract().path("CreateSubnetResponse.subnet.subnetId");
ec2("DescribeSubnets", "SubnetId.1", subnetId)
.statusCode(200)
.body("DescribeSubnetsResponse.subnetSet.item.subnetId", equalTo(subnetId))
.body("DescribeSubnetsResponse.subnetSet.item.vpcId", equalTo(vpcId));
}
/** One bad id among good ones still fails, the way a per-id lookup has to. */
@Test
void oneUnknownIdAmongKnownOnesIsRefused() {
String vpcId = ec2("CreateVpc", "CidrBlock", "10.73.0.0/16")
.statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId");
String subnetId = ec2("CreateSubnet", "VpcId", vpcId, "CidrBlock", "10.73.1.0/24")
.statusCode(200).extract().path("CreateSubnetResponse.subnet.subnetId");
ec2("DescribeSubnets", "SubnetId.1", subnetId, "SubnetId.2", "subnet-0000000000000beef")
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidSubnetID.NotFound"))
.body("Response.Errors.Error.Message",
equalTo("The subnet ID 'subnet-0000000000000beef' does not exist"));
}
/** A describe with no ids lists the region and raises nothing. */
@Test
void describingWithNoIdsStillLists() {
String vpcId = ec2("CreateVpc", "CidrBlock", "10.74.0.0/16")
.statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId");
String subnetId = ec2("CreateSubnet", "VpcId", vpcId, "CidrBlock", "10.74.1.0/24")
.statusCode(200).extract().path("CreateSubnetResponse.subnet.subnetId");
ec2("DescribeSubnets")
.statusCode(200)
.body("DescribeSubnetsResponse.subnetSet.item.subnetId", hasItem(subnetId))
.body("DescribeSubnetsResponse.subnetSet.item.subnetId",
not(hasItem("subnet-0000000000000dead")));
}
}