feat(ec2): support transit gateway route tables, associations and routes (#2348)

* feat(ec2): support transit gateway route tables, associations and routes

CreateTransitGatewayRouteTable, DescribeTransitGatewayRouteTables and
DeleteTransitGatewayRouteTable; Associate, Disassociate and
GetTransitGatewayRouteTableAssociations; Enable, Disable and
GetTransitGatewayRouteTablePropagations; CreateTransitGatewayRoute,
DeleteTransitGatewayRoute and SearchTransitGatewayRoutes.

Part three of #2308, on top of the gateway in #2329 and the attachments
in #2347. Backs aws_ec2_transit_gateway_route_table and the association,
propagation and route resources beside it.

Behaviour taken from a live account rather than the reference:

- a table asked for by name is neither default; only the one a gateway
  mints for itself is
- an attachment associates with exactly one table, so a second attempt is
  Resource.AlreadyAssociated rather than a move
- association reports the transitional associating and disassociating,
  while propagation reports the settled enabled and disabled at once
- enabling propagation twice is
  TransitGatewayRouteTablePropagation.Duplicate
- a blackhole is a static route in the blackhole state rather than a type
  of its own, and carries no attachment
- deleting a table is refused while it is a gateway's default association
  table, and again while attachments are associated, both IncorrectState
  with their own messages
- an id that does not exist is InvalidRouteTableID.NotFound while one of
  the wrong shape is InvalidRouteTableId.Malformed, casing included

Associations live on the attachment record rather than in a store of
their own, since an attachment has at most one; propagations are their
own records, since an attachment may propagate into several tables.

Propagated routes are derived when routes are searched, from each enabled
propagation joined to the attached VPC's CIDR blocks, rather than
written down. Storing them would go stale the moment a VPC's CIDR
associations changed, and would need invalidating from every VPC and
attachment path.

Deleting an attachment removes its propagations and leaves a static route
that named it as a blackhole, which is what the live API does: the
destination stays configured with nowhere to go. Deleting a gateway takes
its tables' propagations, routes and tags with them, so nothing outlives
the table it belongs to. Every mutation runs under the region-scoped
topology lock introduced in #2347.

* fix(ec2): keep route table operations within one gateway and one lock

Three gaps found sweeping the new surface before review rather than after
it, each checked against a live account.

A route table reached attachments of any gateway. Associating,
propagating or routing to another gateway's attachment was accepted and
recorded, leaving a table pointing at something outside its own gateway.
AWS refuses all three as though the attachment did not exist, which is
the same shape a subnet from another VPC gets, and all five call sites
now do the same.

modifyTransitGateway writes route tables through its default-table
markers but held only the gateway's striped lock, so a modify racing a
route table delete could put the deleted table back. It and
createTransitGateway now take the topology lock as well, which makes the
rule exceptionless: every write to a gateway, route table, attachment,
propagation or route happens under that one lock, acquired before any
striped one.

Deleting an attachment left its propagations and any static route naming
it behind. On AWS the propagations go and the route survives as a
blackhole - the destination stays configured with nowhere to go - and
that is now what happens here. Deleting a gateway takes its tables'
propagations, routes and tags with it, so nothing outlives the table it
belongs to.

* feat(ec2): support ReplaceTransitGatewayRoute

Completes the route surface beside create, delete and search. Backs a
provider moving a route between an attachment and a blackhole without
recreating it.

Verified on a live account: replacing a destination the table has never
held writes it rather than reporting it missing, so this is an upsert
rather than an update. The target moves as a unit - a route turned into
a blackhole keeps no attachment, and one pointed back at an attachment
regains its id, resource and resource type together.

It carries the same rules as the rest of the route table surface: the
attachment has to belong to the table's own gateway, the write happens
under the topology lock, and a route it wrote is blackholed like any
other when its attachment is deleted.

* fix(ec2): apply the route search filters instead of ignoring them

SearchTransitGatewayRoutes handled four filter names and let every other
one through a default arm that matched everything, so a search by
route-search.supernet-of-match, subnet-of-match or longest-prefix-match
returned the whole table. A plausible wrong answer rather than an error.

The three take different value forms, which the reference does not spell
out. Observed on a live account, against a table holding 10.0.0.0/8,
10.1.0.0/16, 10.1.1.0/24 and 192.168.0.0/16:

  supernet-of-match 10.1.1.0/24  -> /8, /16 and /24, the exact match included
  subnet-of-match   10.1.0.0/16  -> /16 and /24, the exact match included
  longest-prefix-match 10.1.1.5  -> /24
  longest-prefix-match 10.1.9.9  -> /16
  longest-prefix-match 8.8.8.8   -> nothing

So the containment filters take a CIDR and match inclusively in either
direction, while longest-prefix-match takes a bare address and returns
the one most specific route covering it. Handing either the other's form
returns nothing there, and now here.

An unrecognised filter name is rejected rather than ignored, matching
InvalidParameterValue: Value (nonsense) for parameter Filter is invalid.
Silently accepting one answers a question that was never asked.
This commit is contained in:
Michal Schreiber authored and GitHub committed 2026-08-18 15:59:58 -04:00
1 parent 151e3fcc7a
commit bc0f88c9b1
8 files changed
+1773 -5

No files matched your search

@@ -29,7 +29,9 @@ import io.github.hectorvent.floci.services.ec2.model.Snapshot;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.ec2.model.TransitGateway;
import io.github.hectorvent.floci.services.ec2.model.TransitGatewayOptions;
import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRoute;
import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRouteTable;
import io.github.hectorvent.floci.services.ec2.model.TransitGatewayRouteTablePropagation;
import io.github.hectorvent.floci.services.ec2.model.TransitGatewayVpcAttachment;
import io.github.hectorvent.floci.services.ec2.model.TransitGatewayVpcAttachmentOptions;
import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair;
@@ -2195,6 +2197,404 @@ class Ec2ServiceTest {
"describe serves tags added after creation");
}
// =========================================================================
// Transit gateway route tables, associations, propagations and routes
// =========================================================================
/** A gateway, an attachment, and a second route table to move things onto. */
private static String[] routeTableFixture(Ec2Service service) {
String[] fixture = attachmentFixture(service);
String attachmentId = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0],
fixture[1], List.of(fixture[2]), null, List.of()).getTransitGatewayAttachmentId();
String routeTableId = service.createTransitGatewayRouteTable("us-east-1", fixture[0], List.of())
.getTransitGatewayRouteTableId();
return new String[] {fixture[0], fixture[1], fixture[2], attachmentId, routeTableId};
}
@Test
void aRequestedRouteTableIsNeitherDefault() {
Ec2Service service = prefixListService();
String[] fixture = attachmentFixture(service);
TransitGatewayRouteTable routeTable = service.createTransitGatewayRouteTable("us-east-1",
fixture[0], List.of(new Tag("Name", "spoke")));
assertTrue(routeTable.getTransitGatewayRouteTableId().startsWith("tgw-rtb-"));
assertEquals("available", routeTable.getState());
assertFalse(routeTable.isDefaultAssociationRouteTable());
assertFalse(routeTable.isDefaultPropagationRouteTable());
assertEquals(2, service.describeTransitGatewayRouteTables("us-east-1", List.of(), Map.of()).size(),
"the gateway's own default table is there too");
}
/** Verified live: an attachment associates with exactly one route table. */
@Test
void anAttachmentAssociatesWithOneRouteTableAtATime() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
AwsException already = assertThrows(AwsException.class, () -> service
.associateTransitGatewayRouteTable("us-east-1", fixture[4], fixture[3]));
assertEquals("Resource.AlreadyAssociated", already.getErrorCode());
TransitGateway gateway = service.describeTransitGateways("us-east-1", List.of(fixture[0]), Map.of())
.getFirst();
String defaultTable = gateway.getOptions().getAssociationDefaultRouteTableId();
assertEquals(1, service.associationsOf("us-east-1", defaultTable).size(),
"the attachment starts on the gateway's default table");
service.disassociateTransitGatewayRouteTable("us-east-1", defaultTable, fixture[3]);
assertTrue(service.associationsOf("us-east-1", defaultTable).isEmpty());
service.associateTransitGatewayRouteTable("us-east-1", fixture[4], fixture[3]);
assertEquals(fixture[3], service.associationsOf("us-east-1", fixture[4]).getFirst()
.getTransitGatewayAttachmentId());
}
/** Verified live: propagation reports the settled state at once, and refuses a duplicate. */
@Test
void propagationIsEnabledAtOnceAndOnlyOnce() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
TransitGatewayRouteTablePropagation propagation = service
.enableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]);
assertEquals("enabled", propagation.getState());
assertEquals(fixture[1], propagation.getResourceId());
assertEquals("vpc", propagation.getResourceType());
assertEquals("TransitGatewayRouteTablePropagation.Duplicate", assertThrows(AwsException.class,
() -> service.enableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]))
.getErrorCode());
assertEquals("disabled", service.disableTransitGatewayRouteTablePropagation(
"us-east-1", fixture[4], fixture[3]).getState());
assertTrue(service.propagationsOf("us-east-1", fixture[4]).isEmpty());
}
/**
* Verified live: enabling propagation makes the attached VPC's CIDR show up as a propagated
* route. It is derived from the VPC rather than stored, so a CIDR change cannot leave a stale
* route behind.
*/
@Test
void propagationProducesRoutesForTheAttachedVpcsCidr() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
service.enableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]);
List<TransitGatewayRoute> propagated = service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("type", List.of("propagated")));
assertEquals(1, propagated.size());
assertEquals("10.90.0.0/16", propagated.getFirst().getDestinationCidrBlock());
assertEquals("active", propagated.getFirst().getState());
assertEquals(fixture[3], propagated.getFirst().getTransitGatewayAttachmentId());
service.disableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]);
assertTrue(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("type", List.of("propagated"))).isEmpty(), "the route goes with the propagation");
}
/** A blackhole is a state of a static route, not a type, and carries no attachment. */
@Test
void staticAndBlackholeRoutesDifferByStateNotType() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
TransitGatewayRoute viaAttachment = service.createTransitGatewayRoute("us-east-1", fixture[4],
"10.60.0.0/16", fixture[3], false);
assertEquals("static", viaAttachment.getType());
assertEquals("active", viaAttachment.getState());
assertEquals(fixture[3], viaAttachment.getTransitGatewayAttachmentId());
TransitGatewayRoute blackhole = service.createTransitGatewayRoute("us-east-1", fixture[4],
"10.61.0.0/16", null, true);
assertEquals("static", blackhole.getType());
assertEquals("blackhole", blackhole.getState());
assertNull(blackhole.getTransitGatewayAttachmentId());
assertEquals("RouteAlreadyExists", assertThrows(AwsException.class,
() -> service.createTransitGatewayRoute("us-east-1", fixture[4], "10.60.0.0/16",
fixture[3], false)).getErrorCode());
assertEquals(2, service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("type", List.of("static"))).size(), "a blackhole is still a static route");
TransitGatewayRoute deleted = service.deleteTransitGatewayRoute("us-east-1", fixture[4], "10.61.0.0/16");
assertEquals("deleted", deleted.getState());
assertNull(deleted.getTransitGatewayAttachmentId());
assertEquals(1, service.searchTransitGatewayRoutes("us-east-1", fixture[4], Map.of()).size());
}
/**
* Verified live: a route table will not go while it is the gateway's default association table
* nor while attachments are associated with it, both under IncorrectState.
*/
@Test
void aRouteTableInUseCannotBeDeleted() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
TransitGateway gateway = service.describeTransitGateways("us-east-1", List.of(fixture[0]), Map.of())
.getFirst();
String defaultTable = gateway.getOptions().getAssociationDefaultRouteTableId();
AwsException isDefault = assertThrows(AwsException.class,
() -> service.deleteTransitGatewayRouteTable("us-east-1", defaultTable));
assertEquals("IncorrectState", isDefault.getErrorCode());
assertTrue(isDefault.getMessage().contains("default association route table"), isDefault.getMessage());
service.disassociateTransitGatewayRouteTable("us-east-1", defaultTable, fixture[3]);
service.associateTransitGatewayRouteTable("us-east-1", fixture[4], fixture[3]);
AwsException associated = assertThrows(AwsException.class,
() -> service.deleteTransitGatewayRouteTable("us-east-1", fixture[4]));
assertEquals("IncorrectState", associated.getErrorCode());
assertTrue(associated.getMessage().contains("has associated attachments"), associated.getMessage());
service.disassociateTransitGatewayRouteTable("us-east-1", fixture[4], fixture[3]);
service.createTransitGatewayRoute("us-east-1", fixture[4], "10.70.0.0/16", null, true);
assertEquals("deleted", service.deleteTransitGatewayRouteTable("us-east-1", fixture[4]).getState());
assertEquals("InvalidRouteTableID.NotFound", assertThrows(AwsException.class,
() -> service.searchTransitGatewayRoutes("us-east-1", fixture[4], Map.of())).getErrorCode(),
"its routes went with it");
}
/** Verified live, casing included: NotFound spells it ID, Malformed spells it Id. */
@Test
void routeTableIdErrorsMatchTheLiveApiIncludingCasing() {
Ec2Service service = prefixListService();
assertEquals("InvalidRouteTableID.NotFound", assertThrows(AwsException.class,
() -> service.describeTransitGatewayRouteTables("us-east-1",
List.of("tgw-rtb-0123456789abcdef0"), Map.of())).getErrorCode());
assertEquals("InvalidRouteTableId.Malformed", assertThrows(AwsException.class,
() -> service.describeTransitGatewayRouteTables("us-east-1",
List.of("tgw-rtb-nope"), Map.of())).getErrorCode());
}
/**
* Verified on a live account: deleting an attachment removes its propagations, and a static
* route that named it survives as a blackhole rather than disappearing — the destination is
* still configured, it just has nowhere to go.
*/
@Test
void deletingAnAttachmentClearsPropagationsAndBlackholesItsRoutes() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
service.enableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]);
service.createTransitGatewayRoute("us-east-1", fixture[4], "10.99.0.0/16", fixture[3], false);
service.deleteTransitGatewayVpcAttachment("us-east-1", fixture[3]);
assertTrue(service.propagationsOf("us-east-1", fixture[4]).isEmpty(), "propagations go");
List<TransitGatewayRoute> routes = service.searchTransitGatewayRoutes("us-east-1", fixture[4], Map.of());
assertEquals(1, routes.size(), "the static route stays");
assertEquals("blackhole", routes.getFirst().getState());
assertNull(routes.getFirst().getTransitGatewayAttachmentId());
assertTrue(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("type", List.of("propagated"))).isEmpty(), "and its propagated route with it");
}
/** A gateway's route tables take their propagations, routes and tags with them. */
@Test
void deletingAGatewayLeavesNothingBehindItsRouteTables() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
service.enableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]);
service.createTransitGatewayRoute("us-east-1", fixture[4], "10.99.0.0/16", null, true);
service.createTags("us-east-1", List.of(fixture[4]), List.of(new Tag("env", "prod")));
service.deleteTransitGatewayVpcAttachment("us-east-1", fixture[3]);
service.deleteTransitGateway("us-east-1", fixture[0]);
assertTrue(service.describeTransitGatewayRouteTables("us-east-1", List.of(), Map.of()).isEmpty(),
"the tables go with the gateway");
assertTrue(service.propagationsOf("us-east-1", fixture[4]).isEmpty());
assertTrue(service.describeTags("us-east-1", Map.of("resource-id", List.of(fixture[4]))).isEmpty(),
"and their tags");
}
/**
* Modifying a gateway writes route tables through its default-table markers, so it races the
* deletes the same way tagging does. Whoever wins, a deleted table must not come back.
*/
@Test
void modifyingAGatewayCannotResurrectADeletedRouteTable() throws Exception {
for (int trial = 0; trial < 10; trial++) {
Ec2Service service = prefixListService();
String[] fixture = attachmentFixture(service);
String spare = service.createTransitGatewayRouteTable("us-east-1", fixture[0], List.of())
.getTransitGatewayRouteTableId();
java.util.concurrent.CountDownLatch start = new java.util.concurrent.CountDownLatch(1);
java.util.concurrent.ExecutorService pool =
java.util.concurrent.Executors.newFixedThreadPool(2);
pool.submit(() -> {
try {
start.await();
TransitGatewayOptions changes = new TransitGatewayOptions();
changes.setDefaultRouteTableAssociation("enable");
changes.setAssociationDefaultRouteTableId(spare);
service.modifyTransitGateway("us-east-1", fixture[0], null, changes, List.of(), List.of());
} catch (AwsException | InterruptedException ignored) {
// Losing the race is fine.
}
});
pool.submit(() -> {
try {
start.await();
service.deleteTransitGatewayRouteTable("us-east-1", spare);
} catch (AwsException | InterruptedException ignored) {
// Same.
}
});
start.countDown();
pool.shutdown();
assertTrue(pool.awaitTermination(30, java.util.concurrent.TimeUnit.SECONDS));
boolean tableExists = service.describeTransitGatewayRouteTables("us-east-1", List.of(), Map.of())
.stream().anyMatch(rt -> rt.getTransitGatewayRouteTableId().equals(spare));
TransitGateway gateway = service.describeTransitGateways("us-east-1", List.of(fixture[0]), Map.of())
.getFirst();
if (!tableExists) {
assertNotEquals(spare, gateway.getOptions().getAssociationDefaultRouteTableId(),
"trial " + trial + ": the gateway names a route table that is gone");
}
}
}
/**
* Verified on a live account: a route table only reaches attachments of its own gateway.
* Associating, propagating or routing to another gateway's attachment is refused as though the
* attachment did not exist, which is the same shape as a subnet belonging to another VPC.
*/
@Test
void aRouteTableOnlyReachesItsOwnGatewaysAttachments() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
String otherGateway = service.createTransitGateway("us-east-1", "other", null, List.of())
.getTransitGatewayId();
String foreignTable = service.createTransitGatewayRouteTable("us-east-1", otherGateway, List.of())
.getTransitGatewayRouteTableId();
for (Runnable crossGateway : List.<Runnable>of(
() -> service.associateTransitGatewayRouteTable("us-east-1", foreignTable, fixture[3]),
() -> service.disassociateTransitGatewayRouteTable("us-east-1", foreignTable, fixture[3]),
() -> service.enableTransitGatewayRouteTablePropagation("us-east-1", foreignTable, fixture[3]),
() -> service.disableTransitGatewayRouteTablePropagation("us-east-1", foreignTable, fixture[3]),
() -> service.createTransitGatewayRoute("us-east-1", foreignTable, "10.77.0.0/16",
fixture[3], false))) {
assertEquals("InvalidTransitGatewayAttachmentID.NotFound",
assertThrows(AwsException.class, crossGateway::run).getErrorCode());
}
// Nothing was recorded against the other gateway's table on the way through.
assertTrue(service.associationsOf("us-east-1", foreignTable).isEmpty());
assertTrue(service.propagationsOf("us-east-1", foreignTable).isEmpty());
assertTrue(service.searchTransitGatewayRoutes("us-east-1", foreignTable, Map.of()).isEmpty());
}
/**
* Verified on a live account: replacing a route flips its target in place, and replacing a
* destination the table has never held writes it rather than reporting it missing — an upsert
* rather than an update.
*/
@Test
void replacingARouteMovesItsTargetAndUpsertsWhenAbsent() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
service.createTransitGatewayRoute("us-east-1", fixture[4], "10.88.0.0/16", fixture[3], false);
TransitGatewayRoute blackholed = service.replaceTransitGatewayRoute("us-east-1", fixture[4],
"10.88.0.0/16", null, true);
assertEquals("blackhole", blackholed.getState());
assertNull(blackholed.getTransitGatewayAttachmentId(), "the target moves as one");
assertNull(blackholed.getResourceId());
TransitGatewayRoute restored = service.replaceTransitGatewayRoute("us-east-1", fixture[4],
"10.88.0.0/16", fixture[3], false);
assertEquals("active", restored.getState());
assertEquals(fixture[3], restored.getTransitGatewayAttachmentId());
assertEquals(fixture[1], restored.getResourceId());
TransitGatewayRoute created = service.replaceTransitGatewayRoute("us-east-1", fixture[4],
"10.89.0.0/16", null, true);
assertEquals("blackhole", created.getState());
assertEquals(2, service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("type", List.of("static"))).size(), "replacing an absent route writes it");
// The same ownership rule as the other route table operations.
String otherGateway = service.createTransitGateway("us-east-1", "other", null, List.of())
.getTransitGatewayId();
String foreignTable = service.createTransitGatewayRouteTable("us-east-1", otherGateway, List.of())
.getTransitGatewayRouteTableId();
assertEquals("InvalidTransitGatewayAttachmentID.NotFound", assertThrows(AwsException.class,
() -> service.replaceTransitGatewayRoute("us-east-1", foreignTable, "10.90.0.0/16",
fixture[3], false)).getErrorCode());
// A replaced route is still a route, so losing its attachment blackholes it.
service.deleteTransitGatewayVpcAttachment("us-east-1", fixture[3]);
TransitGatewayRoute afterDetach = service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("route-search.exact-match", List.of("10.88.0.0/16"))).getFirst();
assertEquals("blackhole", afterDetach.getState());
assertNull(afterDetach.getTransitGatewayAttachmentId());
}
/**
* The route search filters, as observed on a live account. The three CIDR relationship filters
* take different value forms: supernet-of-match and subnet-of-match take a CIDR and match
* inclusively, longest-prefix-match takes a bare address and returns the one most specific
* route covering it. Giving either the other's form returns nothing there, and here.
*/
@Test
void routeSearchFiltersMatchTheLiveApi() {
Ec2Service service = prefixListService();
String[] fixture = routeTableFixture(service);
for (String cidr : List.of("10.0.0.0/8", "10.1.0.0/16", "10.1.1.0/24", "192.168.0.0/16")) {
service.createTransitGatewayRoute("us-east-1", fixture[4], cidr, null, true);
}
assertEquals(List.of("10.0.0.0/8", "10.1.0.0/16", "10.1.1.0/24"),
cidrsOf(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("route-search.supernet-of-match", List.of("10.1.1.0/24")))),
"everything containing it, the exact match included");
assertEquals(List.of("10.1.0.0/16", "10.1.1.0/24"),
cidrsOf(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("route-search.subnet-of-match", List.of("10.1.0.0/16")))),
"everything it contains, the exact match included");
assertEquals(List.of("10.1.1.0/24"),
cidrsOf(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("route-search.longest-prefix-match", List.of("10.1.1.5")))),
"the most specific route covering the address");
assertEquals(List.of("10.1.0.0/16"),
cidrsOf(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("route-search.longest-prefix-match", List.of("10.1.9.9")))));
assertTrue(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("route-search.longest-prefix-match", List.of("8.8.8.8"))).isEmpty(),
"nothing covers it");
// Each filter ignores the other's value form, exactly as the live API does.
assertTrue(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("route-search.longest-prefix-match", List.of("10.1.1.5/32"))).isEmpty(),
"longest-prefix-match takes an address, not a CIDR");
assertTrue(service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("route-search.supernet-of-match", List.of("10.1.1.5"))).isEmpty(),
"supernet-of-match takes a CIDR, not an address");
AwsException unknown = assertThrows(AwsException.class,
() -> service.searchTransitGatewayRoutes("us-east-1", fixture[4],
Map.of("nonsense", List.of("x"))));
assertEquals("InvalidParameterValue", unknown.getErrorCode());
assertTrue(unknown.getMessage().contains("nonsense"), unknown.getMessage());
}
private static List<String> cidrsOf(List<TransitGatewayRoute> routes) {
return routes.stream().map(TransitGatewayRoute::getDestinationCidrBlock).sorted().toList();
}
private static EmulatorConfig mockConfig(boolean ec2Mock) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
@@ -0,0 +1,251 @@
package io.github.hectorvent.floci.services.ec2;
import io.quarkus.test.junit.QuarkusTest;
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 static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
/**
* Route tables, associations, propagations and routes over the EC2 Query protocol.
*
* <p>The shapes were captured from a live account, including two asymmetries that are easy to get
* wrong: the association and propagation listings drop the route table id that the mutating calls
* carry, and a blackhole route omits the attachment block entirely.
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class Ec2TransitGatewayRouteTableIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
private static String transitGatewayId;
private static String defaultRouteTableId;
private static String routeTableId;
private static String attachmentId;
private static String vpcId;
@Test
@Order(1)
void buildAGatewayAnAttachmentAndASecondRouteTable() {
String created = given()
.formParam("Action", "CreateTransitGateway")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().asString();
transitGatewayId = extract(created, "transitGatewayId");
defaultRouteTableId = extract(created, "associationDefaultRouteTableId");
vpcId = given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", "10.40.0.0/16")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId");
String subnetId = given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", vpcId)
.formParam("CidrBlock", "10.40.1.0/24")
.formParam("AvailabilityZone", "us-east-1a")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200).extract().path("CreateSubnetResponse.subnet.subnetId");
attachmentId = given()
.formParam("Action", "CreateTransitGatewayVpcAttachment")
.formParam("TransitGatewayId", transitGatewayId)
.formParam("VpcId", vpcId)
.formParam("SubnetIds.1", subnetId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.extract().path("CreateTransitGatewayVpcAttachmentResponse.transitGatewayVpcAttachment"
+ ".transitGatewayAttachmentId");
routeTableId = given()
.formParam("Action", "CreateTransitGatewayRouteTable")
.formParam("TransitGatewayId", transitGatewayId)
.formParam("TagSpecification.1.ResourceType", "transit-gateway-route-table")
.formParam("TagSpecification.1.Tag.1.Key", "Name")
.formParam("TagSpecification.1.Tag.1.Value", "spoke")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("CreateTransitGatewayRouteTableResponse.transitGatewayRouteTable.state", equalTo("available"))
.body("CreateTransitGatewayRouteTableResponse.transitGatewayRouteTable"
+ ".defaultAssociationRouteTable", equalTo("false"))
.body("CreateTransitGatewayRouteTableResponse.transitGatewayRouteTable.tagSet.item.value",
equalTo("spoke"))
.extract().path("CreateTransitGatewayRouteTableResponse.transitGatewayRouteTable"
+ ".transitGatewayRouteTableId");
}
private String extract(String body, String element) {
int start = body.indexOf("<" + element + ">") + element.length() + 2;
return body.substring(start, body.indexOf("</" + element + ">", start));
}
@Test
@Order(2)
void associationsMoveBetweenTablesAndTheListingDropsTheTableId() {
given()
.formParam("Action", "AssociateTransitGatewayRouteTable")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("TransitGatewayAttachmentId", attachmentId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("Resource.AlreadyAssociated"));
given()
.formParam("Action", "DisassociateTransitGatewayRouteTable")
.formParam("TransitGatewayRouteTableId", defaultRouteTableId)
.formParam("TransitGatewayAttachmentId", attachmentId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("DisassociateTransitGatewayRouteTableResponse.association.state", equalTo("disassociating"))
.body("DisassociateTransitGatewayRouteTableResponse.association.transitGatewayRouteTableId",
equalTo(defaultRouteTableId));
given()
.formParam("Action", "AssociateTransitGatewayRouteTable")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("TransitGatewayAttachmentId", attachmentId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("AssociateTransitGatewayRouteTableResponse.association.state", equalTo("associating"));
String listing = given()
.formParam("Action", "GetTransitGatewayRouteTableAssociations")
.formParam("TransitGatewayRouteTableId", routeTableId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("GetTransitGatewayRouteTableAssociationsResponse.associations.item.resourceType",
equalTo("vpc"))
.body("GetTransitGatewayRouteTableAssociationsResponse.associations.item.state",
equalTo("associated"))
.extract().asString();
// The caller named the table in the request, so the listing does not repeat it.
assertThat(listing, not(containsString("transitGatewayRouteTableId")));
}
@Test
@Order(3)
void propagationEnablesAtOnceAndShowsUpAsARoute() {
given()
.formParam("Action", "EnableTransitGatewayRouteTablePropagation")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("TransitGatewayAttachmentId", attachmentId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("EnableTransitGatewayRouteTablePropagationResponse.propagation.state", equalTo("enabled"))
.body("EnableTransitGatewayRouteTablePropagationResponse.propagation.transitGatewayRouteTableId",
equalTo(routeTableId));
String listing = given()
.formParam("Action", "GetTransitGatewayRouteTablePropagations")
.formParam("TransitGatewayRouteTableId", routeTableId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("GetTransitGatewayRouteTablePropagationsResponse.transitGatewayRouteTablePropagations"
+ ".item.state", equalTo("enabled"))
.extract().asString();
assertThat(listing, not(containsString("transitGatewayRouteTableId")));
// The attached VPC's CIDR appears as a propagated route.
given()
.formParam("Action", "SearchTransitGatewayRoutes")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("Filter.1.Name", "type")
.formParam("Filter.1.Value.1", "propagated")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("SearchTransitGatewayRoutesResponse.routeSet.item.destinationCidrBlock",
equalTo("10.40.0.0/16"))
.body("SearchTransitGatewayRoutesResponse.routeSet.item.type", equalTo("propagated"))
.body("SearchTransitGatewayRoutesResponse.routeSet.item.state", equalTo("active"));
}
@Test
@Order(4)
void aBlackholeRouteCarriesNoAttachment() {
given()
.formParam("Action", "CreateTransitGatewayRoute")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("DestinationCidrBlock", "10.60.0.0/16")
.formParam("TransitGatewayAttachmentId", attachmentId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("CreateTransitGatewayRouteResponse.route.type", equalTo("static"))
.body("CreateTransitGatewayRouteResponse.route.state", equalTo("active"))
.body("CreateTransitGatewayRouteResponse.route.transitGatewayAttachments.item"
+ ".transitGatewayAttachmentId", equalTo(attachmentId));
String blackhole = given()
.formParam("Action", "CreateTransitGatewayRoute")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("DestinationCidrBlock", "10.61.0.0/16")
.formParam("Blackhole", "true")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(200)
.body("CreateTransitGatewayRouteResponse.route.type", equalTo("static"))
.body("CreateTransitGatewayRouteResponse.route.state", equalTo("blackhole"))
.extract().asString();
assertThat(blackhole, not(containsString("transitGatewayAttachments")));
given()
.formParam("Action", "CreateTransitGatewayRoute")
.formParam("TransitGatewayRouteTableId", routeTableId)
.formParam("DestinationCidrBlock", "10.60.0.0/16")
.formParam("TransitGatewayAttachmentId", attachmentId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("RouteAlreadyExists"));
}
@Test
@Order(5)
void aTableInUseIsRefusedAndTheIdErrorsKeepTheirCasing() {
given()
.formParam("Action", "DeleteTransitGatewayRouteTable")
.formParam("TransitGatewayRouteTableId", routeTableId)
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("IncorrectState"));
given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("TransitGatewayRouteTableIds.1", "tgw-rtb-0123456789abcdef0")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidRouteTableID.NotFound"));
given()
.formParam("Action", "DescribeTransitGatewayRouteTables")
.formParam("TransitGatewayRouteTableIds.1", "tgw-rtb-nope")
.header("Authorization", AUTH_HEADER)
.when().post("/")
.then().statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidRouteTableId.Malformed"));
}
}