Skip to content
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

fix: move tests to a static bucket solution #1589

Merged
merged 2 commits into from
Nov 23, 2019
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
18 changes: 11 additions & 7 deletions tests/integration/package/package_integ_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to still do this as a fallback if AWS_S3 is not set? Thinking it is good for local where devs do not need to have additional things preconfigured

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, valid concern.


# 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()
Expand All @@ -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"
Expand Down
32 changes: 19 additions & 13 deletions tests/integration/publish/publish_app_integ_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<bucket-name>"
cls.application_name_placeholder = "<application-name>"
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]
Expand All @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/publish/test_command_integ.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/testdata/publish/s3_bucket_policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"Resource": "arn:aws:s3:::<bucket-name>/*"
}
]
}
}
7 changes: 0 additions & 7 deletions tests/regression/deploy/regression_deploy_base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 13 additions & 9 deletions tests/regression/package/regression_package_base.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down