Files
napalm-proxmox/tests/test_credential_expiry.py
T
Christian Manivong fddf8af680 feat: Proxmox VE 9.3 API additions (NetOrk/netork#2728)
On a node running 9.3: HA maintenance over PUT /nodes/{node}/maintenance
(the target node's version decides, SSH below it), the guest uid in
get_vms, the cluster-id as ClusterDict.id, sync-time-on-resume on resume.
get_credential_expiry reads the expiry of the token or account the driver
logged in with; get_update_history lists the node's APT transactions.
Needs napalm-device-types 4.44.0.
2026-11-26 17:35:50 +01:00

127 lines
4.9 KiB
Python

"""When the credential the driver logs in with stops working (NetOrk/netork#2728).
PVE 9.3's datacenter ``token-policy`` can require an expiry on every API token;
the platform keeps one on every token and account anyway. A token is read with
``GET /access/users/{userid}/token/{tokenid}``; a token without an ``expire``
of its own expires with its user (``default: same as user``), read with
``GET /access/users/{userid}``, which is also where a password's account says
when it expires. ``0`` is never.
"""
from __future__ import annotations
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from proxmoxer.core import ResourceException
from napalm_proxmox.driver import ProxmoxDriver
from tests.conftest import _build_mock_api
def _driver(**optional_args: Any) -> tuple[ProxmoxDriver, MagicMock]:
password = "" if optional_args else "secret"
drv = ProxmoxDriver("pve1", "netork", password, optional_args=optional_args)
api = _build_mock_api()
with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=api):
drv.open()
return drv, api
def _token_driver(token_name: str = "netork@pve!poll") -> tuple[ProxmoxDriver, MagicMock]:
return _driver(token_name=token_name, token_value="s3cret", realm="pve")
def _forbidden() -> ResourceException:
return ResourceException(403, "Forbidden", "Permission check failed (/access, User.Modify)")
class TestApiToken:
def test_the_tokens_own_expiry(self):
drv, api = _token_driver()
api.access.users.return_value.token.return_value.get.return_value = {
"expire": 1767225600,
"privsep": 1,
}
assert drv.get_credential_expiry() == {
"kind": "api-token",
"name": "netork@pve!poll",
"expires": 1767225600,
}
api.access.users.assert_called_with("netork@pve")
api.access.users.return_value.token.assert_called_once_with("poll")
def test_zero_never_expires(self):
drv, api = _token_driver()
api.access.users.return_value.token.return_value.get.return_value = {"expire": 0}
assert drv.get_credential_expiry()["expires"] is None
api.access.users.return_value.get.assert_not_called()
def test_a_token_without_expiry_expires_with_its_user(self):
drv, api = _token_driver()
api.access.users.return_value.token.return_value.get.return_value = {"privsep": 1}
api.access.users.return_value.get.return_value = {
"userid": "netork@pve",
"expire": 1798761600,
}
assert drv.get_credential_expiry()["expires"] == 1798761600
def test_a_token_name_without_user_is_the_login_users(self):
drv, api = _token_driver(token_name="poll")
api.access.users.return_value.token.return_value.get.return_value = {"expire": 0}
assert drv.get_credential_expiry()["name"] == "netork@pve!poll"
api.access.users.assert_called_with("netork@pve")
def test_a_refused_read_is_a_permission_error(self):
drv, api = _token_driver()
api.access.users.return_value.token.return_value.get.side_effect = _forbidden()
with pytest.raises(PermissionError, match="User.Modify"):
drv.get_credential_expiry()
def test_a_refused_read_of_the_user_is_a_permission_error(self):
drv, api = _token_driver()
api.access.users.return_value.token.return_value.get.return_value = {}
api.access.users.return_value.get.side_effect = _forbidden()
with pytest.raises(PermissionError):
drv.get_credential_expiry()
class TestAccount:
def test_a_password_login_reads_its_account(self):
drv, api = _driver()
api.access.users.return_value.get.return_value = {
"userid": "netork@pam",
"expire": 1767225600,
}
assert drv.get_credential_expiry() == {
"kind": "account",
"name": "netork@pam",
"expires": 1767225600,
}
api.access.users.assert_called_with("netork@pam")
def test_an_account_without_expiry_never_expires(self):
drv, api = _driver()
api.access.users.return_value.get.return_value = {"userid": "netork@pam", "enable": 1}
assert drv.get_credential_expiry()["expires"] is None
def test_a_refused_read_is_a_permission_error(self):
drv, api = _driver()
api.access.users.return_value.get.side_effect = _forbidden()
with pytest.raises(PermissionError):
drv.get_credential_expiry()
@pytest.mark.parametrize(
"error",
[
ResourceException(500, "Internal Server Error", "cfs lock timeout"),
ConnectionError("reset"),
],
)
def test_another_failure_is_a_runtime_error(self, error):
drv, api = _driver()
api.access.users.return_value.get.side_effect = error
with pytest.raises(RuntimeError, match="netork@pam"):
drv.get_credential_expiry()