From c0a4d23cd9f66ac99c433ecfae60749e4083b67e Mon Sep 17 00:00:00 2001 From: George Fu Date: Fri, 13 Nov 2026 18:11:23 +0000 Subject: [PATCH 1/2] Add HTTP mock functional tests --- .../tests/functional/__init__.py | 2 + .../tests/functional/test_accept_header.py | 30 ++ .../tests/functional/__init__.py | 2 + .../functional/test_bearer_token_from_env.py | 42 +++ .../functional/test_bidirectional_stream.py | 86 ++++++ .../tests/functional/__init__.py | 2 + .../functional/test_bearer_token_from_env.py | 62 ++++ .../tests/functional/__init__.py | 2 + .../tests/functional/test_logging.py | 26 ++ .../tests/functional/__init__.py | 2 + .../functional/test_account_id_endpoint.py | 170 +++++++++++ .../tests/functional/test_env_endpoint.py | 55 ++++ .../tests/functional/test_error_lookup.py | 22 ++ .../tests/functional/test_mutability.py | 60 ++++ .../tests/functional/test_pagination.py | 67 +++++ .../functional/test_retry_customizations.py | 170 +++++++++++ .../tests/functional/test_user_agent.py | 44 +++ .../aws-sdk-ec2/tests/functional/__init__.py | 2 + .../tests/functional/test_ec2_query_name.py | 133 +++++++++ .../tests/functional/test_serde.py | 44 +++ .../tests/functional/test_signing.py | 31 ++ .../tests/functional/__init__.py | 2 + .../functional/test_recursion_detection.py | 21 ++ .../tests/functional/test_retry.py | 24 ++ .../tests/functional/test_streams.py | 105 +++++++ .../tests/functional/__init__.py | 2 + .../functional/test_event_stream_headers.py | 34 +++ .../aws-sdk-s3/tests/functional/__init__.py | 2 + .../functional/test_200_error_responses.py | 98 +++++++ .../tests/functional/test_config.py | 121 ++++++++ .../tests/functional/test_content_length.py | 30 ++ .../test_dot_segment_in_uri_label.py | 33 +++ .../tests/functional/test_endpoint.py | 29 ++ .../tests/functional/test_env_endpoint.py | 49 ++++ .../tests/functional/test_expect_continue.py | 89 ++++++ .../functional/test_flexible_checksums.py | 266 ++++++++++++++++++ .../test_flexible_checksums_retry.py | 34 +++ .../functional/test_location_constraint.py | 75 +++++ .../tests/functional/test_mutability.py | 47 ++++ .../tests/functional/test_object_lambda.py | 37 +++ .../functional/test_s3_customizations.py | 82 ++++++ .../tests/functional/test_s3_express.py | 137 +++++++++ .../functional/test_select_object_content.py | 103 +++++++ .../aws-sdk-s3/tests/functional/test_serde.py | 59 ++++ .../aws-sdk-s3/tests/functional/test_ssec.py | 74 +++++ .../tests/functional/test_user_agent.py | 61 ++++ .../tests/functional/test_waiters.py | 31 ++ .../aws-sdk-sqs/tests/functional/__init__.py | 2 + .../tests/functional/test_long_poll.py | 57 ++++ .../functional/test_sqs_customizations.py | 221 +++++++++++++++ .../aws-sdk-sts/tests/functional/__init__.py | 2 + .../functional/test_retry_customizations.py | 65 +++++ .../tests/functional/__init__.py | 2 + .../functional/test_event_stream_headers.py | 31 ++ codegen/gen_client.py | 1 + 55 files changed, 3080 insertions(+) create mode 100644 clients/aws-sdk-api-gateway/tests/functional/__init__.py create mode 100644 clients/aws-sdk-api-gateway/tests/functional/test_accept_header.py create mode 100644 clients/aws-sdk-bedrock-runtime/tests/functional/__init__.py create mode 100644 clients/aws-sdk-bedrock-runtime/tests/functional/test_bearer_token_from_env.py create mode 100644 clients/aws-sdk-bedrock-runtime/tests/functional/test_bidirectional_stream.py create mode 100644 clients/aws-sdk-bedrock/tests/functional/__init__.py create mode 100644 clients/aws-sdk-bedrock/tests/functional/test_bearer_token_from_env.py create mode 100644 clients/aws-sdk-cloudfront/tests/functional/__init__.py create mode 100644 clients/aws-sdk-cloudfront/tests/functional/test_logging.py create mode 100644 clients/aws-sdk-dynamodb/tests/functional/__init__.py create mode 100644 clients/aws-sdk-dynamodb/tests/functional/test_account_id_endpoint.py create mode 100644 clients/aws-sdk-dynamodb/tests/functional/test_env_endpoint.py create mode 100644 clients/aws-sdk-dynamodb/tests/functional/test_error_lookup.py create mode 100644 clients/aws-sdk-dynamodb/tests/functional/test_mutability.py create mode 100644 clients/aws-sdk-dynamodb/tests/functional/test_pagination.py create mode 100644 clients/aws-sdk-dynamodb/tests/functional/test_retry_customizations.py create mode 100644 clients/aws-sdk-dynamodb/tests/functional/test_user_agent.py create mode 100644 clients/aws-sdk-ec2/tests/functional/__init__.py create mode 100644 clients/aws-sdk-ec2/tests/functional/test_ec2_query_name.py create mode 100644 clients/aws-sdk-ec2/tests/functional/test_serde.py create mode 100644 clients/aws-sdk-ec2/tests/functional/test_signing.py create mode 100644 clients/aws-sdk-lambda/tests/functional/__init__.py create mode 100644 clients/aws-sdk-lambda/tests/functional/test_recursion_detection.py create mode 100644 clients/aws-sdk-lambda/tests/functional/test_retry.py create mode 100644 clients/aws-sdk-lambda/tests/functional/test_streams.py create mode 100644 clients/aws-sdk-lex-runtime-v2/tests/functional/__init__.py create mode 100644 clients/aws-sdk-lex-runtime-v2/tests/functional/test_event_stream_headers.py create mode 100644 clients/aws-sdk-s3/tests/functional/__init__.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_200_error_responses.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_config.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_content_length.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_dot_segment_in_uri_label.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_endpoint.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_env_endpoint.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_expect_continue.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_flexible_checksums.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_flexible_checksums_retry.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_location_constraint.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_mutability.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_object_lambda.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_s3_customizations.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_s3_express.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_select_object_content.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_serde.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_ssec.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_user_agent.py create mode 100644 clients/aws-sdk-s3/tests/functional/test_waiters.py create mode 100644 clients/aws-sdk-sqs/tests/functional/__init__.py create mode 100644 clients/aws-sdk-sqs/tests/functional/test_long_poll.py create mode 100644 clients/aws-sdk-sqs/tests/functional/test_sqs_customizations.py create mode 100644 clients/aws-sdk-sts/tests/functional/__init__.py create mode 100644 clients/aws-sdk-sts/tests/functional/test_retry_customizations.py create mode 100644 clients/aws-sdk-transcribe-streaming/tests/functional/__init__.py create mode 100644 clients/aws-sdk-transcribe-streaming/tests/functional/test_event_stream_headers.py diff --git a/clients/aws-sdk-api-gateway/tests/functional/__init__.py b/clients/aws-sdk-api-gateway/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-api-gateway/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-api-gateway/tests/functional/test_accept_header.py b/clients/aws-sdk-api-gateway/tests/functional/test_accept_header.py new file mode 100644 index 000000000..98f948ad0 --- /dev/null +++ b/clients/aws-sdk-api-gateway/tests/functional/test_accept_header.py @@ -0,0 +1,30 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from aws_sdk_api_gateway.client import APIGatewayClient +from aws_sdk_api_gateway.config import APIGatewayConfig +from aws_sdk_api_gateway.models import GetApiKeysInput, GetExportInput +from smithy_test import mock + + +def test_should_add_default_accept_header() -> None: + """The client sends `Accept: application/json` when the caller sets no Accept header.""" + client = APIGatewayClient(APIGatewayConfig.resolve(region="us-west-2")) + mock(client).expect_request(headers={"accept": "application/json"}) + + client.get_api_keys(GetApiKeysInput()) + + +def test_should_not_override_accept_header() -> None: + """An Accept header from the caller reaches the request unchanged.""" + client = APIGatewayClient(APIGatewayConfig.resolve(region="us-west-2")) + mock(client).expect_request(headers={"accept": "application/yaml"}) + + client.get_export( + GetExportInput( + rest_api_id="rest-api-id", + stage_name="stage-name", + export_type="oas30", + accepts="application/yaml", + ) + ) diff --git a/clients/aws-sdk-bedrock-runtime/tests/functional/__init__.py b/clients/aws-sdk-bedrock-runtime/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-bedrock-runtime/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-bedrock-runtime/tests/functional/test_bearer_token_from_env.py b/clients/aws-sdk-bedrock-runtime/tests/functional/test_bearer_token_from_env.py new file mode 100644 index 000000000..01d8ea890 --- /dev/null +++ b/clients/aws-sdk-bedrock-runtime/tests/functional/test_bearer_token_from_env.py @@ -0,0 +1,42 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_bedrock_runtime.client import BedrockRuntimeClient +from aws_sdk_bedrock_runtime.config import BedrockRuntimeConfig +from pytest import MonkeyPatch +from smithy_test import mock + +MOCK_BEARER_TOKEN = "mock-bearer-token" + + +# TODO: resolve a bearer token from AWS_BEARER_TOKEN_BEDROCK +@pytest.mark.skip +def test_when_environment_bearer_token_is_set(monkeypatch: MonkeyPatch) -> None: + """With AWS_BEARER_TOKEN_BEDROCK set, the client authenticates with that token as a bearer token.""" + monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", MOCK_BEARER_TOKEN) + client = BedrockRuntimeClient( + BedrockRuntimeConfig.resolve( + region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG" + ) + ) + mock(client).expect_request( + headers={"authorization": f"Bearer {MOCK_BEARER_TOKEN}"} + ) + + client.list_async_invokes() + + +def test_when_environment_bearer_token_is_not_set(monkeypatch: MonkeyPatch) -> None: + """Without AWS_BEARER_TOKEN_BEDROCK, the client signs the request with SigV4.""" + monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False) + client = BedrockRuntimeClient( + BedrockRuntimeConfig.resolve( + region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG" + ) + ) + mock(client).expect_request( + hostname="/bedrock/", headers={"authorization": "/AWS4-HMAC-SHA256 Credential/"} + ) + + client.list_async_invokes() diff --git a/clients/aws-sdk-bedrock-runtime/tests/functional/test_bidirectional_stream.py b/clients/aws-sdk-bedrock-runtime/tests/functional/test_bidirectional_stream.py new file mode 100644 index 000000000..7d5ee5e32 --- /dev/null +++ b/clients/aws-sdk-bedrock-runtime/tests/functional/test_bidirectional_stream.py @@ -0,0 +1,86 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import datetime +import json +from base64 import b64encode +from io import BytesIO + +from aws_sdk_bedrock_runtime.aio.client import AsyncBedrockRuntimeClient +from aws_sdk_bedrock_runtime.aio.config import AsyncBedrockRuntimeConfig +from aws_sdk_bedrock_runtime.models import ( + BidirectionalInputPayloadPart, + BidirectionalOutputPayloadPart, + InvokeModelWithBidirectionalStreamInputChunk, + InvokeModelWithBidirectionalStreamOperationInput, + InvokeModelWithBidirectionalStreamOutputChunk, +) +from smithy_aws_event_stream.events import Event, EventMessage +from smithy_test.aio import mock_async + +SERVER_CHUNK = bytes([48, 49, 50, 51, 52, 56, 57, 57, 57]) + + +async def test_should_send_input_stream_chunks_as_signed_event_frames() -> None: + """Chunks sent on the input stream go out as signed event frames, and response frames come back as output chunks.""" + server_event = EventMessage( + headers={ + ":event-type": "chunk", + ":content-type": "application/json", + ":message-type": "event", + }, + payload=json.dumps({"bytes": b64encode(SERVER_CHUNK).decode()}).encode(), + ).encode() + + async with AsyncBedrockRuntimeClient( + await AsyncBedrockRuntimeConfig.resolve( + region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG" + ) + ) as client: + async with await mock_async(client) as controller: + controller.expect_request().respond_with( + headers={"content-type": "application/vnd.amazon.eventstream"}, + body=server_event * 10, + ) + + async with await client.invoke_model_with_bidirectional_stream( + InvokeModelWithBidirectionalStreamOperationInput(model_id="bedrock") + ) as stream: + for chunk in ([0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]): + await stream.input_stream.send( + InvokeModelWithBidirectionalStreamInputChunk( + value=BidirectionalInputPayloadPart(bytes_=bytes(chunk)) + ) + ) + await stream.input_stream.close() + _, output_stream = await stream.await_output() + response_chunks = [event async for event in output_stream] + + assert ( + response_chunks + == [ + InvokeModelWithBidirectionalStreamOutputChunk( + value=BidirectionalOutputPayloadPart(bytes_=SERVER_CHUNK) + ) + ] + * 10 + ) + + request_body = controller.requests[0].body + assert isinstance(request_body, bytes) + source = BytesIO(request_body) + signed_events: list[Event] = [] + while (event := Event.decode(source)) is not None: + signed_events.append(event) + sent_bodies: list[object] = [] + for signed in signed_events: + assert isinstance(signed.message.headers[":date"], datetime.datetime) + assert isinstance(signed.message.headers[":chunk-signature"], bytes) + inner = Event.decode(BytesIO(signed.message.payload)) + if inner is not None: + sent_bodies.append(json.loads(inner.message.payload)) + assert sent_bodies == [ + {"bytes": b64encode(bytes([0, 1, 2, 3])).decode()}, + {"bytes": b64encode(bytes([4, 5, 6, 7])).decode()}, + {"bytes": b64encode(bytes([8, 9, 10, 11])).decode()}, + ] diff --git a/clients/aws-sdk-bedrock/tests/functional/__init__.py b/clients/aws-sdk-bedrock/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-bedrock/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-bedrock/tests/functional/test_bearer_token_from_env.py b/clients/aws-sdk-bedrock/tests/functional/test_bearer_token_from_env.py new file mode 100644 index 000000000..e675bd3b2 --- /dev/null +++ b/clients/aws-sdk-bedrock/tests/functional/test_bearer_token_from_env.py @@ -0,0 +1,62 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_bedrock.client import BedrockClient +from aws_sdk_bedrock.config import BedrockConfig +from pytest import MonkeyPatch +from smithy_test import mock + +MOCK_BEARER_TOKEN = "mock-bearer-token" + + +# TODO: resolve a bearer token from AWS_BEARER_TOKEN_BEDROCK +@pytest.mark.skip +def test_when_environment_bearer_token_is_set(monkeypatch: MonkeyPatch) -> None: + """With AWS_BEARER_TOKEN_BEDROCK set, the client authenticates with that token as a bearer token.""" + monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", MOCK_BEARER_TOKEN) + client = BedrockClient( + BedrockConfig.resolve( + region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG" + ) + ) + mock(client).expect_request( + headers={"authorization": f"Bearer {MOCK_BEARER_TOKEN}"} + ) + + client.list_custom_models() + + +# TODO: add the auth_scheme_preference config option +@pytest.mark.skip +def test_can_be_overridden_by_auth_scheme_preference(monkeypatch: MonkeyPatch) -> None: + """An auth scheme preference for SigV4 takes precedence over a bearer token from the environment.""" + monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", MOCK_BEARER_TOKEN) + client = BedrockClient( + BedrockConfig.resolve( # pyright: ignore[reportUnknownArgumentType] + region="us-west-2", + aws_access_key_id="INTEG", + aws_secret_access_key="INTEG", + auth_scheme_preference=["sigv4"], # pyright: ignore[reportCallIssue] + ) + ) + mock(client).expect_request( + hostname="/bedrock/", headers={"authorization": "/AWS4-HMAC-SHA256 Credential/"} + ) + + client.list_custom_models() + + +def test_when_environment_bearer_token_is_not_set(monkeypatch: MonkeyPatch) -> None: + """Without AWS_BEARER_TOKEN_BEDROCK, the client signs the request with SigV4.""" + monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False) + client = BedrockClient( + BedrockConfig.resolve( + region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG" + ) + ) + mock(client).expect_request( + hostname="/bedrock/", headers={"authorization": "/AWS4-HMAC-SHA256 Credential/"} + ) + + client.list_custom_models() diff --git a/clients/aws-sdk-cloudfront/tests/functional/__init__.py b/clients/aws-sdk-cloudfront/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-cloudfront/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-cloudfront/tests/functional/test_logging.py b/clients/aws-sdk-cloudfront/tests/functional/test_logging.py new file mode 100644 index 000000000..cadaef53d --- /dev/null +++ b/clients/aws-sdk-cloudfront/tests/functional/test_logging.py @@ -0,0 +1,26 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import logging + +from aws_sdk_cloudfront.client import CloudFrontClient +from aws_sdk_cloudfront.config import CloudFrontConfig +from aws_sdk_cloudfront.models import ListDistributionsInput +from pytest import LogCaptureFixture +from smithy_test import mock + + +def test_debug_logging_should_have_no_effect_on_request_creation( + caplog: LogCaptureFixture, +) -> None: + """Debug logging leaves the serialized request unchanged.""" + caplog.set_level(logging.DEBUG) + client = CloudFrontClient(CloudFrontConfig.resolve(region="us-west-2")) + mock(client).expect_request( + method="GET", + hostname="cloudfront.amazonaws.com", + scheme="https", + path="/2020-05-31/distribution", + ) + + client.list_distributions(ListDistributionsInput()) diff --git a/clients/aws-sdk-dynamodb/tests/functional/__init__.py b/clients/aws-sdk-dynamodb/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-dynamodb/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-dynamodb/tests/functional/test_account_id_endpoint.py b/clients/aws-sdk-dynamodb/tests/functional/test_account_id_endpoint.py new file mode 100644 index 000000000..b51946b3a --- /dev/null +++ b/clients/aws-sdk-dynamodb/tests/functional/test_account_id_endpoint.py @@ -0,0 +1,170 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_dynamodb.client import DynamoDBClient +from aws_sdk_dynamodb.config import DynamoDBConfig +from aws_sdk_dynamodb.models import ListTablesInput +from smithy_aws_core.identity.components import AWSCredentialsIdentity +from smithy_aws_core.identity.static import StaticCredentialsResolver +from smithy_core.exceptions import EndpointResolutionError +from smithy_test import mock + +REGION = "us-west-2" + + +# TODO: let the HTTP mock keep the account ID of the credentials it replaces +@pytest.mark.skip +def test_should_default_to_resolving_endpoint_with_account_id_when_it_is_available_in_the_client_credentials( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """When the credentials carry an account ID, the endpoint hostname starts with that account ID.""" + monkeypatch.delenv("AWS_ACCOUNT_ID_ENDPOINT_MODE", raising=False) + client = DynamoDBClient( + DynamoDBConfig.resolve( + region=REGION, + aws_credentials_identity_resolver=StaticCredentialsResolver( + identity=AWSCredentialsIdentity( + access_key_id="INTEG_TEST", + secret_access_key="INTEG_TEST", + account_id="123456789012", + ) + ), + ) + ) + controller = mock(client) + controller.expect_request(hostname="/123456789012.ddb.us-west-2.amazonaws.com/") + + client.list_tables(ListTablesInput()) + + assert len(controller.requests) == 1 + + +# TODO: let the HTTP mock keep the account ID of the credentials it replaces +@pytest.mark.skip +@pytest.mark.parametrize( + ("expected_hostname_prefix", "mode"), + [ + ("dynamodb", "disabled"), + ("123456789012.ddb", "preferred"), + ("123456789012.ddb", "required"), + ], +) +def test_should_match_prefix_for_when_account_id_endpoint_mode_is_set_in_config( + monkeypatch: pytest.MonkeyPatch, expected_hostname_prefix: str, mode: str +) -> None: + """The account_id_endpoint_mode config value decides whether the hostname carries the account ID prefix.""" + monkeypatch.delenv("AWS_ACCOUNT_ID_ENDPOINT_MODE", raising=False) + client = DynamoDBClient( + DynamoDBConfig.resolve( + region=REGION, + aws_credentials_identity_resolver=StaticCredentialsResolver( + identity=AWSCredentialsIdentity( + access_key_id="INTEG_TEST", + secret_access_key="INTEG_TEST", + account_id="123456789012", + ) + ), + account_id_endpoint_mode=mode, + ) + ) + controller = mock(client) + controller.expect_request( + hostname=f"{expected_hostname_prefix}.{REGION}.amazonaws.com" + ) + + client.list_tables(ListTablesInput()) + + assert len(controller.requests) == 1 + + +# TODO: let the HTTP mock keep the account ID of the credentials it replaces +@pytest.mark.skip +@pytest.mark.parametrize( + ("expected_hostname_prefix", "mode"), + [ + ("dynamodb", "disabled"), + ("123456789012.ddb", "preferred"), + ("123456789012.ddb", "required"), + ], +) +def test_should_match_prefix_for_when_account_id_endpoint_mode_is_set_in_env( + monkeypatch: pytest.MonkeyPatch, expected_hostname_prefix: str, mode: str +) -> None: + """The AWS_ACCOUNT_ID_ENDPOINT_MODE environment variable decides whether the hostname carries the account ID prefix.""" + monkeypatch.setenv("AWS_ACCOUNT_ID_ENDPOINT_MODE", mode) + client = DynamoDBClient( + DynamoDBConfig.resolve( + region=REGION, + aws_credentials_identity_resolver=StaticCredentialsResolver( + identity=AWSCredentialsIdentity( + access_key_id="INTEG_TEST", + secret_access_key="INTEG_TEST", + account_id="123456789012", + ) + ), + ) + ) + controller = mock(client) + controller.expect_request( + hostname=f"{expected_hostname_prefix}.{REGION}.amazonaws.com" + ) + + client.list_tables(ListTablesInput()) + + assert len(controller.requests) == 1 + + +def test_should_omit_account_id_prefix_when_credentials_lack_account_id( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """When the credentials lack an account ID, the hostname lacks the account ID prefix.""" + monkeypatch.delenv("AWS_ACCOUNT_ID_ENDPOINT_MODE", raising=False) + client = DynamoDBClient( + DynamoDBConfig.resolve( + region=REGION, + aws_access_key_id="INTEG_TEST", + aws_secret_access_key="INTEG_TEST", + ) + ) + controller = mock(client) + controller.expect_request(hostname="/dynamodb.us-west-2.amazonaws.com/") + + client.list_tables(ListTablesInput()) + + assert len(controller.requests) == 1 + + +def test_will_fail_if_required_by_env(monkeypatch: pytest.MonkeyPatch) -> None: + """Endpoint resolution fails when the environment requires an account ID and the credentials lack one.""" + monkeypatch.setenv("AWS_ACCOUNT_ID_ENDPOINT_MODE", "required") + client = DynamoDBClient( + DynamoDBConfig.resolve( + region=REGION, + aws_access_key_id="INTEG_TEST", + aws_secret_access_key="INTEG_TEST", + ) + ) + controller = mock(client) + controller.expect_request() + + with pytest.raises(EndpointResolutionError): + client.list_tables(ListTablesInput()) + + +def test_will_fail_if_required_by_config(monkeypatch: pytest.MonkeyPatch) -> None: + """Endpoint resolution fails when the config requires an account ID and the credentials lack one.""" + monkeypatch.delenv("AWS_ACCOUNT_ID_ENDPOINT_MODE", raising=False) + client = DynamoDBClient( + DynamoDBConfig.resolve( + region=REGION, + aws_access_key_id="INTEG_TEST", + aws_secret_access_key="INTEG_TEST", + account_id_endpoint_mode="required", + ) + ) + controller = mock(client) + controller.expect_request() + + with pytest.raises(EndpointResolutionError): + client.list_tables(ListTablesInput()) diff --git a/clients/aws-sdk-dynamodb/tests/functional/test_env_endpoint.py b/clients/aws-sdk-dynamodb/tests/functional/test_env_endpoint.py new file mode 100644 index 000000000..7d196c3be --- /dev/null +++ b/clients/aws-sdk-dynamodb/tests/functional/test_env_endpoint.py @@ -0,0 +1,55 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_dynamodb.client import DynamoDBClient +from aws_sdk_dynamodb.config import DynamoDBConfig +from aws_sdk_dynamodb.models import ListTablesInput +from smithy_test import mock + + +def test_should_override_a_client_endpoint_if_none_is_set_on_the_client( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """AWS_ENDPOINT_URL sets the endpoint of a client configured without one.""" + monkeypatch.delenv("AWS_ENDPOINT_URL_DYNAMODB", raising=False) + monkeypatch.setenv("AWS_ENDPOINT_URL", "https://localhost/1") + client = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request(hostname="/localhost/", path="/\\/1/") + + client.list_tables(ListTablesInput()) + + assert len(controller.requests) == 1 + + +def test_should_defer_to_the_clients_manually_set_endpoint( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """An endpoint set on the client takes precedence over AWS_ENDPOINT_URL.""" + monkeypatch.delenv("AWS_ENDPOINT_URL_DYNAMODB", raising=False) + monkeypatch.setenv("AWS_ENDPOINT_URL", "https://localhost/1") + client = DynamoDBClient( + DynamoDBConfig.resolve(region="us-west-2", endpoint_uri="https://localhost/2") + ) + controller = mock(client) + controller.expect_request(hostname="/localhost/", path="/\\/2/") + + client.list_tables(ListTablesInput()) + + assert len(controller.requests) == 1 + + +def test_should_allow_a_specific_service_id_to_be_chosen_in_the_env_variable_name( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """AWS_ENDPOINT_URL_DYNAMODB sets the endpoint for the DynamoDB client.""" + monkeypatch.delenv("AWS_ENDPOINT_URL", raising=False) + monkeypatch.setenv("AWS_ENDPOINT_URL_DYNAMODB", "https://localhost/1") + client = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request(hostname="/localhost/", path="/\\/1/") + + client.list_tables(ListTablesInput()) + + assert len(controller.requests) == 1 diff --git a/clients/aws-sdk-dynamodb/tests/functional/test_error_lookup.py b/clients/aws-sdk-dynamodb/tests/functional/test_error_lookup.py new file mode 100644 index 000000000..0ed1a2bca --- /dev/null +++ b/clients/aws-sdk-dynamodb/tests/functional/test_error_lookup.py @@ -0,0 +1,22 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from aws_sdk_dynamodb.client import DynamoDBClient +from aws_sdk_dynamodb.config import DynamoDBConfig +from aws_sdk_dynamodb.models import DescribeTableInput, ResourceNotFoundException +from pytest import raises +from smithy_test import mock + + +def test_looks_up_errors_by_shape_name_rather_than_the_apparent_shape_id() -> None: + """An error resolves to its modeled exception by shape name, ignoring the namespace in `__type`.""" + client = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2")) + mock(client).expect_request().respond_with( + status_code=400, + headers={"content-type": "application/x-amz-json-1.0"}, + body='{"__type": "com.amazon.coral.service#ResourceNotFoundException",' + ' "message": "Requested resource not found"}', + ) + + with raises(ResourceNotFoundException, match="Requested resource not found"): + client.describe_table(DescribeTableInput(table_name="my-table")) diff --git a/clients/aws-sdk-dynamodb/tests/functional/test_mutability.py b/clients/aws-sdk-dynamodb/tests/functional/test_mutability.py new file mode 100644 index 000000000..b580cd878 --- /dev/null +++ b/clients/aws-sdk-dynamodb/tests/functional/test_mutability.py @@ -0,0 +1,60 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import json + +from aws_sdk_dynamodb.client import DynamoDBClient +from aws_sdk_dynamodb.config import DynamoDBConfig +from aws_sdk_dynamodb.models import AttributeValueS, ScanInput +from smithy_test import mock + + +def test_should_allow_reusing_an_input_object() -> None: + """One input object sent in two calls produces two identical requests and stays unmodified.""" + client = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + hostname="/dynamodb/", + body=lambda body: ( + json.loads(body) + == { + "ExpressionAttributeValues": {":id": {"S": "1"}}, + "FilterExpression": "id = :id", + "TableName": "test", + } + ), + ) + for start_key in ["abc", "def", "ghi"]: + controller.expect_request( + hostname="/dynamodb/", + body=lambda body, start_key=start_key: ( + json.loads(body) + == { + "ExpressionAttributeValues": {":id": {"S": "1"}}, + "FilterExpression": "id = :id", + "TableName": "test", + "ExclusiveStartKey": {"id": {"S": start_key}}, + } + ), + ) + params = ScanInput( + table_name="test", + filter_expression="id = :id", + expression_attribute_values={":id": AttributeValueS("1")}, + ) + + client.scan(params) + params.exclusive_start_key = {"id": AttributeValueS("abc")} + client.scan(params) + params.exclusive_start_key = {"id": AttributeValueS("def")} + client.scan(params) + params.exclusive_start_key = {"id": AttributeValueS("ghi")} + client.scan(params) + + assert len(controller.requests) == 4 + assert params == ScanInput( + table_name="test", + filter_expression="id = :id", + expression_attribute_values={":id": AttributeValueS("1")}, + exclusive_start_key={"id": AttributeValueS("ghi")}, + ) diff --git a/clients/aws-sdk-dynamodb/tests/functional/test_pagination.py b/clients/aws-sdk-dynamodb/tests/functional/test_pagination.py new file mode 100644 index 000000000..aa168a75c --- /dev/null +++ b/clients/aws-sdk-dynamodb/tests/functional/test_pagination.py @@ -0,0 +1,67 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import json + +import pytest +from aws_sdk_dynamodb.client import DynamoDBClient +from aws_sdk_dynamodb.config import DynamoDBConfig +from aws_sdk_dynamodb.models import ScanInput +from smithy_test import mock + + +# TODO: generate paginators +@pytest.mark.skip +def test_makes_sequential_requests_using_a_pagination_token() -> None: + # Imported here so the missing paginator fails this test, not collection. + """The paginator sends the LastEvaluatedKey of each page as the ExclusiveStartKey of the next request.""" + from aws_sdk_dynamodb.paginators import ( # pyright: ignore[reportMissingImports] + paginate_scan, # pyright: ignore[reportUnknownVariableType] + ) + + client = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2")) + controller = mock(client) + + def is_first_page(body: bytes) -> None: + assert b"TableName" in body + assert b"ExclusiveStartKey" not in body + + controller.expect_request(hostname="/dynamodb/", body=is_first_page).respond_with( + status_code=200, + headers={}, + body=json.dumps( + { + "Items": [ + {"id": {"S": "1"}, "name": {"S": "Item 1"}}, + {"id": {"S": "2"}, "name": {"S": "Item 2"}}, + ], + "Count": 2, + "ScannedCount": 2, + "LastEvaluatedKey": {"id": {"S": "2"}}, + } + ), + ) + controller.expect_request( + hostname="/dynamodb/", body="/ExclusiveStartKey/" + ).respond_with( + status_code=200, + headers={}, + body=json.dumps( + { + "Items": [ + {"id": {"S": "3"}, "name": {"S": "Item 3"}}, + {"id": {"S": "4"}, "name": {"S": "Item 4"}}, + ], + "Count": 2, + "ScannedCount": 2, + } + ), + ) + request_params = ScanInput(table_name="test") + + pages = 0 + for _ in paginate_scan(client, request_params): # pyright: ignore[reportUnknownVariableType] + pages += 1 + + assert pages == 2 + assert len(controller.requests) == 2 diff --git a/clients/aws-sdk-dynamodb/tests/functional/test_retry_customizations.py b/clients/aws-sdk-dynamodb/tests/functional/test_retry_customizations.py new file mode 100644 index 000000000..c6fa17b6a --- /dev/null +++ b/clients/aws-sdk-dynamodb/tests/functional/test_retry_customizations.py @@ -0,0 +1,170 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from typing import Any + +import pytest +from aws_sdk_dynamodb.client import DynamoDBClient +from aws_sdk_dynamodb.config import DynamoDBConfig +from aws_sdk_dynamodb.models import ListTablesInput +from smithy_core.client import RequestPipeline +from smithy_core.exceptions import CallError +from smithy_core.retries import ( + AdaptiveRetryStrategy, + ExponentialRetryBackoffStrategy, + StandardRetryStrategy, +) +from smithy_test import mock + +# CAUTION: a 408 because the SDK doesn't retry a 5xx, and a 429 makes the adaptive +# rate limiter wait for real. +TIMEOUT_ERROR = ( + '{"__type": "com.amazonaws.dynamodb#RequestTimeout", "message": "Oh No"}' +) +JSON_HEADERS = {"content-type": "application/x-amz-json-1.0"} + + +def test_defaults_to_4_attempts_with_25ms_delay_base_standard_retry_strategy( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Backoff delays are asserted through the strategy, so the waits are skipped. + """DynamoDB defaults to 4 attempts with a 25 ms backoff base under the standard retry strategy.""" + monkeypatch.setattr(RequestPipeline, "_sleep", _no_sleep) + client = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request().respond_with( + status_code=408, headers=JSON_HEADERS, body=TIMEOUT_ERROR + ) + + with pytest.raises(CallError): + client.list_tables(ListTablesInput()) + + retry_strategy = client._config.retry_strategy + assert type(retry_strategy) is StandardRetryStrategy + assert retry_strategy.max_attempts == 4 + assert isinstance(retry_strategy.backoff_strategy, ExponentialRetryBackoffStrategy) + assert retry_strategy.backoff_strategy._backoff_scale_value == 0.025 + assert len(controller.requests) == 4 + + +def test_preserves_4_attempts_and_25ms_delay_base_if_adaptive_mode_is_set( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Backoff delays are asserted through the strategy, so the waits are skipped. + """Adaptive retry mode keeps the DynamoDB defaults of 4 attempts and a 25 ms backoff base.""" + monkeypatch.setattr(RequestPipeline, "_sleep", _no_sleep) + client = DynamoDBClient( + DynamoDBConfig.resolve(region="us-west-2", retry_mode="adaptive") + ) + controller = mock(client) + controller.expect_request().respond_with( + status_code=408, headers=JSON_HEADERS, body=TIMEOUT_ERROR + ) + + with pytest.raises(CallError): + client.list_tables(ListTablesInput()) + + retry_strategy = client._config.retry_strategy + assert isinstance(retry_strategy, AdaptiveRetryStrategy) + assert retry_strategy.max_attempts == 4 + assert isinstance(retry_strategy.backoff_strategy, ExponentialRetryBackoffStrategy) + assert retry_strategy.backoff_strategy._backoff_scale_value == 0.025 + assert len(controller.requests) == 4 + + +def test_preserves_standard_mode_and_25ms_delay_base_if_10_attempts_are_set( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Backoff delays are asserted through the strategy, so the waits are skipped. + """Setting 10 max attempts keeps standard mode and the 25 ms backoff base.""" + monkeypatch.setattr(RequestPipeline, "_sleep", _no_sleep) + client = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2", max_attempts=10)) + controller = mock(client) + controller.expect_request().respond_with( + status_code=408, headers=JSON_HEADERS, body=TIMEOUT_ERROR + ) + + with pytest.raises(CallError): + client.list_tables(ListTablesInput()) + + retry_strategy = client._config.retry_strategy + assert type(retry_strategy) is StandardRetryStrategy + assert retry_strategy.max_attempts == 10 + assert isinstance(retry_strategy.backoff_strategy, ExponentialRetryBackoffStrategy) + assert retry_strategy.backoff_strategy._backoff_scale_value == 0.025 + assert len(controller.requests) == 10 + + +def test_providing_a_retry_strategy_object_overrides_the_attempt_count_and_delay_base( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Backoff delays are asserted through the strategy, so the waits are skipped. + """A retry strategy object from the caller replaces the DynamoDB attempt count and backoff base.""" + monkeypatch.setattr(RequestPipeline, "_sleep", _no_sleep) + custom = StandardRetryStrategy( + max_attempts=7, + backoff_strategy=ExponentialRetryBackoffStrategy(backoff_scale_value=0.1), + ) + client = DynamoDBClient( + DynamoDBConfig.resolve(region="us-west-2", retry_strategy=custom) + ) + controller = mock(client) + controller.expect_request().respond_with( + status_code=408, headers=JSON_HEADERS, body=TIMEOUT_ERROR + ) + + with pytest.raises(CallError): + client.list_tables(ListTablesInput()) + + retry_strategy = client._config.retry_strategy + assert retry_strategy is custom + assert retry_strategy.max_attempts == 7 + assert isinstance(retry_strategy.backoff_strategy, ExponentialRetryBackoffStrategy) + assert retry_strategy.backoff_strategy._backoff_scale_value == 0.1 + assert len(controller.requests) == 7 + + +def test_resolves_precedence_code_env_ddb_default( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Backoff delays are asserted through the strategy, so the waits are skipped. + """Max attempts from code take precedence over the environment, which takes precedence over the DynamoDB default.""" + monkeypatch.setattr(RequestPipeline, "_sleep", _no_sleep) + + monkeypatch.setenv("AWS_MAX_ATTEMPTS", "9") + client_with_env = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2")) + controller_with_env = mock(client_with_env) + controller_with_env.expect_request().respond_with( + status_code=408, headers=JSON_HEADERS, body=TIMEOUT_ERROR + ) + with pytest.raises(CallError): + client_with_env.list_tables(ListTablesInput()) + assert client_with_env._config.retry_strategy.max_attempts == 9 + assert len(controller_with_env.requests) == 9 + + client_with_code = DynamoDBClient( + DynamoDBConfig.resolve(region="us-west-2", max_attempts=5) + ) + controller_with_code = mock(client_with_code) + controller_with_code.expect_request().respond_with( + status_code=408, headers=JSON_HEADERS, body=TIMEOUT_ERROR + ) + with pytest.raises(CallError): + client_with_code.list_tables(ListTablesInput()) + assert client_with_code._config.retry_strategy.max_attempts == 5 + assert len(controller_with_code.requests) == 5 + + monkeypatch.delenv("AWS_MAX_ATTEMPTS") + client_default = DynamoDBClient(DynamoDBConfig.resolve(region="us-west-2")) + controller_default = mock(client_default) + controller_default.expect_request().respond_with( + status_code=408, headers=JSON_HEADERS, body=TIMEOUT_ERROR + ) + with pytest.raises(CallError): + client_default.list_tables(ListTablesInput()) + assert client_default._config.retry_strategy.max_attempts == 4 + assert len(controller_default.requests) == 4 + + +def _no_sleep(self: RequestPipeline[Any, Any], delay: float) -> None: + pass diff --git a/clients/aws-sdk-dynamodb/tests/functional/test_user_agent.py b/clients/aws-sdk-dynamodb/tests/functional/test_user_agent.py new file mode 100644 index 000000000..0c2501fa1 --- /dev/null +++ b/clients/aws-sdk-dynamodb/tests/functional/test_user_agent.py @@ -0,0 +1,44 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import re + +import pytest +from aws_sdk_dynamodb.client import DynamoDBClient +from aws_sdk_dynamodb.config import DynamoDBConfig +from aws_sdk_dynamodb.models import AttributeValueS, GetItemInput +from smithy_aws_core.identity.components import AWSCredentialsIdentity +from smithy_aws_core.identity.static import StaticCredentialsResolver +from smithy_test import mock + + +# TODO: add the O and R user agent feature IDs, and let the HTTP mock keep the account ID of the credentials it replaces +@pytest.mark.skip +def test_should_detect_account_id_and_account_id_mode() -> None: + """The user agent lists the feature IDs for the resolved account ID and the account ID endpoint mode.""" + client = DynamoDBClient( + DynamoDBConfig.resolve( + region="us-west-2", + account_id_endpoint_mode="required", + aws_credentials_identity_resolver=StaticCredentialsResolver( + identity=AWSCredentialsIdentity( + access_key_id="", secret_access_key="", account_id="123456789012" + ) + ), + ) + ) + controller = mock(client) + + def has_features(user_agent: str) -> None: + match = re.search(r"(?:^| )m/(\S*)", user_agent) + assert match is not None + features = match.group(1).split(",") + assert "E" in features # RETRY_MODE_STANDARD + assert "O" in features # ACCOUNT_ID_ENDPOINT + assert "R" in features # ACCOUNT_ID_MODE_REQUIRED + + controller.expect_request(headers={"user-agent": has_features}) + + client.get_item(GetItemInput(table_name="table", key={"id": AttributeValueS("1")})) + + assert len(controller.requests) == 1 diff --git a/clients/aws-sdk-ec2/tests/functional/__init__.py b/clients/aws-sdk-ec2/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-ec2/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-ec2/tests/functional/test_ec2_query_name.py b/clients/aws-sdk-ec2/tests/functional/test_ec2_query_name.py new file mode 100644 index 000000000..f2305e97a --- /dev/null +++ b/clients/aws-sdk-ec2/tests/functional/test_ec2_query_name.py @@ -0,0 +1,133 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import re + +from aws_sdk_ec2.client import EC2Client +from aws_sdk_ec2.config import EC2Config +from aws_sdk_ec2.models import ( + InstanceIpv6Address, + InstanceNetworkInterfaceSpecification, + PrivateIpAddressSpecification, + RequestSpotFleetInput, + RequestSpotInstancesInput, + RequestSpotLaunchSpecification, + RunInstancesInput, + SpotFleetLaunchSpecification, + SpotFleetRequestConfigData, +) +from smithy_test import mock + + +def test_should_serialize_based_on_ec2_query_name_if_present_run_instances() -> None: + """RunInstances members serialize under their ec2QueryName trait value.""" + client = EC2Client(EC2Config.resolve(region="us-east-1")) + + def has_params(body: bytes) -> None: + params = sorted(body.decode().split("&")) + assert params[0] == "Action=RunInstances" + assert re.fullmatch(r"ClientToken=(.*?)", params[1]) + assert params[2:] == [ + "DryRun=true", + "MaxCount=1", + "MinCount=0", + "NetworkInterface.1.Ipv6Addresses.1.Ipv6Address=xx", + "NetworkInterface.1.PrivateIpAddresses.1.PrivateIpAddress=xx", + "Version=2016-11-15", + ] + + mock(client).expect_request(body=has_params) + + client.run_instances( + RunInstancesInput( + dry_run=True, + max_count=1, + min_count=0, + network_interfaces=[ + InstanceNetworkInterfaceSpecification( + private_ip_addresses=[ + PrivateIpAddressSpecification(private_ip_address="xx") + ], + ipv6_addresses=[InstanceIpv6Address(ipv6_address="xx")], + ) + ], + ) + ) + + +def test_should_serialize_based_on_ec2_query_name_if_present_request_spot_instances() -> ( + None +): + """RequestSpotInstances members serialize under their ec2QueryName trait value.""" + client = EC2Client(EC2Config.resolve(region="us-east-1")) + + def has_params(body: bytes) -> None: + assert sorted(body.decode().split("&")) == [ + "Action=RequestSpotInstances", + "DryRun=true", + "LaunchSpecification.NetworkInterface.1.Ipv6Addresses.1.Ipv6Address=xx", + "LaunchSpecification.NetworkInterface.1.PrivateIpAddresses.1.PrivateIpAddress=xx", + "Version=2016-11-15", + ] + + mock(client).expect_request(body=has_params) + + client.request_spot_instances( + RequestSpotInstancesInput( + dry_run=True, + launch_specification=RequestSpotLaunchSpecification( + network_interfaces=[ + InstanceNetworkInterfaceSpecification( + private_ip_addresses=[ + PrivateIpAddressSpecification(private_ip_address="xx") + ], + ipv6_addresses=[InstanceIpv6Address(ipv6_address="xx")], + ) + ] + ), + ) + ) + + +def test_should_serialize_based_on_ec2_query_name_if_present_request_spot_fleet() -> ( + None +): + """RequestSpotFleet members serialize under their ec2QueryName trait value.""" + client = EC2Client(EC2Config.resolve(region="us-east-1")) + + def has_params(body: bytes) -> None: + assert sorted(body.decode().split("&")) == [ + "Action=RequestSpotFleet", + "DryRun=true", + "SpotFleetRequestConfig.IamFleetRole=", + "SpotFleetRequestConfig.LaunchSpecifications.1.NetworkInterfaceSet.1.Ipv6Addresses.1.Ipv6Address=xx", + "SpotFleetRequestConfig.LaunchSpecifications.1.NetworkInterfaceSet.1.PrivateIpAddresses.1.PrivateIpAddress=xx", + "SpotFleetRequestConfig.TargetCapacity=0", + "Version=2016-11-15", + ] + + mock(client).expect_request(body=has_params) + + client.request_spot_fleet( + RequestSpotFleetInput( + dry_run=True, + spot_fleet_request_config=SpotFleetRequestConfigData( + iam_fleet_role="", + target_capacity=0, + launch_specifications=[ + SpotFleetLaunchSpecification( + network_interfaces=[ + InstanceNetworkInterfaceSpecification( + private_ip_addresses=[ + PrivateIpAddressSpecification( + private_ip_address="xx" + ) + ], + ipv6_addresses=[InstanceIpv6Address(ipv6_address="xx")], + ) + ] + ) + ], + ), + ) + ) diff --git a/clients/aws-sdk-ec2/tests/functional/test_serde.py b/clients/aws-sdk-ec2/tests/functional/test_serde.py new file mode 100644 index 000000000..f3eaa590e --- /dev/null +++ b/clients/aws-sdk-ec2/tests/functional/test_serde.py @@ -0,0 +1,44 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from urllib.parse import parse_qsl + +from aws_sdk_ec2.client import EC2Client +from aws_sdk_ec2.config import EC2Config +from aws_sdk_ec2.models import ConnectionLogOptions, CreateClientVpnEndpointInput +from smithy_test import mock + + +def test_should_serialize_query() -> None: + """CreateClientVpnEndpoint serializes as a form-encoded EC2 query body posted to the root path.""" + client = EC2Client(EC2Config.resolve(region="us-west-2")) + + def has_params(body: bytes) -> None: + params = dict(parse_qsl(body.decode(), keep_blank_values=True)) + assert ( + params.items() + >= { + "Action": "CreateClientVpnEndpoint", + "Version": "2016-11-15", + "ClientCidrBlock": "ClientCidrBlock", + "ServerCertificateArn": "ServerCertificateArn", + }.items() + ) + + mock(client).expect_request( + method="POST", + hostname="ec2.us-west-2.amazonaws.com", + headers={"content-type": "application/x-www-form-urlencoded"}, + body=has_params, + scheme="https", + path="/", + ) + + client.create_client_vpn_endpoint( + CreateClientVpnEndpointInput( + client_cidr_block="ClientCidrBlock", + server_certificate_arn="ServerCertificateArn", + authentication_options=[], + connection_log_options=ConnectionLogOptions(), + ) + ) diff --git a/clients/aws-sdk-ec2/tests/functional/test_signing.py b/clients/aws-sdk-ec2/tests/functional/test_signing.py new file mode 100644 index 000000000..a5b8600a0 --- /dev/null +++ b/clients/aws-sdk-ec2/tests/functional/test_signing.py @@ -0,0 +1,31 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_ec2.client import EC2Client +from aws_sdk_ec2.config import EC2Config +from aws_sdk_ec2.models import ConnectionLogOptions, CreateClientVpnEndpointInput +from smithy_test import mock + + +# TODO: decide whether non-S3 SigV4 requests send x-amz-content-sha256 +@pytest.mark.skip +def test_sign_requests_in_the_header() -> None: + """The client signs EC2 requests with SigV4 headers that include the payload hash.""" + client = EC2Client(EC2Config.resolve(region="us-west-2")) + mock(client).expect_request( + headers={ + "x-amz-date": r"/\d{8}T\d+Z/", + "x-amz-content-sha256": r"/\w{30}\w+/", + "authorization": r"/AWS4-HMAC-SHA256 Credential=(.+)\/(.+)\/(.+)\/(.+)\/aws4_request, SignedHeaders=(.+), Signature=(.+)/", + } + ) + + client.create_client_vpn_endpoint( + CreateClientVpnEndpointInput( + client_cidr_block="ClientCidrBlock", + server_certificate_arn="ServerCertificateArn", + authentication_options=[], + connection_log_options=ConnectionLogOptions(), + ) + ) diff --git a/clients/aws-sdk-lambda/tests/functional/__init__.py b/clients/aws-sdk-lambda/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-lambda/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-lambda/tests/functional/test_recursion_detection.py b/clients/aws-sdk-lambda/tests/functional/test_recursion_detection.py new file mode 100644 index 000000000..bcd0196a8 --- /dev/null +++ b/clients/aws-sdk-lambda/tests/functional/test_recursion_detection.py @@ -0,0 +1,21 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_lambda.client import LambdaClient +from aws_sdk_lambda.config import LambdaConfig +from aws_sdk_lambda.models import InvokeInput +from pytest import MonkeyPatch +from smithy_test import mock + + +# TODO: add recursion detection from _X_AMZN_TRACE_ID +@pytest.mark.skip +def test_should_create_recursion_detection_headers(monkeypatch: MonkeyPatch) -> None: + """Inside a Lambda function, the client copies _X_AMZN_TRACE_ID into the X-Amzn-Trace-Id header.""" + monkeypatch.setenv("AWS_LAMBDA_FUNCTION_NAME", "MyLambdaFunction") + monkeypatch.setenv("_X_AMZN_TRACE_ID", "trace-1234") + client = LambdaClient(LambdaConfig.resolve(region="us-west-2")) + mock(client).expect_request(headers={"X-Amzn-Trace-Id": "/^trace-1234$/"}) + + client.invoke(InvokeInput(function_name="my-function")) diff --git a/clients/aws-sdk-lambda/tests/functional/test_retry.py b/clients/aws-sdk-lambda/tests/functional/test_retry.py new file mode 100644 index 000000000..ac64c8df2 --- /dev/null +++ b/clients/aws-sdk-lambda/tests/functional/test_retry.py @@ -0,0 +1,24 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_lambda.client import LambdaClient +from aws_sdk_lambda.config import LambdaConfig +from aws_sdk_lambda.models import InvokeInput +from smithy_test import mock + + +# TODO: send the amz-sdk-invocation-id and amz-sdk-request headers +@pytest.mark.skip +def test_should_set_retry_headers() -> None: + """Requests carry the amz-sdk-invocation-id and amz-sdk-request retry headers.""" + client = LambdaClient(LambdaConfig.resolve(region="us-west-2")) + mock(client).expect_request( + hostname="lambda.us-west-2.amazonaws.com", + headers={ + "amz-sdk-invocation-id": r"/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/", + "amz-sdk-request": "attempt=1; max=3", + }, + ) + + client.invoke(InvokeInput(function_name="my-function")) diff --git a/clients/aws-sdk-lambda/tests/functional/test_streams.py b/clients/aws-sdk-lambda/tests/functional/test_streams.py new file mode 100644 index 000000000..39943a837 --- /dev/null +++ b/clients/aws-sdk-lambda/tests/functional/test_streams.py @@ -0,0 +1,105 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import json +from io import BytesIO + +import pytest +from aws_sdk_lambda.client import LambdaClient +from aws_sdk_lambda.config import LambdaConfig +from aws_sdk_lambda.models import InvokeInput +from smithy_test import mock + +PAYLOAD = json.dumps({"hello": "world"}, separators=(",", ":")) + + +# TODO: accept str for blob members +@pytest.mark.skip +def test_should_be_uniform_between_str_and_bytes_payloads() -> None: + """A str payload and its bytes encoding produce the same request.""" + client = LambdaClient(LambdaConfig.resolve(region="us-west-2")) + mock(client).expect_request( + method="POST", + hostname="lambda.us-west-2.amazonaws.com", + headers={"content-type": "application/octet-stream", "content-length": "17"}, + body='{"hello":"world"}', + scheme="https", + path="/2015-03-31/functions/echo/invocations", + ).times(2) + + client.invoke(InvokeInput(function_name="echo", payload=PAYLOAD)) # pyright: ignore[reportArgumentType] + client.invoke(InvokeInput(function_name="echo", payload=PAYLOAD.encode())) + + +# TODO: accept str for blob members +@pytest.mark.skip +def test_should_allow_str_as_payload_blob_and_decode_output_payload() -> None: + """The client accepts a str payload, and the output payload converts to a str.""" + client = LambdaClient(LambdaConfig.resolve(region="us-west-2")) + mock(client).expect_request( + method="POST", + hostname="lambda.us-west-2.amazonaws.com", + headers={"content-type": "application/octet-stream"}, + body=PAYLOAD, + scheme="https", + path="/2015-03-31/functions/echo/invocations", + ).respond_with(body=PAYLOAD) + + output = client.invoke(InvokeInput(function_name="echo", payload=PAYLOAD)) # pyright: ignore[reportArgumentType] + + assert json.loads((output.payload or b"{}").decode()) == {"hello": "world"} + + +def test_should_allow_bytes_as_payload_blob() -> None: + """A bytes payload becomes the request body.""" + client = LambdaClient(LambdaConfig.resolve(region="us-west-2")) + mock(client).expect_request( + method="POST", + hostname="lambda.us-west-2.amazonaws.com", + headers={"content-type": "application/octet-stream"}, + body=PAYLOAD, + scheme="https", + path="/2015-03-31/functions/echo/invocations", + ).respond_with(body=PAYLOAD) + + output = client.invoke(InvokeInput(function_name="echo", payload=PAYLOAD.encode())) + + assert json.loads((output.payload or b"{}").decode()) == {"hello": "world"} + + +def test_should_allow_bytearray_as_payload_blob() -> None: + """A bytearray payload becomes the request body.""" + client = LambdaClient(LambdaConfig.resolve(region="us-west-2")) + mock(client).expect_request( + method="POST", + hostname="lambda.us-west-2.amazonaws.com", + headers={"content-type": "application/octet-stream"}, + body=PAYLOAD, + scheme="https", + path="/2015-03-31/functions/echo/invocations", + ).respond_with(body=PAYLOAD) + + output = client.invoke( + InvokeInput(function_name="echo", payload=bytearray(PAYLOAD.encode())) # pyright: ignore[reportArgumentType] + ) + + assert json.loads((output.payload or b"{}").decode()) == {"hello": "world"} + + +def test_should_allow_file_like_object_as_payload_blob() -> None: + """A file-like payload becomes the request body.""" + client = LambdaClient(LambdaConfig.resolve(region="us-west-2")) + mock(client).expect_request( + method="POST", + hostname="lambda.us-west-2.amazonaws.com", + headers={"content-type": "application/octet-stream"}, + body=PAYLOAD, + scheme="https", + path="/2015-03-31/functions/echo/invocations", + ).respond_with(body=PAYLOAD) + + output = client.invoke( + InvokeInput(function_name="echo", payload=BytesIO(PAYLOAD.encode())) # pyright: ignore[reportArgumentType] + ) + + assert json.loads((output.payload or b"{}").decode()) == {"hello": "world"} diff --git a/clients/aws-sdk-lex-runtime-v2/tests/functional/__init__.py b/clients/aws-sdk-lex-runtime-v2/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-lex-runtime-v2/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-lex-runtime-v2/tests/functional/test_event_stream_headers.py b/clients/aws-sdk-lex-runtime-v2/tests/functional/test_event_stream_headers.py new file mode 100644 index 000000000..dddbbd5aa --- /dev/null +++ b/clients/aws-sdk-lex-runtime-v2/tests/functional/test_event_stream_headers.py @@ -0,0 +1,34 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from aws_sdk_lex_runtime_v2.aio.client import AsyncLexRuntimeV2Client +from aws_sdk_lex_runtime_v2.aio.config import AsyncLexRuntimeV2Config +from aws_sdk_lex_runtime_v2.models import StartConversationInput +from smithy_test.aio import mock_async + + +async def test_should_set_streaming_headers() -> None: + """The event stream request carries the event-stream content type and the streaming payload hash.""" + async with AsyncLexRuntimeV2Client( + await AsyncLexRuntimeV2Config.resolve(region="us-west-2") + ) as client: + async with await mock_async(client) as controller: + controller.expect_request( + headers={ + "content-type": "application/vnd.amazon.eventstream", + "x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-EVENTS", + } + ).respond_with( + headers={"content-type": "application/vnd.amazon.eventstream"} + ) + + async with await client.start_conversation( + StartConversationInput( + bot_id="undefined", + bot_alias_id="undefined", + locale_id="undefined", + session_id="undefined", + ) + ) as stream: + await stream.input_stream.close() + await stream.await_output() diff --git a/clients/aws-sdk-s3/tests/functional/__init__.py b/clients/aws-sdk-s3/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-s3/tests/functional/test_200_error_responses.py b/clients/aws-sdk-s3/tests/functional/test_200_error_responses.py new file mode 100644 index 000000000..61c8b06f3 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_200_error_responses.py @@ -0,0 +1,98 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ( + CompletedMultipartUpload, + CompleteMultipartUploadInput, + CopyObjectInput, +) +from smithy_core.retries import RetryStrategyOptions +from smithy_test import mock + +XML_ERROR = """ + + InternalError + We encountered an internal error. Please try again. + EXAMPLE +""" + +COPY_OBJECT_RESULT = """ + + "etag" + 2026-01-01T00:00:00.000Z +""" + + +# TODO: treat 200 responses with an Error body as errors +@pytest.mark.skip +def test_should_retry_on_copy_object_200_with_error_xml_body() -> None: + """A CopyObject 200 response with an Error body counts as a failure and triggers a retry.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request(hostname="/./").times(3).respond_with( + body=XML_ERROR + ).respond_with(body=XML_ERROR).respond_with(body=COPY_OBJECT_RESULT) + + result = client.copy_object( + CopyObjectInput( + bucket="bucket", key="key", copy_source="src-bucket/src-key" + ) + ) + + assert result.copy_object_result is not None + assert result.copy_object_result.e_tag == '"etag"' + + +# TODO: treat 200 responses with an Error body as errors +@pytest.mark.skip +def test_should_retry_on_complete_multipart_upload_200_with_error_xml_body() -> None: + """A CompleteMultipartUpload 200 response with an Error body counts as a failure and triggers a retry.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request(hostname="/./").times(3).respond_with( + body=XML_ERROR + ).respond_with(body=XML_ERROR).respond_with( + body=""" + + string + string + string + string + string + string + string + string + string + string +""" + ) + + result = client.complete_multipart_upload( + CompleteMultipartUploadInput( + bucket="bucket", + key="key", + upload_id="upload-id", + multipart_upload=CompletedMultipartUpload(parts=[]), + ) + ) + + assert result.e_tag == "string" + + +def test_should_not_raise_on_copy_object_200_with_success_xml_body() -> None: + """A CopyObject 200 response with a success body parses as output.""" + client = S3Client( + S3Config.resolve( + region="us-west-2", retry_strategy=RetryStrategyOptions(max_attempts=1) + ) + ) + mock(client).expect_request(hostname="/./").respond_with(body=COPY_OBJECT_RESULT) + + result = client.copy_object( + CopyObjectInput(bucket="bucket", key="key", copy_source="src-bucket/src-key") + ) + + assert result.response_metadata.http_status_code == 200 diff --git a/clients/aws-sdk-s3/tests/functional/test_config.py b/clients/aws-sdk-s3/tests/functional/test_config.py new file mode 100644 index 000000000..14a05dfbb --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_config.py @@ -0,0 +1,121 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import logging +from pathlib import Path + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ListBucketsInput +from smithy_aws_core.protocols import RestXmlClientProtocol +from smithy_core.exceptions import CallError +from smithy_core.retries import StandardRetryStrategy +from smithy_http.interfaces import HTTPRequest, HTTPRequestConfiguration +from smithy_http.interfaces import HTTPResponse as HTTPResponseInterface +from smithy_http.urllib3 import Urllib3HTTPClient +from smithy_test import mock + + +def test_should_accept_shared_credentials_file(tmp_path: Path) -> None: + """Requests carry the access key from a shared credentials file.""" + credentials_file = tmp_path / "credentials" + credentials_file.write_text( + "[default]\naws_access_key_id = TEST\naws_secret_access_key = TEST\n" + ) + config_file = tmp_path / "config" + config_file.write_text("") + + client = S3Client( + S3Config.resolve( + region="us-west-2", + profile="default", + config_file_path=str(config_file), + credentials_file_path=str(credentials_file), + ) + ) + + assert client is not None + client.close() + + +# TODO: log failed calls at error level +@pytest.mark.skip +def test_log_level_should_filter_log_records(caplog: pytest.LogCaptureFixture) -> None: + """The log level filters out debug records, and a failed call logs at error level.""" + with caplog.at_level(logging.DEBUG): + client = S3Client(S3Config.resolve(region="us-west-2")) + mock(client).expect_request(hostname="/s3/").respond_with( + status_code=400, body="" + ) + with pytest.raises(CallError): + client.list_buckets(ListBucketsInput()) + + assert any(r.levelno == logging.DEBUG for r in caplog.records) + assert any(r.levelno == logging.ERROR for r in caplog.records) + + caplog.clear() + with caplog.at_level(logging.INFO): + client = S3Client(S3Config.resolve(region="us-west-2")) + mock(client).expect_request(hostname="/s3/").respond_with( + status_code=400, body="" + ) + with pytest.raises(CallError): + client.list_buckets(ListBucketsInput()) + + assert not any(r.levelno == logging.DEBUG for r in caplog.records) + assert any(r.levelno == logging.ERROR for r in caplog.records) + + +def test_should_accept_extended_aws_rest_xml_protocol() -> None: + """The config accepts a subclass of the REST-XML protocol.""" + + class ExtendedRestXmlClientProtocol(RestXmlClientProtocol): + pass + + config = S3Config.resolve( + region="us-west-2", protocol=ExtendedRestXmlClientProtocol + ) + client = S3Client(config) + + assert isinstance(config.protocol, ExtendedRestXmlClientProtocol) + assert client is not None + + +def test_should_accept_extended_urllib3_http_client() -> None: + """The config accepts a subclass of the urllib3 transport.""" + + class ExtendedUrllib3HTTPClient(Urllib3HTTPClient): + def send( + self, + request: HTTPRequest, + *, + request_config: HTTPRequestConfiguration | None = None, + ) -> HTTPResponseInterface: + return super().send(request, request_config=request_config) + + client = S3Client( + S3Config.resolve(region="us-west-2", transport=ExtendedUrllib3HTTPClient()) + ) + + assert client is not None + client.close() + + +def test_should_accept_configured_retry_strategy_with_custom_backoff() -> None: + """The config accepts a retry strategy with a custom backoff.""" + + class CustomBackoff: + def compute_next_backoff_delay(self, retry_attempt: int) -> float: + return 0.5 + retry_attempt * 1.0 + + client = S3Client( + S3Config.resolve( + region="us-west-2", + retry_strategy=StandardRetryStrategy( + max_attempts=5, backoff_strategy=CustomBackoff() + ), + ) + ) + + assert client is not None diff --git a/clients/aws-sdk-s3/tests/functional/test_content_length.py b/clients/aws-sdk-s3/tests/functional/test_content_length.py new file mode 100644 index 000000000..a00c3a20a --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_content_length.py @@ -0,0 +1,30 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import GetObjectInput, PutObjectInput +from smithy_test import mock + + +def test_should_not_add_content_length_if_no_body() -> None: + """A request without a body omits content-length.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + headers=lambda headers: "content-length" not in headers + ) + + client.get_object(GetObjectInput(bucket="b", key="k")) + + +# TODO: send a checksum header and content-length for bodies of known length in place of an aws-chunked trailer +@pytest.mark.skip +def test_should_add_content_length_if_body_present() -> None: + """A request with a body carries its content-length.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request(headers={"content-length": "/4/"}) + + client.put_object(PutObjectInput(bucket="b", key="k", body=b"abcd")) diff --git a/clients/aws-sdk-s3/tests/functional/test_dot_segment_in_uri_label.py b/clients/aws-sdk-s3/tests/functional/test_dot_segment_in_uri_label.py new file mode 100644 index 000000000..bda06ec69 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_dot_segment_in_uri_label.py @@ -0,0 +1,33 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import GetObjectInput +from smithy_test import mock + + +def test_s3_preserves_leading_dot_segment_in_uri_label() -> None: + """A key that starts with a dot segment keeps it in the request path.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + method="GET", + hostname="mybucket.s3.us-west-2.amazonaws.com", + path="/../key.txt", + ) + + client.get_object(GetObjectInput(bucket="mybucket", key="../key.txt")) + + +def test_s3_preserves_embedded_dot_segment_in_uri_label() -> None: + """A key that contains a dot segment keeps it in the request path.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + method="GET", + hostname="mybucket.s3.us-west-2.amazonaws.com", + path="/foo/../key.txt", + ) + + client.get_object(GetObjectInput(bucket="mybucket", key="foo/../key.txt")) diff --git a/clients/aws-sdk-s3/tests/functional/test_endpoint.py b/clients/aws-sdk-s3/tests/functional/test_endpoint.py new file mode 100644 index 000000000..9a59a06bd --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_endpoint.py @@ -0,0 +1,29 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import HeadBucketInput +from smithy_test import mock + + +def test_should_resolve_endpoints() -> None: + """A multi-region access point ARN resolves to its FIPS dual-stack access point hostname.""" + client = S3Client( + S3Config.resolve( + region="us-west-2", + use_fips_endpoint=True, + use_dualstack_endpoint=True, + use_arn_region=True, + ) + ) + with mock(client) as controller: + controller.expect_request( + hostname=r"/mrap-name-1234567890.s3-accesspoint-fips.dualstack.us-east-1.amazonaws.com$/" + ) + + client.head_bucket( + HeadBucketInput( + bucket="arn:aws:s3:us-east-1:1234567890:accesspoint:mrap-name" + ) + ) diff --git a/clients/aws-sdk-s3/tests/functional/test_env_endpoint.py b/clients/aws-sdk-s3/tests/functional/test_env_endpoint.py new file mode 100644 index 000000000..44377bf56 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_env_endpoint.py @@ -0,0 +1,49 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ListBucketsInput +from smithy_test import mock + + +def test_should_override_a_client_endpoint_if_none_is_set_on_the_client( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """AWS_ENDPOINT_URL sets the endpoint of a client configured without one.""" + monkeypatch.delenv("AWS_ENDPOINT_URL_DYNAMODB", raising=False) + monkeypatch.setenv("AWS_ENDPOINT_URL", "https://localhost/1") + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request(hostname="/localhost/", path="/\\/1/") + + client.list_buckets(ListBucketsInput()) + + +def test_should_defer_to_the_client_s_manually_set_endpoint( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """An endpoint set on the client takes precedence over AWS_ENDPOINT_URL.""" + monkeypatch.delenv("AWS_ENDPOINT_URL_DYNAMODB", raising=False) + monkeypatch.setenv("AWS_ENDPOINT_URL", "https://localhost/1") + client = S3Client( + S3Config.resolve(region="us-west-2", endpoint_uri="https://localhost/2") + ) + with mock(client) as controller: + controller.expect_request(hostname="/localhost/", path="/\\/2/") + + client.list_buckets(ListBucketsInput()) + + +def test_should_allow_a_specific_service_id_to_be_chosen_in_the_env_variable_name( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """AWS_ENDPOINT_URL_S3 sets the endpoint for the S3 client.""" + monkeypatch.delenv("AWS_ENDPOINT_URL", raising=False) + monkeypatch.setenv("AWS_ENDPOINT_URL_DYNAMODB", "https://localhost/1") + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request(hostname="/s3\\.us-west-2\\.amazonaws\\.com/") + + client.list_buckets(ListBucketsInput()) diff --git a/clients/aws-sdk-s3/tests/functional/test_expect_continue.py b/clients/aws-sdk-s3/tests/functional/test_expect_continue.py new file mode 100644 index 000000000..508b6f6a9 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_expect_continue.py @@ -0,0 +1,89 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ListBucketsInput, PutObjectInput +from smithy_test import mock + + +# TODO: add the expect_continue_header config option +@pytest.mark.skip +def test_should_not_set_expect_header_if_there_is_no_body() -> None: + """A request without a body omits the Expect header.""" + client = S3Client( + S3Config.resolve(region="us-west-2", expect_continue_header=True) # pyright: ignore[reportCallIssue, reportUnknownArgumentType] + ) + controller = mock(client) + controller.expect_request() + + client.list_buckets(ListBucketsInput()) + + assert "expect" not in [f.name.lower() for f in controller.requests[0].fields] + + +# TODO: add the expect_continue_header config option +@pytest.mark.skip +def test_should_set_expect_header_if_there_is_a_body() -> None: + """A request with a body carries `Expect: 100-continue`.""" + client = S3Client( + S3Config.resolve(region="us-west-2", expect_continue_header=4) # pyright: ignore[reportCallIssue, reportUnknownArgumentType] + ) + mock(client).expect_request(headers={"Expect": "100-continue"}) + + client.put_object(PutObjectInput(bucket="b", key="k", body=b"abcd")) + + +# TODO: add the expect_continue_header config option +@pytest.mark.skip +def test_should_omit_expect_header_when_disabled() -> None: + """Setting expect_continue_header to False omits the Expect header.""" + client = S3Client( + S3Config.resolve(region="us-west-2", expect_continue_header=False) # pyright: ignore[reportCallIssue, reportUnknownArgumentType] + ) + controller = mock(client) + controller.expect_request() + + client.put_object(PutObjectInput(bucket="b", key="k", body=b"abcd")) + + assert "expect" not in [f.name.lower() for f in controller.requests[0].fields] + + +# TODO: add the expect_continue_header config option +@pytest.mark.skip +def test_should_omit_expect_header_when_body_is_below_threshold() -> None: + """A 5-byte threshold omits the Expect header for a 4-byte body.""" + client = S3Client( + S3Config.resolve(region="us-west-2", expect_continue_header=5) # pyright: ignore[reportCallIssue, reportUnknownArgumentType] + ) + controller = mock(client) + controller.expect_request() + + client.put_object(PutObjectInput(bucket="b", key="k", body=b"abcd")) + + assert "expect" not in [f.name.lower() for f in controller.requests[0].fields] + + +# TODO: add the expect_continue_header config option +@pytest.mark.skip +def test_should_set_expect_header_when_enabled() -> None: + """Setting expect_continue_header to True adds the Expect header.""" + client = S3Client( + S3Config.resolve(region="us-west-2", expect_continue_header=True) # pyright: ignore[reportCallIssue, reportUnknownArgumentType] + ) + mock(client).expect_request(headers={"Expect": "100-continue"}) + + client.put_object(PutObjectInput(bucket="b", key="k", body=b"abcd")) + + +# TODO: add the expect_continue_header config option +@pytest.mark.skip +def test_should_set_expect_header_when_body_meets_threshold() -> None: + """A 4-byte threshold adds the Expect header for a 4-byte body.""" + client = S3Client( + S3Config.resolve(region="us-west-2", expect_continue_header=4) # pyright: ignore[reportCallIssue, reportUnknownArgumentType] + ) + mock(client).expect_request(headers={"Expect": "100-continue"}) + + client.put_object(PutObjectInput(bucket="b", key="k", body=b"abcd")) diff --git a/clients/aws-sdk-s3/tests/functional/test_flexible_checksums.py b/clients/aws-sdk-s3/tests/functional/test_flexible_checksums.py new file mode 100644 index 000000000..de7e7d899 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_flexible_checksums.py @@ -0,0 +1,266 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import base64 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import GetObjectInput, PutObjectInput +from smithy_aws_core.checksums import ChecksumStream, UnsupportedChecksumAlgorithmError +from smithy_aws_core.config.types import ( + RequestChecksumCalculation, + ResponseChecksumValidation, +) +from smithy_core.utils import read_streaming_blob +from smithy_test import mock + +DEFAULT_CHECKSUM_ALGORITHM = "CRC32" + +TEST_CASES: list[tuple[str, str | None, str]] = [ + ("", "CRC32", "AAAAAA=="), + ("abc", "CRC32", "NSRBwg=="), + ("Hello world", "CRC32", "i9aeUg=="), + ("", "CRC32C", "AAAAAA=="), + ("abc", "CRC32C", "Nks/tw=="), + ("Hello world", "CRC32C", "crUfeA=="), + ("", "CRC64NVME", "AAAAAAAAAAA="), + ("abc", "CRC64NVME", "BeXKuz/B+us="), + ("Hello world", "CRC64NVME", "OOJZ0D8xKts="), + ("", "SHA1", "2jmj7l5rSw0yVb/vlWAYkK/YBwk="), + ("abc", "SHA1", "qZk+NkcGgWq6PiVxeFDCbJzQ2J0="), + ("Hello world", "SHA1", "e1AsOh9IyGCa4hLN+2Od7jlnP14="), + ("", "SHA256", "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="), + ("abc", "SHA256", "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0="), + ("Hello world", "SHA256", "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw="), + ("", None, "AAAAAA=="), + ("abc", None, "NSRBwg=="), + ("Hello world", None, "i9aeUg=="), +] + +CASE_IDS = [f"{algorithm}-{body!r}" for body, algorithm, _ in TEST_CASES] + +SETTINGS: list[RequestChecksumCalculation | None] = [ + None, + "when_supported", + "when_required", +] + +VALIDATION_SETTINGS: list[ResponseChecksumValidation | None] = [ + None, + "when_supported", + "when_required", +] + +HELLO_WORLD_2 = base64.b64encode(b"Hello, world.2").decode() + + +# TODO: send a checksum header for bodies of known length in place of an aws-chunked trailer, and decide on the CRC32 default +@pytest.mark.skip +@pytest.mark.parametrize("request_checksum_calculation", SETTINGS) +@pytest.mark.parametrize( + ("body", "checksum_algorithm", "checksum_value"), TEST_CASES, ids=CASE_IDS +) +def test_should_send_checksum_on_put_object( + request_checksum_calculation: RequestChecksumCalculation | None, + body: str, + checksum_algorithm: str | None, + checksum_value: str, +) -> None: + """PutObject sends the expected checksum header for each body, algorithm and calculation setting.""" + client = S3Client( + S3Config.resolve( + region="us-west-2", + request_checksum_calculation=request_checksum_calculation, + ) + ) + checksum_header = ( + f"x-amz-checksum-{(checksum_algorithm or DEFAULT_CHECKSUM_ALGORITHM).lower()}" + ) + headers: dict[str, str] = { + "content-type": "application/octet-stream", + "user-agent": "/./", + "x-amz-date": "/./", + "x-amz-content-sha256": "/./", + "authorization": "/./", + } + if body: + headers["content-length"] = str(len(body)) + if not ( + request_checksum_calculation == "when_required" and checksum_algorithm is None + ): + headers[checksum_header] = checksum_value + if checksum_algorithm is not None: + headers["x-amz-sdk-checksum-algorithm"] = checksum_algorithm + with mock(client) as controller: + controller.expect_request( + method="PUT", + scheme="https", + hostname="s3.us-west-2.amazonaws.com", + path="/b/k", + headers=headers, + query={"x-id": "PutObject"}, + ) + + client.put_object( + PutObjectInput( + bucket="b", + key="k", + body=body.encode(), + checksum_algorithm=checksum_algorithm, + ) + ) + + +@pytest.mark.parametrize("response_checksum_validation", VALIDATION_SETTINGS) +@pytest.mark.parametrize( + ("body", "checksum_algorithm", "checksum_value"), TEST_CASES, ids=CASE_IDS +) +def test_should_validate_checksum_on_get_object( + response_checksum_validation: ResponseChecksumValidation | None, + body: str, + checksum_algorithm: str | None, + checksum_value: str, +) -> None: + """GetObject requests checksum mode and returns the body for each algorithm and validation setting.""" + client = S3Client( + S3Config.resolve( + region="us-west-2", + response_checksum_validation=response_checksum_validation, + ) + ) + checksum_header = ( + f"x-amz-checksum-{(checksum_algorithm or DEFAULT_CHECKSUM_ALGORITHM).lower()}" + ) + headers: dict[str, str] = { + "user-agent": "/./", + "x-amz-date": "/./", + "x-amz-content-sha256": "/./", + "authorization": "/./", + } + if not (response_checksum_validation == "when_required" and not checksum_algorithm): + headers["x-amz-checksum-mode"] = "ENABLED" + mock(client).expect_request( + method="GET", + scheme="https", + hostname="s3.us-west-2.amazonaws.com", + path="/b/k", + headers=headers, + query={"x-id": "GetObject"}, + ).respond_with( + headers={ + "content-type": "application/octet-stream", + "content-length": str(len(body)), + checksum_header: checksum_value, + }, + body=body.encode(), + ) + + output = client.get_object( + GetObjectInput( + bucket="b", + key="k", + # The SDK sets checksum_mode itself when no algorithm is given. + checksum_mode="ENABLED" if checksum_algorithm else None, + ) + ) + + assert read_streaming_blob(output.body) == body.encode() + + +# TODO: send a trailing checksum for bodies of unknown length +@pytest.mark.skip +def test_should_not_set_binary_file_content_length() -> None: + """A body of unknown length goes out without a content-length.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + method="PUT", + scheme="https", + hostname="s3.us-west-2.amazonaws.com", + path="/b/k", + headers=lambda headers: ( + headers.get("content-type") == "application/octet-stream" + and headers.get("x-amz-content-sha256") + == "STREAMING-UNSIGNED-PAYLOAD-TRAILER" + and "content-length" not in headers + ), + query={"x-id": "PutObject"}, + ) + + client.put_object( + PutObjectInput( + bucket="b", + key="k", + body=_UnsizedStream(b"hello"), + checksum_algorithm="SHA256", + ) + ) + + +@pytest.mark.parametrize( + ("algorithm", "feature_id"), + [ + ("SHA256", "Y"), + ("SHA1", "X"), + ("CRC32", "U"), + ("CRC32C", "V"), + ("CRC64NVME", "W"), + ], +) +def test_should_feature_detect_checksum(algorithm: str, feature_id: str) -> None: + """The user agent lists the feature ID for the requested checksum algorithm.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + headers={"user-agent": f"/(.*?) m/(.*?){feature_id}(.*?)$/"} + ) + + client.put_object( + PutObjectInput( + bucket="b", key="k", body=b"abcd", checksum_algorithm=algorithm + ) + ) + + +def test_should_raise_an_error_if_the_requested_algorithm_implementation_is_not_available() -> ( + None +): + """Requesting a checksum algorithm without an implementation raises an error.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + mock(client).expect_request(headers={"x-amz-checksum-helloworld": HELLO_WORLD_2}) + + with pytest.raises(UnsupportedChecksumAlgorithmError, match="HELLOWORLD"): + client.put_object( + PutObjectInput( + bucket="bucket", key="key", body=b"hi", checksum_algorithm="HELLOWORLD" + ) + ) + + +def test_should_ignore_the_checksum_header_and_perform_no_checksum_validation_if_no_matching_algorithm_implementation_is_available() -> ( + None +): + """A response checksum for an unsupported algorithm skips validation.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + mock(client).expect_request( + hostname="bucket.s3.us-west-2.amazonaws.com" + ).respond_with( + headers={"x-amz-checksum-helloworld": HELLO_WORLD_2}, body=b"hi_extra_bytes" + ) + + output = client.get_object(GetObjectInput(bucket="bucket", key="key")) + + assert not isinstance(output.body, ChecksumStream) + assert read_streaming_blob(output.body) == b"hi_extra_bytes" + + +class _UnsizedStream: + def __init__(self, data: bytes) -> None: + self._data = data + + def read(self, size: int = -1) -> bytes: + if size < 0: + size = len(self._data) + chunk, self._data = self._data[:size], self._data[size:] + return chunk diff --git a/clients/aws-sdk-s3/tests/functional/test_flexible_checksums_retry.py b/clients/aws-sdk-s3/tests/functional/test_flexible_checksums_retry.py new file mode 100644 index 000000000..f68e99fd8 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_flexible_checksums_retry.py @@ -0,0 +1,34 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import PutObjectInput +from smithy_core.exceptions import CallError +from smithy_test import mock + + +# TODO: retry 500 responses without a modeled error +@pytest.mark.skip +def test_retry_reuses_the_checksum() -> None: + """Retried attempts send the checksum computed for the first attempt.""" + max_attempts = 3 + client = S3Client(S3Config.resolve(region="us-west-2", max_attempts=max_attempts)) + controller = mock(client) + controller.expect_request(method="PUT").times(max_attempts).respond_with( + status_code=500 + ) + + with pytest.raises(CallError): + client.put_object(PutObjectInput(bucket="b", key="k", body=b"hello")) + + assert len(controller.requests) == max_attempts + checksums = [ + request.fields["x-amz-checksum-crc32"].as_string() + if "x-amz-checksum-crc32" in request.fields + else request.body + for request in controller.requests + ] + assert checksums[0] + assert checksums == [checksums[0]] * max_attempts diff --git a/clients/aws-sdk-s3/tests/functional/test_location_constraint.py b/clients/aws-sdk-s3/tests/functional/test_location_constraint.py new file mode 100644 index 000000000..7cf7a55f3 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_location_constraint.py @@ -0,0 +1,75 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ( + CreateBucketConfiguration, + CreateBucketInput, + LocationInfo, + Tag, +) +from smithy_test import mock + + +# TODO: fill CreateBucket LocationConstraint from the client region +@pytest.mark.skip +def test_should_default_location_constraint_from_region() -> None: + """CreateBucket fills LocationConstraint from the client region.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + mock(client).expect_request( + body="/us-west-2" + "/" + ) + + client.create_bucket(CreateBucketInput(bucket="b")) + + +def test_should_not_default_location_constraint_in_us_east_1() -> None: + """CreateBucket in us-east-1 sends no LocationConstraint.""" + client = S3Client(S3Config.resolve(region="us-east-1")) + mock(client).expect_request(body=lambda body: b"LocationConstraint" not in body) + + client.create_bucket(CreateBucketInput(bucket="b")) + + +def test_should_populate_other_elements_of_the_create_bucket_configuration_regardless_of_location_or_location_constraint() -> ( + None +): + """CreateBucket keeps the other CreateBucketConfiguration elements from the caller.""" + client = S3Client(S3Config.resolve(region="us-east-1")) + mock(client).expect_request( + body=lambda body: ( + b'' + b"my-tag-keymy-tag-value" + b"" in body + ) + ) + + client.create_bucket( + CreateBucketInput( + bucket="b", + create_bucket_configuration=CreateBucketConfiguration( + tags=[Tag(key="my-tag-key", value="my-tag-value")] + ), + ) + ) + + +def test_should_not_default_location_constraint_for_directory_buckets() -> None: + """CreateBucket for a directory bucket sends no default LocationConstraint.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + mock(client).expect_request( + hostname="/-control.us-west-2.amazonaws.com$/", + body=lambda body: b"LocationConstraint" not in body, + ) + + client.create_bucket( + CreateBucketInput( + bucket="s3-express-1234--usw2-az2--x-s3", + create_bucket_configuration=CreateBucketConfiguration( + location=LocationInfo(type="AvailabilityZone", name="usw2-az2") + ), + ) + ) diff --git a/clients/aws-sdk-s3/tests/functional/test_mutability.py b/clients/aws-sdk-s3/tests/functional/test_mutability.py new file mode 100644 index 000000000..8403523af --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_mutability.py @@ -0,0 +1,47 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from typing import Any + +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import HeadBucketInput +from smithy_core.interceptors import InputContext, Interceptor +from smithy_test import mock + + +def test_should_allow_reusing_and_modifying_an_input_object() -> None: + """One input object reused across calls reflects its latest bucket in each request, and an interceptor edit persists on it.""" + + class InputModifyingInterceptor(Interceptor[Any, Any, Any, Any]): + def modify_before_serialization(self, context: InputContext[Any]) -> Any: + context.request.expected_bucket_owner = "me" + return context.request + + def input_modifying_plugin(config: S3Config) -> None: + config.interceptors.append(InputModifyingInterceptor()) + + client = S3Client( + S3Config.resolve(region="us-west-2"), plugins=[input_modifying_plugin] + ) + with mock(client) as controller: + controller.expect_request( + headers={"x-amz-expected-bucket-owner": "/^me$/"} + ).times(4) + + params = HeadBucketInput(bucket="bucket1") + client.head_bucket(params) + params.bucket = "bucket2" + client.head_bucket(params) + params.bucket = "bucket3" + client.head_bucket(params) + params.bucket = "bucket4" + client.head_bucket(params) + + assert [request.destination.host for request in controller.requests] == [ + "bucket1.s3.us-west-2.amazonaws.com", + "bucket2.s3.us-west-2.amazonaws.com", + "bucket3.s3.us-west-2.amazonaws.com", + "bucket4.s3.us-west-2.amazonaws.com", + ] + assert params == HeadBucketInput(bucket="bucket4", expected_bucket_owner="me") diff --git a/clients/aws-sdk-s3/tests/functional/test_object_lambda.py b/clients/aws-sdk-s3/tests/functional/test_object_lambda.py new file mode 100644 index 000000000..a0296cd4f --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_object_lambda.py @@ -0,0 +1,37 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import GetObjectInput +from smithy_test import mock + + +def test_can_make_a_get_request_to_an_s3_object_lambda_arn() -> None: + """GetObject on an Object Lambda access point ARN, in slash or colon form, goes to the Object Lambda hostname with an s3-object-lambda signature.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + hostname="my-access-point-123456789012.s3-object-lambda.us-west-2.amazonaws.com", + query={"x-id": "GetObject"}, + # CAUTION: the mock signs with its own access key, so it precedes the date. + headers={ + "authorization": r"/=\w+/\d+/us-west-2/s3-object-lambda/aws4_request/" + }, + path="/a", + ).times(2) + + # slash delimiter + client.get_object( + GetObjectInput( + bucket="arn:aws:s3-object-lambda:us-west-2:123456789012:accesspoint/my-access-point", + key="a", + ) + ) + # colon delimiter + client.get_object( + GetObjectInput( + bucket="arn:aws:s3-object-lambda:us-west-2:123456789012:accesspoint:my-access-point", + key="a", + ) + ) diff --git a/clients/aws-sdk-s3/tests/functional/test_s3_customizations.py b/clients/aws-sdk-s3/tests/functional/test_s3_customizations.py new file mode 100644 index 000000000..6274b2d68 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_s3_customizations.py @@ -0,0 +1,82 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import logging +import os + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import HeadBucketInput, PutObjectInput +from pytest import LogCaptureFixture, raises +from smithy_core.exceptions import SmithyError +from smithy_test import mock + + +# TODO: validate bucket names before sending +@pytest.mark.skip +def test_validates_bucket_names() -> None: + """A bucket name with a slash raises InvalidBucketName before any request goes out.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request() + + with raises(SmithyError, match="InvalidBucketName"): + client.head_bucket(HeadBucketInput(bucket="/my-bucket/a")) + + assert controller.requests == [] + + +# TODO: warn on bodies of unknown length +@pytest.mark.skip +def test_warns_on_input_streams_of_unknown_length(caplog: LogCaptureFixture) -> None: + """A body of unknown length logs a warning.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + mock(client).expect_request(query={"x-id": "PutObject"}) + + # A pipe isn't seekable, so its length can't be known up front. + read_fd, write_fd = os.pipe() + os.close(write_fd) + + with caplog.at_level(logging.WARNING), open(read_fd, "rb") as body: + client.put_object(PutObjectInput(bucket="my-bucket", key="k", body=body)) + + assert any("unknown length" in record.getMessage() for record in caplog.records), ( + caplog.text + ) + + +# TODO: add the bucket_endpoint config option +@pytest.mark.skip +def test_allows_using_a_bucket_input_value_as_the_endpoint() -> None: + """With bucket_endpoint set, the bucket input serves as the endpoint URL.""" + client = S3Client( + S3Config.resolve(region="us-west-2", bucket_endpoint=True) # pyright: ignore[reportCallIssue, reportUnknownArgumentType] + ) + mock(client).expect_request( + query={"x-id": "PutObject"}, + scheme="https", + hostname="mybucket.com", + port="8888", + path="/my-bucket-path/my-key", + ) + + client.put_object( + PutObjectInput( + bucket="https://mybucket.com:8888/my-bucket-path", + key="my-key", + body=b"abcd", + ) + ) + + +def test_considers_missing_bucket_an_http_label_error() -> None: + """An empty bucket raises an error before any request goes out.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request() + + with raises(SmithyError, match="(?i)bucket"): + client.head_bucket(HeadBucketInput()) + + assert controller.requests == [] diff --git a/clients/aws-sdk-s3/tests/functional/test_s3_express.py b/clients/aws-sdk-s3/tests/functional/test_s3_express.py new file mode 100644 index 000000000..0fab44302 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_s3_express.py @@ -0,0 +1,137 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import re + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ( + CreateBucketConfiguration, + CreateBucketInput, + Delete, + DeleteObjectsInput, + GetObjectInput, + HeadBucketInput, + LocationInfo, + PutObjectInput, +) +from smithy_test import mock + +BUCKET = "test-bucket--usw2-az1--x-s3" + +BUCKET_HOST = f"{BUCKET}.s3express-usw2-az1.us-west-2.amazonaws.com" + +CREATE_SESSION_RESULT = """ + + + S3_EXPRESS_SESSION_TOKEN + S3_EXPRESS_SECRET_ACCESS_KEY + S3_EXPRESS_ACCESS_KEY_ID + 2100-01-01T00:00:00Z + +""" + + +# TODO: add the sigv4-s3express auth scheme +@pytest.mark.skip +def test_should_not_send_the_x_amz_create_session_mode_header() -> None: + """A directory bucket request omits the x-amz-create-session-mode header.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request(hostname=BUCKET_HOST, query={"session": ""}).respond_with( + body=CREATE_SESSION_RESULT + ) + controller.expect_request(hostname=BUCKET_HOST, path="/1") + + client.get_object(GetObjectInput(bucket=BUCKET, key="1")) + + headers = [f.name.lower() for f in controller.requests[-1].fields] + assert "x-amz-create-session-mode" not in headers + + +# TODO: add the sigv4-s3express auth scheme +@pytest.mark.skip +def test_should_default_to_crc32_checksum_when_the_request_requires_a_checksum_and_none_is_specified() -> ( + None +): + """A directory bucket request that requires a checksum defaults to CRC32 in place of Content-MD5.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request(hostname=BUCKET_HOST, query={"session": ""}).respond_with( + body=CREATE_SESSION_RESULT + ) + controller.expect_request( + hostname=BUCKET_HOST, headers={"x-amz-checksum-crc32": "b9SZTg=="} + ) + + client.delete_objects(DeleteObjectsInput(bucket=BUCKET, delete=Delete(objects=[]))) + + headers = [f.name.lower() for f in controller.requests[-1].fields] + assert "content-md5" not in headers + + +def test_does_not_fill_a_default_create_bucket_configuration_location_constraint() -> ( + None +): + """CreateBucket for a directory bucket keeps the location from the caller and omits LocationConstraint.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request(body=lambda body: b"LocationConstraint" not in body) + + client.create_bucket( + CreateBucketInput( + bucket=BUCKET, + create_bucket_configuration=CreateBucketConfiguration( + location=LocationInfo(type="AvailabilityZone", name="usw2-az1") + ), + ) + ) + + headers = [f.name.lower() for f in controller.requests[-1].fields] + assert "content-md5" not in headers + + +# TODO: add the sigv4-s3express auth scheme +@pytest.mark.skip +def test_should_use_the_session_token_header_instead_of_the_security_token_header() -> ( + None +): + """Directory bucket requests carry x-amz-s3session-token in place of x-amz-security-token.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request(hostname=BUCKET_HOST, query={"session": ""}).respond_with( + body=CREATE_SESSION_RESULT + ) + controller.expect_request( + hostname=BUCKET_HOST, + headers={"x-amz-s3session-token": "S3_EXPRESS_SESSION_TOKEN"}, + ) + + client.put_object(PutObjectInput(bucket=BUCKET, key="test.json", body=b"{}")) + + headers = [f.name.lower() for f in controller.requests[-1].fields] + assert "x-amz-security-token" not in headers + + +# TODO: add the sigv4-s3express auth scheme +@pytest.mark.skip +def test_should_feature_detect_s3_express_bucket() -> None: + """The user agent lists the feature ID for S3 Express.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request(hostname=BUCKET_HOST, query={"session": ""}).respond_with( + body=CREATE_SESSION_RESULT + ) + # "J" is the S3_EXPRESS_BUCKET business metric. + controller.expect_request( + hostname=BUCKET_HOST, + headers={ + "user-agent": lambda ua: ( + (metrics := re.search(r" m/(\S*)", ua)) is not None + and "J" in metrics.group(1).split(",") + ) + }, + ) + + client.head_bucket(HeadBucketInput(bucket=BUCKET)) diff --git a/clients/aws-sdk-s3/tests/functional/test_select_object_content.py b/clients/aws-sdk-s3/tests/functional/test_select_object_content.py new file mode 100644 index 000000000..644a5cd15 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_select_object_content.py @@ -0,0 +1,103 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from aws_sdk_s3.aio.client import AsyncS3Client +from aws_sdk_s3.aio.config import AsyncS3Config +from aws_sdk_s3.models import ( + CSVInput, + CSVOutput, + EndEvent, + InputSerialization, + OutputSerialization, + SelectObjectContentEventStreamEnd, + SelectObjectContentInput, +) +from smithy_test.aio import mock_async + +# See https://docs.aws.amazon.com/AmazonS3/latest/API/RESTSelectObjectAppendix.html +END_EVENT = bytes( + [ + # message size + *[0, 0, 0, 97], + # header size + *[0, 0, 0, 81], + # prelude crc + *[221, 28, 224, 246], + # :event-type + 11, + *[58, 101, 118, 101, 110, 116, 45, 116, 121, 112, 101], + 7, + # End + 0, + 3, + *[69, 110, 100], + # :content-type + 13, + *[58, 99, 111, 110, 116, 101, 110, 116, 45, 116, 121, 112, 101], + 7, + # application/octet-stream + 0, + 24, + *[97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 111, 99, 116, 101], + *[116, 45, 115, 116, 114, 101, 97, 109], + # :message-type + 13, + *[58, 109, 101, 115, 115, 97, 103, 101, 45, 116, 121, 112, 101], + 7, + # event + 0, + 5, + *[101, 118, 101, 110, 116], + # message crc + *[75, 107, 103, 102], + ] +) + + +async def test_should_send_select_request_and_read_events() -> None: + """SelectObjectContent sends the SQL request body and yields the events of the response stream.""" + + def has_select_parameters(body: bytes) -> None: + assert ( + b"SELECT user_name FROM S3Object WHERE cast(age as int) > 20" + in body + ) + assert b"SQL" in body + + async with AsyncS3Client(await AsyncS3Config.resolve(region="us-west-2")) as client: + async with await mock_async(client) as controller: + controller.expect_request( + hostname=r"/test-bucket\.s3\.us-west-2\.amazonaws\.com/", + method="POST", + path="/test-key", + headers={ + "authorization": "/AWS4-HMAC-SHA256 Credential/", + "content-type": "application/xml", + "x-amz-content-sha256": "/./", + }, + body=has_select_parameters, + ).respond_with( + headers={"x-amz-checksum-crc32": "3117266289"}, body=END_EVENT + ) + + async with await client.select_object_content( + SelectObjectContentInput( + bucket="test-bucket", + key="test-key", + expression_type="SQL", + expression="SELECT user_name FROM S3Object WHERE cast(age as int) > 20", + input_serialization=InputSerialization( + csv=CSVInput( + file_header_info="USE", + record_delimiter="\n", + field_delimiter=",", + ) + ), + output_serialization=OutputSerialization(csv=CSVOutput()), + ) + ) as stream: + events = [event async for event in stream.output_stream] + + assert stream.output.response_metadata.http_status_code == 200 + assert stream.output_stream is not None + assert events == [SelectObjectContentEventStreamEnd(value=EndEvent())] diff --git a/clients/aws-sdk-s3/tests/functional/test_serde.py b/clients/aws-sdk-s3/tests/functional/test_serde.py new file mode 100644 index 000000000..7044c0693 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_serde.py @@ -0,0 +1,59 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ( + AccessControlPolicy, + Grant, + Grantee, + Owner, + PutBucketAclInput, +) +from smithy_test import mock + + +# TODO: decide whether XML bodies include the XML declaration and which attribute order to use +@pytest.mark.skip +def test_should_serialize_xml() -> None: + """PutBucketAcl serializes its body as the expected XML document.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + method="PUT", + hostname="s3.us-west-2.amazonaws.com", + body='STRING_VALUESTRING_VALUESTRING_VALUESTRING_VALUEREADSTRING_VALUESTRING_VALUE', + scheme="https", + path="/STRING_VALUE/", + headers={ + "content-type": "application/xml", + "x-amz-acl": "private", + "content-length": "509", + "x-amz-checksum-crc32": "XnKFaw==", + "x-amz-content-sha256": "c0a89780e1aac5dfa17604e9e25616e7babba0b655db189be49b4c352543bb22", + }, + query={"acl": ""}, + ) + + client.put_bucket_acl( + PutBucketAclInput( + acl="private", + access_control_policy=AccessControlPolicy( + grants=[ + Grant( + grantee=Grantee( + display_name="STRING_VALUE", + email_address="STRING_VALUE", + id="STRING_VALUE", + uri="STRING_VALUE", + type="CanonicalUser", + ), + permission="READ", + ) + ], + owner=Owner(display_name="STRING_VALUE", id="STRING_VALUE"), + ), + bucket="STRING_VALUE", + ) + ) diff --git a/clients/aws-sdk-s3/tests/functional/test_ssec.py b/clients/aws-sdk-s3/tests/functional/test_ssec.py new file mode 100644 index 000000000..710f63252 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_ssec.py @@ -0,0 +1,74 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import base64 +import hashlib + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import CopyObjectInput, PutObjectInput +from smithy_test import mock + + +# TODO: base64-encode SSE-C keys and send their MD5 digests +@pytest.mark.skip +def test_adds_hash_for_sse_customer_key_and_copy_source_sse_customer_key() -> None: + """CopyObject sends base64 SSE-C keys and their MD5 digests for the source and target.""" + client = S3Client(S3Config.resolve(region="us-west-2")) + mock(client).expect_request( + method="PUT", + scheme="https", + hostname="s3.us-west-2.amazonaws.com", + path="/b/k", + query={"x-id": "CopyObject"}, + headers={ + "x-amz-copy-source": "s", + "x-amz-server-side-encryption-customer-key": "c3NlLWN1c3RvbWVyLWtleQ==", + "x-amz-server-side-encryption-customer-key-md5": "E+/nTVpQe9+cNzOKBFXSlA==", + "x-amz-copy-source-server-side-encryption-customer-key": "Y29weS1zb3VyY2Utc3NlLWN1c3RvbWVyLWtleQ==", + "x-amz-copy-source-server-side-encryption-customer-key-md5": "4CgOx9iMMPvAQi7bWXEJEw==", + }, + ) + + client.copy_object( + CopyObjectInput( + sse_customer_key="sse-customer-key", + copy_source_sse_customer_key="copy-source-sse-customer-key", + bucket="b", + copy_source="s", + key="k", + ) + ) + + +# TODO: send SSE-C key MD5 digests, and send a checksum header for bodies of known length +@pytest.mark.skip +def test_verifies_headers_for_put_object_with_base64_encoded_sse_customer_key() -> None: + """PutObject with a base64 SSE-C key sends the key and its MD5 digest.""" + client = S3Client(S3Config.resolve(region="us-east-1")) + mock(client).expect_request( + method="PUT", + scheme="https", + hostname="testbucket.s3.us-east-1.amazonaws.com", + path="/foo", + query={"x-id": "PutObject"}, + headers={ + "x-amz-server-side-encryption-customer-algorithm": "AES256", + "x-amz-server-side-encryption-customer-key": "UNhY4JhezH9gQYqvDMWrWH9CwlcKiECVqejMrND2VFw=", + "x-amz-server-side-encryption-customer-key-md5": "SwoBWUcJBbc/WRhR6hZGCA==", + "content-length": "14", + }, + body="This is a test", + ) + example_key = hashlib.sha256(b"example").digest() + + client.put_object( + PutObjectInput( + bucket="testbucket", + body=b"This is a test", + key="foo", + sse_customer_key=base64.b64encode(example_key).decode(), + sse_customer_algorithm="AES256", + ) + ) diff --git a/clients/aws-sdk-s3/tests/functional/test_user_agent.py b/clients/aws-sdk-s3/tests/functional/test_user_agent.py new file mode 100644 index 000000000..9884670d0 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_user_agent.py @@ -0,0 +1,61 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import platform +import re + +import pytest +import smithy_aws_core +from aws_sdk_s3 import __version__ as s3_version +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ListBucketsInput +from smithy_test import mock + + +# TODO: decide whether the api/ user agent component uses the lowercase service ID +@pytest.mark.skip +def test_populates_user_agent_string(monkeypatch: pytest.MonkeyPatch) -> None: + """The user agent names the SDK, service, platform and language versions.""" + monkeypatch.delenv("AWS_SDK_UA_APP_ID", raising=False) + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request( + headers={ + # CAUTION: the mock always supplies static credentials, so the + # credential feature is CREDENTIALS_CODE (e), not env vars (g). + "user-agent": ( + f"/^aws-sdk-python/{re.escape(smithy_aws_core.__version__)}" + rf"(?: \S+)* ua/2\.1 api/s3#{re.escape(s3_version)}" + f" os/{re.escape(platform.system().lower())}" + f"#{re.escape(platform.release())}" + rf"(?: \S+)* lang/python#{re.escape(platform.python_version())}" + r"(?: \S+)* m/E,e$/" + ) + } + ) + + client.list_buckets(ListBucketsInput()) + + +# TODO: decide whether the app ID keeps # and goes at the end of the user agent +@pytest.mark.skip +def test_should_allow_characters_from_the_set_0_9_a_za_z( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """The app ID keeps the characters that the user agent format permits.""" + monkeypatch.delenv("AWS_SDK_UA_APP_ID", raising=False) + client = S3Client( + S3Config.resolve( + region="us-west-2", + sdk_ua_app_id="!#$%&'*+-.^_`|~abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", + ) + ) + with mock(client) as controller: + controller.expect_request( + headers={ + "user-agent": r"/app/!#\$%&'\*\+-\.\^_`\|~abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$/" + } + ) + + client.list_buckets(ListBucketsInput()) diff --git a/clients/aws-sdk-s3/tests/functional/test_waiters.py b/clients/aws-sdk-s3/tests/functional/test_waiters.py new file mode 100644 index 000000000..d34dfff97 --- /dev/null +++ b/clients/aws-sdk-s3/tests/functional/test_waiters.py @@ -0,0 +1,31 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import HeadObjectInput +from smithy_test import mock + + +# TODO: generate waiters +@pytest.mark.skip +def test_should_poll_until_a_return_condition_is_met_http_status() -> None: + # Imported here so the rest of the suite still collects while waiters don't exist. + """The object-exists waiter polls HeadObject until the status matches its success condition.""" + from aws_sdk_s3.waiters import ( # pyright: ignore[reportMissingImports] + wait_until_object_exists, # pyright: ignore[reportUnknownVariableType] + ) + + client = S3Client(S3Config.resolve(region="us-west-2")) + with mock(client) as controller: + controller.expect_request(hostname="/s3\\.us-west-2\\.amazonaws\\.com/").times( + 2 + ).respond_with(status_code=404).respond_with(status_code=200) + + wait_until_object_exists( + client, + HeadObjectInput(bucket="@@@@", key="####"), + max_delay=5, + max_wait_time=20, + ) diff --git a/clients/aws-sdk-sqs/tests/functional/__init__.py b/clients/aws-sdk-sqs/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-sqs/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-sqs/tests/functional/test_long_poll.py b/clients/aws-sdk-sqs/tests/functional/test_long_poll.py new file mode 100644 index 000000000..65f2eb0ac --- /dev/null +++ b/clients/aws-sdk-sqs/tests/functional/test_long_poll.py @@ -0,0 +1,57 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import time + +import pytest +from aws_sdk_sqs.client import SQSClient +from aws_sdk_sqs.config import SQSConfig +from aws_sdk_sqs.models import ReceiveMessageInput +from pytest import raises +from smithy_core.exceptions import CallError +from smithy_core.retries import ( + ExponentialBackoffJitterType, + ExponentialRetryBackoffStrategy, + StandardRetryQuota, + StandardRetryStrategy, +) +from smithy_test import mock + + +# TODO: retry 500 responses without a modeled error +@pytest.mark.skip +def test_performs_retry_in_a_long_poll_mode() -> None: + """ReceiveMessage retries a 500 response with a backoff in long poll mode.""" + client = SQSClient( + SQSConfig.resolve( + region="us-west-2", + retry_strategy=StandardRetryStrategy( + # Deterministic backoff: 50ms, 100ms, 200ms. + backoff_strategy=ExponentialRetryBackoffStrategy( + backoff_scale_value=0.05, + jitter_type=ExponentialBackoffJitterType.NONE, + ), + max_attempts=4, + # Two transient retries cost 14 each, so the third is denied. + retry_quota=StandardRetryQuota(initial_capacity=28), + ), + ) + ) + controller = mock(client) + controller.expect_request(hostname="sqs.us-west-2.amazonaws.com").respond_with( + status_code=500 + ) + + request_begins = time.monotonic() + with raises(CallError): + client.receive_message( + ReceiveMessageInput( + queue_url="https://sqs.us-west-2.amazonaws.com/000000000000/Glorp" + ) + ) + time_elapsed = time.monotonic() - request_begins + + assert len(controller.requests) == 3 + # 150ms from the two retries plus 200ms of long-poll backoff once the quota + # is exhausted. + assert time_elapsed >= 0.35 diff --git a/clients/aws-sdk-sqs/tests/functional/test_sqs_customizations.py b/clients/aws-sdk-sqs/tests/functional/test_sqs_customizations.py new file mode 100644 index 000000000..8b3c540c6 --- /dev/null +++ b/clients/aws-sdk-sqs/tests/functional/test_sqs_customizations.py @@ -0,0 +1,221 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import json +from hashlib import md5 + +import pytest +from aws_sdk_sqs.client import SQSClient +from aws_sdk_sqs.config import SQSConfig +from aws_sdk_sqs.models import ( + ReceiveMessageInput, + SendMessageBatchInput, + SendMessageBatchRequestEntry, + SendMessageInput, +) +from pytest import raises +from smithy_test import mock + +JSON_HEADERS = {"content-type": "application/x-amz-json-1.0"} +CORRECT_MD5 = md5(b"body").hexdigest() +INCORRECT_MD5 = md5(b"bodya").hexdigest() + + +def test_runs_md5_checksums_on_received_messages_with_correct_md5_hashes() -> None: + """ReceiveMessage accepts messages whose MD5 digests match their bodies.""" + client = SQSClient(SQSConfig.resolve(region="us-west-2")) + mock(client).expect_request().respond_with( + headers=JSON_HEADERS, + body=json.dumps( + { + "Messages": [ + {"MessageId": "MSGID", "MD5OfBody": CORRECT_MD5, "Body": "body"} + ] + } + ), + ) + + receive = client.receive_message( + ReceiveMessageInput(queue_url="https://my-queue.queue") + ) + + assert receive.messages is not None + assert receive.messages[0].body == "body" + + +def test_runs_md5_checksums_on_sent_messages_with_correct_md5_hashes() -> None: + """SendMessage accepts a response whose MD5 digest matches the sent body.""" + client = SQSClient(SQSConfig.resolve(region="us-west-2")) + mock(client).expect_request().respond_with( + headers=JSON_HEADERS, + body=json.dumps({"MD5OfMessageBody": CORRECT_MD5, "MessageId": "MSGID"}), + ) + + send = client.send_message( + SendMessageInput(queue_url="https://my-queue.queue", message_body="body") + ) + + assert send.message_id == "MSGID" + + +def test_runs_md5_checksums_on_batch_sent_messages_with_correct_md5_hashes() -> None: + """SendMessageBatch accepts responses whose MD5 digests match the sent bodies.""" + client = SQSClient(SQSConfig.resolve(region="us-west-2")) + mock(client).expect_request().respond_with( + headers=JSON_HEADERS, + body=json.dumps( + { + "Successful": [ + {"Id": id, "MessageId": id, "MD5OfMessageBody": CORRECT_MD5} + for id in ("MSGID1", "MSGID2", "MSGID3") + ] + } + ), + ) + + batch_send = client.send_message_batch( + SendMessageBatchInput( + queue_url="https://my-queue.queue", + entries=[ + SendMessageBatchRequestEntry(id="MSGID1", message_body="body"), + SendMessageBatchRequestEntry(id="MSGID2", message_body="body"), + SendMessageBatchRequestEntry(id="MSGID3", message_body="body"), + ], + ) + ) + + assert batch_send.successful[0].message_id == "MSGID1" + + +# TODO: validate SQS message MD5 digests +@pytest.mark.skip +def test_runs_md5_checksums_on_received_messages_with_incorrect_md5_hashes() -> None: + """ReceiveMessage raises an error for a message whose MD5 digest mismatches its body.""" + client = SQSClient(SQSConfig.resolve(region="us-west-2")) + mock(client).expect_request().respond_with( + headers=JSON_HEADERS, + body=json.dumps( + { + "Messages": [ + {"MessageId": "MSGID", "MD5OfBody": INCORRECT_MD5, "Body": "body"} + ] + } + ), + ) + + with raises(Exception, match="Invalid MD5 checksum"): + client.receive_message(ReceiveMessageInput(queue_url="https://my-queue.queue")) + + +# TODO: validate SQS message MD5 digests +@pytest.mark.skip +def test_runs_md5_checksums_on_sent_messages_with_incorrect_md5_hashes() -> None: + """SendMessage raises an error for a response whose MD5 digest mismatches the sent body.""" + client = SQSClient(SQSConfig.resolve(region="us-west-2")) + mock(client).expect_request().respond_with( + headers=JSON_HEADERS, + body=json.dumps({"MD5OfMessageBody": INCORRECT_MD5, "MessageId": "MSGID"}), + ) + + with raises(Exception, match="InvalidChecksumError"): + client.send_message( + SendMessageInput(queue_url="https://my-queue.queue", message_body="body") + ) + + +# TODO: validate SQS message MD5 digests +@pytest.mark.skip +def test_runs_md5_checksums_on_batch_sent_messages_with_incorrect_md5_hashes() -> None: + """SendMessageBatch raises an error for a response whose MD5 digest mismatches a sent body.""" + client = SQSClient(SQSConfig.resolve(region="us-west-2")) + mock(client).expect_request().respond_with( + headers=JSON_HEADERS, + body=json.dumps( + { + "Successful": [ + {"Id": id, "MessageId": id, "MD5OfMessageBody": INCORRECT_MD5} + for id in ("MSGID1", "MSGID2", "MSGID3") + ] + } + ), + ) + + with raises(Exception, match="Invalid MD5 checksum"): + client.send_message_batch( + SendMessageBatchInput( + queue_url="https://my-queue.queue", + entries=[ + SendMessageBatchRequestEntry(id="MSGID1", message_body="body"), + SendMessageBatchRequestEntry(id="MSGID2", message_body="body"), + SendMessageBatchRequestEntry(id="MSGID3", message_body="body"), + ], + ) + ) + + +# TODO: use QueueUrl as the endpoint +@pytest.mark.skip +def test_should_send_the_x_amzn_query_mode_header_when_in_aws_query_compatible_mode() -> ( + None +): + """Requests carry `x-amzn-query-mode: true` for the awsQuery-compatible protocol.""" + client = SQSClient(SQSConfig.resolve(region="us-west-2")) + mock(client).expect_request( + hostname="abc.com", + scheme="https", + path="/", + headers={"x-amzn-query-mode": "true"}, + ) + + client.send_message( + SendMessageInput(queue_url="https://abc.com/123/MyQueue", message_body="hello") + ) + + +# TODO: use QueueUrl as the endpoint +@pytest.mark.skip +def test_should_override_resolved_endpoint_by_default() -> None: + """The QueueUrl input replaces the resolved endpoint.""" + client = SQSClient(SQSConfig.resolve(region="us-west-2")) + mock(client).expect_request(hostname="abc.com", scheme="https", path="/") + + client.send_message( + SendMessageInput(queue_url="https://abc.com/123/MyQueue", message_body="hello") + ) + + +# TODO: add the use_queue_url_as_endpoint config option +@pytest.mark.skip +def test_does_not_override_endpoint_if_shut_off_with_use_queue_url_as_endpoint_false() -> ( + None +): + """With use_queue_url_as_endpoint set to False, the resolved endpoint stays in place.""" + client = SQSClient( + SQSConfig.resolve( # pyright: ignore[reportUnknownArgumentType] + region="us-west-2", + use_queue_url_as_endpoint=False, # pyright: ignore[reportCallIssue] + ) + ) + mock(client).expect_request( + hostname="sqs.us-west-2.amazonaws.com", scheme="https", path="/" + ) + + client.send_message( + SendMessageInput(queue_url="https://abc.com/123/MyQueue", message_body="hello") + ) + + +def test_does_not_override_endpoint_if_custom_endpoint_given_to_client() -> None: + """A custom client endpoint takes precedence over the QueueUrl input.""" + client = SQSClient( + SQSConfig.resolve( + region="us-west-2", endpoint_uri="https://custom-endpoint.com/" + ) + ) + mock(client).expect_request( + hostname="custom-endpoint.com", scheme="https", path="/" + ) + + client.send_message( + SendMessageInput(queue_url="https://abc.com/123/MyQueue", message_body="hello") + ) diff --git a/clients/aws-sdk-sts/tests/functional/__init__.py b/clients/aws-sdk-sts/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-sts/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-sts/tests/functional/test_retry_customizations.py b/clients/aws-sdk-sts/tests/functional/test_retry_customizations.py new file mode 100644 index 000000000..0889eaa1f --- /dev/null +++ b/clients/aws-sdk-sts/tests/functional/test_retry_customizations.py @@ -0,0 +1,65 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from typing import Any + +import pytest +from aws_sdk_sts.client import STSClient +from aws_sdk_sts.config import STSConfig +from aws_sdk_sts.models import GetCallerIdentityInput +from smithy_core.client import RequestPipeline +from smithy_test import mock + + +# TODO: retry IDPCommunicationError +@pytest.mark.skip +def test_idp_communication_error_is_retryable(monkeypatch: pytest.MonkeyPatch) -> None: + # Only the attempt count matters here, so the backoff waits are skipped. + """STS retries IDPCommunicationError.""" + monkeypatch.setattr(RequestPipeline, "_sleep", _no_sleep) + client = STSClient(STSConfig.resolve(region="us-west-2")) + controller = mock(client) + controller.expect_request(hostname="/./").respond_with( + status_code=400, + headers={"content-type": "text/xml"}, + body=""" + + Sender + IDPCommunicationError + Oh No 1 + + c6104cbe-af31-11e0-8154-cbc7ccf896c7 +""", + ).respond_with( + status_code=400, + headers={"content-type": "text/xml"}, + body=""" + + Sender + IDPCommunicationError + Oh No 2 + + c6104cbe-af31-11e0-8154-cbc7ccf896c7 +""", + ).respond_with( + status_code=200, + headers={"content-type": "text/xml"}, + body=""" + + arn:aws:sts::123456789012:assumed-role/demo/TestAR + ARO123EXAMPLE123:TestAR + 123456789012 + + + c6104cbe-af31-11e0-8154-cbc7ccf896c7 + +""", + ) + + client.get_caller_identity(GetCallerIdentityInput()) + + assert len(controller.requests) == 3 + + +def _no_sleep(self: RequestPipeline[Any, Any], delay: float) -> None: + pass diff --git a/clients/aws-sdk-transcribe-streaming/tests/functional/__init__.py b/clients/aws-sdk-transcribe-streaming/tests/functional/__init__.py new file mode 100644 index 000000000..04f8b7b76 --- /dev/null +++ b/clients/aws-sdk-transcribe-streaming/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/clients/aws-sdk-transcribe-streaming/tests/functional/test_event_stream_headers.py b/clients/aws-sdk-transcribe-streaming/tests/functional/test_event_stream_headers.py new file mode 100644 index 000000000..66dc8dd03 --- /dev/null +++ b/clients/aws-sdk-transcribe-streaming/tests/functional/test_event_stream_headers.py @@ -0,0 +1,31 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +from aws_sdk_transcribe_streaming.aio.client import AsyncTranscribeStreamingClient +from aws_sdk_transcribe_streaming.aio.config import AsyncTranscribeStreamingConfig +from aws_sdk_transcribe_streaming.models import StartStreamTranscriptionInput +from smithy_test.aio import mock_async + + +async def test_should_set_streaming_headers() -> None: + """The event stream request carries the event-stream content type and the streaming payload hash.""" + async with AsyncTranscribeStreamingClient( + await AsyncTranscribeStreamingConfig.resolve(region="us-west-2") + ) as client: + async with await mock_async(client) as controller: + controller.expect_request( + headers={ + "content-type": "application/vnd.amazon.eventstream", + "x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-EVENTS", + } + ).respond_with( + headers={"content-type": "application/vnd.amazon.eventstream"} + ) + + async with await client.start_stream_transcription( + StartStreamTranscriptionInput( + media_sample_rate_hertz=144, media_encoding="ogg-opus" + ) + ) as stream: + await stream.input_stream.close() + await stream.await_output() diff --git a/codegen/gen_client.py b/codegen/gen_client.py index d68cc6dc3..fbf9d7cdb 100644 --- a/codegen/gen_client.py +++ b/codegen/gen_client.py @@ -38,6 +38,7 @@ ".changes", "CHANGELOG.md", "examples", + "tests/functional", "tests/integration", ) From 6bcf47f1bdf006314a6dfd909e42133323a27502 Mon Sep 17 00:00:00 2001 From: George Fu Date: Fri, 13 Nov 2026 20:22:00 +0000 Subject: [PATCH 2/2] Use the S3 endpoint variable in S3 env endpoint tests --- clients/aws-sdk-s3/tests/functional/test_env_endpoint.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clients/aws-sdk-s3/tests/functional/test_env_endpoint.py b/clients/aws-sdk-s3/tests/functional/test_env_endpoint.py index 44377bf56..33f1ac6e2 100644 --- a/clients/aws-sdk-s3/tests/functional/test_env_endpoint.py +++ b/clients/aws-sdk-s3/tests/functional/test_env_endpoint.py @@ -12,7 +12,7 @@ def test_should_override_a_client_endpoint_if_none_is_set_on_the_client( monkeypatch: pytest.MonkeyPatch, ) -> None: """AWS_ENDPOINT_URL sets the endpoint of a client configured without one.""" - monkeypatch.delenv("AWS_ENDPOINT_URL_DYNAMODB", raising=False) + monkeypatch.delenv("AWS_ENDPOINT_URL_S3", raising=False) monkeypatch.setenv("AWS_ENDPOINT_URL", "https://localhost/1") client = S3Client(S3Config.resolve(region="us-west-2")) with mock(client) as controller: @@ -25,7 +25,7 @@ def test_should_defer_to_the_client_s_manually_set_endpoint( monkeypatch: pytest.MonkeyPatch, ) -> None: """An endpoint set on the client takes precedence over AWS_ENDPOINT_URL.""" - monkeypatch.delenv("AWS_ENDPOINT_URL_DYNAMODB", raising=False) + monkeypatch.delenv("AWS_ENDPOINT_URL_S3", raising=False) monkeypatch.setenv("AWS_ENDPOINT_URL", "https://localhost/1") client = S3Client( S3Config.resolve(region="us-west-2", endpoint_uri="https://localhost/2") @@ -41,9 +41,9 @@ def test_should_allow_a_specific_service_id_to_be_chosen_in_the_env_variable_nam ) -> None: """AWS_ENDPOINT_URL_S3 sets the endpoint for the S3 client.""" monkeypatch.delenv("AWS_ENDPOINT_URL", raising=False) - monkeypatch.setenv("AWS_ENDPOINT_URL_DYNAMODB", "https://localhost/1") + monkeypatch.setenv("AWS_ENDPOINT_URL_S3", "https://localhost/1") client = S3Client(S3Config.resolve(region="us-west-2")) with mock(client) as controller: - controller.expect_request(hostname="/s3\\.us-west-2\\.amazonaws\\.com/") + controller.expect_request(hostname="/localhost/", path="/\\/1/") client.list_buckets(ListBucketsInput())