mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 18:48:00 -05:00
* feat(ec2): support transit gateway VPC attachments CreateTransitGatewayVpcAttachment, DescribeTransitGatewayVpcAttachments, DescribeTransitGatewayAttachments, ModifyTransitGatewayVpcAttachment and DeleteTransitGatewayVpcAttachment. Part two of #2308, on top of the gateway itself. The two describes are separate shapes rather than one being a superset: the VPC-specific form carries the subnets and options, while the resource-agnostic form drops both, types the VPC as a resource, and is the only place the route table association appears. Behaviour taken from a live account rather than the reference. The attachment's option defaults are its own: dnsSupport and securityGroupReferencingSupport enabled, ipv6Support and applianceModeSupport disabled. The second of those is enabled here while the gateway that owns the attachment defaults it to disabled, which is not something the documentation calls out. An attachment associates with the gateway's default route table only when the gateway asks for it, and an association already made survives the gateway later disabling that default: the association belongs to the attachment from creation, and only later attachments are affected. Rejections, all as observed: a VPC attached twice to one gateway is DuplicateTransitGatewayAttachment, two subnets in one zone are DuplicateSubnetsInSameZone, a subnet from another VPC is reported missing rather than mismatched, removing a subnet that is not attached is InvalidSubnetID.NotFound with its own wording, and removing the last one is InsufficientSubnetsException. A malformed attachment id is rejected before the lookup, with the terse message the real API uses. Two changes reach outside the new surface, both deliberate: DeleteTransitGateway now refuses while attachments exist, reporting IncorrectState and naming them. Part one could not have hit this, since nothing could attach then. parseTagsForResource reads TagSpecifications as well as TagSpecification. CreateTransitGatewayVpcAttachment declares no locationName on that member and so serializes the plural, unlike its neighbours; every tag sent by the AWS CLI was being dropped. The change only adds a spelling, so actions using the singular are untouched. State is reported settled rather than transitional, as for the gateway. Ipv6Support is accepted without checking that the subnets carry IPv6 CIDRs, which real AWS rejects; Floci does not model subnet IPv6 allocation, and inventing that state to enforce it seemed worse than documenting the gap. * fix(ec2): make attachment uniqueness atomic and guard attached resources Two defects in the VPC attachment work, both found in review. The one-attachment-per-VPC rule scanned for an existing attachment and then stored the new one without holding a lock between the two, so concurrent creates for the same VPC could all find nothing and all persist. Reproduced by disabling the fix: eight threads, eight attachments, where the API promises one. The check and the write now share the gateway's lock, and the test asserts one winner, the right error for every loser, and one stored attachment, over forty trials. Deleting a VPC or a subnet that an attachment uses left the attachment naming resources that no longer existed, which surfaced later as a modify failing on a subnet the caller never touched. Checked on a live account: both refuse, with the same code and shape of message. DependencyViolation: The subnet 'subnet-…' has dependencies and cannot be deleted. DependencyViolation: The vpc 'vpc-…' has dependencies and cannot be deleted. Both deletes now refuse the same way. A subnet of the attached VPC that the attachment does not use still deletes, so the guard is no wider than the dependency it protects. * test(ec2): lighten the attachment concurrency harness Forty trials of eight threads was heavier than the race needs — without the lock it shows on the first trial — and the extra load disturbed two thread-timing-sensitive tests elsewhere in the suite, PostgresProtocolHandlerTest and ElbV2HealthCheckerTest, neither of which this branch touches. Ten trials of six still reproduces it and leaves those alone. * fix(ec2): guard everything an attachment depends on under one lock The previous fix locked the uniqueness check alone, which was too narrow twice over. The gateway was resolved before the locked section, so a concurrent DeleteTransitGateway could see no attachments, delete the gateway, and let the create persist against the object it had already read. The VPC and subnet checks were outside any shared lock at all, and lockFor is striped, so a gateway's monitor never guarded a VPC or subnet delete regardless. Everything an attachment depends on now resolves and writes under one region-scoped monitor: the gateway, the VPC, the subnets, and the uniqueness rule. Deleting a gateway, a VPC or a subnet takes the same monitor across its dependency check and its delete. Where a gateway lock is also taken it is acquired inside this one, so the two cannot interleave in opposite orders. The test races a create against each delete and asserts the invariant rather than a winner: whoever wins, the store must never hold an attachment naming a gateway, VPC or subnet that is gone. It reproduces both holes — with the create-side lock removed, "attachment outlived its gateway" fails on the first trial. * fix(ec2): stop a tag write restoring a deleted transit gateway resource Tagging is a read-modify-write, so a tag call overlapping a delete could read the record, lose the race, and put it back afterwards. A resurrected attachment goes on blocking the VPC and subnets it names; a resurrected gateway goes on refusing its own deletion. The gateway and route table branches were as exposed as the attachment one, so all three moved together rather than only the case reported. Ordering matters as much as the lock here. The deletes take the topology lock and then a striped resource lock, so the tag paths have to take them the same way round; taking the topology lock further down the stack, with the resource lock already held, deadlocks against a concurrent delete. That is what the first attempt did, and the race test caught it as a pool that never drained. createTags and deleteTags now take the topology lock outermost for tgw- resources and updateResourceTags relies on its callers for it. The test races tagging against deletion for an attachment and for a gateway, asserting neither comes back.
444 lines
19 KiB
Bash
444 lines
19 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=""
|
|
}
|
|
|
|
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_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=""
|
|
}
|