Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from aws_sdk_bedrock_runtime.aio.client import AsyncBedrockRuntimeClient
from aws_sdk_bedrock_runtime.aio.config import AsyncBedrockRuntimeConfig
from smithy_aws_core.aio.config import AsyncAwsConfigOverrides
from smithy_aws_core.aio.identity import AsyncEnvironmentCredentialsResolver
from smithy_http.aio.interfaces import AsyncHTTPClient

MODEL_ID = "global.amazon.nova-2-lite-v1:0"
Expand All @@ -23,7 +22,6 @@ async def create_bedrock_client(
overrides: AsyncAwsConfigOverrides = {
"endpoint_uri": f"https://bedrock-runtime.{region}.amazonaws.com",
"region": region,
"aws_credentials_identity_resolver": AsyncEnvironmentCredentialsResolver(),
}
if transport is not None:
overrides["transport"] = transport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from aws_sdk_bedrock_runtime.models import (
ContentBlockDeltaText,
ConverseStreamMetadataEvent,
ConverseStreamOperationOutput,
ConverseStreamOutputContentBlockDelta,
ConverseStreamOutputMetadata,
Expand All @@ -21,7 +22,7 @@ async def test_converse_stream() -> None:
)

received_text: list[str] = []
metadata_received = False
metadata: list[ConverseStreamMetadataEvent] = []

async with response as stream:
async for event in stream.output_stream:
Expand All @@ -30,12 +31,17 @@ async def test_converse_stream() -> None:
if isinstance(delta, ContentBlockDeltaText):
received_text.append(delta.value)
elif isinstance(event, ConverseStreamOutputMetadata):
metadata_received = True
assert event.value.usage.input_tokens > 0
assert event.value.usage.output_tokens > 0
metadata.append(event.value)

full_response = "".join(received_text)
assert full_response

assert metadata_received
assert len(metadata) == 1
# Bedrock sometimes reports zero usage for a completed stream, so check
# that the counts deserialized consistently rather than that they are positive.
usage = metadata[0].usage
assert usage.input_tokens >= 0
assert usage.output_tokens >= 0
assert usage.total_tokens == usage.input_tokens + usage.output_tokens
assert metadata[0].metrics.latency_ms > 0
assert isinstance(stream.output, ConverseStreamOperationOutput)
81 changes: 81 additions & 0 deletions clients/aws-sdk-cloudformation/tests/integration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

"""Live CloudFormation integration tests, written once and run in both sync and async modes."""

import json
import uuid

from aws_sdk_cloudformation.aio.client import AsyncCloudFormationClient
from aws_sdk_cloudformation.aio.config import AsyncCloudFormationConfig
from aws_sdk_cloudformation.client import CloudFormationClient
from aws_sdk_cloudformation.config import CloudFormationConfig
from aws_sdk_cloudformation.models import DescribeStacksInput, Stack
from waiters import wait_until

REGION = "us-west-2"

# Lets an interrupted run's leftovers be found and removed.
STACK_PREFIX = "aws-sdk-python-integ-"

QUEUE_TEMPLATE = json.dumps(
{
"Description": "aws-sdk-python integration test",
"Parameters": {"Delay": {"Type": "Number", "Default": 0}},
"Resources": {
"Queue": {
"Type": "AWS::SQS::Queue",
"Properties": {"DelaySeconds": {"Ref": "Delay"}},
}
},
"Outputs": {"QueueUrl": {"Value": {"Ref": "Queue"}}},
}
)

# A fifo-suffixed name without FifoQueue=true is rejected by SQS during creation.
FAILING_TEMPLATE = json.dumps(
{
"Resources": {
"Queue": {
"Type": "AWS::SQS::Queue",
"Properties": {"QueueName": "aws-sdk-python-integ-invalid.fifo"},
}
}
}
)


def unique_stack_name() -> str:
return f"{STACK_PREFIX}{uuid.uuid4().hex[:16]}"


async def create_async_client() -> AsyncCloudFormationClient:
config = await AsyncCloudFormationConfig.resolve(region=REGION)
return AsyncCloudFormationClient(config=config)


def create_sync_client() -> CloudFormationClient:
config = CloudFormationConfig.resolve(region=REGION)
return CloudFormationClient(config=config)


async def wait_for_stack_status(
client: AsyncCloudFormationClient, stack_id: str, target: str, timeout: float = 600
) -> Stack:
"""Wait for the stack to reach ``target``, failing at once on any other settled status."""

async def describe() -> Stack:
output = await client.describe_stacks(DescribeStacksInput(stack_name=stack_id))
assert output.stacks
return output.stacks[0]

def reached_target(stack: Stack) -> bool:
status = stack.stack_status or ""
if status == target:
return True
assert status.endswith("_IN_PROGRESS"), (
f"stack reached {status}: {stack.stack_status_reason}"
)
return False

return await wait_until(describe, reached_target, interval=3, timeout=timeout)
39 changes: 39 additions & 0 deletions clients/aws-sdk-cloudformation/tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

"""Fixtures for the CloudFormation integration tests."""

from collections.abc import AsyncIterator, Iterator
from contextlib import AsyncExitStack
from typing import cast

import pytest
from aws_sdk_cloudformation.aio.client import AsyncCloudFormationClient
from aws_sdk_cloudformation.models import DeleteStackInput
from dual_mode import MODES, Mode, open_client

from . import create_async_client, create_sync_client


@pytest.fixture(params=MODES)
def mode(request: pytest.FixtureRequest) -> Mode:
return cast(Mode, request.param)


@pytest.fixture
async def cloudformation(mode: Mode) -> AsyncIterator[AsyncCloudFormationClient]:
"""A client in the test's mode, closed when the test ends."""
async with AsyncExitStack() as stack:
yield await open_client(stack, mode, create_async_client, create_sync_client)


@pytest.fixture
def created_stacks() -> Iterator[list[str]]:
"""Stack IDs a test appends to are deleted at teardown, even after failures."""
ids: list[str] = []
try:
yield ids
finally:
with create_sync_client() as client:
for stack_id in ids:
client.delete_stack(DeleteStackInput(stack_name=stack_id))
93 changes: 93 additions & 0 deletions clients/aws-sdk-cloudformation/tests/integration/test_stacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

"""Stack lifecycle, validation and pagination, from JS v3 cloudformation-features.e2e."""

from aws_sdk_cloudformation.aio.client import AsyncCloudFormationClient
from aws_sdk_cloudformation.models import (
CreateStackInput,
DeleteStackInput,
DescribeStackEventsInput,
Parameter,
StackStatus,
Tag,
ValidateTemplateInput,
)

from . import FAILING_TEMPLATE, QUEUE_TEMPLATE, unique_stack_name, wait_for_stack_status


async def test_create_describe_delete_stack(
cloudformation: AsyncCloudFormationClient, created_stacks: list[str]
) -> None:
"""A stack created with a parameter and tag reaches CREATE_COMPLETE with its outputs, then deletes."""
name = unique_stack_name()
created = await cloudformation.create_stack(
CreateStackInput(
stack_name=name,
template_body=QUEUE_TEMPLATE,
parameters=[Parameter(parameter_key="Delay", parameter_value="1")],
tags=[Tag(key="Purpose", value="IntegTest")],
)
)
assert created.stack_id is not None
created_stacks.append(created.stack_id)

stack = await wait_for_stack_status(
cloudformation, created.stack_id, StackStatus.CREATE_COMPLETE
)
assert stack.stack_name == name
assert stack.stack_id == created.stack_id
assert [(p.parameter_key, p.parameter_value) for p in stack.parameters or []] == [
("Delay", "1")
]
assert [(t.key, t.value) for t in stack.tags or []] == [("Purpose", "IntegTest")]
outputs = {o.output_key: o.output_value for o in stack.outputs or []}
assert (outputs["QueueUrl"] or "").startswith("https://sqs.")

await cloudformation.delete_stack(DeleteStackInput(stack_name=created.stack_id))
# Deleted stacks stay describable by ID only.
deleted = await wait_for_stack_status(
cloudformation, created.stack_id, StackStatus.DELETE_COMPLETE
)
assert deleted.deletion_time is not None


async def test_failed_create_rolls_back_with_reason(
cloudformation: AsyncCloudFormationClient, created_stacks: list[str]
) -> None:
"""A stack whose resource fails to create rolls back and records the failure in its events."""
created = await cloudformation.create_stack(
CreateStackInput(stack_name=unique_stack_name(), template_body=FAILING_TEMPLATE)
)
assert created.stack_id is not None
created_stacks.append(created.stack_id)

await wait_for_stack_status(
cloudformation, created.stack_id, StackStatus.ROLLBACK_COMPLETE
)

events = await cloudformation.describe_stack_events(
DescribeStackEventsInput(stack_name=created.stack_id)
)
failures = [
e
for e in events.stack_events or []
if e.resource_status == StackStatus.CREATE_FAILED
and e.logical_resource_id == "Queue"
]
assert failures
assert failures[0].resource_status_reason


async def test_validate_template_reports_parameters_and_description(
cloudformation: AsyncCloudFormationClient,
) -> None:
"""ValidateTemplate returns the template's description and parameter declarations."""
result = await cloudformation.validate_template(
ValidateTemplateInput(template_body=QUEUE_TEMPLATE)
)
assert result.description == "aws-sdk-python integration test"
assert [(p.parameter_key, p.default_value) for p in result.parameters or []] == [
("Delay", "0")
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

"""Live CloudFront KeyValueStore integration tests, written once and run in both sync and async modes."""

import os
import uuid

from aws_sdk_cloudfront_keyvaluestore.aio.client import (
AsyncCloudFrontKeyValueStoreClient,
)
from aws_sdk_cloudfront_keyvaluestore.aio.config import (
AsyncCloudFrontKeyValueStoreConfig,
)
from aws_sdk_cloudfront_keyvaluestore.client import CloudFrontKeyValueStoreClient
from aws_sdk_cloudfront_keyvaluestore.config import CloudFrontKeyValueStoreConfig

REGION = "us-west-2"

# The store is pre-provisioned because creating one needs the CloudFront client,
# which is not a dependency of this package's tests.
STORE_ARN_ENV = "AWS_SDK_PYTHON_INTEG_KVS_ARN"

# Lets an interrupted run's leftovers be found and removed.
NAME_PREFIX = "aws-sdk-python-integ-"


def store_arn() -> str | None:
return os.environ.get(STORE_ARN_ENV)


def unique_name() -> str:
return f"{NAME_PREFIX}{uuid.uuid4().hex[:16]}"


async def create_async_client() -> AsyncCloudFrontKeyValueStoreClient:
config = await AsyncCloudFrontKeyValueStoreConfig.resolve(region=REGION)
return AsyncCloudFrontKeyValueStoreClient(config=config)


def create_sync_client() -> CloudFrontKeyValueStoreClient:
config = CloudFrontKeyValueStoreConfig.resolve(region=REGION)
return CloudFrontKeyValueStoreClient(config=config)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

"""Fixtures for the CloudFront KeyValueStore integration tests."""

from collections.abc import AsyncIterator
from contextlib import AsyncExitStack
from typing import cast

import pytest
from aws_sdk_cloudfront_keyvaluestore.aio.client import (
AsyncCloudFrontKeyValueStoreClient,
)
from dual_mode import MODES, Mode, open_client

from . import STORE_ARN_ENV, create_async_client, create_sync_client, store_arn


@pytest.fixture(params=MODES)
def mode(request: pytest.FixtureRequest) -> Mode:
return cast(Mode, request.param)


@pytest.fixture
async def kvs(mode: Mode) -> AsyncIterator[AsyncCloudFrontKeyValueStoreClient]:
"""A client in the test's mode, closed when the test ends."""
async with AsyncExitStack() as stack:
yield await open_client(stack, mode, create_async_client, create_sync_client)


@pytest.fixture
def kvs_arn() -> str:
"""The ARN of an empty, pre-provisioned store the tests may modify."""
arn = store_arn()
if arn is None:
pytest.skip(f"{STORE_ARN_ENV} is not set")
return arn
Loading