Skip to content

Commit fae6ee4

Browse files
authored
chore: Use pre-created SAR app during integration tests (#2507)
* chore: Use pre-created SAR app during integration tests * make black
1 parent f836cd9 commit fae6ee4

File tree

7 files changed

+111
-29
lines changed

7 files changed

+111
-29
lines changed

integration/conftest.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
from integration.helpers.deployer.utils.retry import retry_with_exponential_backoff_and_jitter
1111
from integration.helpers.stack import Stack
1212
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+
from integration.helpers.resource import (
14+
read_test_config_file,
15+
write_test_config_file_to_json,
16+
current_region_does_not_support,
17+
)
1418

1519
try:
1620
from pathlib import Path
@@ -21,6 +25,9 @@
2125

2226
COMPANION_STACK_NAME = "sam-integ-stack-companion"
2327
COMPANION_STACK_TEMPLATE = "companion-stack.yaml"
28+
SAR_APP_TEMPLATE = "example-sar-app.yaml"
29+
SAR_APP_NAME = "sam-integration-test-sar-app"
30+
SAR_APP_VERSION = "1.0.3"
2431

2532

2633
def _get_all_buckets():
@@ -68,6 +75,38 @@ def setup_companion_stack_once(tmpdir_factory, get_prefix):
6875
companion_stack.create_or_update(_stack_exists(stack_name))
6976

7077

78+
@pytest.fixture()
79+
def get_serverless_application_repository_app():
80+
"""Create or re-use a simple SAR app"""
81+
if current_region_does_not_support(["ServerlessRepo"]):
82+
LOG.info("Creating SAR application is skipped since SAR tests are not supported in this region.")
83+
return
84+
85+
sar_client = ClientProvider().sar_client
86+
sar_apps = sar_client.list_applications().get("Applications", [])
87+
for sar_app in sar_apps:
88+
if sar_app.get("Name") == SAR_APP_NAME:
89+
LOG.info("SAR Application was already created, skipping SAR application publish")
90+
return sar_app.get("ApplicationId")
91+
92+
LOG.info("SAR application not found, publishing new one...")
93+
94+
tests_integ_dir = Path(__file__).resolve().parents[1]
95+
template_foler = Path(tests_integ_dir, "integration", "setup")
96+
sar_app_template_path = Path(template_foler, SAR_APP_TEMPLATE)
97+
with open(sar_app_template_path) as f:
98+
sar_template_contents = f.read()
99+
create_app_result = sar_client.create_application(
100+
Author="SAM Team",
101+
Description="SAR Application for Integration Tests",
102+
Name=SAR_APP_NAME,
103+
SemanticVersion=SAR_APP_VERSION,
104+
TemplateBody=sar_template_contents,
105+
)
106+
LOG.info("SAR application creation result: %s", create_app_result)
107+
return create_app_result.get("ApplicationId")
108+
109+
71110
@pytest.fixture()
72111
def upload_resources(get_s3):
73112
"""

integration/helpers/client_provider.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self):
2424
self._iot_client = None
2525
self._kafka_client = None
2626
self._code_deploy_client = None
27+
self._sar_client = None
2728

2829
@property
2930
def cfn_client(self):
@@ -205,3 +206,13 @@ def code_deploy_client(self):
205206
if not self._code_deploy_client:
206207
self._code_deploy_client = boto3.client("codedeploy")
207208
return self._code_deploy_client
209+
210+
@property
211+
def sar_client(self):
212+
"""
213+
Serverless Application Repo. Client
214+
"""
215+
with self._lock:
216+
if not self._sar_client:
217+
self._sar_client = boto3.client("serverlessrepo")
218+
return self._sar_client
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
Parameters:
2+
SarApplicationId:
3+
Type: String
4+
15
Resources:
26
MyNestedApp:
37
Type: AWS::Serverless::Application
48
Properties:
59
Location:
6-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
7-
SemanticVersion: 1.0.2
10+
ApplicationId: !Ref SarApplicationId
11+
SemanticVersion: 1.0.3
812
Parameters:
913
IdentityNameParameter: test

integration/resources/templates/single/basic_application_sar_location_with_intrinsics.yaml

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
11
Parameters:
2-
SemanticVersion:
2+
SarApplicationId:
33
Type: String
4-
Default: 1.0.2
54

65
Mappings:
76
SARApplication:
87
us-east-1:
9-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
8+
SemanticVersion: 1.0.3
109
us-east-2:
11-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
10+
SemanticVersion: 1.0.3
1211
us-west-1:
13-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
12+
SemanticVersion: 1.0.3
1413
us-west-2:
15-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
14+
SemanticVersion: 1.0.3
1615
eu-central-1:
17-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
16+
SemanticVersion: 1.0.3
1817
eu-west-1:
19-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
18+
SemanticVersion: 1.0.3
2019
eu-west-2:
21-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
20+
SemanticVersion: 1.0.3
2221
eu-west-3:
23-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
22+
SemanticVersion: 1.0.3
2423
ap-south-1:
25-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
24+
SemanticVersion: 1.0.3
2625
ap-northeast-1:
27-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
26+
SemanticVersion: 1.0.3
2827
ap-northeast-2:
29-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
28+
SemanticVersion: 1.0.3
3029
ap-southeast-1:
31-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
30+
SemanticVersion: 1.0.3
3231
ap-southeast-2:
33-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
32+
SemanticVersion: 1.0.3
3433
ca-central-1:
35-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
34+
SemanticVersion: 1.0.3
3635
sa-east-1:
37-
ApplicationId: arn:aws:serverlessrepo:us-east-1:077246666028:applications/hello-world-python3
36+
SemanticVersion: 1.0.3
3837

3938
Resources:
4039
MyNestedApp:
4140
Type: AWS::Serverless::Application
4241
Properties:
4342
Location:
4443
ApplicationId:
44+
Ref: SarApplicationId
45+
SemanticVersion:
4546
Fn::FindInMap:
4647
- SARApplication
47-
- {Ref: 'AWS::Region'}
48-
- ApplicationId
49-
SemanticVersion:
50-
Ref: SemanticVersion
48+
- { Ref: 'AWS::Region' }
49+
- SemanticVersion
5150
Parameters:
5251
IdentityNameParameter: test
5352
NotificationARNs:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: Hello world app with inline code
4+
5+
Parameters:
6+
IdentityNameParameter:
7+
Type: String
8+
9+
Resources:
10+
HelloWorldPython3:
11+
Type: AWS::Serverless::Function
12+
Properties:
13+
InlineCode: |
14+
def handler(*args):
15+
return 'Hello world!'
16+
Handler: index.handler
17+
Runtime: python3.9
18+
Timeout: 5
19+
Environment:
20+
Variables:
21+
IdentityNameParameter: !Ref IdentityNameParameter

integration/setup/test_setup_teardown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
@pytest.mark.setup
6-
def test_setup(setup_companion_stack_once, upload_resources, get_s3):
6+
def test_setup(setup_companion_stack_once, upload_resources, get_s3, get_serverless_application_repository_app):
77
assert s3_upload_successful()
88

99

integration/single/test_basic_application.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from unittest.case import skipIf
22

3+
import pytest
4+
35
from integration.helpers.base_test import BaseTest
46
from integration.helpers.resource import current_region_does_not_support
57

@@ -9,6 +11,10 @@ class TestBasicApplication(BaseTest):
911
Basic AWS::Serverless::Application tests
1012
"""
1113

14+
@pytest.fixture(autouse=True)
15+
def companion_stack_outputs(self, get_serverless_application_repository_app):
16+
self.sar_app_id = get_serverless_application_repository_app
17+
1218
@skipIf(
1319
current_region_does_not_support(["ServerlessRepo"]), "ServerlessRepo is not supported in this testing region"
1420
)
@@ -32,13 +38,14 @@ def test_basic_application_sar_location(self):
3238
"""
3339
Creates an application with a lamda function
3440
"""
35-
self.create_and_verify_stack("single/basic_application_sar_location")
41+
parameters = [self.generate_parameter("SarApplicationId", self.sar_app_id)]
42+
self.create_and_verify_stack("single/basic_application_sar_location", parameters)
3643

3744
nested_stack_resource = self.get_stack_nested_stack_resources()
3845
functions = self.get_stack_resources("AWS::Lambda::Function", nested_stack_resource)
3946

4047
self.assertEqual(len(functions), 1)
41-
self.assertEqual(functions[0]["LogicalResourceId"], "helloworldpython3")
48+
self.assertEqual(functions[0]["LogicalResourceId"], "HelloWorldPython3")
4249

4350
@skipIf(
4451
current_region_does_not_support(["ServerlessRepo"]), "ServerlessRepo is not supported in this testing region"
@@ -47,8 +54,9 @@ def test_basic_application_sar_location_with_intrinsics(self):
4754
"""
4855
Creates an application with a lambda function with intrinsics
4956
"""
50-
expected_function_name = "helloworldpython3"
51-
self.create_and_verify_stack("single/basic_application_sar_location_with_intrinsics")
57+
expected_function_name = "HelloWorldPython3"
58+
parameters = [self.generate_parameter("SarApplicationId", self.sar_app_id)]
59+
self.create_and_verify_stack("single/basic_application_sar_location_with_intrinsics", parameters)
5260

5361
nested_stack_resource = self.get_stack_nested_stack_resources()
5462
functions = self.get_stack_resources("AWS::Lambda::Function", nested_stack_resource)

0 commit comments

Comments
 (0)