Skip to content

Tests

Detection logic can be tested using the built-in testing framework.

Overview

Tests are defined as YAML files. Each test file can contain multiple separate test cases.

tests/password-spray-test.yaml
rule_id: 100
event_time_sample: '{"timestamp": "2025-01-01T10:00:00Z"}'

tests:
  - name: detect password spray
    events:
      - '{"action": "login-failure", "source": {"ip": "10.0.0.1"}, "user": {"name": "user1"}}'
      - '{"action": "login-failure", "source": {"ip": "10.0.0.1"}, "user": {"name": "user2"}}'
      - '{"action": "login-failure", "source": {"ip": "10.0.0.1"}, "user": {"name": "user3"}}'
      - '{"action": "login-failure", "source": {"ip": "10.0.0.1"}, "user": {"name": "user4"}}'
      - '{"action": "login-failure", "source": {"ip": "10.0.0.1"}, "user": {"name": "user5"}}'
      - '{"action": "login-failure", "source": {"ip": "10.0.0.1"}, "user": {"name": "user6"}}'
    expect_alert: true
    expect_fields:
      total_failures: 6

  - name: single user repeated failures
    events:
      - '{"action": "login-failure", "source": {"ip": "10.0.0.2"}, "user": {"name": "admin"}}'
      - '{"action": "login-failure", "source": {"ip": "10.0.0.2"}, "user": {"name": "admin"}}'
      - '{"action": "login-failure", "source": {"ip": "10.0.0.2"}, "user": {"name": "admin"}}'
    expect_alert: false

Top-Level Fields

Field Required Description
rule_id ID of rule to test
requires List of capabilities the test needs (e.g. [geoip]). Skipped with a warning when not satisfied.
event_time_sample Sample event for validating event_time parsing (aggregation rules)
event_time_samples Per-part sample events for validating event_time parsing (composite rules). Map of part name to a sample event string.
tests Array of test cases

When testing composite rules, use event_time_samples instead of event_time_sample:

event_time_samples:
  network_event: '{"timestamp": "2025-09-17T23:56:15.665825"}'
  file_event: '{"timestamp": "2025-09-11T23:56:15.665825"}'

Event time in tests

Tests always run in processing time. event_time is only checked against the sample events, so gaps between event timestamps do not affect the outcome of a test case.

Test Case Fields

Field Required Description
name Test case description
events Array of JSON event strings
expect_alert Whether an alert should be generated
expect_fields Expected fields in alert. Nested maps can be used to match nested structures.
expect_risk Expected risk contributions. Array of entity_type + entity_value pairs.

Example expect_fields:

expect_fields:
  source:
    ip: 192.168.10.10
  total_logins: 5
  distinct_users:
    count: 4

For composite rules, part fields are nested under the part name and data:

expect_fields:
  failed_logins:
    data:
      source:
        ip: 203.0.113.50

Tests & State

Test cases in the same test file share key-value and windowing state and can interfere with each other. Use different values for your rule's grouping keys (e.g., different IP addresses like 10.0.0.1 vs 10.0.0.2) in each test case to keep them isolated.

Tests run against a new, empty key-value store that is deleted after the tests are completed. This ensures reproducible results and isolation from production state.

Testing Risk Contributions

Rules with risk entries can be tested using expect_risk to verify that entity values are correctly extracted and normalized.

tests/risk-failed-login-test.yaml
rule_id: 200

tests:
  - name: failed login contributes risk to user and IP
    events:
      - '{"action": "login-failure", "user": {"name": "john.doe"}, "source": {"ip": "10.0.0.1"}}'
    expect_alert: false
    expect_risk:
      - entity_type: user
        entity_value: john.doe
      - entity_type: ip
        entity_value: 10.0.0.1

The test passes if all listed entries are found in the rule's risk contributions.

Running Tests

Test a single rule:

./inobi test -c config.yaml tests/password-spray-test.yaml

Testing: "Password spraying activity" (100)

   Passed: "single user"
   Passed: "event_time_sample"

   Failed: "password spray" - expect_alert: alert did not fire

   Result: 2/3 tests passed

Test every file in a directory (default: tests/):

./inobi test -c config.yaml tests/

inobi test exits with status 3 if any test fails.

Debugging

  • Print the alerts each test produces: --alerts, or --indent for indented output
  • Print engine debug logs: --debug

Requirements

Tests can declare requires to indicate they depend on a capability that may not always be available (e.g. a GeoIP database in CI). Tests with unsatisfied requirements are skipped with a warning.

rule_id: 102
requires: [geoip]

The geoip requirement is automatically satisfied when geoip.path is set. Custom requirements can be satisfied with --satisfy:

./inobi test -c config.yaml --satisfy custom_udf