The SNMP poll asks each device's sysName in the same GET and keeps it (snmp_003_sys_name). A syslog source told apart by name finds its own address from the SNMP device (reported or given name) or host of that name, so its lines reach the SNMP device's and host's pages again; the edit page gains Device address, which always wins (syslog_004). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
57 lines
3.0 KiB
Python
57 lines
3.0 KiB
Python
"""Reading the kept syslog lines (#6000): the filter read from a page's
|
|
arguments, severity by name or number, and which sources a host's or a
|
|
device's page shows."""
|
|
from types import SimpleNamespace as NS
|
|
|
|
import pytest
|
|
from werkzeug.datastructures import MultiDict
|
|
|
|
from plugins.syslog.service import DEFAULT_RANGE, device_address, line_filter, severity_level, sources_for
|
|
|
|
|
|
def test_a_severity_reads_by_name_alias_or_number():
|
|
assert severity_level("err") == 3 and severity_level("error") == 3 and severity_level("3") == 3
|
|
assert severity_level("Warning") == 4 and severity_level("warn") == 4
|
|
assert severity_level("") is None and severity_level(None) is None
|
|
with pytest.raises(ValueError, match="0-7"):
|
|
severity_level("loud")
|
|
|
|
|
|
def test_a_page_filter_drops_what_does_not_read():
|
|
f = line_filter(MultiDict({"severity": "loud", "range": "9y", "q": " nfsd ", "app": "kernel"}))
|
|
assert (f.severity, f.range, f.text, f.app) == (None, DEFAULT_RANGE, "nfsd", "kernel")
|
|
assert f.args() == {"q": "nfsd", "app": "kernel"}
|
|
assert line_filter(MultiDict({"severity": "4", "range": "7d"})).args() == {"severity": "4", "range": "7d"}
|
|
|
|
|
|
def _src(sid, state="accepted", host_id=None, address="10.0.0.5", sender="", device_address=""):
|
|
return NS(id=sid, state=state, host_id=host_id, address=address, sender=sender, device_address=device_address)
|
|
|
|
|
|
def test_a_page_shows_sources_bound_to_its_host_or_unbound_at_its_address():
|
|
sources = [_src("bound", host_id="h1", address="10.9.9.9"), _src("by-address"),
|
|
_src("elsewhere", host_id="h2"), _src("offered", state="offered"),
|
|
_src("other", address="10.0.0.6")]
|
|
assert [s.id for s in sources_for(sources, "h1", "10.0.0.5")] == ["bound", "by-address"]
|
|
# An SNMP device bound to no host matches by its address alone.
|
|
assert [s.id for s in sources_for(sources, None, "10.0.0.5")] == ["by-address"]
|
|
assert sources_for(sources, None, None) == []
|
|
|
|
|
|
def test_a_device_through_dockers_address_is_placed_by_its_name_or_the_address_set():
|
|
"""#6096: through Swarm's ingress every line comes from Docker's address;
|
|
the device's own is the one set on it, else that of the SNMP device or
|
|
host its name is."""
|
|
named = {"vdnt-ds923": "192.168.0.7", "vdnt-as6508t": "192.168.0.5"}
|
|
syn = _src("syn", address="10.0.0.19", sender="VDNT-DS923")
|
|
asus = _src("asus", address="10.0.0.19", sender="VDNT-AS6508T", device_address="192.168.0.50")
|
|
lone = _src("lone", address="10.0.0.19", sender="router")
|
|
assert device_address(syn, named) == "192.168.0.7"
|
|
assert device_address(asus, named) == "192.168.0.50" # set by hand wins
|
|
assert device_address(lone, named) == "10.0.0.19"
|
|
assert device_address(_src("plain", sender=""), named) == "10.0.0.5"
|
|
sources = [syn, asus, lone]
|
|
assert [s.id for s in sources_for(sources, None, "192.168.0.7", named)] == ["syn"]
|
|
assert [s.id for s in sources_for(sources, None, "192.168.0.50", named)] == ["asus"]
|
|
assert sources_for(sources, None, "192.168.0.7") == [] # no names: Docker's address only
|