Skip to content

Commit 671acad

Browse files
committed
feat: add test same target multiple rules different conditions
1 parent 44e84e0 commit 671acad

File tree

3 files changed

+191
-2
lines changed

3 files changed

+191
-2
lines changed

tests/aws/services/events/test_events.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,81 @@ def test_process_to_multiple_matching_rules_single_target(
15591559
)
15601560
snapshot.match("events", events)
15611561

1562+
@markers.aws.validated
1563+
def test_process_to_single_matching_rules_single_target(
1564+
self,
1565+
create_lambda_function,
1566+
events_create_event_bus,
1567+
events_put_rule,
1568+
aws_client,
1569+
snapshot,
1570+
):
1571+
"""Three rules with all the same lambda target, but different patterns as condition.
1572+
The lambda should onl be invoked by the rule matching the event pattern."""
1573+
1574+
bus_name = f"test-bus-{short_uid()}"
1575+
events_create_event_bus(Name=bus_name)
1576+
1577+
# create lambda target
1578+
function_name = f"lambda-func-{short_uid()}"
1579+
create_lambda_response = create_lambda_function(
1580+
handler_file=TEST_LAMBDA_PYTHON_ECHO,
1581+
func_name=function_name,
1582+
runtime=Runtime.python3_12,
1583+
)
1584+
lambda_function_arn = create_lambda_response["CreateFunctionResponse"]["FunctionArn"]
1585+
1586+
# create rules
1587+
sources = ["source-one", "source-two", "source-three"]
1588+
for i, source in zip(range(3), sources):
1589+
rule_name = f"test-rule-{i}-{short_uid()}"
1590+
rule = events_put_rule(
1591+
Name=rule_name,
1592+
EventBusName=bus_name,
1593+
EventPattern=json.dumps({"source": [source]}),
1594+
State="ENABLED",
1595+
)
1596+
rule_arn = rule["RuleArn"]
1597+
1598+
aws_client.lambda_.add_permission(
1599+
FunctionName=function_name,
1600+
StatementId=f"{rule_name}-Event",
1601+
Action="lambda:InvokeFunction",
1602+
Principal="events.amazonaws.com",
1603+
SourceArn=rule_arn,
1604+
)
1605+
1606+
target_id = f"test-target-{i}-{short_uid()}"
1607+
aws_client.events.put_targets(
1608+
Rule=rule_name,
1609+
EventBusName=bus_name,
1610+
Targets=[{"Id": target_id, "Arn": lambda_function_arn}],
1611+
)
1612+
1613+
for i, source in zip(range(3), sources):
1614+
num_events = i + 1
1615+
aws_client.events.put_events(
1616+
Entries=[
1617+
{
1618+
"EventBusName": bus_name,
1619+
"Source": source,
1620+
"DetailType": TEST_EVENT_PATTERN_NO_DETAIL["detail-type"][0],
1621+
"Detail": json.dumps(TEST_EVENT_DETAIL),
1622+
}
1623+
],
1624+
)
1625+
1626+
# check lambda invocation
1627+
events = retry(
1628+
check_expected_lambda_log_events_length,
1629+
retries=3,
1630+
sleep=1,
1631+
function_name=function_name,
1632+
expected_length=num_events,
1633+
logs_client=aws_client.logs,
1634+
)
1635+
snapshot.match(f"events-{source}", events)
1636+
15621637

15631638
class TestEventPattern:
15641639
@markers.aws.validated

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

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,5 +2027,118 @@
20272027
}
20282028
]
20292029
}
2030+
},
2031+
"tests/aws/services/events/test_events.py::TestEventRule::test_process_to_single_matching_rules_single_target": {
2032+
"recorded-date": "02-01-2025, 12:09:21",
2033+
"recorded-content": {
2034+
"events-source-one": [
2035+
{
2036+
"version": "0",
2037+
"id": "<uuid:1>",
2038+
"detail-type": "core.update-account-command",
2039+
"source": "source-one",
2040+
"account": "111111111111",
2041+
"time": "date",
2042+
"region": "<region>",
2043+
"resources": [],
2044+
"detail": {
2045+
"command": "update-account",
2046+
"payload": {
2047+
"acc_id": "0a787ecb-4015",
2048+
"sf_id": "baz"
2049+
}
2050+
}
2051+
}
2052+
],
2053+
"events-source-two": [
2054+
{
2055+
"version": "0",
2056+
"id": "<uuid:1>",
2057+
"detail-type": "core.update-account-command",
2058+
"source": "source-one",
2059+
"account": "111111111111",
2060+
"time": "date",
2061+
"region": "<region>",
2062+
"resources": [],
2063+
"detail": {
2064+
"command": "update-account",
2065+
"payload": {
2066+
"acc_id": "0a787ecb-4015",
2067+
"sf_id": "baz"
2068+
}
2069+
}
2070+
},
2071+
{
2072+
"version": "0",
2073+
"id": "<uuid:2>",
2074+
"detail-type": "core.update-account-command",
2075+
"source": "source-two",
2076+
"account": "111111111111",
2077+
"time": "date",
2078+
"region": "<region>",
2079+
"resources": [],
2080+
"detail": {
2081+
"command": "update-account",
2082+
"payload": {
2083+
"acc_id": "0a787ecb-4015",
2084+
"sf_id": "baz"
2085+
}
2086+
}
2087+
}
2088+
],
2089+
"events-source-three": [
2090+
{
2091+
"version": "0",
2092+
"id": "<uuid:1>",
2093+
"detail-type": "core.update-account-command",
2094+
"source": "source-one",
2095+
"account": "111111111111",
2096+
"time": "date",
2097+
"region": "<region>",
2098+
"resources": [],
2099+
"detail": {
2100+
"command": "update-account",
2101+
"payload": {
2102+
"acc_id": "0a787ecb-4015",
2103+
"sf_id": "baz"
2104+
}
2105+
}
2106+
},
2107+
{
2108+
"version": "0",
2109+
"id": "<uuid:2>",
2110+
"detail-type": "core.update-account-command",
2111+
"source": "source-two",
2112+
"account": "111111111111",
2113+
"time": "date",
2114+
"region": "<region>",
2115+
"resources": [],
2116+
"detail": {
2117+
"command": "update-account",
2118+
"payload": {
2119+
"acc_id": "0a787ecb-4015",
2120+
"sf_id": "baz"
2121+
}
2122+
}
2123+
},
2124+
{
2125+
"version": "0",
2126+
"id": "<uuid:3>",
2127+
"detail-type": "core.update-account-command",
2128+
"source": "source-three",
2129+
"account": "111111111111",
2130+
"time": "date",
2131+
"region": "<region>",
2132+
"resources": [],
2133+
"detail": {
2134+
"command": "update-account",
2135+
"payload": {
2136+
"acc_id": "0a787ecb-4015",
2137+
"sf_id": "baz"
2138+
}
2139+
}
2140+
}
2141+
]
2142+
}
20302143
}
20312144
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"tests/aws/services/events/test_events.py::TestEventRule::test_process_to_multiple_matching_rules_single_target": {
3-
"last_validated_date": "2025-01-02T11:37:47+00:00"
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"
44
}
55
}
6+
}
67

78

89
}

0 commit comments

Comments
 (0)