The rename replaced \b<old>\b, which also matches before "." and "-": "pve.old.lan pve" became "new.dom.old.lan new.dom" and "pve-backup" became "new-backup". Whole whitespace-separated names are renamed now, comments left alone. The live move of /etc/pve/nodes/<old> the issue also asks about is not changed here; it needs a decision on clustered nodes. Refs #21
73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
"""set_hostname: what it writes to /etc/hosts (napalm-proxmox#21)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import re
|
|
|
|
import pytest
|
|
|
|
from napalm_proxmox import ProxmoxDriver
|
|
|
|
HOSTS = """\
|
|
127.0.0.1 localhost.localdomain localhost
|
|
# pve is the node; pve-backup its backup target
|
|
10.0.0.1 pve.old.lan pve
|
|
10.0.0.2 pve-backup.old.lan pve-backup
|
|
"""
|
|
|
|
|
|
class _Node:
|
|
"""The node's shell: /etc/hostname reads "pve", /etc/hosts reads HOSTS."""
|
|
|
|
def __init__(self) -> None:
|
|
self.ran: list[str] = []
|
|
|
|
def __call__(self, command: str) -> str:
|
|
self.ran.append(command)
|
|
if command.startswith("cat /etc/hostname"):
|
|
return "pve\n"
|
|
if command.startswith("cat /etc/hosts"):
|
|
return HOSTS
|
|
return ""
|
|
|
|
def written_hosts(self) -> str:
|
|
write = next(c for c in self.ran if c.rstrip().endswith("chmod 644 /etc/hosts"))
|
|
return base64.b64decode(re.match(r"echo (\S+) \| base64 -d", write).group(1)).decode()
|
|
|
|
|
|
@pytest.fixture
|
|
def node() -> _Node:
|
|
return _Node()
|
|
|
|
|
|
@pytest.fixture
|
|
def driver(node: _Node) -> ProxmoxDriver:
|
|
d = ProxmoxDriver("pve", "root@pam", "pw")
|
|
d._exec_ssh_command = node
|
|
return d
|
|
|
|
|
|
def test_an_fqdn_renames_the_nodes_names_and_nothing_else(driver, node):
|
|
driver.set_hostname("new.dom.lan")
|
|
|
|
assert node.written_hosts() == (
|
|
"127.0.0.1 localhost.localdomain localhost\n"
|
|
"# pve is the node; pve-backup its backup target\n"
|
|
"10.0.0.1 new.dom.lan new\n"
|
|
"10.0.0.2 pve-backup.old.lan pve-backup\n"
|
|
)
|
|
|
|
|
|
def test_a_short_name_keeps_the_old_domain(driver, node):
|
|
driver.set_hostname("new")
|
|
|
|
assert "10.0.0.1 new.old.lan new\n" in node.written_hosts()
|
|
assert "10.0.0.2 pve-backup.old.lan pve-backup\n" in node.written_hosts()
|
|
|
|
|
|
def test_the_same_name_leaves_hosts_alone(driver, node):
|
|
driver.set_hostname("pve")
|
|
|
|
assert not any("/etc/hosts" in c and "base64 -d" in c for c in node.ran)
|