Skip to content

Commit

Permalink
fix: Remove str type constrain in Fn:FindInMap to avoid the issue of …
Browse files Browse the repository at this point in the history
…lookup int value
  • Loading branch information
dkphm committed Aug 8, 2024
1 parent 8b8eed6 commit a616f17
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def handle_find_in_map(self, intrinsic_value, ignore_errors):
)

second_level_value = top_level_value.get(second_level_key)
verify_intrinsic_type_str(
verify_non_null(
second_level_value,
IntrinsicResolver.FN_FIND_IN_MAP,
message="The SecondLevelKey is missing in the Mappings dictionary in Fn::FindInMap "
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/local/start_lambda/test_start_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,28 @@ def test_invoke_with_function_timeout(self, use_full_path):
self.assertIsNone(response.get("FunctionError"))
self.assertEqual(response.get("StatusCode"), 200)

@parameterized.expand([("False"), ("True")])
@pytest.mark.flaky(reruns=3)
@pytest.mark.timeout(timeout=300, method="thread")
def test_invoke_with_function_timeout_using_lookup_value(self, use_full_path):
"""
This behavior does not match the actually Lambda Service. For functions that timeout, data returned like the
following:
{"errorMessage":"<timestamp> <request_id> Task timed out after 5.00 seconds"}
For Local Lambda's, however, timeouts are an interrupt on the thread that runs invokes the function. Since the
invoke is on a different thread, we do not (currently) have a way to communicate this back to the caller. So
when a timeout happens locally, we do not add the FunctionError: Unhandled to the response and have an empty
string as the data returned (because no data was found in stdout from the container).
"""
response = self.lambda_client.invoke(
FunctionName=f"{self.parent_path if use_full_path == 'True' else ''}TimeoutFunctionUsingLookupValue"
)

self.assertEqual(response.get("Payload").read().decode("utf-8"), "")
self.assertIsNone(response.get("FunctionError"))
self.assertEqual(response.get("StatusCode"), 200)


class TestWarmContainersBaseClass(StartLambdaIntegBaseClass):
def setUp(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/testdata/invoke/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Parameters:
Type: String
Default: "2"

Mappings:
common:
LambdaFunction:
Timeout: 5

Resources:
HelloWorldServerlessFunction:
Type: AWS::Serverless::Function
Expand Down Expand Up @@ -58,6 +63,14 @@ Resources:
CodeUri: .
Timeout: 5

TimeoutFunctionUsingLookupValue:
Type: AWS::Serverless::Function
Properties:
Handler: main.sleep_handler
Runtime: python3.9
CodeUri: .
Timeout: !FindInMap [common, LambdaFunction, Timeout]

HelloWorldSleepFunction:
Type: AWS::Serverless::Function
Properties:
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/lib/intrinsic_resolver/test_intrinsic_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def setUp(self):
"Basic": {"Test": {"key": "value"}},
"value": {"anotherkey": {"key": "result"}},
"result": {"value": {"key": "final"}},
"NonStrValue": {"Test": {"key": 0}},
}
}
self.resolver = IntrinsicResolver(symbol_resolver=IntrinsicsSymbolTable(), template=template)
Expand All @@ -218,6 +219,11 @@ def test_basic_find_in_map(self):
result = self.resolver.intrinsic_property_resolver(intrinsic, True)
self.assertEqual(result, "value")

def test_basic_find_in_map_with_non_string_value(self):
intrinsic = {"Fn::FindInMap": ["NonStrValue", "Test", "key"]}
result = self.resolver.intrinsic_property_resolver(intrinsic, True)
self.assertEqual(result, 0)

def test_nested_find_in_map(self):
intrinsic_base_1 = {"Fn::FindInMap": ["Basic", "Test", "key"]}
intrinsic_base_2 = {"Fn::FindInMap": [intrinsic_base_1, "anotherkey", "key"]}
Expand Down

0 comments on commit a616f17

Please sign in to comment.