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,67 @@
package com.floci.test;
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.Ec2Exception;
import software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client;
import software.amazon.awssdk.services.elasticloadbalancingv2.model.ElasticLoadBalancingV2Exception;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class Ec2DescribeSubnetsNotFoundTest {
private static final String MISSING = "subnet-0000000000000dead";
@Test
@DisplayName("DescribeSubnets raises InvalidSubnetID.NotFound for an id that does not exist")
void missingSubnetIdIsAnSdkError() {
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
Ec2Exception error = assertThrows(Ec2Exception.class,
() -> ec2.describeSubnets(r -> r.subnetIds(MISSING)));
assertThat(error.statusCode()).isEqualTo(400);
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("InvalidSubnetID.NotFound");
String vpcId = ec2.createVpc(r -> r.cidrBlock("10.75.0.0/16")).vpc().vpcId();
String subnetId = null;
try {
subnetId = ec2.createSubnet(r -> r.vpcId(vpcId).cidrBlock("10.75.1.0/24"))
.subnet().subnetId();
String created = subnetId;
assertThat(ec2.describeSubnets(r -> r.subnetIds(created)).subnets())
.singleElement().satisfies(s -> assertThat(s.vpcId()).isEqualTo(vpcId));
Ec2Exception mixed = assertThrows(Ec2Exception.class,
() -> ec2.describeSubnets(r -> r.subnetIds(created, MISSING)));
assertThat(mixed.awsErrorDetails().errorCode()).isEqualTo("InvalidSubnetID.NotFound");
} finally {
if (subnetId != null) {
String cleanup = subnetId;
ec2.deleteSubnet(r -> r.subnetId(cleanup));
}
ec2.deleteVpc(r -> r.vpcId(vpcId));
}
}
}
/**
* describeSubnets is shared with the services that build on it, and each reports a missing
* subnet in its own words. ELBv2 owes its callers SubnetNotFound, so the EC2 error must not
* reach through.
*/
@Test
@DisplayName("ELBv2 still reports its own SubnetNotFound for a missing subnet")
void elbV2KeepsItsOwnSubnetError() {
try (ElasticLoadBalancingV2Client elb = TestFixtures.elbV2Client()) {
ElasticLoadBalancingV2Exception error = assertThrows(
ElasticLoadBalancingV2Exception.class,
() -> elb.createLoadBalancer(r -> r.name("lb-" + UUID.randomUUID().toString().substring(0, 8))
.type("application")
.subnets(MISSING)));
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("SubnetNotFound");
}
}
}