64 lines
2.7 KiB
Python
64 lines
2.7 KiB
Python
"""Package preparation admission uses synthetic OS/dpkg state; no host mutation."""
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def installer(tmp_path, monkeypatch):
|
|
source = Path(__file__).parents[2] / 'apps/node-agent/packaging/netbird_install.py'
|
|
spec = importlib.util.spec_from_file_location('node_netbird_install', source)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
system = tmp_path / 'os-release'
|
|
system.write_text('ID=ubuntu\nVERSION_ID="24.04"\n')
|
|
monkeypatch.setattr(module, 'Path', lambda name: system if name == '/etc/os-release' else Path(name))
|
|
monkeypatch.setattr(module.os, 'uname', lambda: SimpleNamespace(machine='x86_64'))
|
|
module.ROOT = tmp_path
|
|
payload = b'synthetic pinned Debian payload'
|
|
(tmp_path / 'netbird.deb').write_bytes(payload)
|
|
lock = {'version': '0.82.0', 'items': [{'id': 'linux-amd64', 'filename': 'netbird.deb', 'bytes': len(payload), 'sha256': hashlib.sha256(payload).hexdigest()}]}
|
|
(tmp_path / 'clients.lock.json').write_text(json.dumps(lock))
|
|
return module
|
|
|
|
|
|
def test_existing_other_version_is_preserved(installer, monkeypatch):
|
|
calls = []
|
|
def run(argv, **kwargs):
|
|
calls.append(argv)
|
|
return SimpleNamespace(returncode=0, stdout='0.99.0')
|
|
monkeypatch.setattr(installer.subprocess, 'run', run)
|
|
with pytest.raises(ValueError, match='другая версия'):
|
|
installer.install()
|
|
assert len(calls) == 1 and calls[0][0] == '/usr/bin/dpkg-query'
|
|
|
|
|
|
def test_corrupted_payload_never_reaches_package_manager(installer, monkeypatch):
|
|
(installer.ROOT / 'netbird.deb').write_bytes(b'corrupt')
|
|
monkeypatch.setattr(installer.subprocess, 'run', lambda *a, **kw: pytest.fail('OS mutation'))
|
|
with pytest.raises(ValueError, match='повреждён'):
|
|
installer.install()
|
|
|
|
|
|
def test_first_install_and_repeat_preserve_credentials(installer, monkeypatch):
|
|
calls = []
|
|
installed = False
|
|
def run(argv, **kwargs):
|
|
nonlocal installed
|
|
calls.append(argv)
|
|
if argv[0] == '/usr/bin/dpkg-query':
|
|
return SimpleNamespace(returncode=0 if installed else 1, stdout='0.82.0\tinstalled' if installed else '')
|
|
if argv[0] == '/usr/bin/apt-get':
|
|
installed = True
|
|
return SimpleNamespace(returncode=0, stdout='')
|
|
monkeypatch.setattr(installer.subprocess, 'run', run)
|
|
installer.install()
|
|
installer.install()
|
|
assert sum(c[0] == '/usr/bin/apt-get' for c in calls) == 1
|
|
assert all('up' not in c and 'down' not in c and 'login' not in c for c in calls)
|
|
assert any('--no-remove' in c for c in calls)
|