diff --git a/tests/test_installers.py b/tests/test_installers.py index c485365..244a7fb 100644 --- a/tests/test_installers.py +++ b/tests/test_installers.py @@ -61,35 +61,32 @@ def test_choose_mode(tmp_path): def test_proxmox_node_creates_container_and_installs_inside(tmp_path): - """On a Proxmox node (pct present) the installer makes an LXC and runs itself inside it.""" - fake = tmp_path / "bin" - fake.mkdir() + """On a Proxmox node (pct present) the installer makes an LXC and runs itself inside it. + pct/pveam/pvesm/pvesh are stubbed as exported bash functions (tmp may be mounted noexec).""" log = tmp_path / "calls.log" pushed = tmp_path / "pushed.sh" - - def stub(name, body): - (fake / name).write_text(f'#!/bin/bash\necho "{name} $*" >> {log}\n{body}') - (fake / name).chmod(0o755) - stub("pct", f'''case $1 in - list) printf 'VMID Status Lock Name\\n100 running other\\n'; [ -f {pushed}.created ] && echo "105 running netmon-hub" ; true ;; - create) touch {pushed}.created ;; - config) [ "$2" = 100 ] && echo "tags: web" ; [ "$2" = 105 ] && printf 'hostname: netmon-hub\\ntags: netmon-hub\\n' ; true ;; + created = tmp_path / "created" + stubs = f''' +pct() {{ echo "pct $*" >> {log}; case $1 in + list) printf 'VMID Status Lock Name\\n100 running other\\n'; [ -f {created} ] && echo "105 running netmon-hub"; true ;; + create) touch {created} ;; + config) [ "$2" = 100 ] && echo "tags: web"; [ "$2" = 105 ] && printf 'hostname: netmon-hub\\ntags: netmon-hub\\n'; true ;; status) echo "status: stopped" ;; push) cp "$3" {pushed} ;; - exec) shift 3; [ "$1" = hostname ] && echo "192.168.68.50 " ; true ;; -esac''') - stub("pvesh", 'echo 105') - stub("pveam", '[ "$1" = available ] && printf "system debian-12-standard_12.7-1_amd64.tar.zst\\nsystem debian-12-standard_12.12-1_amd64.tar.zst\\n"; true') - stub("pvesm", 'printf "Name Type Status Total\\nlocal dir active 1\\nlocal-lvm lvmthin active 2\\n"') - stub("sleep", "true") + exec) shift 3; [ "$1" = hostname ] && echo "192.168.68.50 "; true ;; +esac; }} +pvesh() {{ echo "pvesh $*" >> {log}; echo 105; }} +pveam() {{ echo "pveam $*" >> {log}; [ "$1" = available ] && printf "system debian-12-standard_12.7-1_amd64.tar.zst\\nsystem debian-12-standard_12.12-1_amd64.tar.zst\\n"; true; }} +pvesm() {{ printf "Name Type Status Total\\nlocal dir active 1\\nlocal-lvm lvmthin active 2\\n"; }} +sleep() {{ :; }} +id() {{ if [ "$1" = -u ]; then echo 0; else command id "$@"; fi; }} # root on the node, whoever runs the tests +export -f pct pvesh pveam pvesm sleep id +''' pve = tmp_path / "pve" pve.mkdir() - env = {"PATH": f"{fake}:{os.environ['PATH']}", "NETMON_PVE_DIR": str(pve), "INSTALL_DIR": str(tmp_path / "opt"), "HOST_PORT": "8000"} - r = subprocess.run(["bash", "install.sh"], cwd=ROOT, capture_output=True, text=True, env={**os.environ, **env}, timeout=60, - input="") - if os.geteuid() != 0: - assert r.returncode != 0 and "run the installer as root" in r.stderr - return + env = {**os.environ, "NETMON_PVE_DIR": str(pve), "INSTALL_DIR": str(tmp_path / "opt"), "HOST_PORT": "8000"} + run = lambda: subprocess.run(["bash", "-c", stubs + "bash install.sh"], cwd=ROOT, capture_output=True, text=True, env=env, timeout=60) + r = run() assert r.returncode == 0, r.stdout + r.stderr calls = log.read_text() assert "pveam download local debian-12-standard_12.12-1_amd64.tar.zst" in calls @@ -108,5 +105,5 @@ esac''') assert subprocess.run(["bash", "-n", str(pushed)]).returncode == 0 # A second run finds the tagged container instead of creating another. log.write_text("") - r = subprocess.run(["bash", "install.sh"], cwd=ROOT, capture_output=True, text=True, env={**os.environ, **env}, timeout=60) + r = run() assert r.returncode == 0 and "reusing container 105" in r.stdout and "pct create" not in log.read_text()