From d133a15d0daba8eb2a3085db64936502ebffaab9 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Tue, 10 Dec 2024 14:10:09 -0500 Subject: [PATCH 1/2] add validation before creating an s3 bucket for scenario testing --- .../localstack/testing/scenario/provisioning.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/localstack-core/localstack/testing/scenario/provisioning.py b/localstack-core/localstack/testing/scenario/provisioning.py index 62c984a821694..71ddf40fad11e 100644 --- a/localstack-core/localstack/testing/scenario/provisioning.py +++ b/localstack-core/localstack/testing/scenario/provisioning.py @@ -6,7 +6,7 @@ from typing import TYPE_CHECKING, Callable, ContextManager, Optional import aws_cdk as cdk -from botocore.exceptions import WaiterError +from botocore.exceptions import ClientError, WaiterError from localstack.config import is_env_true from localstack.testing.aws.util import is_aws_cloud @@ -402,7 +402,12 @@ def _template_bucket_name(self): return f"localstack-testing-assets-{account_id}-{region}" def _create_bucket_if_not_exists(self, template_bucket_name: str): - create_s3_bucket(template_bucket_name, s3_client=self.aws_client.s3) + try: + self.aws_client.s3.head_bucket(Bucket=template_bucket_name) + except ClientError as exc: + if exc.response["Error"]["Message"] != "Not Found": + raise exc + create_s3_bucket(template_bucket_name, s3_client=self.aws_client.s3) def _synth(self): # TODO: this doesn't actually synth a CloudAssembly yet From 4282d6028bdb9d3c663e52bd190762147f69dad7 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 11 Dec 2024 17:48:51 -0500 Subject: [PATCH 2/2] requested changes --- .../localstack/testing/scenario/provisioning.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/localstack-core/localstack/testing/scenario/provisioning.py b/localstack-core/localstack/testing/scenario/provisioning.py index 71ddf40fad11e..cc384d3046c65 100644 --- a/localstack-core/localstack/testing/scenario/provisioning.py +++ b/localstack-core/localstack/testing/scenario/provisioning.py @@ -28,7 +28,8 @@ "Delay": 6, "MaxAttempts": 600, } # total timeout ~1 hour (6 * 600 = 3_600 seconds) -WAITER_CONFIG_LS = {"Delay": 1, "MaxAttempts": 600} # total timeout ~10 minutes +# total timeout ~10 minutes +WAITER_CONFIG_LS = {"Delay": 1, "MaxAttempts": 600} CFN_MAX_TEMPLATE_SIZE = 51_200 @@ -320,7 +321,8 @@ def add_cdk_stack( with open(template_path, "wt") as fd: template_json = cdk.assertions.Template.from_stack(cdk_stack).to_json() json.dump(template_json, fd, indent=2) - fd.write("\n") # add trailing newline for linter and Git compliance + # add trailing newline for linter and Git compliance + fd.write("\n") self.cloudformation_stacks[cdk_stack.stack_name] = { "StackName": cdk_stack.stack_name, @@ -405,8 +407,8 @@ def _create_bucket_if_not_exists(self, template_bucket_name: str): try: self.aws_client.s3.head_bucket(Bucket=template_bucket_name) except ClientError as exc: - if exc.response["Error"]["Message"] != "Not Found": - raise exc + if exc.response["Error"]["Code"] != "404": + raise create_s3_bucket(template_bucket_name, s3_client=self.aws_client.s3) def _synth(self):