From c535c4a0ec44aa7fcc0c933c0f9a6d99c95bcb1f Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Thu, 28 Sep 2023 06:20:00 -0400 Subject: [PATCH 1/3] test: e2e test for run_module (#588) https://issues.redhat.com/browse/AAP-14735 Added e2e test for run_module --- tests/e2e/test_run_module_output.py | 108 ++++++++++++++++++++++++++++ tests/e2e/utils.py | 1 + 2 files changed, 109 insertions(+) create mode 100644 tests/e2e/test_run_module_output.py diff --git a/tests/e2e/test_run_module_output.py b/tests/e2e/test_run_module_output.py new file mode 100644 index 000000000..9a7048aca --- /dev/null +++ b/tests/e2e/test_run_module_output.py @@ -0,0 +1,108 @@ +""" +Module with tests for websockets +""" +import asyncio +import logging +from functools import partial + +import dpath +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_run_module_output(): + """ + Verify that ansible-rulebook can handle output of + run_module and then used in a condition + """ + # variables + host = "localhost" + endpoint = "/api/ws2" + proc_id = "42" + port = 31415 + rulebook = utils.EXAMPLES_PATH / "29_run_module.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 + rule_matches = { + "r1": { + "action": "run_module", + "event_key": "m/i", + "event_value": 1, + }, + "r2": { + "action": "print_event", + "event_key": "m/message", + "event_value": "FRED FLINTSTONE", + }, + } + 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_uuid"] is not None + assert data["ruleset_uuid"] is not None + assert data["rule_uuid"] is not None + assert data["status"] == "successful" + rule_name = data["rule"] + assert rule_name in rule_matches.keys() + + matching_events = data["matching_events"] + assert ( + dpath.get( + matching_events, rule_matches[rule_name]["event_key"] + ) + == rule_matches[rule_name]["event_value"] + ) + assert data["action"] == rule_matches[rule_name]["action"] + + if data["type"] == "SessionStats": + session_stats_counter += 1 + stats = data["stats"] + assert stats["ruleSetName"] == "29 run module" + assert stats["numberOfRules"] == 2 + assert stats["numberOfDisabledRules"] == 0 + assert data["activation_id"] == proc_id + + assert stats["rulesTriggered"] == 2 + assert stats["eventsProcessed"] == 6 + assert stats["eventsMatched"] == 2 + + assert session_stats_counter >= 2 + assert action_counter == 2 diff --git a/tests/e2e/utils.py b/tests/e2e/utils.py index 358d82297..93eaf669d 100644 --- a/tests/e2e/utils.py +++ b/tests/e2e/utils.py @@ -11,6 +11,7 @@ BASE_DATA_PATH = Path(f"{__file__}").parent / Path("files") DEFAULT_SOURCES = Path(f"{__file__}").parent / Path("../sources") +EXAMPLES_PATH = Path(f"{__file__}").parent / Path("../examples") DEFAULT_INVENTORY = BASE_DATA_PATH / "inventories/default_inventory.yml" From f67865a5994985ecce095b18e672fd954a52ac89 Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Fri, 29 Sep 2023 07:08:14 -0400 Subject: [PATCH 2/3] refactor: removed duplicate rulebook (#589) We can use the rulebook from the examples directory --- .../e2e/files/rulebooks/82_non_alpha_keys.yml | 26 ------------------- tests/e2e/test_non_alpha_keys.py | 2 +- 2 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 tests/e2e/files/rulebooks/82_non_alpha_keys.yml diff --git a/tests/e2e/files/rulebooks/82_non_alpha_keys.yml b/tests/e2e/files/rulebooks/82_non_alpha_keys.yml deleted file mode 100644 index a7fbf3547..000000000 --- a/tests/e2e/files/rulebooks/82_non_alpha_keys.yml +++ /dev/null @@ -1,26 +0,0 @@ ---- -- name: 82 non alpha keys - hosts: all - sources: - - ansible.eda.generic: - payload: - - "http://www.example.com": "down" - - urls: - "http://www.example.com": "up" - - नाम: മധു - - rules: - - name: r1 - condition: event["http://www.example.com"] == "down" - action: - debug: - msg: "First check worked" - - name: r2 - condition: event.urls["http://www.example.com"] == "up" - action: - debug: - msg: "Second check worked" - - name: r3 - condition: event["नाम"] is search("മധു", ignorecase=true) - action: - print_event: diff --git a/tests/e2e/test_non_alpha_keys.py b/tests/e2e/test_non_alpha_keys.py index f959e6a1e..1c0b0b65e 100644 --- a/tests/e2e/test_non_alpha_keys.py +++ b/tests/e2e/test_non_alpha_keys.py @@ -27,7 +27,7 @@ async def test_non_alpha_numeric_keys(): endpoint = "/api/ws2" proc_id = "42" port = 31415 - rulebook = utils.BASE_DATA_PATH / "rulebooks/82_non_alpha_keys.yml" + rulebook = utils.EXAMPLES_PATH / "82_non_alpha_keys.yml" websocket_address = f"ws://localhost:{port}{endpoint}" cmd = utils.Command( rulebook=rulebook, From b5a3666df99c5edca6de197f90cd39c40ca6d4d3 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 3 Oct 2023 18:33:03 +0200 Subject: [PATCH 3/3] ci: rollback exclude files for docs (#591) exclude paths is incompatible with required checks. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/troubleshooting-required-status-checks#handling-skipped-but-required-checks --- .github/workflows/ci.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f642b6982..783dfd4e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,15 +4,9 @@ on: push: branches: - "main" - paths-ignore: - - "docs/**" - - "*.md" pull_request: branches: - "main" - paths-ignore: - - "docs/**" - - "*.md" workflow_dispatch: jobs: