-
Notifications
You must be signed in to change notification settings - Fork 16
Add conftest fixture to transcribe-streaming integration tests #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Alan4506
merged 5 commits into
awslabs:develop
from
Alan4506:feat/transcribe-streaming-conftest
May 12, 2026
+187
−115
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2688582
Add conftest fixture to transcribe-streaming integration tests
Alan4506 304d386
Ensure fixture cleanup on partial setup failure
Alan4506 be73af5
Address reviewer feedback regarding conftest and IAM role propagation
Alan4506 395d0a6
Address reviewer feedback on conftest and retry handling
Alan4506 a87462a
Add tags and prefix to integ-test resources for orphan cleanup
Alan4506 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
clients/aws-sdk-transcribe-streaming/tests/integration/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Pytest fixtures for Transcribe Streaming integration tests. | ||
|
|
||
| Creates and tears down an IAM role and S3 bucket needed for medical scribe | ||
| integration tests once per test session. The ``healthscribe_resources`` | ||
| fixture provides the role ARN and bucket name. | ||
| """ | ||
|
|
||
| import json | ||
| import uuid | ||
| from typing import Any | ||
|
|
||
| import boto3 | ||
|
Alan4506 marked this conversation as resolved.
|
||
| import pytest | ||
|
|
||
| REGION = "us-east-1" | ||
|
|
||
| # Tags applied to all resources so orphaned resources from interrupted | ||
| # test runs can be discovered and cleaned up. | ||
| _TAGS = [{"Key": "Purpose", "Value": "IntegTest"}] | ||
|
|
||
|
|
||
| def _create_iam_role(iam_client: Any, role_name: str, bucket_name: str) -> None: | ||
| """Create an IAM role with S3 PutObject access for Transcribe Streaming. | ||
|
|
||
| Args: | ||
| iam_client: A boto3 IAM client. | ||
| role_name: The name of the IAM role to create. | ||
| bucket_name: The name of the S3 bucket the role is allowed to write to. | ||
| """ | ||
| trust_policy = { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Effect": "Allow", | ||
| "Principal": {"Service": ["transcribe.streaming.amazonaws.com"]}, | ||
| "Action": "sts:AssumeRole", | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| iam_client.create_role( | ||
| RoleName=role_name, | ||
| AssumeRolePolicyDocument=json.dumps(trust_policy), | ||
| Tags=_TAGS, | ||
| ) | ||
|
|
||
| permissions_policy = { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Action": ["s3:PutObject"], | ||
| "Resource": [ | ||
| f"arn:aws:s3:::{bucket_name}", | ||
| f"arn:aws:s3:::{bucket_name}/*", | ||
| ], | ||
| "Effect": "Allow", | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| iam_client.put_role_policy( | ||
| RoleName=role_name, | ||
| PolicyName="healthscribe-s3-access", | ||
| PolicyDocument=json.dumps(permissions_policy), | ||
| ) | ||
|
|
||
|
|
||
| def _create_healthscribe_resources( | ||
| iam_client: Any, s3_client: Any, sts_client: Any, role_name: str, bucket_name: str | ||
| ) -> str: | ||
| """Create an IAM role and S3 bucket for medical scribe tests. | ||
|
|
||
| Args: | ||
| iam_client: A boto3 IAM client. | ||
| s3_client: A boto3 S3 client. | ||
| sts_client: A boto3 STS client. | ||
| role_name: The name of the IAM role to create. | ||
| bucket_name: The name of the S3 bucket to create. | ||
|
|
||
| Returns: | ||
| The IAM role ARN. | ||
| """ | ||
| account_id = sts_client.get_caller_identity()["Account"] | ||
|
|
||
| s3_client.create_bucket(Bucket=bucket_name) | ||
| s3_client.put_bucket_tagging(Bucket=bucket_name, Tagging={"TagSet": _TAGS}) | ||
| _create_iam_role(iam_client, role_name, bucket_name) | ||
|
|
||
| return f"arn:aws:iam::{account_id}:role/{role_name}" | ||
|
|
||
|
|
||
| def _delete_healthscribe_resources( | ||
| iam_client: Any, s3_client: Any, role_name: str, bucket_name: str | ||
| ) -> None: | ||
| """Delete the IAM role and S3 bucket created for tests. | ||
|
|
||
| Args: | ||
| iam_client: A boto3 IAM client. | ||
| s3_client: A boto3 S3 client. | ||
| role_name: The name of the IAM role to delete. | ||
| bucket_name: The name of the S3 bucket to delete. | ||
| """ | ||
| # Empty and delete the bucket | ||
| try: | ||
| paginator = s3_client.get_paginator("list_objects_v2") | ||
| for page in paginator.paginate(Bucket=bucket_name): | ||
| objects = page.get("Contents") | ||
| if not objects: | ||
| continue | ||
| s3_client.delete_objects( | ||
| Bucket=bucket_name, | ||
| Delete={"Objects": [{"Key": o["Key"]} for o in objects]}, | ||
| ) | ||
| s3_client.delete_bucket(Bucket=bucket_name) | ||
| except s3_client.exceptions.NoSuchBucket: | ||
| pass | ||
|
|
||
| # Delete inline policy then role | ||
| try: | ||
| iam_client.delete_role_policy( | ||
| RoleName=role_name, PolicyName="healthscribe-s3-access" | ||
| ) | ||
| except iam_client.exceptions.NoSuchEntityException: | ||
| pass | ||
|
|
||
| try: | ||
| iam_client.delete_role(RoleName=role_name) | ||
| except iam_client.exceptions.NoSuchEntityException: | ||
| pass | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def healthscribe_resources(): | ||
| """Create HealthScribe resources for the test session and delete them after.""" | ||
| # Shortened UUID to keep IAM role name under the 64-character limit. | ||
| unique_suffix = uuid.uuid4().hex[:16] | ||
| role_name = f"integ-test-transcribe-streaming-role-{unique_suffix}" | ||
| bucket_name = f"integ-test-transcribe-streaming-bucket-{unique_suffix}" | ||
|
|
||
| iam_client = boto3.client("iam") | ||
| s3_client = boto3.client("s3", region_name=REGION) | ||
| sts_client = boto3.client("sts") | ||
|
|
||
| try: | ||
| role_arn = _create_healthscribe_resources( | ||
| iam_client, s3_client, sts_client, role_name, bucket_name | ||
| ) | ||
| yield role_arn, bucket_name | ||
| finally: | ||
| _delete_healthscribe_resources(iam_client, s3_client, role_name, bucket_name) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 0 additions & 97 deletions
97
clients/aws-sdk-transcribe-streaming/tests/setup_resources.py
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.