-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support firing of multiple rules (#565)
https://issues.redhat.com/browse/AAP-9755 https://issues.redhat.com/browse/AAP-13131 Drools has always supported firing of multiple rules when an event or a fact comes in. Durable rules only supported this behavior for facts but not for events. This PR adds a feature to a ruleset to indicate if it should match_multiple rules. The default is false https://github.com/ansible/ansible-rulebook/assets/6452699/20daa5aa-75c7-4c63-93f0-01678ba4c986
- Loading branch information
Showing
11 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
## [Unreleased] | ||
|
||
### Added | ||
- support for firing multiple rules | ||
|
||
### Fixed | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
- name: Test match multiple rules | ||
hosts: all | ||
match_multiple_rules: true | ||
sources: | ||
- name: range | ||
range: | ||
limit: 5 | ||
rules: | ||
- name: r1 | ||
condition: event.i == 1 | ||
action: | ||
debug: | ||
- name: r11 | ||
condition: event.i == 1 | ||
action: | ||
print_event: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
Module with tests for websockets | ||
""" | ||
import asyncio | ||
import logging | ||
from functools import partial | ||
|
||
import pytest | ||
import websockets.server as ws_server | ||
|
||
from . import utils | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
DEFAULT_TIMEOUT = 15 | ||
|
||
|
||
@pytest.mark.e2e | ||
@pytest.mark.asyncio | ||
async def test_match_multiple_rules(): | ||
""" | ||
Verify that ansible-rulebook can handle rulebook | ||
which matches multiple rules for a single event | ||
and send the event messages to a websocket server | ||
""" | ||
# variables | ||
host = "localhost" | ||
endpoint = "/api/ws2" | ||
proc_id = "42" | ||
port = 31415 | ||
rulebook = utils.BASE_DATA_PATH / "rulebooks/test_match_multiple_rules.yml" | ||
websocket_address = f"ws://localhost:{port}{endpoint}" | ||
cmd = utils.Command( | ||
rulebook=rulebook, | ||
websocket=websocket_address, | ||
proc_id=proc_id, | ||
heartbeat=2, | ||
) | ||
|
||
# run server and ansible-rulebook | ||
queue = asyncio.Queue() | ||
handler = partial(utils.msg_handler, queue=queue) | ||
async with ws_server.serve(handler, host, port): | ||
LOGGER.info(f"Running command: {cmd}") | ||
proc = await asyncio.create_subprocess_shell( | ||
str(cmd), | ||
cwd=utils.BASE_DATA_PATH, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE, | ||
) | ||
|
||
await asyncio.wait_for(proc.wait(), timeout=DEFAULT_TIMEOUT) | ||
assert proc.returncode == 0 | ||
|
||
# Verify data | ||
assert not queue.empty() | ||
|
||
action_counter = 0 | ||
session_stats_counter = 0 | ||
stats = None | ||
while not queue.empty(): | ||
data = await queue.get() | ||
assert data["path"] == endpoint | ||
data = data["payload"] | ||
|
||
if data["type"] == "Action": | ||
action_counter += 1 | ||
assert data["action"] in ("print_event", "debug") | ||
assert data["action_uuid"] is not None | ||
assert data["ruleset_uuid"] is not None | ||
assert data["rule_uuid"] is not None | ||
matching_events = data["matching_events"] | ||
del matching_events["m"]["meta"] | ||
assert matching_events == {"m": {"i": 1}} | ||
assert data["status"] == "successful" | ||
|
||
if data["type"] == "SessionStats": | ||
session_stats_counter += 1 | ||
stats = data["stats"] | ||
assert stats["ruleSetName"] == "Test match multiple rules" | ||
assert stats["numberOfRules"] == 2 | ||
assert stats["numberOfDisabledRules"] == 0 | ||
assert data["activation_id"] == proc_id | ||
|
||
assert stats["rulesTriggered"] == 2 | ||
assert stats["eventsProcessed"] == 5 | ||
assert stats["eventsMatched"] == 1 | ||
|
||
assert session_stats_counter >= 2 | ||
assert action_counter == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
- name: 80 match multiple rules | ||
hosts: all | ||
match_multiple_rules: true | ||
sources: | ||
- name: range | ||
range: | ||
limit: 5 | ||
rules: | ||
- name: r1 | ||
condition: event.i == 1 | ||
action: | ||
debug: | ||
- name: r11 | ||
condition: event.i == 1 | ||
action: | ||
print_event: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
- name: 81 match single rule | ||
hosts: all | ||
sources: | ||
- name: range | ||
range: | ||
limit: 5 | ||
rules: | ||
- name: r1 | ||
condition: event.i == 1 | ||
action: | ||
debug: | ||
- name: r11 | ||
condition: event.i == 1 | ||
action: | ||
print_event: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters