Files
floci/compatibility-tests/sdk-test-awscli/test/ec2.bats
T
Michal Schreiber 20470b7f4a feat(ec2): support transit gateways (#2329)
* feat(ec2): support transit gateways

CreateTransitGateway, DescribeTransitGateways, ModifyTransitGateway and
DeleteTransitGateway, as EC2 Query actions alongside the existing VPC and
route table ones. Metadata only: nothing routes packets, and the value is
in ids later resources can reference and describes that round-trip so
plans converge.

Option defaults come from a live AWS account rather than the reference:
amazonSideAsn 64512, dnsSupport, vpnEcmpSupport and both default route
table options enabled, autoAcceptSharedAttachments,
securityGroupReferencingSupport and multicastSupport disabled. The last
of those is the one worth stating, since disable is easy to assume the
other way round. transitGatewayCidrBlocks is omitted entirely when unset
rather than sent empty.

Creating a gateway with either default route table option enabled also
mints the route table AWS mints, and reports its id as both
associationDefaultRouteTableId and propagationDefaultRouteTableId. Both
name the same table, and it is already on the create response rather than
appearing later, so a caller reading either id sees what AWS shows. The
actions that operate on route tables directly are not implemented; nor
are attachments.

State is reported settled rather than transitional. AWS returns a new
gateway as pending and reaches available in about 50 seconds, and reports
deleting for about a minute before deleted; nothing here is slow, so the
settled state is what the caller sees, matching how
createManagedPrefixList already compresses create-in-progress.

Modify and delete echo the gateway without its tagSet while create and
describe include it, which is what the live API does.

Part one of #2308.

* fix(ec2): keep a transit gateway's default route table options coherent

ModifyTransitGateway changed the DefaultRouteTableAssociation and
DefaultRouteTablePropagation flags without touching their route table
ids, so a gateway created with both disabled could be modified into
reporting an option enabled while carrying no id for it.

AWS does not create a route table on that request; it rejects it unless
an existing table is named, and rejects an id supplied alongside a
disable. An unknown table comes back as InvalidRouteTableID.NotFound
rather than a transit-gateway-specific code. Checked on a live account:

  enable DefaultRouteTableAssociation conflicts with
  AssociationDefaultRouteTableId null

  disable DefaultRouteTableAssociation conflicts with
  AssociationDefaultRouteTableId tgw-rtb-0123456789abcdef0

  Transit Gateway Route Table tgw-rtb-0123456789abcdef0 was deleted or
  does not exist.

Validate instead: the flag and its id have to move together in both
directions, and a named table has to exist. The divergent state is now
unreachable and a rejected call leaves the gateway untouched.

* fix(ec2): carry a default route table change through to what it governs

Disabling DefaultRouteTableAssociation or DefaultRouteTablePropagation
changed the flag alone, leaving the id it governs on the gateway and the
matching marker on the route table. A describe then reported the option
disabled while still naming a table, and the table still claimed to be
the default for it.

Checked on a live account. Disabling association on a gateway created
with both defaults enabled drops AssociationDefaultRouteTableId from the
options entirely rather than blanking it, clears DefaultAssociationRouteTable
on the table, and leaves the table itself in place with its propagation
marker and the gateway's PropagationDefaultRouteTableId untouched:

  DefaultRouteTableAssociation  disable      (no AssociationDefaultRouteTableId)
  DefaultRouteTablePropagation  enable       PropagationDefaultRouteTableId tgw-rtb-…
  tgw-rtb-…  available  defAssoc False  defProp True

Apply the same: the id follows its flag, the table's marker follows with
it, and the table survives. Enabling with a named table marks that table
instead.

* fix(ec2): require a default route table to belong to the gateway naming it

The route table named on a ModifyTransitGateway request was only checked
for existence in the region, so a gateway could take another gateway's
table as its default. Worse than a bad reference: applying the change
then rewrote the foreign table's own default marker, leaving the gateway
that actually owns it describing a table that no longer claims to be its
default.

AWS rejects this, under the same code as a table that exists nowhere but
with the gateway named in the message. Checked on a live account, gateway
A naming gateway B's default table:

  Transit Gateway Route Table tgw-rtb-0381f… was deleted or does not
  exist in Transit Gateway tgw-0438f…

Both wordings are now reproduced: unqualified when the table exists
nowhere, gateway-qualified when it exists but belongs elsewhere.

The three rules governing these options - flag and id agreeing in both
directions, and the named table existing and belonging to the gateway -
now sit in one validation block rather than accumulating as separate
guards.

* fix(ec2): judge a default route table change against the stored gateway

Validation looked only at the request, so a route table id arriving
without its flag skipped the coherence check entirely: the id was stored
while the table's default marker was left alone, and a disabled option
could end up naming a table.

AWS judges the pair against the gateway as it stands. Probed the whole
combination surface on a live account rather than the one case reported:

  id alone, option enabled     accepted
  id alone, option disabled    InvalidParameterCombination, and the
                               message quotes the stored flag, not the request
  enable alone, table named    accepted, keeps that table
  enable alone, no table       InvalidParameterCombination
  disable with an id           InvalidParameterCombination
  disable with an unknown id   the combination is reported, not the lookup

So the flag defaults to the stored one when the request omits it, enabling
without an id keeps the table already named rather than dropping it, and an
id on its own marks its table. The lookup still runs after the combination
check, which is the order AWS reports them in.

Two other behaviours from the same pass, both already correct and now
covered: removals apply before additions, so a CIDR added and removed in
one call survives, and removing a CIDR that is not present is accepted.

* fix(ec2): serialize transit gateway CIDR blocks as the string list they are

transitGatewayCidrBlocks is a plain string list, so each item carries the
CIDR as its own text. The items wrapped it in a cidrBlock element
instead, which the AWS CLI parsed as an empty string: a caller that added
a block got it back as "" from create, describe and modify alike.

Caught by the CLI compatibility suite, not by the integration test, which
asserted the same wrong shape the handler produced and so agreed with it.
The bats case now reads the list back through describe as well as modify,
since one helper serializes all three responses.

* fix(ec2): apply tag changes to a transit gateway, not just to the tag store

CreateTags and DeleteTags on a tgw- id updated the tag store but left the
gateway object alone, because updateResourceTags had no branch for it.
DescribeTags saw the change and DescribeTransitGateways did not, so a
gateway kept serving whatever tags its TagSpecification carried at
creation.

That is the path a Terraform tag update takes: the provider sends
CreateTags, then re-reads with DescribeTransitGateways and compares. With
the two disagreeing, aws_ec2_transit_gateway would never converge.

Route tables get the same branch, so their tags behave once part three
can create them.

The AWS CLI cases now cover the tag round trip and CIDR blocks supplied
at creation, both of which were only asserted against shapes this branch
produced itself.
2026-08-15 18:11:24 -04:00

342 lines
14 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=""
}
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 "$TRANSIT_GATEWAY_ID" ]; then
aws_cmd ec2 delete-transit-gateway --transit-gateway-id "$TRANSIT_GATEWAY_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"
}