From 9bc5f50b19d7e7b2e5d459b00f482a6a162f2fcd Mon Sep 17 00:00:00 2001 From: Ava Wang <57644099+avawang1@users.noreply.github.com> Date: Mon, 5 Oct 2020 15:56:05 -0700 Subject: [PATCH] infra: change check for s3 bucket exists (#164) --- test/integ_tests/conftest.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/test/integ_tests/conftest.py b/test/integ_tests/conftest.py index a4cb2bf2b..1a3b63c41 100644 --- a/test/integ_tests/conftest.py +++ b/test/integ_tests/conftest.py @@ -15,6 +15,7 @@ import boto3 import pytest +from botocore.exceptions import ClientError from braket.aws.aws_session import AwsSession @@ -36,15 +37,32 @@ def s3_resource(boto_session): @pytest.fixture(scope="session") -def s3_bucket(s3_resource, boto_session): +def s3_client(boto_session): + return boto_session.client("s3") + + +@pytest.fixture(scope="session") +def account_id(boto_session): + return boto_session.client("sts").get_caller_identity()["Account"] + + +@pytest.fixture(scope="session") +def s3_bucket(s3_resource, s3_client, account_id, boto_session): """Create / get S3 bucket for tests""" region_name = boto_session.region_name - account_id = boto_session.client("sts").get_caller_identity()["Account"] - bucket_name = f"amazon-braket-sdk-integ-tests-{account_id}" + bucket_name = f"amazon-braket-ocean-plugin-integ-tests-{account_id}" bucket = s3_resource.Bucket(bucket_name) - if not bucket.creation_date: - bucket.create(ACL="private", CreateBucketConfiguration={"LocationConstraint": region_name}) + + try: + # Determine if bucket exists + s3_client.head_bucket(Bucket=bucket_name) + except ClientError as e: + error_code = e.response["Error"]["Code"] + if error_code == "404": + bucket.create( + ACL="private", CreateBucketConfiguration={"LocationConstraint": region_name} + ) return bucket_name