Files
floci/compatibility-tests/sdk-test-awscli/test/s3.bats
T
Michal Schreiber 8d33c65129 feat(s3): support bucket metrics configurations (#2382)
* feat(s3): support bucket metrics configurations

PutBucketMetricsConfiguration was not routed, so a metrics call fell through to
the unqualified CreateBucket and came back as BucketAlreadyOwnedByYou, which
tells the caller nothing about what happened. The DELETE was worse: it fell
through to DeleteBucket and silently removed the whole bucket while reporting
success, so delete-bucket-metrics-configuration destroyed the bucket it was
called on.

Adds the four actions, stored per bucket and keyed by id:

- PutBucketMetricsConfiguration, GetBucketMetricsConfiguration,
  ListBucketMetricsConfigurations, DeleteBucketMetricsConfiguration
- the body is parsed and re-serialized rather than echoed back, so responses are
  built by floci and a filter survives the round trip in AWS's shape: repeated
  Tag elements inside And, with no wrapping element
- an id in the body that disagrees with the query string is MalformedXML, and an
  unknown id is NoSuchConfiguration on both get and delete, neither of which AWS
  treats as a no-op
- put and delete take the bucket monitor the other bucket-scoped mutations use,
  so concurrent puts of different ids cannot drop one another

Behaviour taken from a live AWS account: the status codes (204 on put and
delete, 404 for an unknown id), both error codes, and the exact XML of the
single-configuration and list responses.

floci records the configurations and returns them; no metrics are produced from
them, and the listing is unpaged, so a continuation token is neither issued nor
honoured.

* fix(s3): validate metrics filters, use the shared XML utilities, guard the delete

Three problems in the previous commit.

The filter was normalized rather than validated, so bodies AWS rejects were
accepted: a filter naming more than one predicate or none, an And of fewer than
two conjuncts, an unknown predicate, and a tag missing its key or value.
Verified against a live account that each is MalformedXML, and the parser now
answers the same.

Parsing and response building went around XmlParser and XmlBuilder, which
AGENTS.md requires, and the S3 namespace was declared again rather than taken
from AwsNamespaces. The bespoke DocumentBuilderFactory carried its own security
configuration, where XmlParser disables DTDs and external entities for everyone
in one place. The controller branches now delegate to handler methods like their
siblings instead of inlining the work.

A metrics mutation resolved the bucket record before writing it back, while
DeleteBucket took no monitor, so a delete landing in between was undone by the
write and the bucket returned. DeleteBucket now takes the same bucket monitor as
the bucket-scoped mutations, and those re-read the record under it, so a
mutation that lost the race reports NoSuchBucket rather than restoring the
bucket. Its test forces that interleaving rather than racing for it.

* fix(s3): reject malformed metrics bodies and stale writes over a recreated bucket

Re-reading the record for presence was not enough. A bucket deleted and then
recreated under the same name leaves a different record in the store, so a
mutation that had resolved the old one passed the presence check and wrote it
back, replacing the new bucket with the old one's state. The check now compares
the record itself, and a mutation holding a superseded one reports NoSuchBucket.

The parser also accepted bodies whose elements were duplicated or misplaced: a
second Id or Filter, or a stray element under the root, was normalized away
rather than refused, so what was stored was not what was sent. The root now has
to be exactly one Id and at most one Filter, in either order.

The concurrency tests wait on thread state rather than sleeping, so they observe
the interleaving instead of hoping for it: instant on a fast machine and not
flaky on a slow one. DeleteBucket taking the bucket monitor is asserted directly,
since the window it closes — a delete between a mutation's re-read and its write
— is too narrow to force from a test.
2026-08-18 18:46:35 -04:00

342 lines
9.7 KiB
Bash

#!/usr/bin/env bats
# S3 tests
setup() {
load 'test_helper/common-setup'
BUCKET="bats-test-bucket-$(unique_name)"
}
teardown() {
# Clean up all objects in bucket
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 "S3: create bucket" {
run aws_cmd s3api create-bucket --bucket "$BUCKET"
assert_success
}
@test "S3: list buckets" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
run aws_cmd s3api list-buckets
assert_success
found=$(echo "$output" | jq --arg name "$BUCKET" '.Buckets | any(.Name == $name)')
[ "$found" = "true" ]
}
@test "S3: put object" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
local body_file
body_file=$(mktemp)
echo -n "hello-s3-bats" > "$body_file"
run aws_cmd s3api put-object --bucket "$BUCKET" --key "test.txt" --body "$body_file"
assert_success
rm -f "$body_file"
}
@test "S3: get object" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
local body_file get_file
body_file=$(mktemp)
get_file=$(mktemp)
echo -n "hello-s3-bats" > "$body_file"
aws_cmd s3api put-object --bucket "$BUCKET" --key "test.txt" --body "$body_file" >/dev/null
run aws_cmd s3api get-object --bucket "$BUCKET" --key "test.txt" "$get_file"
assert_success
content=$(cat "$get_file")
[ "$content" = "hello-s3-bats" ]
rm -f "$body_file" "$get_file"
}
@test "S3: head object" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
local body_file
body_file=$(mktemp)
echo -n "hello-s3-bats" > "$body_file"
aws_cmd s3api put-object --bucket "$BUCKET" --key "test.txt" --body "$body_file" >/dev/null
run aws_cmd s3api head-object --bucket "$BUCKET" --key "test.txt"
assert_success
length=$(json_get "$output" '.ContentLength')
[ "$length" = "13" ]
rm -f "$body_file"
}
@test "S3: list objects" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
local body_file
body_file=$(mktemp)
echo -n "hello" > "$body_file"
aws_cmd s3api put-object --bucket "$BUCKET" --key "test.txt" --body "$body_file" >/dev/null
run aws_cmd s3api list-objects-v2 --bucket "$BUCKET"
assert_success
found=$(echo "$output" | jq '.Contents | any(.Key == "test.txt")')
[ "$found" = "true" ]
rm -f "$body_file"
}
@test "S3: copy object" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
local body_file
body_file=$(mktemp)
echo -n "hello" > "$body_file"
aws_cmd s3api put-object --bucket "$BUCKET" --key "src.txt" --body "$body_file" >/dev/null
run aws_cmd s3api copy-object --bucket "$BUCKET" --copy-source "$BUCKET/src.txt" --key "dst.txt"
assert_success
rm -f "$body_file"
}
@test "S3: put and get object tagging" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
local body_file
body_file=$(mktemp)
echo -n "hello" > "$body_file"
aws_cmd s3api put-object --bucket "$BUCKET" --key "test.txt" --body "$body_file" >/dev/null
run aws_cmd s3api put-object-tagging \
--bucket "$BUCKET" \
--key "test.txt" \
--tagging 'TagSet=[{Key=env,Value=test}]'
assert_success
run aws_cmd s3api get-object-tagging --bucket "$BUCKET" --key "test.txt"
assert_success
found=$(echo "$output" | jq '.TagSet | any(.Key == "env" and .Value == "test")')
[ "$found" = "true" ]
rm -f "$body_file"
}
@test "S3: delete object" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
local body_file
body_file=$(mktemp)
echo -n "hello" > "$body_file"
aws_cmd s3api put-object --bucket "$BUCKET" --key "test.txt" --body "$body_file" >/dev/null
run aws_cmd s3api delete-object --bucket "$BUCKET" --key "test.txt"
assert_success
rm -f "$body_file"
}
@test "S3: delete bucket" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
run aws_cmd s3api delete-bucket --bucket "$BUCKET"
assert_success
}
# --- S3 Versioning Tests ---
@test "S3: put bucket versioning" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
run aws_cmd s3api put-bucket-versioning \
--bucket "$BUCKET" \
--versioning-configuration Status=Enabled
assert_success
}
@test "S3: versioned objects have version IDs" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
aws_cmd s3api put-bucket-versioning \
--bucket "$BUCKET" \
--versioning-configuration Status=Enabled >/dev/null
local body_file
body_file=$(mktemp)
echo -n "version-one" > "$body_file"
run aws_cmd s3api put-object --bucket "$BUCKET" --key "ver.txt" --body "$body_file"
assert_success
v1=$(json_get "$output" '.VersionId')
[ -n "$v1" ]
echo -n "version-two" > "$body_file"
run aws_cmd s3api put-object --bucket "$BUCKET" --key "ver.txt" --body "$body_file"
assert_success
v2=$(json_get "$output" '.VersionId')
[ -n "$v2" ]
[ "$v1" != "$v2" ]
rm -f "$body_file"
}
# --- S3 Multipart Upload Tests ---
@test "S3: create multipart upload" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
run aws_cmd s3api create-multipart-upload \
--bucket "$BUCKET" \
--key "multipart.bin"
assert_success
upload_id=$(json_get "$output" '.UploadId')
[ -n "$upload_id" ]
# Cleanup: abort the upload
aws_cmd s3api abort-multipart-upload \
--bucket "$BUCKET" \
--key "multipart.bin" \
--upload-id "$upload_id" >/dev/null 2>&1 || true
}
@test "S3: upload part" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
output=$(aws_cmd s3api create-multipart-upload \
--bucket "$BUCKET" \
--key "multipart.bin")
upload_id=$(json_get "$output" '.UploadId')
local part_file
part_file=$(mktemp)
echo -n "part-one-data" > "$part_file"
run aws_cmd s3api upload-part \
--bucket "$BUCKET" \
--key "multipart.bin" \
--upload-id "$upload_id" \
--part-number 1 \
--body "$part_file"
assert_success
etag=$(json_get "$output" '.ETag')
[ -n "$etag" ]
rm -f "$part_file"
aws_cmd s3api abort-multipart-upload \
--bucket "$BUCKET" \
--key "multipart.bin" \
--upload-id "$upload_id" >/dev/null 2>&1 || true
}
@test "S3: complete multipart upload" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
output=$(aws_cmd s3api create-multipart-upload \
--bucket "$BUCKET" \
--key "multipart.bin")
upload_id=$(json_get "$output" '.UploadId')
local part1_file part2_file
part1_file=$(mktemp)
part2_file=$(mktemp)
echo -n "part-one" > "$part1_file"
echo -n "part-two" > "$part2_file"
output=$(aws_cmd s3api upload-part \
--bucket "$BUCKET" \
--key "multipart.bin" \
--upload-id "$upload_id" \
--part-number 1 \
--body "$part1_file")
etag1=$(json_get "$output" '.ETag')
output=$(aws_cmd s3api upload-part \
--bucket "$BUCKET" \
--key "multipart.bin" \
--upload-id "$upload_id" \
--part-number 2 \
--body "$part2_file")
etag2=$(json_get "$output" '.ETag')
local mp_file
mp_file=$(mktemp)
cat > "$mp_file" <<EOF
{
"Parts": [
{ "ETag": $etag1, "PartNumber": 1 },
{ "ETag": $etag2, "PartNumber": 2 }
]
}
EOF
run aws_cmd s3api complete-multipart-upload \
--bucket "$BUCKET" \
--key "multipart.bin" \
--upload-id "$upload_id" \
--multipart-upload "file://$mp_file"
assert_success
rm -f "$part1_file" "$part2_file" "$mp_file"
}
# --- S3 Large File Test ---
@test "S3: put object 25 MB" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
local large_file
large_file=$(mktemp)
dd if=/dev/zero of="$large_file" bs=1048576 count=25 2>/dev/null
run aws_cmd s3api put-object \
--bucket "$BUCKET" \
--key "large-25mb.bin" \
--body "$large_file"
assert_success
run aws_cmd s3api head-object \
--bucket "$BUCKET" \
--key "large-25mb.bin"
assert_success
length=$(json_get "$output" '.ContentLength')
[ "$length" = "26214400" ]
rm -f "$large_file"
}
@test "S3: metrics configurations round-trip and never touch the bucket" {
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null
run aws_cmd s3api put-bucket-metrics-configuration \
--bucket "$BUCKET" --id EntireBucket --metrics-configuration 'Id=EntireBucket'
assert_success
run aws_cmd s3api get-bucket-metrics-configuration --bucket "$BUCKET" --id EntireBucket
assert_success
[ "$(json_get "$output" '.MetricsConfiguration.Id')" = "EntireBucket" ]
run aws_cmd s3api put-bucket-metrics-configuration \
--bucket "$BUCKET" --id Filtered \
--metrics-configuration 'Id=Filtered,Filter={And={Prefix=logs/,Tags=[{Key=env,Value=prod}]}}'
assert_success
run aws_cmd s3api list-bucket-metrics-configurations --bucket "$BUCKET"
assert_success
[ "$(json_get "$output" '.MetricsConfigurationList | length')" = "2" ]
[ "$(json_get "$output" '.MetricsConfigurationList[1].Filter.And.Prefix')" = "logs/" ]
[ "$(json_get "$output" '.MetricsConfigurationList[1].Filter.And.Tags[0].Key')" = "env" ]
# A sub-resource DELETE removes only that configuration; the bucket must survive it.
run aws_cmd s3api delete-bucket-metrics-configuration --bucket "$BUCKET" --id EntireBucket
assert_success
run aws_cmd s3api head-bucket --bucket "$BUCKET"
assert_success
run aws_cmd s3api get-bucket-metrics-configuration --bucket "$BUCKET" --id EntireBucket
assert_failure
assert_output --partial "NoSuchConfiguration"
}