Files
napalm-proxmox/tests/test_login_sessions.py
T
Christian Manivong 3d661e0b3f
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 17s
driver / lowest (pull_request) Successful in 41s
driver / test (3.12) (pull_request) Successful in 1m6s
driver / test (3.13) (pull_request) Successful in 58s
driver / types (pull_request) Successful in 1m13s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 1m18s
CI / driver (pull_request) Successful in 2m18s
driver / publish (pull_request) Skipped
feat(logins): get_login_sessions on PVE and PBS, 0.21.0 (NetOrk/netork#2881)
napalm-device-types' LoginSessionsMixin over the node's exec path: the
interactive logins the node recorded, someone's own ssh or the web
interface's shell, with the driver's own SSH client address.
2026-12-01 09:41:28 +01:00

64 lines
2.3 KiB
Python

"""`get_login_sessions`: the interactive logins a node recorded (NetOrk/netork#2881).
A shell on a node that did not go through the application -- someone's own ``ssh``, or
the web interface's shell -- is in the node's wtmp. The command and its parse are
napalm-device-types' (``LoginSessionsMixin``); both drivers carry the command over their
own exec path, where ``SSH_CONNECTION`` names the driver's own address.
"""
from __future__ import annotations
from unittest.mock import patch
import pytest
from napalm_device_types import LoginSessionsMixin, LoginSessionsUnavailable
from napalm_proxmox.backup_driver import ProxmoxBackupDriver
from napalm_proxmox.driver import ProxmoxDriver
WIRE = "\n".join(
[
"LOGSESSIONS_BEGIN",
"[own]",
"198.51.100.2",
"[now]",
"2026-11-30T12:00:00Z",
"[wtmp since]",
"root pts/0 192.0.2.10 2026-11-30T10:15:02+01:00 still logged in",
"root pts/1 0.0.0.0 2026-11-29T09:00:00+01:00 - 2026-11-29T09:30:00+01:00 (00:30)",
"LOGSESSIONS_END",
]
)
@pytest.mark.parametrize("cls", [ProxmoxDriver, ProxmoxBackupDriver])
def test_both_drivers_declare_the_contract(cls):
assert issubclass(cls, LoginSessionsMixin)
assert hasattr(cls, "get_login_sessions")
def test_the_node_runs_the_shared_command_over_its_exec_path(driver):
with patch.object(driver, "_exec_ssh_command", return_value=WIRE) as exec_:
reading = driver.get_login_sessions()
exec_.assert_called_once()
assert exec_.call_args.args[0].startswith("sh -c ")
assert reading["own_address"] == "198.51.100.2"
assert [s["address"] for s in reading["sessions"]] == ["192.0.2.10", None]
def test_the_backup_server_reads_it_the_same_way():
drv = ProxmoxBackupDriver("192.0.2.80", "root", "", optional_args={"token_name": "a!b"})
with patch.object(drv, "_exec_ssh_command", return_value=WIRE):
reading = drv.get_login_sessions()
assert reading["sessions"][0]["user"] == "root"
def test_a_node_without_login_records_raises(driver):
empty = "LOGSESSIONS_BEGIN\n[own]\n\n[now]\n2026-11-30T12:00:00Z\nLOGSESSIONS_END"
with patch.object(driver, "_exec_ssh_command", return_value=empty):
with pytest.raises(LoginSessionsUnavailable):
driver.get_login_sessions()