Files
stackpilot/backend/tests/test_walkthrough_fixes.py
T
menzeljandClaude Opus 5.5 d6cebb3371
CI / check (push) Successful in 4m9s
CI / build-and-push (push) Failing after 16m10s
Fix what a review of the whole app found, end to end (0.67.1)
A pass over every feature against a real Docker daemon, then clicking
through the UI as admin and read-only user, on a desktop and a phone.

Security. /ws/deploy ran compose up for the read-only role; it is
admin-only now. A container's inspect view, a stack's compose file
(POSTGRES_PASSWORD: hunter2) and the viewer's .env tab no longer show
credentials to that role. Git branch and URL values that git would read
as options (-x, ext::) are refused. SFTP host keys are checked: pinned
on first use or entered by hand (key line or SHA256 fingerprint), a
different key is refused before the password is sent. A backup name
from the API can no longer reach outside the destination folder. The
file browser refuses to move or rename a browse root, as delete did.

Docker lifecycle. Closing the deploy console released the stack lock
while compose was still running and logged rc=None as success; deploys
and updates are now background runs that hold the lock to the end,
report the real exit code and can be re-attached to. Every closed log
view left a `compose logs -f` behind. A dozen open tabs exhausted the
default thread pool and hung the API; event and exec streams have their
own threads. Auto-update and the update endpoint ignored failed pulls.
A deleted git stack was recreated and started by the next webhook or
poll; deleting a stack now drops its git source, auto-update policy and
schedule entries. Deleting no longer walks into mounted shares, is
refused while a backup runs, and removes the containers even when the
compose file is broken. Stack folders compose has to normalise
(Nextcloud -> nextcloud) work everywhere and group correctly on the
volumes, networks and images pages.

Detection. Host port conflicts read the container's own network
namespace; /proc/1/net is read instead. Images referenced as docker.io/…
were never update-checked; local builds no longer report "needs
credentials". NVIDIA GPUs are read from the driver's procfs. The backup
dialog's "path mismatch" warning came from mountinfo's filesystem-relative
root and fired for any stacks folder on its own disk, ZFS dataset or
btrfs subvolume; the host path now comes from Docker's mounts.

Backups and destinations. The download ignored the folder/volume
selection (axios sent binds[]). Restoring under a new name kept the old
one. A report header past nginx's buffer turned a good backup into a
502. SFTP/S3 errors were bare 500s and "Test" did not try to write.
A destination used by a schedule could be deleted. Backup file names
are in the schedule's or the browser's time zone instead of UTC.

Editor and converter. `docker run` conversion produced invalid compose
for --network host, named volumes and -dit; --gpus and --mount were
dropped; a multi-line command pasted into the one-line field became
`image: ' '` (now a textarea, and flattened continuations are read).
Validation failed for env_file stacks. The editor overwrote unsaved
edits on every container event, lost them on a stray click (now asks),
threw when closing the diff, and its helper panel listed the starter
service "app" for every existing stack. Monaco is bundled instead of
loaded from jsdelivr, so the editor works offline (and monaco-editor
0.57 drops the dompurify advisory; axios and react-router updated).

UI. All times were two hours off: offset-less UTC from SQLite was read
as local time, and "next run" always said "just now". A token refresh
mid-session duplicated deploys, reset terminals and doubled logs. A
backend restart (update, self-update) signed everyone out on reload; the
page now waits for it. The editor was unusable on a phone, tables and
tabs overflowed, dialog text was black on the dark theme, and the file
browser opened on an error with the default roots. Read-only users get
"Admin only" pages instead of 403s; in-use networks offer no delete;
"Scan all" covers StackPilot's own stacks only and runs in the
background instead of timing out at 600s.

Deployment. nginx passes the port in Host (the OIDC redirect URI lost
:5009) and keeps an outer proxy's X-Forwarded-Proto. The frontend waits
for a healthy backend (healthcheck probes every 2s while starting). The
frontend image builds with npm ci. Secrets are 0644 in a 0700 folder so
non-root containers can read them; existing files are fixed at startup.
The test suite no longer starts real containers on a host with Docker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fx7gcuy8d6nPd95g43rGHu
2026-10-15 13:51:26 +02:00

243 lines
9.0 KiB
Python

"""Found clicking through the UI for 0.67.1.
* The file browser opens at ``/``, which the default roots (/mnt, /opt, …)
do not include: the page opened on "outside the allowed browse roots" and
offered no way into any root. Paths above the roots now list the way to them.
* A root could not be deleted, but it could be moved — the whole tree with it.
* Locally built images (compose ``build:``) were checked against Docker Hub,
which answers 401 for a repository that does not exist: "needs credentials".
* Volumes, networks and images of a stack whose folder name compose had to
normalise ("Nextcloud" -> project "nextcloud") were grouped as leftovers of a
removed stack.
"""
from __future__ import annotations
import asyncio
import pytest
@pytest.fixture
def roots(tmp_path, monkeypatch):
from config import settings
(tmp_path / "srv" / "data").mkdir(parents=True)
(tmp_path / "opt").mkdir()
monkeypatch.setattr(settings, "HOST_ROOT_PREFIX", str(tmp_path), raising=False)
monkeypatch.setattr(
settings, "ALLOWED_BROWSE_ROOTS", ["/opt", "/srv/data", "/media"], raising=False
)
return tmp_path
def test_the_top_of_the_tree_lists_the_way_to_the_roots(roots):
from services import device_service
top = device_service.browse("/")
assert top["virtual"] is True
# /media does not exist on this host, so it is not offered.
assert [e["name"] for e in top["entries"]] == ["opt", "srv"]
assert [e["name"] for e in device_service.browse("/srv")["entries"]] == ["data"]
inside = device_service.browse("/srv/data")
assert "virtual" not in inside and inside["parent"] == "/srv"
def test_a_path_leading_nowhere_is_still_refused(roots):
from services import device_service
with pytest.raises(device_service.BrowseError):
device_service.browse("/etc")
def test_a_root_cannot_be_moved_or_renamed(roots):
from services import file_service
with pytest.raises(file_service.BrowseError, match="browse root"):
file_service.move("/srv/data", "/opt")
with pytest.raises(file_service.BrowseError, match="browse root"):
file_service.rename("/opt", "opt2")
assert (roots / "srv" / "data").is_dir()
def test_moving_inside_a_root_still_works(roots):
from services import file_service
(roots / "opt" / "a.txt").write_text("x")
file_service.move("/opt/a.txt", "/srv/data")
assert (roots / "srv" / "data" / "a.txt").read_text() == "x"
@pytest.mark.parametrize(
"attrs, local",
[
({"Identity": {"Build": [{"Ref": "x"}]}, "RepoDigests": ["app@sha256:1"]}, True),
({"Identity": {"Pull": [{"Repository": "docker.io/library/nginx"}]}}, False),
({"RepoDigests": []}, True), # older daemon, never pulled
({"RepoDigests": ["nginx@sha256:1"]}, False),
({"Identity": {}, "RepoDigests": ["nginx@sha256:1"]}, False),
(None, False),
],
)
def test_where_an_image_came_from(attrs, local):
from services import update_service
assert update_service._built_locally(attrs) is local
def test_a_local_build_is_not_asked_about_at_the_registry(monkeypatch):
from services import update_service
monkeypatch.setattr(
update_service, "_local_attrs", lambda image: {"Identity": {"Build": [{"Ref": "x"}]}}
)
async def no_registry(image):
raise AssertionError("registry contacted for a local build")
monkeypatch.setattr(update_service, "remote_digest", no_registry)
status = asyncio.run(update_service.check_image("myapp-web:latest"))
assert status.local is True and status.error is None and not status.update_available
def test_resources_of_a_normalised_stack_are_grouped_under_it(as_admin, db, monkeypatch):
from sqlmodel import Session
from database import engine
from models.stack import Stack
from services import network_service, volume_service
with Session(engine) as session:
if not session.get(Stack, "Nextcloud"):
session.add(Stack(id="Nextcloud", name="Nextcloud"))
session.commit()
monkeypatch.setattr(
volume_service, "list_volumes", lambda: [{"name": "nextcloud_db", "stack": "nextcloud"}]
)
monkeypatch.setattr(
network_service,
"list_networks",
lambda: [{"name": "x", "stack": "elsewhere"}, {"name": "bridge", "stack": None}],
)
assert as_admin.get("/api/volumes").json()[0]["stack"] == "Nextcloud"
assert [n["stack"] for n in as_admin.get("/api/networks").json()] == ["elsewhere", None]
class _Container:
def __init__(self, mounts):
self.attrs = {"Mounts": mounts}
@pytest.mark.parametrize(
"mounts, host",
[
# The usual: same path on both sides — no warning.
([{"Type": "bind", "Source": "/opt/stacks", "Destination": "/opt/stacks"}], None),
# A real mismatch.
(
[{"Type": "bind", "Source": "/srv/compose", "Destination": "/opt/stacks"}],
"/srv/compose",
),
# The folder above STACKS_DIR is what is mounted.
([{"Type": "bind", "Source": "/srv", "Destination": "/opt"}], "/srv/stacks"),
# Not mounted at all.
([{"Type": "volume", "Source": "/var/lib/docker/volumes/x", "Destination": "/data"}], None),
],
)
def test_the_host_stacks_path_comes_from_docker_not_mountinfo(monkeypatch, mounts, host):
"""mountinfo's root field is relative to the filesystem: a stacks folder on
its own disk or btrfs subvolume showed a mismatch that was not there."""
from config import settings
from services import self_update_service, stack_assets_service
monkeypatch.setattr(settings, "STACKS_DIR", "/opt/stacks")
monkeypatch.setattr(stack_assets_service, "_HOST_STACKS_DIR", None)
monkeypatch.setattr(self_update_service, "_own_container", lambda: _Container(mounts))
mismatch = stack_assets_service.stacks_path_mismatch()
assert (mismatch["host"] if mismatch else None) == host
def test_backup_names_carry_the_time_on_the_users_clock(monkeypatch):
"""Named in UTC, a backup taken at 12:21 in Berlin read as …-102154."""
from datetime import datetime, timezone
from services import backup_service, compose_service
monkeypatch.setattr(
compose_service, "now", lambda: datetime(2026, 10, 15, 10, 21, 54, tzinfo=timezone.utc)
)
berlin = backup_service.backup_filename("app", True, tz="Europe/Berlin")
assert berlin == "backup-app-full-20261015-122154.tar.gz"
# No zone, or one that does not exist: UTC, as before.
assert backup_service.backup_filename("app", False).endswith("-20261015-102154.tar.gz")
assert backup_service.backup_filename("app", False, tz="Mars/Olympus").endswith("-102154.tar.gz")
# Retention still recognises the files.
assert backup_service.backup_name_pattern("app").match(berlin)
COMPOSE_WITH_SECRETS = """services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: hunter2
POSTGRES_USER: app
API_KEY: ${API_KEY:-changeme}
DB_PASS: ${DB_PASS}
KEYCLOAK_ADMIN: admin
app:
environment:
- "TS_AUTHKEY=tskey-123"
- TZ=Europe/Berlin
"""
def test_inline_credentials_are_masked_and_the_rest_left_alone():
from services.compose_service import MASK, mask_secrets
masked = mask_secrets(COMPOSE_WITH_SECRETS)
assert "hunter2" not in masked and "tskey-123" not in masked and "changeme" not in masked
assert f"POSTGRES_PASSWORD: {MASK}" in masked
assert f'- "TS_AUTHKEY={MASK}"' in masked
# A bare variable is no secret; names that merely contain "key" are not either.
for kept in ("DB_PASS: ${DB_PASS}", "KEYCLOAK_ADMIN: admin", "POSTGRES_USER: app", "TZ=Europe/Berlin"):
assert kept in masked
assert masked.count("\n") == COMPOSE_WITH_SECRETS.count("\n")
def test_the_read_only_role_gets_the_masked_compose_file(as_admin, as_user, db):
from services import compose_service
assert as_admin.post("/api/stacks", json={"name": "masked", "yaml": COMPOSE_WITH_SECRETS}).status_code == 201
try:
assert "hunter2" in as_admin.get("/api/stacks/masked").json()["yaml"]
assert "hunter2" not in as_user.get("/api/stacks/masked").json()["yaml"]
finally:
compose_service.delete_stack_files("masked")
def test_scan_all_covers_only_stackpilots_own_stacks(as_admin, db, monkeypatch):
from sqlmodel import Session
from database import engine
from models.stack import Stack
from services import image_service, scan_service
with Session(engine) as session:
if not session.get(Stack, "Mine"):
session.add(Stack(id="Mine", name="Mine"))
session.commit()
monkeypatch.setattr(
image_service,
"list_images",
lambda: [
{"tag": "mine:1", "stacks": ["mine"]},
{"tag": "theirs:1", "stacks": ["some-other-project"]},
{"tag": "loose:1", "stacks": []},
],
)
swept = []
monkeypatch.setattr(
scan_service, "start_sweep", lambda images, on_done=None: swept.append(images) or {"active": True}
)
assert as_admin.post("/api/images/scan-all?background=true").status_code == 202
assert swept == [["mine:1"]]