Files
floci/compatibility-tests/sdk-test-awscli/test/cloudformation.bats
T
Jackson Price 3cb1643d30 fix(cloudformation): publish content-hashed versions for SAM AutoPublishAlias (#5744)
* fix(cloudformation): publish content-hashed versions for SAM AutoPublishAlias

Point the alias AutoPublishAlias generates at the Version attribute of
the generated AWS::Lambda::Version, as SAM does, instead of $LATEST,
and name that version as SAM names it: <Function>Version plus the
first ten hex digits of the SHA-1 of the function's code, with
template parameters resolved as a Ref or as ${Name} in an Fn::Sub,
merged with its Environment and MemorySize, as sorted compact JSON
escaped the way Python's json.dumps escapes it. Under
AutoPublishAliasAllProperties the hash covers every property floci
gives the function. A string AutoPublishCodeSha256 takes the hash's
place with its first ten characters. As in SAM, the version is
retained (DeletionPolicy: Retain) unless the function says otherwise,
and version properties of the wrong type are refused.

With the alias on the published version, a template change to the
code names a new version resource, which publishes, and the alias
moves to it, while changes made to $LATEST outside the stack no longer
reach the alias. The $LATEST pin worked around #1987 and #1988: a cold
invoke of a published version timed out, and a warm $LATEST container
served version-qualified invokes. Both are fixed on main.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(cloudformation): hash an applied SnapStart into the SAM version id

SAM adds a function's SnapStart to the hash behind the AutoPublishAlias
version's logical id when its ApplyOn is anything but None, so turning
SnapStart on names a new version, which publishes and gets its
snapshot. Without it a SnapStart function got a different id than SAM
gives it, and changing SnapStart published nothing. Under
AutoPublishAliasAllProperties SnapStart is hashed like the other
properties.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* refactor(cloudformation): resolve SAM destination types with parameterValue

destinationType repeated the parameter lookup parameterValue already
does for the version hash: the stack's value, else the Default. Use the
one helper. A non-string Default still names no destination type.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* docs(cloudformation): describe the SAM version hash in SAM terms

Reword the comments and test names around the AutoPublishAlias version
id to state what SAM and CloudFormation do, rather than how another
project or language implements it.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(cloudformation): hash a null SnapStart ApplyOn and state what it changes

SAM leaves SnapStart out of the version hash only when ApplyOn is
missing or "None"; a null ApplyOn is hashed. The check read a null as
"None".

floci does not pass SnapStart to the function, so turning it on
changes the version's logical id but publishes no new version, and
no snapshot is taken. 44eae301d said it publishes; the docs and the
comment now say what happens.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* docs(cloudformation): say which characters the SAM version hash escapes

Characters outside printable ASCII are hex escapes except where JSON
has a short escape, such as a newline's, and SnapStart is hashed only
when it sets an ApplyOn other than None.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* refactor(cloudformation): leave the optional SAM version properties to their own change

Take AutoPublishCodeSha256, AutoPublishAliasAllProperties and
VersionDeletionPolicy, with their validation, docs and tests, out of
the AutoPublishAlias fix so they can follow as their own change. The
transform no longer reads them, as on main: each is ignored, never
refused, and none changes the version's logical id or its
DeletionPolicy. The CapacityProviderConfig default goes with
VersionDeletionPolicy.

The fix keeps the alias on Fn::GetAtt of the version's Version, the
hashed version id over the code, Environment, MemorySize and an
applied SnapStart, and the version's default DeletionPolicy: Retain.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
2026-10-16 07:07:51 -04:00

364 lines
12 KiB
Bash

#!/usr/bin/env bats
# CloudFormation tests
setup() {
load 'test_helper/common-setup'
STACK_NAME="bats-cfn-stack-$(unique_name)"
TEMPLATE_FILE=$(mktemp /tmp/cfn-bats-XXXXXX.yaml)
}
teardown() {
aws_cmd cloudformation delete-stack --stack-name "$STACK_NAME" >/dev/null 2>&1 || true
[ -n "$TEMPLATE_FILE" ] && rm -f "$TEMPLATE_FILE"
}
# ── CreateStack / DescribeStacks ──────────────────────────────────────────────
@test "CloudFormation: create stack reaches CREATE_COMPLETE" {
cat > "$TEMPLATE_FILE" << 'EOF'
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: bats-cfn-basic-queue
EOF
run aws_cmd cloudformation create-stack \
--stack-name "$STACK_NAME" \
--template-body "file://$TEMPLATE_FILE"
assert_success
run aws_cmd cloudformation describe-stacks --stack-name "$STACK_NAME"
assert_success
local stack_status
stack_status=$(json_get "$output" '.Stacks[0].StackStatus')
[ "$stack_status" = "CREATE_COMPLETE" ]
}
@test "CloudFormation: describe-stack-resources lists provisioned resources" {
cat > "$TEMPLATE_FILE" << 'EOF'
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: bats-cfn-resources-queue
EOF
aws_cmd cloudformation create-stack \
--stack-name "$STACK_NAME" \
--template-body "file://$TEMPLATE_FILE" >/dev/null
run aws_cmd cloudformation describe-stack-resources --stack-name "$STACK_NAME"
assert_success
local count
count=$(json_get "$output" '.StackResources | length')
[ "$count" -gt 0 ]
}
@test "CloudFormation: delete stack removes resources" {
cat > "$TEMPLATE_FILE" << 'EOF'
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: bats-cfn-delete-queue
EOF
aws_cmd cloudformation create-stack \
--stack-name "$STACK_NAME" \
--template-body "file://$TEMPLATE_FILE" >/dev/null
run aws_cmd cloudformation delete-stack --stack-name "$STACK_NAME"
assert_success
run aws_cmd cloudformation describe-stacks --stack-name "$STACK_NAME"
# Stack should no longer exist
[[ "$output" == *"does not exist"* ]]
STACK_NAME="" # prevent teardown from trying again
}
# ── aws cloudformation deploy (CreateChangeSet + ExecuteChangeSet by ARN) ─────
#
# Regression: DescribeChangeSet / ExecuteChangeSet failed when called with the
# changeset ARN (the AWS CLI always passes the ARN, not the short name).
# See: https://github.com/floci-io/floci/issues/606
@test "CloudFormation: deploy creates stack via changeset" {
cat > "$TEMPLATE_FILE" << 'EOF'
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: bats-cfn-deploy-queue
EOF
run aws_cmd cloudformation deploy \
--stack-name "$STACK_NAME" \
--template-file "$TEMPLATE_FILE"
assert_success
run aws_cmd cloudformation describe-stacks --stack-name "$STACK_NAME"
assert_success
local stack_status
stack_status=$(json_get "$output" '.Stacks[0].StackStatus')
[ "$stack_status" = "CREATE_COMPLETE" ]
}
@test "CloudFormation: deploy provisions resources correctly" {
cat > "$TEMPLATE_FILE" << 'EOF'
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: bats-cfn-deploy-res-queue
EOF
aws_cmd cloudformation deploy \
--stack-name "$STACK_NAME" \
--template-file "$TEMPLATE_FILE" >/dev/null
run aws_cmd cloudformation describe-stack-resources --stack-name "$STACK_NAME"
assert_success
local resource_status
resource_status=$(json_get "$output" '.StackResources[0].ResourceStatus')
[ "$resource_status" = "CREATE_COMPLETE" ]
}
# ── EC2 VPC/Subnet provisioning (regression: issue #1297) ─────────────────────
#
# CloudFormation must create AWS::EC2::VPC / AWS::EC2::Subnet resources for real
# (in EC2), not stub them. Before the fix, describe-stacks reported CREATE_COMPLETE
# and exported the subnet ids, but ec2 describe-subnets did not return them and
# elbv2 create-load-balancer failed with "The subnet ID '...' does not exist".
# https://github.com/floci-io/floci/issues/1297
@test "CloudFormation: VPC/subnet stack exports subnet ids that EC2 and ELBv2 can use" {
cat > "$TEMPLATE_FILE" << 'EOF'
{
"Resources": {
"Vpc": {
"Type": "AWS::EC2::VPC",
"Properties": {"CidrBlock": "10.20.0.0/16"}
},
"Subnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {"Ref": "Vpc"},
"CidrBlock": "10.20.1.0/24",
"AvailabilityZone": "us-east-1a"
}
},
"Subnet2": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {"Ref": "Vpc"},
"CidrBlock": "10.20.2.0/24",
"AvailabilityZone": "us-east-1b"
}
}
},
"Outputs": {
"Subnet1Id": {"Value": {"Ref": "Subnet1"}},
"Subnet2Id": {"Value": {"Ref": "Subnet2"}}
}
}
EOF
run aws_cmd cloudformation create-stack \
--stack-name "$STACK_NAME" \
--template-body "file://$TEMPLATE_FILE"
assert_success
run aws_cmd cloudformation describe-stacks --stack-name "$STACK_NAME"
assert_success
local stack_status
stack_status=$(json_get "$output" '.Stacks[0].StackStatus')
[ "$stack_status" = "CREATE_COMPLETE" ]
# The exported subnet ids must be real subnet-xxxx ids (not the old stub shape).
local subnet1 subnet2
subnet1=$(json_get "$output" '.Stacks[0].Outputs[] | select(.OutputKey=="Subnet1Id") | .OutputValue')
subnet2=$(json_get "$output" '.Stacks[0].Outputs[] | select(.OutputKey=="Subnet2Id") | .OutputValue')
[[ "$subnet1" == subnet-* ]]
[[ "$subnet2" == subnet-* ]]
# describe-subnets must return the exact subnet ids CloudFormation exported.
run aws_cmd ec2 describe-subnets --subnet-ids "$subnet1" "$subnet2"
assert_success
local count
count=$(json_get "$output" '.Subnets | length')
[ "$count" -eq 2 ]
# ELBv2 create-load-balancer must accept the exported subnets (no SubnetNotFound).
run aws_cmd elbv2 create-load-balancer \
--name "bats-vpc-alb-${RANDOM}" \
--type application \
--scheme internal \
--subnets "$subnet1" "$subnet2"
assert_success
local lb_arn
lb_arn=$(json_get "$output" '.LoadBalancers[0].LoadBalancerArn')
[ -n "$lb_arn" ]
[[ "$lb_arn" == *":loadbalancer/"* ]]
}
# ── DeleteStack with managed S3 buckets (regression: issue #1539) ──────────────
#
# Real AWS CloudFormation does not report a stack as DELETE_COMPLETE while a
# managed resource still exists. Deleting a stack that owns a NON-EMPTY
# AWS::S3::Bucket must put the stack into DELETE_FAILED (S3 DeleteBucket refuses
# a non-empty bucket) and leave the bucket in place — it must NOT silently report
# success while the bucket and its objects remain.
# An EMPTY managed bucket must be deleted as part of the stack delete.
# https://github.com/floci-io/floci/issues/1539
# Poll describe-stacks until the stack reaches the expected status, or report the
# special value DOES_NOT_EXIST once the stack is gone.
cfn_wait_status() {
local stack="$1" expected="$2" tries="${3:-40}"
local i out status
for ((i = 0; i < tries; i++)); do
# `|| true`: describe-stacks exits non-zero once the stack is gone, which
# would otherwise abort this command substitution under bats' errexit.
out=$(aws_cmd cloudformation describe-stacks --stack-name "$stack" 2>&1 || true)
if [[ "$out" == *"does not exist"* ]]; then
status="DOES_NOT_EXIST"
else
status=$(echo "$out" | jq -r '.Stacks[0].StackStatus' 2>/dev/null)
fi
if [ "$status" = "$expected" ]; then
return 0
fi
sleep 0.25
done
echo "timeout waiting for stack '$stack' to reach '$expected' (last status: '$status')" >&2
return 1
}
@test "CloudFormation: delete-stack with non-empty S3 bucket fails and keeps the bucket (issue #1539)" {
local bucket="bats-cfn-nonempty-$(unique_name bkt)"
cat > "$TEMPLATE_FILE" << EOF
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": { "BucketName": "$bucket" }
}
}
}
EOF
run aws_cmd cloudformation create-stack \
--stack-name "$STACK_NAME" \
--template-body "file://$TEMPLATE_FILE"
assert_success
cfn_wait_status "$STACK_NAME" "CREATE_COMPLETE"
# Put an object so the managed bucket is non-empty.
local body_file
body_file=$(mktemp)
echo -n "hello floci 1539" > "$body_file"
run aws_cmd s3api put-object --bucket "$bucket" --key "object.txt" --body "$body_file"
assert_success
rm -f "$body_file"
run aws_cmd cloudformation delete-stack --stack-name "$STACK_NAME"
assert_success
# AWS leaves the stack in DELETE_FAILED — it must NOT report DELETE_COMPLETE
# (i.e. the stack must NOT silently disappear).
cfn_wait_status "$STACK_NAME" "DELETE_FAILED"
# The managed bucket and its object must still exist after the failed delete.
run aws_cmd s3api head-bucket --bucket "$bucket"
assert_success
# Cleanup: empty + remove the leftover bucket, then drop the stack.
aws_cmd s3 rm "s3://$bucket" --recursive >/dev/null 2>&1 || true
aws_cmd s3api delete-bucket --bucket "$bucket" >/dev/null 2>&1 || true
}
@test "CloudFormation: delete-stack with empty S3 bucket removes the bucket" {
local bucket="bats-cfn-empty-$(unique_name bkt)"
cat > "$TEMPLATE_FILE" << EOF
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": { "BucketName": "$bucket" }
}
}
}
EOF
run aws_cmd cloudformation create-stack \
--stack-name "$STACK_NAME" \
--template-body "file://$TEMPLATE_FILE"
assert_success
cfn_wait_status "$STACK_NAME" "CREATE_COMPLETE"
run aws_cmd s3api head-bucket --bucket "$bucket"
assert_success
run aws_cmd cloudformation delete-stack --stack-name "$STACK_NAME"
assert_success
cfn_wait_status "$STACK_NAME" "DOES_NOT_EXIST"
STACK_NAME="" # prevent teardown from trying again
# The empty managed bucket must be gone.
run aws_cmd s3api head-bucket --bucket "$bucket"
assert_failure
}
# ── SAM AutoPublishAlias (regression: issue #5676) ────────────────────────────
#
# The alias AutoPublishAlias generates must point at the version the stack
# published, not at $LATEST, and a template code change must publish the next
# version and move the alias to it, as AWS SAM does.
# https://github.com/floci-io/floci/issues/5676
sam_alias_template() {
cat << EOF
Transform: AWS::Serverless-2016-10-31
Resources:
Fn:
Type: AWS::Serverless::Function
Properties:
FunctionName: $1
Runtime: nodejs20.x
Handler: index.handler
AutoPublishAlias: live
InlineCode: "exports.handler = async () => ({ release: '$2' });"
EOF
}
@test "CloudFormation: SAM AutoPublishAlias points the alias at the published version (issue #5676)" {
local fn
fn=$(unique_name bats-sam-alias)
sam_alias_template "$fn" first > "$TEMPLATE_FILE"
run aws_cmd cloudformation deploy \
--stack-name "$STACK_NAME" \
--template-file "$TEMPLATE_FILE" \
--capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND
assert_success
run aws_cmd lambda get-alias --function-name "$fn" --name live
assert_success
[ "$(json_get "$output" '.FunctionVersion')" = "1" ]
# A code change publishes the next version and the alias moves to it.
sam_alias_template "$fn" second > "$TEMPLATE_FILE"
run aws_cmd cloudformation deploy \
--stack-name "$STACK_NAME" \
--template-file "$TEMPLATE_FILE" \
--capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND
assert_success
run aws_cmd lambda get-alias --function-name "$fn" --name live
assert_success
[ "$(json_get "$output" '.FunctionVersion')" = "2" ]
run aws_cmd lambda list-versions-by-function --function-name "$fn"
assert_success
[ "$(json_get "$output" '[.Versions[].Version] | join(",")')" = '$LATEST,1,2' ]
}