Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DDB Global Tables: add failing test to expose the missing stream on replicas #12622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion tests/aws/services/dynamodb/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
from localstack_snapshot.snapshots.transformer import SortingTransformer

from localstack import config
from localstack.aws.api.dynamodb import PointInTimeRecoverySpecification
from localstack.aws.api.dynamodb import (
PointInTimeRecoverySpecification,
StreamSpecification,
StreamViewType,
)
from localstack.constants import AWS_REGION_US_EAST_1
from localstack.services.dynamodbstreams.dynamodbstreams_api import get_kinesis_stream_name
from localstack.testing.aws.lambda_utils import _await_dynamodb_table_active
Expand All @@ -34,6 +38,8 @@
{"Key": "TestKey", "Value": "true"},
]

WAIT_SEC = 10 if is_aws_cloud() else 1


@pytest.fixture(autouse=True)
def dynamodb_snapshot_transformer(snapshot):
Expand Down Expand Up @@ -1131,6 +1137,68 @@ def test_global_tables_version_2019(
response = dynamodb_ap_south_1.describe_table(TableName=table_name)
assert "Replicas" not in response["Table"]

@markers.aws.validated
@pytest.mark.skipif(
condition=not is_aws_cloud(), reason="Streams do not work on the regional replica"
)
def test_streams_on_global_tables(
self,
aws_client_factory,
dynamodb_wait_for_table_active,
cleanups,
snapshot,
region_name,
secondary_region_name,
):
"""
This test exposes an issue in LocalStack with Global tables and streams. In AWS, each regional replica should
get a separate DynamoDB Stream. This does not happen in LocalStack since DynamoDB Stream does not have any
redirect logic towards the original region (unlike DDB).
"""
region_1_factory = aws_client_factory(region_name=region_name)
region_2_factory = aws_client_factory(region_name=secondary_region_name)
snapshot.add_transformer(snapshot.transform.regex(secondary_region_name, "<region-2>"))
snapshot.add_transformer(
snapshot.transform.jsonpath("$..Streams..StreamLabel", "stream-label")
)

# Create table in the original region
table_name = f"table-{short_uid()}"
snapshot.add_transformer(snapshot.transform.regex(table_name, "<table-name>"))
region_1_factory.dynamodb.create_table(
TableName=table_name,
KeySchema=[
{"AttributeName": "Artist", "KeyType": "HASH"},
{"AttributeName": "SongTitle", "KeyType": "RANGE"},
],
AttributeDefinitions=[
{"AttributeName": "Artist", "AttributeType": "S"},
{"AttributeName": "SongTitle", "AttributeType": "S"},
],
BillingMode="PAY_PER_REQUEST",
StreamSpecification=StreamSpecification(
StreamEnabled=True, StreamViewType=StreamViewType.NEW_AND_OLD_IMAGES
),
)
cleanups.append(lambda: region_1_factory.dynamodb.delete_table(TableName=table_name))
# Note: we might be unable to delete tables that act as source region immediately on AWS
waiter = region_1_factory.dynamodb.get_waiter("table_exists")
waiter.wait(TableName=table_name, WaiterConfig={"Delay": WAIT_SEC, "MaxAttempts": 20})
# Update the Table by adding a replica
region_1_factory.dynamodb.update_table(
TableName=table_name,
ReplicaUpdates=[{"Create": {"RegionName": secondary_region_name}}],
)
cleanups.append(lambda: region_2_factory.dynamodb.delete_table(TableName=table_name))
waiter = region_2_factory.dynamodb.get_waiter("table_exists")
waiter.wait(TableName=table_name, WaiterConfig={"Delay": WAIT_SEC, "MaxAttempts": 20})

us_streams = region_1_factory.dynamodbstreams.list_streams(TableName=table_name)
snapshot.match("region-streams", us_streams)
# FIXME: LS doesn't have a stream on the replica region
eu_streams = region_2_factory.dynamodbstreams.list_streams(TableName=table_name)
snapshot.match("secondary-region-streams", eu_streams)

@markers.aws.only_localstack
def test_global_tables(self, aws_client, ddb_test_table):
dynamodb = aws_client.dynamodb
Expand Down
31 changes: 31 additions & 0 deletions tests/aws/services/dynamodb/test_dynamodb.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -1728,5 +1728,36 @@
}
}
}
},
"tests/aws/services/dynamodb/test_dynamodb.py::TestDynamoDB::test_streams_on_global_tables": {
"recorded-date": "15-05-2025, 13:42:48",
"recorded-content": {
"region-streams": {
"Streams": [
{
"StreamArn": "arn:<partition>:dynamodb:<region>:111111111111:table/<table-name>/stream/<stream-label:1>",
"StreamLabel": "<stream-label:1>",
"TableName": "<table-name>"
}
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"secondary-region-streams": {
"Streams": [
{
"StreamArn": "arn:<partition>:dynamodb:<region-2>:111111111111:table/<table-name>/stream/<stream-label:2>",
"StreamLabel": "<stream-label:2>",
"TableName": "<table-name>"
}
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
}
}
}
}
3 changes: 3 additions & 0 deletions tests/aws/services/dynamodb/test_dynamodb.validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
"tests/aws/services/dynamodb/test_dynamodb.py::TestDynamoDB::test_return_values_on_conditions_check_failure": {
"last_validated_date": "2024-01-03T17:52:19+00:00"
},
"tests/aws/services/dynamodb/test_dynamodb.py::TestDynamoDB::test_streams_on_global_tables": {
"last_validated_date": "2025-05-15T13:42:45+00:00"
},
"tests/aws/services/dynamodb/test_dynamodb.py::TestDynamoDB::test_transact_get_items": {
"last_validated_date": "2023-08-23T14:33:37+00:00"
},
Expand Down
Loading