Fix the Proxmox installer test under verify.sh

The test container runs pytest as a non-root user with /tmp mounted noexec,
so stub scripts written there couldn't run. Stub pct, pveam, pvesm, pvesh,
sleep and id as exported bash functions instead, acting as root on the node
whoever runs the tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
neil 2026-11-04 18:31:48 -08:00
parent a17f2b502c
commit 1dad1ad85d

View file

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