diff --git a/tests/integration/package/package_integ_base.py b/tests/integration/package/package_integ_base.py index b39115eb30..88e966419a 100644 --- a/tests/integration/package/package_integ_base.py +++ b/tests/integration/package/package_integ_base.py @@ -7,23 +7,26 @@ import boto3 +S3_SLEEP = 3 + class PackageIntegBase(TestCase): @classmethod def setUpClass(cls): cls.region_name = os.environ.get("AWS_DEFAULT_REGION") - cls.bucket_name = str(uuid.uuid4()) + cls.pre_created_bucket = os.environ.get("AWS_S3", False) + cls.bucket_name = cls.pre_created_bucket if cls.pre_created_bucket else str(uuid.uuid4()) cls.test_data_path = Path(__file__).resolve().parents[1].joinpath("testdata", "package") - # Create S3 bucket + # Intialize S3 client s3 = boto3.resource("s3") # Use a pre-created KMS Key cls.kms_key = os.environ.get("AWS_KMS_KEY") + # Use a pre-created S3 Bucket if present else create a new one cls.s3_bucket = s3.Bucket(cls.bucket_name) - cls.s3_bucket.create() - - # Given 3 seconds for all the bucket creation to complete - time.sleep(3) + if not cls.pre_created_bucket: + cls.s3_bucket.create() + time.sleep(S3_SLEEP) def setUp(self): super(PackageIntegBase, self).setUp() @@ -34,7 +37,8 @@ def tearDown(self): @classmethod def tearDownClass(cls): cls.s3_bucket.objects.all().delete() - cls.s3_bucket.delete() + if not cls.pre_created_bucket: + cls.s3_bucket.delete() def base_command(self): command = "sam" diff --git a/tests/integration/publish/publish_app_integ_base.py b/tests/integration/publish/publish_app_integ_base.py index 7d975aa8c5..cf0a1e2d12 100644 --- a/tests/integration/publish/publish_app_integ_base.py +++ b/tests/integration/publish/publish_app_integ_base.py @@ -2,37 +2,42 @@ import json import uuid import shutil -import tempfile import time +import tempfile from unittest import TestCase import boto3 from pathlib import Path +S3_SLEEP = 3 + class PublishAppIntegBase(TestCase): @classmethod def setUpClass(cls): cls.region_name = os.environ.get("AWS_DEFAULT_REGION") - cls.bucket_name = str(uuid.uuid4()) + cls.pre_created_bucket = os.environ.get("AWS_S3", False) + cls.bucket_name = cls.pre_created_bucket if cls.pre_created_bucket else str(uuid.uuid4()) cls.bucket_name_placeholder = "" cls.application_name_placeholder = "" cls.temp_dir = Path(tempfile.mkdtemp()) cls.test_data_path = Path(__file__).resolve().parents[1].joinpath("testdata", "publish") cls.sar_client = boto3.client("serverlessrepo", region_name=cls.region_name) - # Create S3 bucket + # Intialize S3 client s3 = boto3.resource("s3") + # Use a pre-created S3 Bucket if present else create a new one cls.s3_bucket = s3.Bucket(cls.bucket_name) - cls.s3_bucket.create() - - # Given 3 seconds for all the bucket creation to complete - time.sleep(3) - - # Grant serverlessrepo read access to the bucket - bucket_policy_template = cls.test_data_path.joinpath("s3_bucket_policy.json").read_text(encoding="utf-8") - bucket_policy = bucket_policy_template.replace(cls.bucket_name_placeholder, cls.bucket_name) - cls.s3_bucket.Policy().put(Policy=bucket_policy) + if not cls.pre_created_bucket: + cls.s3_bucket.create() + # Wait for bucket to be created. + time.sleep(S3_SLEEP) + # Grant serverlessrepo read access to the bucket + bucket_policy_template = cls.test_data_path.joinpath("s3_bucket_policy.json").read_text(encoding="utf-8") + bucket_policy = bucket_policy_template.replace(cls.bucket_name_placeholder, cls.bucket_name) + cls.s3_bucket.Policy().put(Policy=bucket_policy) + # Wait for bucket policy to be applied. + time.sleep(S3_SLEEP) # Upload test files to S3 root_path = Path(__file__).resolve().parents[3] @@ -53,7 +58,8 @@ def tearDownClass(cls): "Objects": [{"Key": "LICENSE"}, {"Key": "README.md"}, {"Key": "README_UPDATE.md"}, {"Key": "main.py"}] } ) - cls.s3_bucket.delete() + if not cls.pre_created_bucket: + cls.s3_bucket.delete() @classmethod def replace_template_placeholder(cls, placeholder, replace_text): diff --git a/tests/integration/publish/test_command_integ.py b/tests/integration/publish/test_command_integ.py index 0b509b9d73..deb49407c5 100644 --- a/tests/integration/publish/test_command_integ.py +++ b/tests/integration/publish/test_command_integ.py @@ -87,6 +87,8 @@ class TestPublishNewApp(PublishAppIntegBase): def setUp(self): super(TestPublishNewApp, self).setUp() self.application_id = None + # Sleep for a little bit to make server happy + time.sleep(2) def tearDown(self): super(TestPublishNewApp, self).tearDown() diff --git a/tests/integration/testdata/publish/s3_bucket_policy.json b/tests/integration/testdata/publish/s3_bucket_policy.json index 1eb9115c3c..abf3a0cc64 100644 --- a/tests/integration/testdata/publish/s3_bucket_policy.json +++ b/tests/integration/testdata/publish/s3_bucket_policy.json @@ -10,4 +10,4 @@ "Resource": "arn:aws:s3:::/*" } ] -} +} \ No newline at end of file diff --git a/tests/regression/deploy/regression_deploy_base.py b/tests/regression/deploy/regression_deploy_base.py index c564128243..dd8acbc2da 100644 --- a/tests/regression/deploy/regression_deploy_base.py +++ b/tests/regression/deploy/regression_deploy_base.py @@ -1,14 +1,7 @@ import os -import uuid -import json -import tempfile -import time -from pathlib import Path from subprocess import Popen, PIPE from unittest import TestCase -import boto3 - class DeployRegressionBase(TestCase): @classmethod diff --git a/tests/regression/package/regression_package_base.py b/tests/regression/package/regression_package_base.py index 7f80dbb1ba..6827c190a9 100644 --- a/tests/regression/package/regression_package_base.py +++ b/tests/regression/package/regression_package_base.py @@ -1,34 +1,38 @@ import os -import uuid import json -import tempfile import time +import tempfile +import uuid from pathlib import Path from subprocess import Popen, PIPE from unittest import TestCase import boto3 +S3_SLEEP = 3 + class PackageRegressionBase(TestCase): @classmethod def setUpClass(cls): cls.region_name = os.environ.get("AWS_DEFAULT_REGION") - cls.bucket_name = str(uuid.uuid4()) + cls.pre_created_bucket = os.environ.get("AWS_S3", False) + cls.bucket_name = cls.pre_created_bucket if cls.pre_created_bucket else str(uuid.uuid4()) cls.test_data_path = Path(__file__).resolve().parents[2].joinpath("integration", "testdata", "package") - # Create S3 bucket + # Intialize S3 client s3 = boto3.resource("s3") + # Use a pre-created S3 Bucket if present else create a new one cls.s3_bucket = s3.Bucket(cls.bucket_name) - cls.s3_bucket.create() - - # Given 3 seconds for all the bucket creation to complete - time.sleep(3) + if not cls.pre_created_bucket: + cls.s3_bucket.create() + time.sleep(S3_SLEEP) @classmethod def tearDownClass(cls): cls.s3_bucket.objects.all().delete() - cls.s3_bucket.delete() + if not cls.pre_created_bucket: + cls.s3_bucket.delete() def base_command(self, base): command = [base]