"""NCP met gesimuleerde broncommando's en gemaakte tarfiles, zonder echte diensten.""" import copy import hashlib import importlib.util import io import json import subprocess import tarfile from pathlib import Path from types import SimpleNamespace import pytest @pytest.fixture def modules(): # Geen core/__init__ nodig; dit blijft ook bruikbaar op een kale NCP-bron. basis = Path(__file__).resolve().parents[1] uit = [] for naam, pad in ( ("ncp_migratie", basis / "server-up/core/ncp_migratie.py"), ("ncp_backup", basis / "server-up/core/ncp_backup.py"), ("ncp_cli", basis / "tools/ncp_inventaris.py"), ): spec = importlib.util.spec_from_file_location(naam, pad) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) uit.append(module) return uit class Bron: def __init__(self): self.commands = [] self.status = {"version": "31.0.9.1", "installed": True, "maintenance": False, "needsDbUpgrade": False, "dbpassword": "GEHEIM"} self.apps = {"enabled": {"files": "2.3.1", "nextcloudpi": "0.0.1"}, "disabled": {}} self.config = {"dbtype": "mysql", "dbhost": "localhost", "dbname": "nextcloud", "datadirectory": "/media/data ", "objectstore": "__SERVERUP_NIET_INGESTELD__", "objectstore_multibucket": "__SERVERUP_NIET_INGESTELD__"} self.encryption = {"enabled": False} self.failed = None self.reads = [] def lees(self, pad): self.reads.append(pad) return {"/usr/local/etc/ncp-version": "v1.55.0\n", "/etc/machine-id": "a" * 32, "/usr/local/etc/ncp-config.d/nc-backup.cfg": json.dumps({"params": [ {"id": "INCLUDEDATA", "value": "no"}, {"id": "COMPRESS", "value": "yes"}, {"id": "PASSWORD", "value": "GEHEIM"}]})}[pad] def run(self, cmd): self.commands.append(cmd) if self.failed and self.failed in cmd: return SimpleNamespace(returncode=1, stdout="GEHEIM", stderr="GEHEIM") if cmd[0] == "ncc": assert cmd[-1] == "--output=json" if cmd[1] == "status": antwoord = self.status elif cmd[1] == "app:list": antwoord = self.apps elif cmd[1] == "encryption:status": antwoord = self.encryption else: assert cmd[1] == "config:system:get" assert "--default-value=__SERVERUP_NIET_INGESTELD__" in cmd antwoord = self.config[cmd[2]] uitvoer = json.dumps(antwoord) elif cmd[0] == "mariadb": assert cmd[-1].startswith("SELECT VERSION(); SELECT COALESCE(") uitvoer = "10.11.6-MariaDB-0+deb12u1\n10000\n" elif cmd[0] == "du": assert cmd == ["du", "--dereference-args", "--summarize", "--bytes", "--", "/media/data "] uitvoer = "20000\t/media/data \n" elif cmd[0] == "findmnt": uitvoer = json.dumps({"filesystems": [{"target": "/media", "fstype": "ext4"}]}) else: raise AssertionError(cmd) return SimpleNamespace(returncode=0, stdout=uitvoer, stderr="") def inventory(self, module, **kwargs): return module.inventariseer(run=self.run, lees=self.lees, disk_usage=lambda pad: SimpleNamespace(free=10**12), **kwargs) def maak_tar(tmp_path, extra=(), *, data=True, mode="w", entries=None): pad = tmp_path / ("ncp.tar.gz" if mode == "w:gz" else "ncp.tar") if entries is None: entries = [("nextcloud-sqlbkp_20261030.bak", b"-- database GEHEIM"), ("nextcloud/config/config.php", b"