Skip to content

Commit

Permalink
Fix test examples (#336)
Browse files Browse the repository at this point in the history
* Some of them were incorrectly using range, switched them to use
generic plugin.

* Use the SourceTask to directly run the source for some examples so we
don't have to inject the data manually.
  • Loading branch information
mkanoor authored Feb 3, 2023
1 parent 31bae65 commit fd7c9d1
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 141 deletions.
4 changes: 2 additions & 2 deletions tests/examples/40_in.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
sources:
- name: range
range:
limit: 5
limit: 1
rules:
- name: r1
condition: event.i in [1,2,3,4]
condition: event.i in [0,1,2,3,4]
action:
debug:
2 changes: 1 addition & 1 deletion tests/examples/41_not_in.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
sources:
- name: range
range:
limit: 5
limit: 1
rules:
- name: r1
condition: event.i not in [11,21,31,41]
Expand Down
10 changes: 7 additions & 3 deletions tests/examples/42_contains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
- name: 42 contains
hosts: all
sources:
- name: range
range:
limit: 5
- name: generic
generic:
payload:
- id_list:
- 1
- 2
- 3
rules:
- name: r1
condition: event.id_list contains 1
Expand Down
10 changes: 7 additions & 3 deletions tests/examples/43_not_contains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
- name: 43 not contains
hosts: all
sources:
- name: range
range:
limit: 5
- name: generic
generic:
payload:
- id_list:
- 1
- 2
- 3
rules:
- name: r1
condition: event.id_list not contains 10
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/47_generic_plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: 47 Generic Plugin with boolean and integer test
- name: 47 Generic Plugin
hosts: all
sources:
- name: generic
Expand All @@ -9,7 +9,7 @@
- i: 42
rules:
- name: r1
condition: event.b == true
condition: event.b
action:
print_event:
- name: r2
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/48_echo.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: 53 echo
- name: 48 echo
hosts: all
sources:
- name: range
Expand Down
261 changes: 132 additions & 129 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,181 +1016,184 @@ async def test_38_shutdown_action():
@pytest.mark.asyncio
async def test_40_in():
ruleset_queues, event_log = load_rulebook("examples/40_in.yml")

queue = ruleset_queues[0][1]
queue.put_nowait(dict(i=1))
queue.put_nowait(Shutdown())

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "debug", "1"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "3"
rs = ruleset_queues[0][0]
with SourceTask(rs.sources[0], "sources", {}, queue):
await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
checks = {
"max_events": 2,
"shutdown_events": 1,
"actions": [
"40 in::r1::debug",
],
}
validate_events(event_log, **checks)


@pytest.mark.asyncio
async def test_41_not_in():
ruleset_queues, event_log = load_rulebook("examples/41_not_in.yml")

queue = ruleset_queues[0][1]
queue.put_nowait(dict(i=1))
queue.put_nowait(Shutdown())

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "debug", "1"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "3"
rs = ruleset_queues[0][0]
with SourceTask(rs.sources[0], "sources", {}, queue):
await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
checks = {
"max_events": 2,
"shutdown_events": 1,
"actions": [
"41 not in::r1::debug",
],
}
validate_events(event_log, **checks)


@pytest.mark.asyncio
async def test_42_contains():
ruleset_queues, event_log = load_rulebook("examples/42_contains.yml")

queue = ruleset_queues[0][1]
queue.put_nowait(dict(id_list=[1, 2, 3]))
queue.put_nowait(Shutdown())

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "debug", "1"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "3"
rs = ruleset_queues[0][0]
with SourceTask(rs.sources[0], "sources", {}, queue):
await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
checks = {
"max_events": 2,
"shutdown_events": 1,
"actions": [
"42 contains::r1::debug",
],
}
validate_events(event_log, **checks)


@pytest.mark.asyncio
async def test_43_not_contains():
ruleset_queues, event_log = load_rulebook("examples/43_not_contains.yml")

queue = ruleset_queues[0][1]
queue.put_nowait(dict(id_list=[1, 2, 3]))
queue.put_nowait(Shutdown())

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "debug", "1"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "3"
rs = ruleset_queues[0][0]
with SourceTask(rs.sources[0], "sources", {}, queue):
await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
checks = {
"max_events": 2,
"shutdown_events": 1,
"actions": [
"43 not contains::r1::debug",
],
}
validate_events(event_log, **checks)


@pytest.mark.asyncio
async def test_44_in_and():
ruleset_queues, event_log = load_rulebook("examples/44_in_and.yml")

queue = ruleset_queues[0][1]
queue.put_nowait(dict(i=0))
queue.put_nowait(dict(i=1))
queue.put_nowait(dict(i=2))
queue.put_nowait(dict(i=3))
queue.put_nowait(dict(i=4))
queue.put_nowait(Shutdown())

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "debug", "1"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "3"
rs = ruleset_queues[0][0]
with SourceTask(rs.sources[0], "sources", {}, queue):
await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
checks = {
"max_events": 2,
"shutdown_events": 1,
"actions": [
"44 in and::r1::debug",
],
}
validate_events(event_log, **checks)


@pytest.mark.asyncio
async def test_45_in_or():
ruleset_queues, event_log = load_rulebook("examples/45_in_or.yml")

queue = ruleset_queues[0][1]
queue.put_nowait(dict(i=4))
queue.put_nowait(dict(i=8))
queue.put_nowait(Shutdown())

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "debug", "1"
event = event_log.get_nowait()
assert event["type"] == "Action", "3"
assert event["action"] == "debug", "3"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "5"
rs = ruleset_queues[0][0]
with SourceTask(rs.sources[0], "sources", {}, queue):
await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
checks = {
"max_events": 5,
"shutdown_events": 1,
"actions": [
"45 in or::r1::debug",
"45 in or::r1::debug",
"45 in or::r1::debug",
"45 in or::r1::debug",
],
}
validate_events(event_log, **checks)


@pytest.mark.asyncio
async def test_47_generic_plugin():
ruleset_queues, event_log = load_rulebook("examples/47_generic_plugin.yml")

queue = ruleset_queues[0][1]
queue.put_nowait(dict(i=42))
queue.put_nowait(dict(b=True))
queue.put_nowait(Shutdown())

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "print_event", "1"
event = event_log.get_nowait()
assert event["type"] == "Action", "3"
assert event["action"] == "print_event", "3"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "5"
rs = ruleset_queues[0][0]
with SourceTask(rs.sources[0], "sources", {}, queue):
await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
checks = {
"max_events": 3,
"shutdown_events": 1,
"actions": [
"47 Generic Plugin::r1::print_event",
"47 Generic Plugin::r2::print_event",
],
}
validate_events(event_log, **checks)


@pytest.mark.asyncio
async def test_48_echo():
ruleset_queues, event_log = load_rulebook("examples/48_echo.yml")

queue = ruleset_queues[0][1]
queue.put_nowait(dict(i=1))
queue.put_nowait(Shutdown())

await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
event = event_log.get_nowait()
assert event["type"] == "Action", "1"
assert event["action"] == "echo", "1"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "5"
rs = ruleset_queues[0][0]
with SourceTask(rs.sources[0], "sources", {}, queue):
await run_rulesets(
event_log,
ruleset_queues,
dict(),
load_inventory("playbooks/inventory.yml"),
)
checks = {
"max_events": 2,
"shutdown_events": 1,
"actions": [
"48 echo::r1::echo",
],
}
validate_events(event_log, **checks)


@pytest.mark.asyncio
Expand Down

0 comments on commit fd7c9d1

Please sign in to comment.