fix(ipam): keep extra addresses across a guest reboot
Testing on a live AlmaLinux container showed the alias disappearing after
a restart even though it was reported as applied: Proxmox regenerates the
guest network configuration on every start (pve-container rewrites
/etc/network/interfaces, ifcfg or the NM keyfile from netN), wiping any
address added to the native stack.
before reboot: address1=10.10.10.103/24 address2=10.10.10.106/24
after reboot: addresses=10.10.10.103/24 (file recreated by PVE)
Install a oneshot systemd unit that re-adds the recorded addresses after
network-online.target — /etc/systemd is not touched by Proxmox. The native
branches stay, so persist is now reported as e.g. "nm+systemd"; the unit
and its helper are removed once the last address is released.
Also fixed while testing on the two test instances:
- the alias script is ASCII-only now: it travels through the Proxmox API
and its marker ends up in guest system configs
- a composite persist marker no longer falls through the parser, which
used to report a successful apply as runtime_only
- allocate_ip marks the first address of a guest as primary on its own,
so allocations coming from the creation wizard are flagged correctly
This commit is contained in:
@@ -31,6 +31,11 @@ FAKE_IFUP = """#!/bin/sh
|
||||
exit 0
|
||||
"""
|
||||
|
||||
FAKE_SYSTEMCTL = """#!/bin/sh
|
||||
echo "$*" >> "$FAKE_LOG/systemctl.log"
|
||||
exit 0
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sandbox(tmp_path):
|
||||
@@ -41,7 +46,8 @@ def sandbox(tmp_path):
|
||||
for path in (root, bindir, logdir):
|
||||
path.mkdir(parents=True)
|
||||
|
||||
for name, body in (("ip", FAKE_IP), ("nmcli", FAKE_NMCLI), ("ifup", FAKE_IFUP)):
|
||||
for name, body in (("ip", FAKE_IP), ("nmcli", FAKE_NMCLI), ("ifup", FAKE_IFUP),
|
||||
("systemctl", FAKE_SYSTEMCTL)):
|
||||
target = bindir / name
|
||||
target.write_text(body)
|
||||
target.chmod(0o755)
|
||||
@@ -57,7 +63,7 @@ def sandbox(tmp_path):
|
||||
"ADDRESSES": addresses,
|
||||
}
|
||||
# Прячем инструменты, которых в этом сценарии быть не должно
|
||||
for name in ("ip", "nmcli", "ifup"):
|
||||
for name in ("ip", "nmcli", "ifup", "systemctl"):
|
||||
path = bindir / name
|
||||
hidden = bindir / f"{name}.hidden"
|
||||
if name in tools and hidden.exists():
|
||||
@@ -296,3 +302,75 @@ def test_missing_iface_is_rejected(sandbox):
|
||||
|
||||
assert result.returncode == 2
|
||||
assert "missing IFACE" in result.stdout
|
||||
|
||||
|
||||
def test_script_is_ascii_only():
|
||||
"""Скрипт уезжает в гостя через API Proxmox, который спотыкается на
|
||||
не-ASCII, и его маркер попадает в системные конфиги — держим файл в ASCII."""
|
||||
raw = SCRIPT.read_bytes()
|
||||
non_ascii = [b for b in raw if b > 127]
|
||||
assert not non_ascii, f"в скрипте {len(non_ascii)} не-ASCII байт"
|
||||
|
||||
|
||||
# --- systemd-подстраховка ------------------------------------------------------
|
||||
|
||||
def test_systemd_unit_is_installed_alongside_native_stack(sandbox):
|
||||
"""Proxmox переписывает сетевой конфиг гостя при каждом старте, поэтому
|
||||
адреса восстанавливает ещё и oneshot-юнит — /etc/systemd он не трогает."""
|
||||
root = sandbox["root"]
|
||||
_mkdirs(root, "etc/sysconfig/network-scripts", "etc/systemd/system")
|
||||
|
||||
result = sandbox["run"](addresses="10.10.10.90/24", tools=("ip", "systemctl"))
|
||||
|
||||
assert sandbox["persist_of"](result) == "sysconfig+systemd"
|
||||
unit = (root / "etc/systemd/system/pvemanager-aliases.service").read_text()
|
||||
assert "After=network-online.target" in unit
|
||||
assert "ExecStart=/usr/local/sbin/pvemanager-aliases" in unit
|
||||
helper = root / "usr/local/sbin/pvemanager-aliases"
|
||||
assert "ip addr add" in helper.read_text()
|
||||
assert "enable pvemanager-aliases.service" in sandbox["log"]("systemctl")
|
||||
|
||||
|
||||
def test_systemd_unit_alone_when_no_native_stack(sandbox):
|
||||
root = sandbox["root"]
|
||||
_mkdirs(root, "etc/systemd/system")
|
||||
|
||||
result = sandbox["run"](addresses="10.10.10.90/24", tools=("ip", "systemctl"))
|
||||
|
||||
assert sandbox["persist_of"](result) == "systemd"
|
||||
assert (root / "etc/systemd/system/pvemanager-aliases.service").exists()
|
||||
|
||||
|
||||
def test_systemd_unit_removed_when_last_address_goes(sandbox):
|
||||
root = sandbox["root"]
|
||||
_mkdirs(root, "etc/systemd/system")
|
||||
|
||||
sandbox["run"](addresses="10.10.10.90/24", tools=("ip", "systemctl"))
|
||||
result = sandbox["run"](addresses="", tools=("ip", "systemctl"))
|
||||
|
||||
assert sandbox["persist_of"](result) == "none"
|
||||
assert not (root / "etc/systemd/system/pvemanager-aliases.service").exists()
|
||||
assert not (root / "usr/local/sbin/pvemanager-aliases").exists()
|
||||
assert "disable pvemanager-aliases.service" in sandbox["log"]("systemctl")
|
||||
|
||||
|
||||
def test_systemd_unit_stays_while_another_interface_has_addresses(sandbox):
|
||||
"""Опустошили eth0, но на eth1 адрес остался — юнит нужен дальше."""
|
||||
root = sandbox["root"]
|
||||
_mkdirs(root, "etc/systemd/system")
|
||||
|
||||
sandbox["run"](addresses="10.10.10.90/24", iface="eth0", tools=("ip", "systemctl"))
|
||||
sandbox["run"](addresses="10.10.10.91/24", iface="eth1", tools=("ip", "systemctl"))
|
||||
sandbox["run"](addresses="", iface="eth0", tools=("ip", "systemctl"))
|
||||
|
||||
assert (root / "etc/systemd/system/pvemanager-aliases.service").exists()
|
||||
|
||||
|
||||
def test_without_systemd_persist_has_no_suffix(sandbox):
|
||||
root = sandbox["root"]
|
||||
_mkdirs(root, "etc/sysconfig/network-scripts", "etc/systemd/system")
|
||||
|
||||
result = sandbox["run"](addresses="10.10.10.90/24", tools=("ip",))
|
||||
|
||||
assert sandbox["persist_of"](result) == "sysconfig"
|
||||
assert not (root / "etc/systemd/system/pvemanager-aliases.service").exists()
|
||||
|
||||
@@ -216,3 +216,25 @@ def test_unknown_guest_is_pending_not_crash(guest, monkeypatch):
|
||||
|
||||
assert result.status == guest_ip_service.PENDING
|
||||
assert recorder.calls == []
|
||||
|
||||
|
||||
def test_combined_persist_marker_is_understood(guest, monkeypatch):
|
||||
"""Скрипт сообщает составной способ закрепления: родной стек + systemd."""
|
||||
allocation = guest["add_alias"]("10.10.10.90")
|
||||
_patch(monkeypatch, script_engine.ExecResult(
|
||||
True, "PVEMANAGER_PERSIST=nm+systemd\nPVEMANAGER_OK=1\n", 0))
|
||||
|
||||
result = guest_ip_service.apply_address(guest["db"], allocation)
|
||||
|
||||
assert result.persist == "nm+systemd"
|
||||
assert result.status == guest_ip_service.APPLIED
|
||||
assert allocation.apply_status == guest_ip_service.APPLIED
|
||||
|
||||
|
||||
def test_plain_systemd_persist_is_applied(guest, monkeypatch):
|
||||
allocation = guest["add_alias"]("10.10.10.90")
|
||||
_patch(monkeypatch, script_engine.ExecResult(
|
||||
True, "PVEMANAGER_PERSIST=systemd\nPVEMANAGER_OK=1\n", 0))
|
||||
|
||||
assert guest_ip_service.apply_address(guest["db"], allocation).status == \
|
||||
guest_ip_service.APPLIED
|
||||
|
||||
@@ -255,3 +255,48 @@ def test_network_scan_skips_node_and_cached_guest_ips(env):
|
||||
env["network"].id, None, allow_network_scan=True)
|
||||
|
||||
assert ip not in ("10.10.10.1", "10.10.10.2", "10.10.10.3")
|
||||
|
||||
|
||||
def test_first_address_of_a_guest_becomes_primary_automatically(env):
|
||||
"""Мастер создания инстанса не передаёт is_primary — флаг ставится сам."""
|
||||
first, error = env["service"].allocate_ip(
|
||||
ip_address="10.10.10.60", network_id=env["network"].id, resource_type="lxc",
|
||||
resource_name="gitea", proxmox_server_id=env["server"].id, proxmox_vmid=109,
|
||||
proxmox_node="dev", allocated_by="wizard",
|
||||
)
|
||||
assert error is None and first.is_primary is True
|
||||
|
||||
second, error = env["service"].allocate_ip(
|
||||
ip_address="10.10.10.61", network_id=env["network"].id, resource_type="lxc",
|
||||
resource_name="gitea", proxmox_server_id=env["server"].id, proxmox_vmid=109,
|
||||
proxmox_node="dev", allocated_by="wizard",
|
||||
)
|
||||
assert error is None and second.is_primary is False
|
||||
|
||||
|
||||
def test_alias_never_claims_primary_by_itself(env):
|
||||
alias, error = env["service"].allocate_ip(
|
||||
ip_address="10.10.10.62", network_id=env["network"].id,
|
||||
proxmox_server_id=env["server"].id, proxmox_vmid=109, allocated_by="tester",
|
||||
assignment_kind="alias", target_interface="eth0",
|
||||
)
|
||||
assert error is None and alias.is_primary is False
|
||||
|
||||
|
||||
def test_explicit_flag_still_wins(env):
|
||||
env["allocate"]("10.10.10.63", primary=True, kind="primary")
|
||||
forced, error = env["service"].allocate_ip(
|
||||
ip_address="10.10.10.64", network_id=env["network"].id,
|
||||
proxmox_server_id=env["server"].id, proxmox_vmid=109, allocated_by="tester",
|
||||
is_primary=False,
|
||||
)
|
||||
assert error is None and forced.is_primary is False
|
||||
|
||||
|
||||
def test_reservation_without_a_guest_is_not_primary(env):
|
||||
"""Ручная бронь без привязки к гостю основной быть не может."""
|
||||
manual, error = env["service"].allocate_ip(
|
||||
ip_address="10.10.10.65", network_id=env["network"].id,
|
||||
resource_name="reserved by hand", allocated_by="admin",
|
||||
)
|
||||
assert error is None and manual.is_primary is False
|
||||
|
||||
Reference in New Issue
Block a user