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: use correct defnition field name for CFN resources #4870

Merged
merged 7 commits into from Mar 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion samcli/lib/sync/sync_flow.py
Expand Up @@ -14,6 +14,7 @@
from samcli.lib.sync.exceptions import MissingLockException, MissingPhysicalResourceError
from samcli.lib.utils.boto_utils import get_boto_client_provider_from_session_with_config
from samcli.lib.utils.lock_distributor import LockChain, LockDistributor
from samcli.lib.utils.resources import RESOURCES_WITH_LOCAL_PATHS

if TYPE_CHECKING: # pragma: no cover
from samcli.commands.build.build_context import BuildContext
Expand Down Expand Up @@ -419,8 +420,15 @@ def get_definition_path(
Optional[Path]
A resolved absolute path for the definition file
"""
definition_field_names = RESOURCES_WITH_LOCAL_PATHS.get(resource.get("Type", ""))
if not definition_field_names:
LOG.error("Couldn't find definition field name for resource {}", identifier)
return None
definition_field_name = definition_field_names[0]
LOG.debug("Found definition field name as {}", definition_field_name)

properties = resource.get("Properties", {})
definition_file = properties.get("DefinitionUri")
definition_file = properties.get(definition_field_name)
definition_path = None
if definition_file:
definition_path = Path(base_dir).joinpath(definition_file)
Expand Down
39 changes: 34 additions & 5 deletions tests/unit/lib/sync/test_sync_flow.py
Expand Up @@ -13,6 +13,15 @@
)
from parameterized import parameterized

from samcli.lib.utils.resources import (
AWS_SERVERLESS_HTTPAPI,
AWS_SERVERLESS_API,
AWS_SERVERLESS_STATEMACHINE,
AWS_APIGATEWAY_RESTAPI,
AWS_APIGATEWAY_V2_API,
AWS_STEPFUNCTIONS_STATEMACHINE,
)


class TestSyncFlow(TestCase):
def create_sync_flow(self, mock_update_local_hash=True):
Expand Down Expand Up @@ -220,20 +229,40 @@ def test_hash(self):
sync_flow._equality_keys.return_value = "A"
self.assertEqual(hash(sync_flow), hash((type(sync_flow), "A")))

@parameterized.expand(
[
(AWS_SERVERLESS_HTTPAPI, "DefinitionUri"),
(AWS_SERVERLESS_API, "DefinitionUri"),
(AWS_SERVERLESS_STATEMACHINE, "DefinitionUri"),
(AWS_APIGATEWAY_V2_API, "BodyS3Location"),
(AWS_APIGATEWAY_RESTAPI, "BodyS3Location"),
(AWS_STEPFUNCTIONS_STATEMACHINE, "DefinitionS3Location"),
]
)
@patch("samcli.lib.sync.sync_flow.Stack.get_stack_by_full_path")
def test_get_definition_path(self, get_stack_mock):
resource = {"Properties": {"DefinitionUri": "test_uri"}}
def test_get_definition_path(self, resource_type, definition_field, get_stack_mock):
resource = {"Properties": {definition_field: "test_uri"}, "Type": resource_type}
get_stack_mock.return_value = Stack("parent_path", "stack_name", "location/template.yaml", None, {})

definition_path = get_definition_path(resource, "identifier", False, "base_dir", [])
self.assertEqual(definition_path, Path("location").joinpath("test_uri"))

resource = {"Properties": {"DefinitionUri": ""}}
resource = {"Properties": {definition_field: ""}, "Type": resource_type}
definition_path = get_definition_path(resource, "identifier", False, "base_dir", [])
self.assertEqual(definition_path, None)

def test_get_definition_file_with_base_dir(self):
resource = {"Properties": {"DefinitionUri": "test_uri"}}
@parameterized.expand(
[
(AWS_SERVERLESS_HTTPAPI, "DefinitionUri"),
(AWS_SERVERLESS_API, "DefinitionUri"),
(AWS_SERVERLESS_STATEMACHINE, "DefinitionUri"),
(AWS_APIGATEWAY_V2_API, "BodyS3Location"),
(AWS_APIGATEWAY_RESTAPI, "BodyS3Location"),
(AWS_STEPFUNCTIONS_STATEMACHINE, "DefinitionS3Location"),
]
)
def test_get_definition_file_with_base_dir(self, resource_type, definition_field):
resource = {"Properties": {definition_field: "test_uri"}, "Type": resource_type}

definition_path = get_definition_path(resource, "identifier", True, "base_dir", [])
self.assertEqual(definition_path, Path("base_dir").joinpath("test_uri"))
Expand Down