|
6 | 6 |
|
7 | 7 | from integration.helpers.base_test import S3_BUCKET_PREFIX
|
8 | 8 | from integration.helpers.client_provider import ClientProvider
|
9 |
| -from integration.helpers.deployer.exceptions.exceptions import ThrottlingError |
| 9 | +from integration.helpers.deployer.exceptions.exceptions import S3DoesNotExistException, ThrottlingError |
10 | 10 | from integration.helpers.deployer.utils.retry import retry_with_exponential_backoff_and_jitter
|
11 | 11 | from integration.helpers.stack import Stack
|
12 | 12 | from integration.helpers.yaml_utils import load_yaml
|
| 13 | +from integration.helpers.resource import read_test_config_file, write_test_config_file_to_json |
13 | 14 |
|
14 | 15 | try:
|
15 | 16 | from pathlib import Path
|
@@ -63,10 +64,57 @@ def setup_companion_stack_once(tmpdir_factory, get_prefix):
|
63 | 64 | cfn_client = ClientProvider().cfn_client
|
64 | 65 | output_dir = tmpdir_factory.mktemp("data")
|
65 | 66 | stack_name = get_prefix + COMPANION_STACK_NAME
|
66 |
| - if _stack_exists(stack_name): |
67 |
| - return |
68 | 67 | companion_stack = Stack(stack_name, companion_stack_tempalte_path, cfn_client, output_dir)
|
69 |
| - companion_stack.create() |
| 68 | + companion_stack.create_or_update(_stack_exists(stack_name)) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture() |
| 72 | +def upload_resources(get_s3): |
| 73 | + """ |
| 74 | + Creates the bucket and uploads the files used by the tests to it |
| 75 | + """ |
| 76 | + s3_bucket = get_s3 |
| 77 | + if not _s3_exists(s3_bucket): |
| 78 | + raise S3DoesNotExistException(get_s3, "Check companion stack status") |
| 79 | + code_dir = Path(__file__).resolve().parents[0].joinpath("resources").joinpath("code") |
| 80 | + file_to_s3_uri_map = read_test_config_file("file_to_s3_map.json") |
| 81 | + |
| 82 | + if not file_to_s3_uri_map or not file_to_s3_uri_map.items(): |
| 83 | + LOG.debug("No resources to upload") |
| 84 | + return |
| 85 | + |
| 86 | + current_file_name = "" |
| 87 | + |
| 88 | + try: |
| 89 | + s3_client = ClientProvider().s3_client |
| 90 | + session = boto3.session.Session() |
| 91 | + region = session.region_name |
| 92 | + for file_name, file_info in file_to_s3_uri_map.items(): |
| 93 | + current_file_name = file_name |
| 94 | + code_path = str(Path(code_dir, file_name)) |
| 95 | + LOG.debug("Uploading file %s to bucket %s", file_name, s3_bucket) |
| 96 | + s3_client.upload_file(code_path, s3_bucket, file_name) |
| 97 | + LOG.debug("File %s uploaded successfully to bucket %s", file_name, s3_bucket) |
| 98 | + file_info["uri"] = get_s3_uri(file_name, file_info["type"], s3_bucket, region) |
| 99 | + except ClientError as error: |
| 100 | + LOG.error("Upload of file %s to bucket %s failed", current_file_name, s3_bucket, exc_info=error) |
| 101 | + raise error |
| 102 | + |
| 103 | + write_test_config_file_to_json("file_to_s3_map_modified.json", file_to_s3_uri_map) |
| 104 | + |
| 105 | + |
| 106 | +def get_s3_uri(file_name, uri_type, bucket, region): |
| 107 | + if uri_type == "s3": |
| 108 | + return "s3://{}/{}".format(bucket, file_name) |
| 109 | + |
| 110 | + if region == "us-east-1": |
| 111 | + return "https://s3.amazonaws.com/{}/{}".format(bucket, file_name) |
| 112 | + if region == "us-iso-east-1": |
| 113 | + return "https://s3.us-iso-east-1.c2s.ic.gov/{}/{}".format(bucket, file_name) |
| 114 | + if region == "us-isob-east-1": |
| 115 | + return "https://s3.us-isob-east-1.sc2s.sgov.gov/{}/{}".format(bucket, file_name) |
| 116 | + |
| 117 | + return "https://s3-{}.amazonaws.com/{}/{}".format(region, bucket, file_name) |
70 | 118 |
|
71 | 119 |
|
72 | 120 | @pytest.fixture()
|
@@ -99,6 +147,12 @@ def get_companion_stack_outputs(get_prefix):
|
99 | 147 | return get_stack_outputs(companion_stack_description)
|
100 | 148 |
|
101 | 149 |
|
| 150 | +@pytest.fixture() |
| 151 | +def get_s3(get_companion_stack_outputs): |
| 152 | + s3_bucket = get_companion_stack_outputs.get("PreCreatedS3Bucket") |
| 153 | + return str(s3_bucket) |
| 154 | + |
| 155 | + |
102 | 156 | @pytest.fixture()
|
103 | 157 | def get_prefix(request):
|
104 | 158 | prefix = ""
|
@@ -171,3 +225,15 @@ def _stack_exists(stack_name):
|
171 | 225 | raise ex
|
172 | 226 |
|
173 | 227 | return True
|
| 228 | + |
| 229 | + |
| 230 | +@retry_with_exponential_backoff_and_jitter(ThrottlingError, 5, 360) |
| 231 | +def _s3_exists(s3_bucket): |
| 232 | + s3 = boto3.resource("s3") |
| 233 | + bucket = s3.Bucket(s3_bucket) |
| 234 | + try: |
| 235 | + s3.meta.client.head_bucket(Bucket=bucket.name) |
| 236 | + except ClientError: |
| 237 | + return False |
| 238 | + |
| 239 | + return True |
0 commit comments