fix(ec2): honor dry run for replace route (#4300)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-24 08:31:11 -04:00
1 parent 876ddc7422
commit 55f9ff5a8b
3 files changed
+62 -1

No files matched your search

@@ -3651,7 +3651,8 @@ public class Ec2QueryHandler {
}
gwId = LOCAL_GATEWAY_ID;
}
service.replaceRoute(region, rtId, dest, destIpv6, destPrefixList, gwId, natGwId, pcxId);
service.replaceRoute(region, rtId, dest, destIpv6, destPrefixList, gwId, natGwId, pcxId,
Boolean.parseBoolean(p.getFirst("DryRun")));
return booleanResponse("ReplaceRoute");
}
@@ -7196,6 +7196,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
public void replaceRoute(String region, String routeTableId, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId,
String gatewayId, String natGatewayId, String vpcPeeringConnectionId) {
replaceRoute(region, routeTableId, destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId, gatewayId, natGatewayId, vpcPeeringConnectionId, false);
}
public void replaceRoute(String region, String routeTableId, String destinationCidrBlock,
String destinationIpv6CidrBlock, String destinationPrefixListId,
String gatewayId, String natGatewayId, String vpcPeeringConnectionId,
boolean dryRun) {
requireExactlyOneDestination("ReplaceRoute", destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId);
// AWS takes exactly one target. Rejecting both-or-neither also keeps the targets this
@@ -7225,6 +7233,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
destinationPrefixListId)
+ " does not exist", 400));
if (dryRun) {
throw new AwsException("DryRunOperation",
"Request would have succeeded, but DryRun flag is set.", 412);
}
// The target the request does not name is cleared rather than carried over from the
// route being replaced.
Route replacement = new Route(canonicalDestinationCidrBlock, hasGateway ? gatewayId : null, existing.getOrigin());
@@ -337,4 +337,51 @@ class Ec2ReplaceRouteIntegrationTest {
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidRouteTableID.NotFound"));
}
@Test
@Order(12)
void dryRunValidReplacementReportsSuccessWithoutChangingTheRoute() {
given()
.formParam("Action", "ReplaceRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationCidrBlock", DEFAULT_ROUTE)
.formParam("NatGatewayId", NAT_GATEWAY)
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(412)
.body("Response.Errors.Error.Code", equalTo("DryRunOperation"));
String table = given()
.formParam("Action", "DescribeRouteTables")
.formParam("RouteTableId.1", routeTableId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body(DEFAULT_ROUTE_NODE + ".gatewayId", equalTo(INTERNET_GATEWAY))
.extract().asString();
assertThat(table, not(containsString(NAT_GATEWAY)));
}
@Test
@Order(13)
void dryRunStillRejectsAnUnknownDestination() {
given()
.formParam("Action", "ReplaceRoute")
.formParam("RouteTableId", routeTableId)
.formParam("DestinationCidrBlock", UNROUTED_CIDR)
.formParam("NatGatewayId", NAT_GATEWAY)
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidRoute.NotFound"));
}
}