mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
* 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.
609 lines
27 KiB
Bash
609 lines
27 KiB
Bash
#!/usr/bin/env bats
|
|
# EC2 tests
|
|
|
|
setup() {
|
|
load 'test_helper/common-setup'
|
|
PREFIX_LIST_NAME="bats-prefix-list-$(unique_name)"
|
|
PREFIX_LIST_ID=""
|
|
SG_VPC_ID=""
|
|
SG_SOURCE_ID=""
|
|
SG_TARGET_ID=""
|
|
TRANSIT_GATEWAY_ID=""
|
|
TGW_ATTACHMENT_ID=""
|
|
TGW_VPC_ID=""
|
|
TGW_ROUTE_TABLE_ID=""
|
|
}
|
|
|
|
teardown() {
|
|
if [ -n "$PREFIX_LIST_ID" ]; then
|
|
aws_cmd ec2 delete-managed-prefix-list --prefix-list-id "$PREFIX_LIST_ID" >/dev/null 2>&1 || true
|
|
fi
|
|
if [ -n "$TGW_ROUTE_TABLE_ID" ]; then
|
|
aws_cmd ec2 delete-transit-gateway-route-table \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" >/dev/null 2>&1 || true
|
|
fi
|
|
if [ -n "$TGW_ATTACHMENT_ID" ]; then
|
|
aws_cmd ec2 delete-transit-gateway-vpc-attachment \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID" >/dev/null 2>&1 || true
|
|
fi
|
|
if [ -n "$TRANSIT_GATEWAY_ID" ]; then
|
|
aws_cmd ec2 delete-transit-gateway --transit-gateway-id "$TRANSIT_GATEWAY_ID" >/dev/null 2>&1 || true
|
|
fi
|
|
if [ -n "$TGW_VPC_ID" ]; then
|
|
aws_cmd ec2 delete-vpc --vpc-id "$TGW_VPC_ID" >/dev/null 2>&1 || true
|
|
fi
|
|
for sg in "$SG_TARGET_ID" "$SG_SOURCE_ID"; do
|
|
if [ -n "$sg" ]; then
|
|
aws_cmd ec2 delete-security-group --group-id "$sg" >/dev/null 2>&1 || true
|
|
fi
|
|
done
|
|
if [ -n "$SG_VPC_ID" ]; then
|
|
aws_cmd ec2 delete-vpc --vpc-id "$SG_VPC_ID" >/dev/null 2>&1 || true
|
|
fi
|
|
}
|
|
|
|
# Creates a prefix list holding 10.0.0.0/8 and sets PREFIX_LIST_ID.
|
|
create_prefix_list() {
|
|
local out
|
|
out=$(aws_cmd ec2 create-managed-prefix-list \
|
|
--prefix-list-name "$PREFIX_LIST_NAME" \
|
|
--address-family IPv4 \
|
|
--max-entries 5 \
|
|
--entries 'Cidr=10.0.0.0/8,Description=corporate')
|
|
PREFIX_LIST_ID=$(json_get "$out" '.PrefixList.PrefixListId')
|
|
}
|
|
|
|
@test "EC2: create managed prefix list" {
|
|
run aws_cmd ec2 create-managed-prefix-list \
|
|
--prefix-list-name "$PREFIX_LIST_NAME" \
|
|
--address-family IPv4 \
|
|
--max-entries 5 \
|
|
--entries 'Cidr=10.0.0.0/8,Description=corporate'
|
|
assert_success
|
|
PREFIX_LIST_ID=$(json_get "$output" '.PrefixList.PrefixListId')
|
|
[ -n "$PREFIX_LIST_ID" ]
|
|
|
|
state=$(json_get "$output" '.PrefixList.State')
|
|
[ "$state" = "create-complete" ]
|
|
version=$(json_get "$output" '.PrefixList.Version')
|
|
[ "$version" = "1" ]
|
|
}
|
|
|
|
@test "EC2: describe managed prefix list by id" {
|
|
create_prefix_list
|
|
|
|
run aws_cmd ec2 describe-managed-prefix-lists --prefix-list-ids "$PREFIX_LIST_ID"
|
|
assert_success
|
|
name=$(json_get "$output" '.PrefixLists[0].PrefixListName')
|
|
[ "$name" = "$PREFIX_LIST_NAME" ]
|
|
}
|
|
|
|
@test "EC2: describe managed prefix lists exposes the AWS-managed S3 list" {
|
|
run aws_cmd ec2 describe-managed-prefix-lists \
|
|
--filters "Name=prefix-list-name,Values=com.amazonaws.${AWS_DEFAULT_REGION}.s3"
|
|
assert_success
|
|
id=$(json_get "$output" '.PrefixLists[0].PrefixListId')
|
|
[ "$id" = "pl-63a5400a" ]
|
|
owner=$(json_get "$output" '.PrefixLists[0].OwnerId')
|
|
[ "$owner" = "AWS" ]
|
|
}
|
|
|
|
@test "EC2: get managed prefix list entries" {
|
|
create_prefix_list
|
|
|
|
run aws_cmd ec2 get-managed-prefix-list-entries --prefix-list-id "$PREFIX_LIST_ID"
|
|
assert_success
|
|
cidr=$(json_get "$output" '.Entries[0].Cidr')
|
|
[ "$cidr" = "10.0.0.0/8" ]
|
|
desc=$(json_get "$output" '.Entries[0].Description')
|
|
[ "$desc" = "corporate" ]
|
|
}
|
|
|
|
@test "EC2: modify managed prefix list bumps version and keeps history" {
|
|
create_prefix_list
|
|
|
|
run aws_cmd ec2 modify-managed-prefix-list \
|
|
--prefix-list-id "$PREFIX_LIST_ID" \
|
|
--add-entries 'Cidr=192.168.0.0/16,Description=lab'
|
|
assert_success
|
|
version=$(json_get "$output" '.PrefixList.Version')
|
|
[ "$version" = "2" ]
|
|
|
|
run aws_cmd ec2 get-managed-prefix-list-entries --prefix-list-id "$PREFIX_LIST_ID"
|
|
assert_success
|
|
count=$(json_get "$output" '.Entries | length')
|
|
[ "$count" = "2" ]
|
|
|
|
run aws_cmd ec2 get-managed-prefix-list-entries --prefix-list-id "$PREFIX_LIST_ID" --target-version 1
|
|
assert_success
|
|
count=$(json_get "$output" '.Entries | length')
|
|
[ "$count" = "1" ]
|
|
}
|
|
|
|
@test "EC2: modify with a stale current version is rejected" {
|
|
create_prefix_list
|
|
aws_cmd ec2 modify-managed-prefix-list --prefix-list-id "$PREFIX_LIST_ID" \
|
|
--add-entries 'Cidr=192.168.0.0/16' >/dev/null
|
|
|
|
run aws_cmd ec2 modify-managed-prefix-list \
|
|
--prefix-list-id "$PREFIX_LIST_ID" \
|
|
--current-version 1 \
|
|
--add-entries 'Cidr=172.16.0.0/12'
|
|
assert_failure
|
|
assert_output --partial "PrefixListVersionMismatch"
|
|
}
|
|
|
|
@test "EC2: AWS-managed prefix list cannot be modified" {
|
|
run aws_cmd ec2 modify-managed-prefix-list \
|
|
--prefix-list-id pl-63a5400a \
|
|
--add-entries 'Cidr=10.1.0.0/16'
|
|
assert_failure
|
|
assert_output --partial "UnsupportedOperation"
|
|
}
|
|
|
|
@test "EC2: delete managed prefix list" {
|
|
create_prefix_list
|
|
local created_id="$PREFIX_LIST_ID"
|
|
|
|
run aws_cmd ec2 delete-managed-prefix-list --prefix-list-id "$created_id"
|
|
assert_success
|
|
state=$(json_get "$output" '.PrefixList.State')
|
|
[ "$state" = "delete-complete" ]
|
|
PREFIX_LIST_ID=""
|
|
|
|
run aws_cmd ec2 describe-managed-prefix-lists --prefix-list-ids "$created_id"
|
|
assert_failure
|
|
assert_output --partial "InvalidPrefixListID.NotFound"
|
|
}
|
|
|
|
@test "EC2: legacy describe-prefix-lists still serves the gateway lists" {
|
|
run aws_cmd ec2 describe-prefix-lists \
|
|
--filters "Name=prefix-list-name,Values=com.amazonaws.${AWS_DEFAULT_REGION}.s3"
|
|
assert_success
|
|
id=$(json_get "$output" '.PrefixLists[0].PrefixListId')
|
|
[ "$id" = "pl-63a5400a" ]
|
|
}
|
|
|
|
# ─── security group rules sourced from a prefix list ────────────────────────
|
|
|
|
@test "EC2: authorize a security group rule from a prefix list" {
|
|
create_prefix_list
|
|
local sg
|
|
sg=$(aws_cmd ec2 create-security-group --group-name "$(unique_name bats-sg)" \
|
|
--description "prefix list source" --query 'GroupId' --output text)
|
|
|
|
run aws_cmd ec2 authorize-security-group-ingress --group-id "$sg" \
|
|
--ip-permissions "IpProtocol=tcp,FromPort=5432,ToPort=5432,PrefixListIds=[{PrefixListId=$PREFIX_LIST_ID,Description=from-corp}]"
|
|
assert_success
|
|
pl=$(json_get "$output" '.SecurityGroupRules[0].PrefixListId')
|
|
[ "$pl" = "$PREFIX_LIST_ID" ]
|
|
|
|
run aws_cmd ec2 describe-security-groups --group-ids "$sg"
|
|
assert_success
|
|
nested=$(json_get "$output" '.SecurityGroups[0].IpPermissions[0].PrefixListIds[0].PrefixListId')
|
|
[ "$nested" = "$PREFIX_LIST_ID" ]
|
|
|
|
run aws_cmd ec2 describe-security-group-rules --filters "Name=group-id,Values=$sg"
|
|
assert_success
|
|
found=$(echo "$output" | jq --arg pl "$PREFIX_LIST_ID" '.SecurityGroupRules | any(.PrefixListId == $pl)')
|
|
[ "$found" = "true" ]
|
|
|
|
aws_cmd ec2 delete-security-group --group-id "$sg" >/dev/null 2>&1 || true
|
|
}
|
|
|
|
@test "EC2: authorizing from an unknown prefix list is rejected" {
|
|
local sg
|
|
sg=$(aws_cmd ec2 create-security-group --group-name "$(unique_name bats-sg)" \
|
|
--description "unknown prefix list" --query 'GroupId' --output text)
|
|
|
|
run aws_cmd ec2 authorize-security-group-ingress --group-id "$sg" \
|
|
--ip-permissions 'IpProtocol=tcp,FromPort=1,ToPort=1,PrefixListIds=[{PrefixListId=pl-doesnotexist}]'
|
|
assert_failure
|
|
assert_output --partial "InvalidPrefixListID.NotFound"
|
|
|
|
aws_cmd ec2 delete-security-group --group-id "$sg" >/dev/null 2>&1 || true
|
|
}
|
|
|
|
# Creates a VPC holding a source and a target security group, and sets SG_VPC_ID,
|
|
# SG_SOURCE_ID and SG_TARGET_ID.
|
|
create_sg_pair() {
|
|
local out
|
|
out=$(aws_cmd ec2 create-vpc --cidr-block 10.0.0.0/16)
|
|
SG_VPC_ID=$(json_get "$out" '.Vpc.VpcId')
|
|
out=$(aws_cmd ec2 create-security-group \
|
|
--group-name "$(unique_name bats-sg-source)" \
|
|
--description "traffic source" --vpc-id "$SG_VPC_ID")
|
|
SG_SOURCE_ID=$(json_get "$out" '.GroupId')
|
|
out=$(aws_cmd ec2 create-security-group \
|
|
--group-name "$(unique_name bats-sg-target)" \
|
|
--description "traffic target" --vpc-id "$SG_VPC_ID")
|
|
SG_TARGET_ID=$(json_get "$out" '.GroupId')
|
|
}
|
|
|
|
@test "EC2: ingress rule keeps its source security group" {
|
|
create_sg_pair
|
|
|
|
# The CLI serializes UserIdGroupPairs under the wire name "Groups", so this exercises the
|
|
# form the reporter of #2190 actually sent.
|
|
run aws_cmd ec2 authorize-security-group-ingress --group-id "$SG_TARGET_ID" \
|
|
--ip-permissions "IpProtocol=tcp,FromPort=443,ToPort=443,UserIdGroupPairs=[{GroupId=$SG_SOURCE_ID,Description=from-source-sg}]"
|
|
assert_success
|
|
ref=$(json_get "$output" '.SecurityGroupRules[0].ReferencedGroupInfo.GroupId')
|
|
[ "$ref" = "$SG_SOURCE_ID" ]
|
|
|
|
# Control: the CIDR form of the same rule.
|
|
aws_cmd ec2 authorize-security-group-ingress --group-id "$SG_TARGET_ID" \
|
|
--ip-permissions 'IpProtocol=tcp,FromPort=8443,ToPort=8443,IpRanges=[{CidrIp=10.0.0.0/8,Description=control}]' >/dev/null
|
|
|
|
run aws_cmd ec2 describe-security-groups --group-ids "$SG_TARGET_ID"
|
|
assert_success
|
|
gid=$(json_get "$output" '.SecurityGroups[0].IpPermissions[] | select(.FromPort==443) | .UserIdGroupPairs[0].GroupId')
|
|
[ "$gid" = "$SG_SOURCE_ID" ]
|
|
desc=$(json_get "$output" '.SecurityGroups[0].IpPermissions[] | select(.FromPort==443) | .UserIdGroupPairs[0].Description')
|
|
[ "$desc" = "from-source-sg" ]
|
|
cidr=$(json_get "$output" '.SecurityGroups[0].IpPermissions[] | select(.FromPort==8443) | .IpRanges[0].CidrIp')
|
|
[ "$cidr" = "10.0.0.0/8" ]
|
|
|
|
run aws_cmd ec2 describe-security-group-rules --filters "Name=group-id,Values=$SG_TARGET_ID"
|
|
assert_success
|
|
ref=$(json_get "$output" '.SecurityGroupRules[] | select(.FromPort==443) | .ReferencedGroupInfo.GroupId')
|
|
[ "$ref" = "$SG_SOURCE_ID" ]
|
|
desc=$(json_get "$output" '.SecurityGroupRules[] | select(.FromPort==443) | .Description')
|
|
[ "$desc" = "from-source-sg" ]
|
|
cidr=$(json_get "$output" '.SecurityGroupRules[] | select(.FromPort==8443) | .CidrIpv4')
|
|
[ "$cidr" = "10.0.0.0/8" ]
|
|
}
|
|
|
|
# ─── transit gateways ───────────────────────────────────────────────────────
|
|
|
|
@test "EC2: create a transit gateway and read it back" {
|
|
run aws_cmd ec2 create-transit-gateway \
|
|
--description "bats hub" \
|
|
--tag-specifications 'ResourceType=transit-gateway,Tags=[{Key=Name,Value=bats-tgw}]'
|
|
assert_success
|
|
TRANSIT_GATEWAY_ID=$(json_get "$output" '.TransitGateway.TransitGatewayId')
|
|
[ "$(json_get "$output" '.TransitGateway.State')" = "available" ]
|
|
[ "$(json_get "$output" '.TransitGateway.Options.AmazonSideAsn')" = "64512" ]
|
|
[ "$(json_get "$output" '.TransitGateway.Options.SecurityGroupReferencingSupport')" = "disable" ]
|
|
|
|
# The default route table is minted with the gateway, so both ids are already set and equal.
|
|
assoc=$(json_get "$output" '.TransitGateway.Options.AssociationDefaultRouteTableId')
|
|
prop=$(json_get "$output" '.TransitGateway.Options.PropagationDefaultRouteTableId')
|
|
[ "$assoc" = "$prop" ]
|
|
case "$assoc" in tgw-rtb-*) ;; *) return 1 ;; esac
|
|
|
|
run aws_cmd ec2 describe-transit-gateways --transit-gateway-ids "$TRANSIT_GATEWAY_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.TransitGateways[0].TransitGatewayId')" = "$TRANSIT_GATEWAY_ID" ]
|
|
[ "$(json_get "$output" '.TransitGateways[0].Tags[0].Value')" = "bats-tgw" ]
|
|
}
|
|
|
|
@test "EC2: create a transit gateway with CIDR blocks" {
|
|
run aws_cmd ec2 create-transit-gateway \
|
|
--options 'TransitGatewayCidrBlocks=[10.99.0.0/16]'
|
|
assert_success
|
|
TRANSIT_GATEWAY_ID=$(json_get "$output" '.TransitGateway.TransitGatewayId')
|
|
[ "$(json_get "$output" '.TransitGateway.Options.TransitGatewayCidrBlocks[0]')" = "10.99.0.0/16" ]
|
|
|
|
run aws_cmd ec2 describe-transit-gateways --transit-gateway-ids "$TRANSIT_GATEWAY_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.TransitGateways[0].Options.TransitGatewayCidrBlocks[0]')" = "10.99.0.0/16" ]
|
|
}
|
|
|
|
# Tags are changed after creation with create-tags rather than a tag specification, and read back
|
|
# from describe-transit-gateways, which is how a Terraform tag update converges.
|
|
@test "EC2: transit gateway tags changed after creation are visible on describe" {
|
|
local out
|
|
out=$(aws_cmd ec2 create-transit-gateway --tag-specifications \
|
|
'ResourceType=transit-gateway,Tags=[{Key=Name,Value=bats-tgw-tags}]')
|
|
TRANSIT_GATEWAY_ID=$(json_get "$out" '.TransitGateway.TransitGatewayId')
|
|
|
|
aws_cmd ec2 create-tags --resources "$TRANSIT_GATEWAY_ID" --tags 'Key=env,Value=prod' >/dev/null
|
|
|
|
run aws_cmd ec2 describe-transit-gateways --transit-gateway-ids "$TRANSIT_GATEWAY_ID"
|
|
assert_success
|
|
count=$(json_get "$output" '.TransitGateways[0].Tags | length')
|
|
[ "$count" = "2" ]
|
|
env_value=$(json_get "$output" '.TransitGateways[0].Tags[] | select(.Key=="env") | .Value')
|
|
[ "$env_value" = "prod" ]
|
|
|
|
aws_cmd ec2 delete-tags --resources "$TRANSIT_GATEWAY_ID" --tags 'Key=env' >/dev/null
|
|
|
|
run aws_cmd ec2 describe-transit-gateways --transit-gateway-ids "$TRANSIT_GATEWAY_ID"
|
|
assert_success
|
|
count=$(json_get "$output" '.TransitGateways[0].Tags | length')
|
|
[ "$count" = "1" ]
|
|
}
|
|
|
|
@test "EC2: modify a transit gateway" {
|
|
local out
|
|
out=$(aws_cmd ec2 create-transit-gateway --description "before")
|
|
TRANSIT_GATEWAY_ID=$(json_get "$out" '.TransitGateway.TransitGatewayId')
|
|
|
|
run aws_cmd ec2 modify-transit-gateway \
|
|
--transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--description "after" \
|
|
--options 'DnsSupport=disable,AddTransitGatewayCidrBlocks=[10.100.0.0/16]'
|
|
assert_success
|
|
[ "$(json_get "$output" '.TransitGateway.Description')" = "after" ]
|
|
[ "$(json_get "$output" '.TransitGateway.Options.DnsSupport')" = "disable" ]
|
|
[ "$(json_get "$output" '.TransitGateway.Options.TransitGatewayCidrBlocks[0]')" = "10.100.0.0/16" ]
|
|
|
|
# Describe serializes the same list, so read it back through the CLI's own parser too.
|
|
run aws_cmd ec2 describe-transit-gateways --transit-gateway-ids "$TRANSIT_GATEWAY_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.TransitGateways[0].Options.TransitGatewayCidrBlocks[0]')" = "10.100.0.0/16" ]
|
|
}
|
|
|
|
@test "EC2: delete a transit gateway" {
|
|
local out tgw
|
|
out=$(aws_cmd ec2 create-transit-gateway --description "short lived")
|
|
tgw=$(json_get "$out" '.TransitGateway.TransitGatewayId')
|
|
|
|
run aws_cmd ec2 delete-transit-gateway --transit-gateway-id "$tgw"
|
|
assert_success
|
|
|
|
run aws_cmd ec2 describe-transit-gateways --transit-gateway-ids "$tgw"
|
|
assert_failure
|
|
assert_output --partial "InvalidTransitGatewayID.NotFound"
|
|
}
|
|
|
|
@test "EC2: describing an unknown transit gateway is rejected" {
|
|
run aws_cmd ec2 describe-transit-gateways --transit-gateway-ids tgw-0123456789abcdef0
|
|
assert_failure
|
|
assert_output --partial "InvalidTransitGatewayID.NotFound"
|
|
}
|
|
|
|
# ─── transit gateway VPC attachments ────────────────────────────────────────
|
|
|
|
# Creates a gateway, a VPC and one subnet, setting TRANSIT_GATEWAY_ID, TGW_VPC_ID and TGW_SUBNET_ID.
|
|
create_attachment_fixture() {
|
|
local out
|
|
out=$(aws_cmd ec2 create-transit-gateway --description "bats attachment host")
|
|
TRANSIT_GATEWAY_ID=$(json_get "$out" '.TransitGateway.TransitGatewayId')
|
|
out=$(aws_cmd ec2 create-vpc --cidr-block 10.80.0.0/16)
|
|
TGW_VPC_ID=$(json_get "$out" '.Vpc.VpcId')
|
|
out=$(aws_cmd ec2 create-subnet --vpc-id "$TGW_VPC_ID" --cidr-block 10.80.1.0/24 \
|
|
--availability-zone "${AWS_DEFAULT_REGION}a")
|
|
TGW_SUBNET_ID=$(json_get "$out" '.Subnet.SubnetId')
|
|
}
|
|
|
|
@test "EC2: attach a VPC to a transit gateway and read it back both ways" {
|
|
create_attachment_fixture
|
|
|
|
run aws_cmd ec2 create-transit-gateway-vpc-attachment \
|
|
--transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--vpc-id "$TGW_VPC_ID" \
|
|
--subnet-ids "$TGW_SUBNET_ID" \
|
|
--tag-specifications 'ResourceType=transit-gateway-attachment,Tags=[{Key=Name,Value=bats-attach}]'
|
|
assert_success
|
|
TGW_ATTACHMENT_ID=$(json_get "$output" '.TransitGatewayVpcAttachment.TransitGatewayAttachmentId')
|
|
[ "$(json_get "$output" '.TransitGatewayVpcAttachment.State')" = "available" ]
|
|
[ "$(json_get "$output" '.TransitGatewayVpcAttachment.SubnetIds[0]')" = "$TGW_SUBNET_ID" ]
|
|
# The attachment's own default, which differs from the gateway's.
|
|
[ "$(json_get "$output" '.TransitGatewayVpcAttachment.Options.SecurityGroupReferencingSupport')" = "enable" ]
|
|
|
|
run aws_cmd ec2 describe-transit-gateway-vpc-attachments \
|
|
--transit-gateway-attachment-ids "$TGW_ATTACHMENT_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.TransitGatewayVpcAttachments[0].VpcId')" = "$TGW_VPC_ID" ]
|
|
[ "$(json_get "$output" '.TransitGatewayVpcAttachments[0].Tags[0].Value')" = "bats-attach" ]
|
|
|
|
# The resource-agnostic form is a different shape: typed resource plus the association.
|
|
run aws_cmd ec2 describe-transit-gateway-attachments \
|
|
--transit-gateway-attachment-ids "$TGW_ATTACHMENT_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.TransitGatewayAttachments[0].ResourceType')" = "vpc" ]
|
|
[ "$(json_get "$output" '.TransitGatewayAttachments[0].ResourceId')" = "$TGW_VPC_ID" ]
|
|
[ "$(json_get "$output" '.TransitGatewayAttachments[0].Association.State')" = "associated" ]
|
|
rtb=$(json_get "$output" '.TransitGatewayAttachments[0].Association.TransitGatewayRouteTableId')
|
|
case "$rtb" in tgw-rtb-*) ;; *) return 1 ;; esac
|
|
}
|
|
|
|
@test "EC2: modify a transit gateway VPC attachment" {
|
|
create_attachment_fixture
|
|
local out subnet_b
|
|
out=$(aws_cmd ec2 create-transit-gateway-vpc-attachment --transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--vpc-id "$TGW_VPC_ID" --subnet-ids "$TGW_SUBNET_ID")
|
|
TGW_ATTACHMENT_ID=$(json_get "$out" '.TransitGatewayVpcAttachment.TransitGatewayAttachmentId')
|
|
out=$(aws_cmd ec2 create-subnet --vpc-id "$TGW_VPC_ID" --cidr-block 10.80.2.0/24 \
|
|
--availability-zone "${AWS_DEFAULT_REGION}b")
|
|
subnet_b=$(json_get "$out" '.Subnet.SubnetId')
|
|
|
|
run aws_cmd ec2 modify-transit-gateway-vpc-attachment \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID" \
|
|
--add-subnet-ids "$subnet_b" \
|
|
--options 'DnsSupport=disable'
|
|
assert_success
|
|
[ "$(json_get "$output" '.TransitGatewayVpcAttachment.Options.DnsSupport')" = "disable" ]
|
|
count=$(json_get "$output" '.TransitGatewayVpcAttachment.SubnetIds | length')
|
|
[ "$count" = "2" ]
|
|
|
|
run aws_cmd ec2 modify-transit-gateway-vpc-attachment \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID" \
|
|
--remove-subnet-ids "$TGW_SUBNET_ID" "$subnet_b"
|
|
assert_failure
|
|
assert_output --partial "InsufficientSubnetsException"
|
|
}
|
|
|
|
@test "EC2: a transit gateway with an attachment cannot be deleted" {
|
|
create_attachment_fixture
|
|
local out
|
|
out=$(aws_cmd ec2 create-transit-gateway-vpc-attachment --transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--vpc-id "$TGW_VPC_ID" --subnet-ids "$TGW_SUBNET_ID")
|
|
TGW_ATTACHMENT_ID=$(json_get "$out" '.TransitGatewayVpcAttachment.TransitGatewayAttachmentId')
|
|
|
|
run aws_cmd ec2 delete-transit-gateway --transit-gateway-id "$TRANSIT_GATEWAY_ID"
|
|
assert_failure
|
|
assert_output --partial "IncorrectState"
|
|
|
|
run aws_cmd ec2 delete-transit-gateway-vpc-attachment \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID"
|
|
assert_success
|
|
TGW_ATTACHMENT_ID=""
|
|
|
|
run aws_cmd ec2 delete-transit-gateway --transit-gateway-id "$TRANSIT_GATEWAY_ID"
|
|
assert_success
|
|
TRANSIT_GATEWAY_ID=""
|
|
}
|
|
|
|
# ─── transit gateway route tables, associations, propagations and routes ────
|
|
|
|
@test "EC2: route table associations move between tables" {
|
|
create_attachment_fixture
|
|
local out
|
|
out=$(aws_cmd ec2 create-transit-gateway-vpc-attachment --transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--vpc-id "$TGW_VPC_ID" --subnet-ids "$TGW_SUBNET_ID")
|
|
TGW_ATTACHMENT_ID=$(json_get "$out" '.TransitGatewayVpcAttachment.TransitGatewayAttachmentId')
|
|
out=$(aws_cmd ec2 describe-transit-gateways --transit-gateway-ids "$TRANSIT_GATEWAY_ID")
|
|
local default_rtb
|
|
default_rtb=$(json_get "$out" '.TransitGateways[0].Options.AssociationDefaultRouteTableId')
|
|
|
|
run aws_cmd ec2 create-transit-gateway-route-table --transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--tag-specifications 'ResourceType=transit-gateway-route-table,Tags=[{Key=Name,Value=bats-rtb}]'
|
|
assert_success
|
|
TGW_ROUTE_TABLE_ID=$(json_get "$output" '.TransitGatewayRouteTable.TransitGatewayRouteTableId')
|
|
[ "$(json_get "$output" '.TransitGatewayRouteTable.State')" = "available" ]
|
|
[ "$(json_get "$output" '.TransitGatewayRouteTable.DefaultAssociationRouteTable')" = "false" ]
|
|
|
|
# The attachment starts on the gateway's default table, so a second association is refused.
|
|
run aws_cmd ec2 associate-transit-gateway-route-table \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID"
|
|
assert_failure
|
|
assert_output --partial "Resource.AlreadyAssociated"
|
|
|
|
run aws_cmd ec2 disassociate-transit-gateway-route-table \
|
|
--transit-gateway-route-table-id "$default_rtb" \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.Association.State')" = "disassociating" ]
|
|
|
|
run aws_cmd ec2 associate-transit-gateway-route-table \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.Association.State')" = "associating" ]
|
|
|
|
run aws_cmd ec2 get-transit-gateway-route-table-associations \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.Associations[0].TransitGatewayAttachmentId')" = "$TGW_ATTACHMENT_ID" ]
|
|
[ "$(json_get "$output" '.Associations[0].ResourceType')" = "vpc" ]
|
|
}
|
|
|
|
@test "EC2: propagation produces a route for the attached VPC" {
|
|
create_attachment_fixture
|
|
local out
|
|
out=$(aws_cmd ec2 create-transit-gateway-vpc-attachment --transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--vpc-id "$TGW_VPC_ID" --subnet-ids "$TGW_SUBNET_ID")
|
|
TGW_ATTACHMENT_ID=$(json_get "$out" '.TransitGatewayVpcAttachment.TransitGatewayAttachmentId')
|
|
out=$(aws_cmd ec2 create-transit-gateway-route-table --transit-gateway-id "$TRANSIT_GATEWAY_ID")
|
|
TGW_ROUTE_TABLE_ID=$(json_get "$out" '.TransitGatewayRouteTable.TransitGatewayRouteTableId')
|
|
|
|
run aws_cmd ec2 enable-transit-gateway-route-table-propagation \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.Propagation.State')" = "enabled" ]
|
|
|
|
run aws_cmd ec2 get-transit-gateway-route-table-propagations \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.TransitGatewayRouteTablePropagations[0].State')" = "enabled" ]
|
|
|
|
run aws_cmd ec2 search-transit-gateway-routes \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--filters 'Name=type,Values=propagated'
|
|
assert_success
|
|
[ "$(json_get "$output" '.Routes[0].DestinationCidrBlock')" = "10.80.0.0/16" ]
|
|
[ "$(json_get "$output" '.Routes[0].Type')" = "propagated" ]
|
|
[ "$(json_get "$output" '.Routes[0].State')" = "active" ]
|
|
|
|
run aws_cmd ec2 enable-transit-gateway-route-table-propagation \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID"
|
|
assert_failure
|
|
assert_output --partial "TransitGatewayRouteTablePropagation.Duplicate"
|
|
}
|
|
|
|
@test "EC2: static and blackhole transit gateway routes" {
|
|
create_attachment_fixture
|
|
local out
|
|
out=$(aws_cmd ec2 create-transit-gateway-vpc-attachment --transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--vpc-id "$TGW_VPC_ID" --subnet-ids "$TGW_SUBNET_ID")
|
|
TGW_ATTACHMENT_ID=$(json_get "$out" '.TransitGatewayVpcAttachment.TransitGatewayAttachmentId')
|
|
out=$(aws_cmd ec2 create-transit-gateway-route-table --transit-gateway-id "$TRANSIT_GATEWAY_ID")
|
|
TGW_ROUTE_TABLE_ID=$(json_get "$out" '.TransitGatewayRouteTable.TransitGatewayRouteTableId')
|
|
|
|
run aws_cmd ec2 create-transit-gateway-route \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--destination-cidr-block 10.60.0.0/16 \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.Route.Type')" = "static" ]
|
|
[ "$(json_get "$output" '.Route.State')" = "active" ]
|
|
[ "$(json_get "$output" '.Route.TransitGatewayAttachments[0].TransitGatewayAttachmentId')" = "$TGW_ATTACHMENT_ID" ]
|
|
|
|
run aws_cmd ec2 create-transit-gateway-route \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--destination-cidr-block 10.61.0.0/16 --blackhole
|
|
assert_success
|
|
[ "$(json_get "$output" '.Route.State')" = "blackhole" ]
|
|
# A blackhole carries no attachment, so the key is absent rather than empty.
|
|
count=$(json_get "$output" '.Route.TransitGatewayAttachments // [] | length')
|
|
[ "$count" = "0" ]
|
|
|
|
run aws_cmd ec2 search-transit-gateway-routes \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--filters 'Name=type,Values=static'
|
|
assert_success
|
|
count=$(json_get "$output" '.Routes | length')
|
|
[ "$count" = "2" ]
|
|
|
|
run aws_cmd ec2 delete-transit-gateway-route \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--destination-cidr-block 10.61.0.0/16
|
|
assert_success
|
|
[ "$(json_get "$output" '.Route.State')" = "deleted" ]
|
|
}
|
|
|
|
@test "EC2: replacing a transit gateway route moves its target and upserts" {
|
|
create_attachment_fixture
|
|
local out
|
|
out=$(aws_cmd ec2 create-transit-gateway-vpc-attachment --transit-gateway-id "$TRANSIT_GATEWAY_ID" \
|
|
--vpc-id "$TGW_VPC_ID" --subnet-ids "$TGW_SUBNET_ID")
|
|
TGW_ATTACHMENT_ID=$(json_get "$out" '.TransitGatewayVpcAttachment.TransitGatewayAttachmentId')
|
|
out=$(aws_cmd ec2 create-transit-gateway-route-table --transit-gateway-id "$TRANSIT_GATEWAY_ID")
|
|
TGW_ROUTE_TABLE_ID=$(json_get "$out" '.TransitGatewayRouteTable.TransitGatewayRouteTableId')
|
|
aws_cmd ec2 create-transit-gateway-route --transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--destination-cidr-block 10.88.0.0/16 --transit-gateway-attachment-id "$TGW_ATTACHMENT_ID" >/dev/null
|
|
|
|
run aws_cmd ec2 replace-transit-gateway-route \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--destination-cidr-block 10.88.0.0/16 --blackhole
|
|
assert_success
|
|
[ "$(json_get "$output" '.Route.State')" = "blackhole" ]
|
|
count=$(json_get "$output" '.Route.TransitGatewayAttachments // [] | length')
|
|
[ "$count" = "0" ]
|
|
|
|
run aws_cmd ec2 replace-transit-gateway-route \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--destination-cidr-block 10.88.0.0/16 \
|
|
--transit-gateway-attachment-id "$TGW_ATTACHMENT_ID"
|
|
assert_success
|
|
[ "$(json_get "$output" '.Route.State')" = "active" ]
|
|
|
|
# Replacing a destination the table never held writes it.
|
|
run aws_cmd ec2 replace-transit-gateway-route \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--destination-cidr-block 10.89.0.0/16 --blackhole
|
|
assert_success
|
|
run aws_cmd ec2 search-transit-gateway-routes \
|
|
--transit-gateway-route-table-id "$TGW_ROUTE_TABLE_ID" \
|
|
--filters 'Name=type,Values=static'
|
|
assert_success
|
|
count=$(json_get "$output" '.Routes | length')
|
|
[ "$count" = "2" ]
|
|
}
|