Some checks failed
Deploy server-up (dev) / deploy (push) Failing after 3m40s
- Nieuwe knop "Wat staat er?" bij de migratiemelding. Toont controller, door de app aangemaakte containers en volumes, en of de datamap inhoud heeft; conclusie signalen, geen_signalen of onbekend. - core/inventaris.py: alleen docker ps (strikt), inspect en volume ls via een allowlist; geen run, exec of logs. Uit inspect alleen NEXTCLOUD_DATADIR/NEXTCLOUD_MOUNT en mounts, nooit de volledige Env. - Fail-closed: Docker-fouten, onverwachte vormen in inspect, compose of volume ls, en een datamap die Server Up niet ziet (in een container alleen onder BASE_DIR, ook na symlinks) geven onbekend. - GET /api/stacks/<naam>/inventaris, alleen voor lifecycle external; zonder stackslot omdat het niets wijzigt. - templatebeleid.splits_containers: stack-eigen containers los van siblings, in elke status (de mastercontainer heeft hetzelfde voorvoegsel). - Nieuw optioneel sjabloonveld lifecycle.volume_prefix; AIO: nextcloud_aio_. - UI: inventaris per stack los van de berichtenlijst, zodat herladen het antwoord niet wist. disabled-binding met !!: de meegeleverde Alpine zette disabled ook bij undefined (gevonden in de headless controle). - tests/test_inventaris.py: 46 tests, waaronder Codex' reviewgevallen (onverwachte types, corrupte volumeregels, symlink buiten BASE_DIR) en een allowlist-test op alle Docker-aanroepen. menu.test.js: 5 checks. - Docs: apps-maken.md (volume_prefix), nextcloud-aio.md (de knop en de drie conclusies), CHANGELOG, overleg. Volledige suite: 7742 passed; JS-menucontroles en rendervalidatie groen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WPhDUEiVvmH9F4uUkPGKsT
355 lines
13 KiB
Python
355 lines
13 KiB
Python
"""Migratie-inventaris: wat een oude AIO-installatie werkelijk gebruikt.
|
|
|
|
Alleen lezen. Uit de uitkomst volgt of je de app kunt verwijderen en opnieuw
|
|
installeren, of moet migreren via de AIO-backup. Een fout mag daarom nooit
|
|
`geen_signalen` worden.
|
|
"""
|
|
import json
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from conftest import login
|
|
|
|
GEHEIM = "hunter2-geheim"
|
|
|
|
# Wat release v0.8.31 rendert: schema 1, configvolume <servicenaam>_config.
|
|
OUDE_COMPOSE = """services:
|
|
nextcloud-aio-mastercontainer:
|
|
image: nextcloud/all-in-one:latest
|
|
init: true
|
|
container_name: nextcloud-aio-mastercontainer
|
|
ports:
|
|
- "8082:8080"
|
|
environment:
|
|
- APACHE_PORT=11000
|
|
- NEXTCLOUD_DATADIR={datadir}
|
|
- TZ=Etc/UTC
|
|
volumes:
|
|
- nextcloud-aio-mastercontainer_config:/mnt/docker-aio-config
|
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
|
restart: unless-stopped
|
|
|
|
volumes:
|
|
nextcloud-aio-mastercontainer_config:
|
|
name: nextcloud-aio-mastercontainer_config
|
|
"""
|
|
|
|
MASTER = "nextcloud-aio-mastercontainer"
|
|
|
|
|
|
def _ps_regel(naam, staat, project=""):
|
|
return "\t".join(["abc123", naam, "img", staat, staat, "", project, "", ""])
|
|
|
|
|
|
@pytest.fixture
|
|
def aio(env, monkeypatch):
|
|
"""Een oude AIO-stack plus een nagebootste Docker die alles vastlegt."""
|
|
from core import docker, git, paden
|
|
monkeypatch.setattr(paden, "_in_container", lambda: False)
|
|
shutil.copytree(Path(__file__).resolve().parent.parent / "apps" / "nextcloud-aio",
|
|
git.cache_dir("server-up") / "apps" / "nextcloud-aio")
|
|
datadir = env["tmp"] / "ncdata"
|
|
d = env["lib"] / "nextcloud-aio"
|
|
d.mkdir()
|
|
(d / "docker-compose.yml").write_text(OUDE_COMPOSE.format(datadir=datadir))
|
|
(d / ".serverup.json").write_text(json.dumps({
|
|
"source": "nextcloud-aio", "repo_id": "server-up", "template_schema": 1,
|
|
"values": {"service_name": MASTER}}))
|
|
|
|
docker_ = {
|
|
"ps": [_ps_regel(MASTER, "exited", "nextcloud-aio")],
|
|
"volumes": ["nextcloud-aio-mastercontainer_config", "iets_anders"],
|
|
"env": [f"NEXTCLOUD_DATADIR={datadir}", "APACHE_PORT=11000",
|
|
f"ADMIN_PASSWORD={GEHEIM}"],
|
|
"rc": {}, "stdout": {},
|
|
"opdrachten": [],
|
|
}
|
|
|
|
def _run(cmd, **kw):
|
|
args = cmd[1:]
|
|
docker_["opdrachten"].append(args)
|
|
soort = "volume" if args[:2] == ["volume", "ls"] else args[0]
|
|
if soort in docker_["rc"]:
|
|
return subprocess.CompletedProcess(cmd, docker_["rc"][soort], "", "kapot")
|
|
if soort in docker_["stdout"]:
|
|
return subprocess.CompletedProcess(cmd, 0, docker_["stdout"][soort], "")
|
|
if soort == "ps":
|
|
uit = "\n".join(docker_["ps"]) + "\n"
|
|
elif soort == "inspect":
|
|
namen = [a for a in args[1:] if not a.startswith("--") and a != "container"]
|
|
uit = json.dumps([{
|
|
"Name": "/" + n,
|
|
"Config": {"Env": docker_["env"]},
|
|
"Mounts": [{"Type": "volume", "Name": "nextcloud-aio-mastercontainer_config",
|
|
"Source": "/var/lib/docker/volumes/x/_data",
|
|
"Destination": "/mnt/docker-aio-config"}],
|
|
} for n in namen])
|
|
elif soort == "volume":
|
|
uit = "\n".join(docker_["volumes"]) + "\n"
|
|
else:
|
|
pytest.fail(f"Niet-lezende Docker-opdracht: {args}")
|
|
return subprocess.CompletedProcess(cmd, 0, uit, "")
|
|
|
|
echte_run = docker._run
|
|
monkeypatch.setattr(docker, "_run", _run)
|
|
return {"d": d, "datadir": datadir, "docker": docker_, "echte_run": echte_run}
|
|
|
|
|
|
def _maak(aio):
|
|
from core import boilerplates, inventaris
|
|
bron = boilerplates.metadata(
|
|
Path(__file__).resolve().parent.parent / "apps" / "nextcloud-aio")
|
|
return inventaris.maak(aio["d"], bron)
|
|
|
|
|
|
# ── Conclusie ───────────────────────────────────────────────────────────────
|
|
|
|
def test_nooit_gestart_geeft_geen_signalen(aio):
|
|
"""Het geval van release v0.8.31: AIO weigerde het configvolume."""
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "geen_signalen", uit["bevindingen"]
|
|
assert "bewijst niet" in uit["uitleg"]
|
|
assert [c["naam"] for c in uit["controller"]] == [MASTER]
|
|
assert uit["siblings"] == [] and uit["volumes"] == []
|
|
|
|
|
|
def test_mastercontainer_is_geen_sibling(aio):
|
|
"""Hij begint ook met 'nextcloud-aio-', maar hoort bij de stack zelf."""
|
|
aio["docker"]["ps"] = [_ps_regel(MASTER, "running")] # zonder projectlabel
|
|
uit = _maak(aio)
|
|
assert uit["siblings"] == [] and uit["conclusie"] == "geen_signalen"
|
|
|
|
|
|
def test_siblings_zijn_een_signaal(aio):
|
|
aio["docker"]["ps"] += [_ps_regel("nextcloud-aio-nextcloud", "exited"),
|
|
_ps_regel("nextcloud-aio-database", "running")]
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "signalen"
|
|
assert {s["naam"] for s in uit["siblings"]} == {
|
|
"nextcloud-aio-nextcloud", "nextcloud-aio-database"}
|
|
|
|
|
|
def test_datavolume_is_een_signaal(aio):
|
|
aio["docker"]["volumes"].append("nextcloud_aio_nextcloud_data")
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "signalen"
|
|
assert uit["volumes"] == [{"naam": "nextcloud_aio_nextcloud_data"}]
|
|
|
|
|
|
def test_eigen_configvolume_telt_niet(aio, monkeypatch):
|
|
"""Het nieuwe sjabloon noemt zijn configvolume nextcloud_aio_mastercontainer;
|
|
dat is AIO-configuratie, geen Nextcloud-gegevens."""
|
|
from core import inventaris
|
|
aio["docker"]["volumes"].append("nextcloud_aio_mastercontainer")
|
|
oud = inventaris._inspecteer
|
|
|
|
def met_nieuw_volume(namen):
|
|
uit = oud(namen)
|
|
for info in uit.values():
|
|
info["mounts"][0]["naam"] = "nextcloud_aio_mastercontainer"
|
|
return uit
|
|
monkeypatch.setattr(inventaris, "_inspecteer", met_nieuw_volume)
|
|
uit = _maak(aio)
|
|
assert uit["volumes"] == [] and uit["conclusie"] == "geen_signalen"
|
|
|
|
|
|
@pytest.mark.parametrize("vul,verwacht", [
|
|
(None, "geen_signalen"), ("leeg", "geen_signalen"), ("bestand", "signalen")])
|
|
def test_datamap(aio, vul, verwacht):
|
|
if vul:
|
|
aio["datadir"].mkdir()
|
|
if vul == "bestand":
|
|
(aio["datadir"] / "nextcloud.log").write_text("x")
|
|
assert _maak(aio)["conclusie"] == verwacht
|
|
|
|
|
|
def test_onleesbare_datamap_is_een_signaal(aio, monkeypatch):
|
|
aio["datadir"].mkdir()
|
|
echte = Path.iterdir
|
|
|
|
def weigert(self):
|
|
if self == aio["datadir"]:
|
|
raise PermissionError("www-data")
|
|
return echte(self)
|
|
monkeypatch.setattr(Path, "iterdir", weigert)
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "signalen"
|
|
assert uit["datamap"]["inhoud"] == "niet_leesbaar"
|
|
|
|
|
|
def test_onzichtbare_datamap_is_onbekend(aio, monkeypatch):
|
|
"""In een container ziet Server Up buiten BASE_DIR niet wat de host ziet."""
|
|
from core import paden
|
|
monkeypatch.setattr(paden, "_in_container", lambda: True)
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "onbekend"
|
|
assert uit["datamap"]["zichtbaar"] is False
|
|
|
|
|
|
def test_datamap_uit_compose_als_de_container_weg_is(aio):
|
|
aio["docker"]["ps"] = []
|
|
aio["datadir"].mkdir()
|
|
(aio["datadir"] / "data").mkdir()
|
|
uit = _maak(aio)
|
|
assert uit["controller"] == []
|
|
assert uit["datamap"]["pad"] == str(aio["datadir"])
|
|
assert uit["conclusie"] == "signalen"
|
|
|
|
|
|
# ── Docker-fouten worden nooit geen_signalen ────────────────────────────────
|
|
|
|
@pytest.mark.parametrize("soort", ["ps", "inspect", "volume"])
|
|
def test_dockerfout_is_onbekend(aio, soort):
|
|
aio["docker"]["rc"][soort] = 1
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "onbekend" and uit["fout"]
|
|
|
|
|
|
@pytest.mark.parametrize("soort,uitvoer", [
|
|
("ps", "kapotte regel\n"),
|
|
("ps", _ps_regel(MASTER, "vreemd") + "\n"),
|
|
("inspect", "{geen json"),
|
|
("inspect", "{}"),
|
|
("inspect", "[]"), # container ontbreekt in het antwoord
|
|
])
|
|
def test_onleesbare_uitvoer_is_onbekend(aio, soort, uitvoer):
|
|
aio["docker"]["stdout"][soort] = uitvoer
|
|
assert _maak(aio)["conclusie"] == "onbekend"
|
|
|
|
|
|
def _inspect_met(**velden):
|
|
item = {"Name": "/" + MASTER, "Config": {"Env": []}, "Mounts": []}
|
|
item.update(velden)
|
|
return json.dumps([item])
|
|
|
|
|
|
@pytest.mark.parametrize("uitvoer", [
|
|
_inspect_met(Config="string"),
|
|
_inspect_met(Config=["lijst"]),
|
|
_inspect_met(Config=None),
|
|
_inspect_met(Config={"Env": "NEXTCLOUD_DATADIR=/x"}),
|
|
_inspect_met(Config={"Env": [{"geen": "string"}]}),
|
|
_inspect_met(Mounts={"Name": "x"}),
|
|
_inspect_met(Mounts=["string"]),
|
|
_inspect_met(Name=None),
|
|
json.dumps(["geen dict"]),
|
|
])
|
|
def test_onverwachte_vorm_van_inspect_is_onbekend(aio, uitvoer):
|
|
"""Review Codex: dit gaf een AttributeError, en dus een 500 in plaats van
|
|
een conclusie."""
|
|
aio["docker"]["stdout"]["inspect"] = uitvoer
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "onbekend" and "inspect" in uit["fout"]
|
|
|
|
|
|
@pytest.mark.parametrize("compose", [
|
|
"- een\n- lijst\n",
|
|
"services:\n - lijst\n",
|
|
"services:\n app:\n environment: NEXTCLOUD_DATADIR=/x\n",
|
|
"services:\n app:\n environment:\n - {a: b}\n",
|
|
"services: [\n",
|
|
])
|
|
def test_onverwachte_compose_is_onbekend(aio, compose):
|
|
(aio["d"] / "docker-compose.yml").write_text(compose)
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "onbekend" and "compose" in uit["fout"]
|
|
|
|
|
|
@pytest.mark.parametrize("regel", ["kapot volume!", " nextcloud_aio_x", "x", "-begin"])
|
|
def test_onleesbare_volumeregel_is_onbekend(aio, regel):
|
|
"""Een corrupte regel kan net het datavolume zijn."""
|
|
aio["docker"]["volumes"].append(regel)
|
|
assert _maak(aio)["conclusie"] == "onbekend"
|
|
|
|
|
|
def test_symlink_onder_base_dir_naar_elders_is_niet_zichtbaar(aio, monkeypatch, env):
|
|
"""In de container wijst de link naar iets anders dan op de host."""
|
|
from core import paden
|
|
basis = env["tmp"] / "base"
|
|
elders = env["tmp"] / "elders"
|
|
(elders / "ncdata").mkdir(parents=True)
|
|
(elders / "ncdata" / "bestand").write_text("x")
|
|
basis.mkdir()
|
|
(basis / "link").symlink_to(elders)
|
|
env["core"].patch({"BASE_DIR": str(basis)})
|
|
monkeypatch.setattr(paden, "_in_container", lambda: True)
|
|
aio["docker"]["env"] = [f"NEXTCLOUD_DATADIR={basis / 'link' / 'ncdata'}"]
|
|
uit = _maak(aio)
|
|
assert uit["datamap"]["zichtbaar"] is False and uit["conclusie"] == "onbekend"
|
|
# Zonder link, gewoon onder BASE_DIR: wel zichtbaar.
|
|
(basis / "echt").mkdir()
|
|
aio["docker"]["env"] = [f"NEXTCLOUD_DATADIR={basis / 'echt'}"]
|
|
assert _maak(aio)["datamap"]["zichtbaar"] is True
|
|
|
|
|
|
def test_timeout_is_onbekend(aio, monkeypatch):
|
|
"""Via de echte `docker._run`, die een timeout omzet in returncode 1."""
|
|
from core import docker
|
|
|
|
def traag(cmd, **kw):
|
|
raise subprocess.TimeoutExpired(cmd, kw.get("timeout", 1))
|
|
monkeypatch.setattr(docker, "_run", aio["echte_run"])
|
|
monkeypatch.setattr(docker.subprocess, "run", traag)
|
|
uit = _maak(aio)
|
|
assert uit["conclusie"] == "onbekend" and "timeout" in uit["fout"]
|
|
|
|
|
|
# ── Alleen lezen, en geen geheimen ──────────────────────────────────────────
|
|
|
|
def test_alleen_lezende_opdrachten(aio):
|
|
aio["docker"]["ps"] += [_ps_regel("nextcloud-aio-nextcloud", "exited")]
|
|
aio["datadir"].mkdir()
|
|
_maak(aio)
|
|
for args in aio["docker"]["opdrachten"]:
|
|
assert args[0] in ("ps", "inspect") or args[:2] == ["volume", "ls"], args
|
|
|
|
|
|
def test_andere_opdracht_wordt_geweigerd():
|
|
from core import inventaris
|
|
for args in (("run", "--rm", "x"), ("exec", "x", "sh"), ("logs", "x"),
|
|
("volume", "rm", "x"), ("compose", "up")):
|
|
with pytest.raises(ValueError):
|
|
inventaris._docker(*args)
|
|
|
|
|
|
def test_geen_omgeving_buiten_de_allowlist(aio):
|
|
uit = _maak(aio)
|
|
assert GEHEIM not in json.dumps(uit)
|
|
assert set(uit["controller"][0]["env"]) <= {"NEXTCLOUD_DATADIR", "NEXTCLOUD_MOUNT"}
|
|
|
|
|
|
# ── API ─────────────────────────────────────────────────────────────────────
|
|
|
|
def test_api_geeft_de_inventaris(client, aio):
|
|
login(client)
|
|
r = client.get("/api/stacks/nextcloud-aio/inventaris")
|
|
assert r.status_code == 200
|
|
d = r.get_json()
|
|
assert d["ok"] is True and d["conclusie"] == "geen_signalen"
|
|
assert GEHEIM not in r.get_data(as_text=True)
|
|
|
|
|
|
def test_api_alleen_voor_extern_beheerde_apps(client, env):
|
|
login(client)
|
|
d = env["lib"] / "web"
|
|
d.mkdir()
|
|
(d / "docker-compose.yml").write_text("services:\n web:\n image: nginx\n")
|
|
r = client.get("/api/stacks/web/inventaris")
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_api_vraagt_een_sessie(anon_client, aio):
|
|
r = anon_client.get("/api/stacks/nextcloud-aio/inventaris")
|
|
assert r.status_code in (401, 403)
|
|
|
|
|
|
def test_volume_prefix_in_sjabloonmetadata():
|
|
from core import boilerplates
|
|
lc = boilerplates.metadata(
|
|
Path(__file__).resolve().parent.parent / "apps" / "nextcloud-aio")["lifecycle"]
|
|
assert lc["volume_prefix"] == "nextcloud_aio_"
|
|
assert boilerplates._lifecycleconfig(
|
|
{"mode": "external", "volume_prefix": "x"}).get("volume_prefix") is None
|