Files
DECNET/tests/logging/test_logging_forwarder.py
anti f2b3393669 chore: relicense to AGPL-3.0-or-later and add SPDX headers
Replaces LICENSE (GPLv3 -> AGPLv3) and prepends
`SPDX-License-Identifier: AGPL-3.0-or-later` to every source file
across decnet/, decnet_web/, tests/, scripts/, and tools/.

Rationale: closes the GPLv3 ASP loophole so any party operating a
modified DECNET as a network service must offer their modified
source. Personal copyright (Samuel Paschuan) + inbound=outbound
contributions make a future unilateral relicense infeasible.

- LICENSE: full AGPL-3.0 text (gnu.org/licenses/agpl-3.0.txt)
- COPYRIGHT: project copyright notice
- tools/add_spdx_headers.py: idempotent header injector
  (shebang- and PEP 263-aware)

Touches 1565 source files (.py, .ts, .tsx, .js, .jsx, .css, .sh).
No behavior change; comments only.
2026-05-22 21:04:16 -04:00

63 lines
2.3 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Tests for decnet.logging.forwarder — parse_log_target, probe_log_target.
"""
from unittest.mock import MagicMock, patch
import pytest
from decnet.logging.forwarder import parse_log_target, probe_log_target
class TestParseLogTarget:
def test_valid_ip_port(self):
host, port = parse_log_target("192.168.1.5:5140")
assert host == "192.168.1.5"
assert port == 5140
def test_valid_hostname_port(self):
host, port = parse_log_target("logstash.internal:9600")
assert host == "logstash.internal"
assert port == 9600
def test_no_colon_raises(self):
with pytest.raises(ValueError, match="Invalid log_target"):
parse_log_target("192.168.1.5")
def test_non_digit_port_raises(self):
with pytest.raises(ValueError, match="Invalid log_target"):
parse_log_target("192.168.1.5:syslog")
def test_empty_string_raises(self):
with pytest.raises(ValueError):
parse_log_target("")
def test_multiple_colons_uses_last_as_port(self):
# IPv6-style or hostname with colons — rsplit takes the last segment
host, port = parse_log_target("::1:514")
assert port == 514
class TestProbeLogTarget:
def test_returns_true_when_reachable(self):
mock_conn = MagicMock()
mock_conn.__enter__ = MagicMock(return_value=mock_conn)
mock_conn.__exit__ = MagicMock(return_value=False)
with patch("decnet.logging.forwarder.socket.create_connection",
return_value=mock_conn):
assert probe_log_target("192.168.1.5:5140") is True
def test_returns_false_when_connection_refused(self):
with patch("decnet.logging.forwarder.socket.create_connection",
side_effect=OSError("Connection refused")):
assert probe_log_target("192.168.1.5:5140") is False
def test_returns_false_on_timeout(self):
with patch("decnet.logging.forwarder.socket.create_connection",
side_effect=TimeoutError("timed out")):
assert probe_log_target("192.168.1.5:5140") is False
def test_returns_false_on_invalid_target(self):
# ValueError from parse_log_target is caught and returns False
assert probe_log_target("not-a-valid-target") is False