Skip to content

Commit 718f429

Browse files
committed
feat: add test multiple rules multiple patterns same target
1 parent 671acad commit 718f429

File tree

3 files changed

+129
-7
lines changed

3 files changed

+129
-7
lines changed

tests/aws/services/events/test_events.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,105 @@ def test_process_to_single_matching_rules_single_target(
16341634
)
16351635
snapshot.match(f"events-{source}", events)
16361636

1637+
@markers.aws.validated
1638+
def test_process_pattern_to_single_matching_rules_single_target(
1639+
self,
1640+
create_lambda_function,
1641+
events_create_event_bus,
1642+
events_put_rule,
1643+
aws_client,
1644+
snapshot,
1645+
):
1646+
"""Three rules with all the same lambda target, but different patterns as condition.
1647+
The lambda should onl be invoked by the rule matching the event pattern."""
1648+
1649+
bus_name = f"test-bus-{short_uid()}"
1650+
events_create_event_bus(Name=bus_name)
1651+
1652+
# create lambda target
1653+
function_name = f"lambda-func-{short_uid()}"
1654+
create_lambda_response = create_lambda_function(
1655+
handler_file=TEST_LAMBDA_PYTHON_ECHO,
1656+
func_name=function_name,
1657+
runtime=Runtime.python3_12,
1658+
)
1659+
lambda_function_arn = create_lambda_response["CreateFunctionResponse"]["FunctionArn"]
1660+
1661+
# create rules
1662+
input_path_map = {"detail": "$.detail"}
1663+
patterns = [
1664+
{"detail": {"payload": {"id": [{"exists": True}]}}},
1665+
{"detail": {"id": [{"exists": True}]}},
1666+
]
1667+
input_transformers = [
1668+
{
1669+
"InputPathsMap": input_path_map,
1670+
"InputTemplate": '{"detail-payload-with-id": <detail>}',
1671+
},
1672+
{
1673+
"InputPathsMap": input_path_map,
1674+
"InputTemplate": '{"detail-with-id": <detail>}',
1675+
},
1676+
]
1677+
for i, pattern, input_transformer in zip(range(3), patterns, input_transformers):
1678+
rule_name = f"test-rule-{i}-{short_uid()}"
1679+
rule = events_put_rule(
1680+
Name=rule_name,
1681+
EventBusName=bus_name,
1682+
EventPattern=json.dumps(pattern),
1683+
State="ENABLED",
1684+
)
1685+
rule_arn = rule["RuleArn"]
1686+
1687+
aws_client.lambda_.add_permission(
1688+
FunctionName=function_name,
1689+
StatementId=f"{rule_name}-Event",
1690+
Action="lambda:InvokeFunction",
1691+
Principal="events.amazonaws.com",
1692+
SourceArn=rule_arn,
1693+
)
1694+
1695+
target_id = f"test-target-{i}-{short_uid()}"
1696+
aws_client.events.put_targets(
1697+
Rule=rule_name,
1698+
EventBusName=bus_name,
1699+
Targets=[
1700+
{
1701+
"Id": target_id,
1702+
"Arn": lambda_function_arn,
1703+
"InputTransformer": input_transformer,
1704+
}
1705+
],
1706+
)
1707+
1708+
details = [
1709+
{"payload": {"id": "123"}},
1710+
{"id": "123"},
1711+
]
1712+
for i, detail in zip(range(3), details):
1713+
num_events = i + 1
1714+
aws_client.events.put_events(
1715+
Entries=[
1716+
{
1717+
"EventBusName": bus_name,
1718+
"Source": TEST_EVENT_PATTERN_NO_DETAIL["source"][0],
1719+
"DetailType": TEST_EVENT_PATTERN_NO_DETAIL["detail-type"][0],
1720+
"Detail": json.dumps(detail),
1721+
}
1722+
],
1723+
)
1724+
1725+
# check lambda invocation
1726+
events = retry(
1727+
check_expected_lambda_log_events_length,
1728+
retries=3,
1729+
sleep=1,
1730+
function_name=function_name,
1731+
expected_length=num_events,
1732+
logs_client=aws_client.logs,
1733+
)
1734+
snapshot.match(f"events-{num_events}", events)
1735+
16371736

16381737
class TestEventPattern:
16391738
@markers.aws.validated

tests/aws/services/events/test_events.snapshot.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,5 +2140,33 @@
21402140
}
21412141
]
21422142
}
2143+
},
2144+
"tests/aws/services/events/test_events.py::TestEventRule::test_process_pattern_to_single_matching_rules_single_target": {
2145+
"recorded-date": "02-01-2025, 12:47:30",
2146+
"recorded-content": {
2147+
"events-1": [
2148+
{
2149+
"detail-payload-with-id": {
2150+
"payload": {
2151+
"id": "123"
2152+
}
2153+
}
2154+
}
2155+
],
2156+
"events-2": [
2157+
{
2158+
"detail-payload-with-id": {
2159+
"payload": {
2160+
"id": "123"
2161+
}
2162+
}
2163+
},
2164+
{
2165+
"detail-with-id": {
2166+
"id": "123"
2167+
}
2168+
}
2169+
]
2170+
}
21432171
}
21442172
}

tests/aws/services/events/test_events.validation.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
{
2-
"tests/aws/services/events/test_events.py::TestEventRule::test_process_to_single_matching_rules_single_target": {
3-
"last_validated_date": "2025-01-02T12:09:21+00:00"
2+
"tests/aws/services/events/test_events.py::TestEventRule::test_process_pattern_to_single_matching_rules_single_target": {
3+
"last_validated_date": "2025-01-02T12:47:30+00:00"
44
}
55
}
6-
}
7-
8-
9-
}
10-
"
116
}
127
}
138
"

0 commit comments

Comments
 (0)