/** * The ports containers publish, kept per server from what Dockhand says and used to name them on the Ports pages and in the new-port alert: which published port belongs to which server, * what a refresh replaces and what it leaves alone, how long a name is trusted, and what the two ports routes say. * * Run with: npm test --workspace server */ import { test, describe, before, after } from "node:test"; import assert from "node:assert/strict"; import { createServer as createHttpServer, type Server } from "node:http"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import type { AddressInfo } from "node:net"; const dir = mkdtempSync(join(tmpdir(), "hm-containerports-")); process.env.DATABASE_PATH = join(dir, "test.sqlite"); process.env.SESSION_DIR = join(dir, "sessions"); process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32); const express = (await import("express")).default; const { db } = await import("../src/db/client.js"); const { users, servers, serverPorts, serverContainerPorts, integrations } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const cp = await import("../src/services/containerPorts.js"); const { requireAuth } = await import("../src/auth/middleware.js"); const { serverPortsRouter } = await import("../src/routes/serverPorts.js"); const { portsRouter } = await import("../src/routes/ports.js"); const { newPortMessage } = await import("../src/services/newPorts.js"); const { collectSnapshot } = await import("../src/services/healthMonitor.js"); const { getSettings, updateSettings } = await import("../src/services/settingsStore.js"); const { eq } = await import("drizzle-orm"); await runMigrations(); const env = (id: number, name: string) => ({ id, name }); const box = (environmentId: number, name: string, state: string, ports: { publicPort: number | null; protocol: string }[], image = `img/${name}`) => ({ name, image, state, environmentId, ports }); const servs = [ { id: 1, name: "docker1", hostname: "docker1.lan", ips: ["192.168.1.10"] }, { id: 2, name: "nas", hostname: null, ips: [] }, ]; describe("planContainerPorts", () => { test("a running container's published ports go to the server its Docker host is", () => { const { rows } = cp.planContainerPorts( { environments: [env(1, "docker1"), env(2, "nas.lan")], unreachable: [], containers: [ box(1, "jellyfin", "running", [{ publicPort: 8096, protocol: "tcp" }, { publicPort: 1900, protocol: "udp" }, { publicPort: null, protocol: "tcp" }]), box(2, "/pihole", "running", [{ publicPort: 53, protocol: "udp" }, { publicPort: 53, protocol: "tcp" }]), ], }, servs, ); assert.deepEqual(rows.get(1)?.map((r) => [r.port, r.protocol, r.container]), [[1900, "udp", "jellyfin"], [8096, "tcp", "jellyfin"]]); assert.deepEqual(rows.get(2)?.map((r) => [r.port, r.protocol, r.container]), [[53, "tcp", "pihole"], [53, "udp", "pihole"]], "the leading slash of a Docker name is dropped; TCP and UDP are two ports"); }); test("a container that isn't running, a port with no host side and a protocol that isn't TCP or UDP name nothing", () => { const { rows } = cp.planContainerPorts( { environments: [env(1, "docker1")], unreachable: [], containers: [box(1, "stopped", "exited", [{ publicPort: 81, protocol: "tcp" }]), box(1, "inner", "running", [{ publicPort: null, protocol: "tcp" }]), box(1, "odd", "running", [{ publicPort: 82, protocol: "sctp" }])], }, servs, ); assert.deepEqual(rows.get(1), [], "the host was read, and it publishes nothing: so old names are cleared"); }); test("a host Dockhand couldn't read, and one that is no server of ours, are left out altogether", () => { const { rows } = cp.planContainerPorts( { environments: [env(1, "docker1"), env(2, "nas"), env(3, "ghost")], unreachable: [{ environmentId: 2 }], containers: [box(1, "a", "running", [{ publicPort: 80, protocol: "tcp" }]), box(2, "b", "running", [{ publicPort: 81, protocol: "tcp" }]), box(3, "c", "running", [{ publicPort: 82, protocol: "tcp" }])], }, servs, ); assert.deepEqual([...rows.keys()], [1], "nas wasn't read, so what is kept for it stays; ghost is nobody"); }); test("the same host port twice is one name, and if two containers claim it the first by name has it", () => { const { rows } = cp.planContainerPorts( { environments: [env(1, "docker1")], unreachable: [], containers: [box(1, "zeta", "running", [{ publicPort: 8080, protocol: "tcp" }, { publicPort: 8080, protocol: "tcp" }]), box(1, "alpha", "running", [{ publicPort: 8080, protocol: "tcp" }])], }, servs, ); assert.deepEqual(rows.get(1)?.map((r) => r.container), ["alpha"]); }); test("two Docker hosts on one server pool their ports", () => { const { rows } = cp.planContainerPorts( { environments: [env(1, "docker1"), env(5, "docker1.lan")], unreachable: [], containers: [box(1, "a", "running", [{ publicPort: 80, protocol: "tcp" }]), box(5, "b", "running", [{ publicPort: 81, protocol: "tcp" }])] }, servs, ); assert.deepEqual(rows.get(1)?.map((r) => r.container), ["a", "b"]); }); }); describe("kept names and the routes", () => { let app: Server; let base = ""; const ids: Record = {}; const json = async (r: Response) => (await r.json()) as T; const read = (rest: Partial[0]> = {}) => ({ environments: [env(1, "web"), env(2, "db")], unreachable: [], containers: [box(1, "jellyfin", "running", [{ publicPort: 8096, protocol: "tcp" }]), box(2, "postgres", "running", [{ publicPort: 5432, protocol: "tcp" }])], ...rest, }); const namesFor = async (serverId: number) => [...((await cp.containerPortNames(serverId)).get(serverId) ?? new Map()).entries()].map(([k, v]) => `${k}=${v.container}`); before(async () => { await db.insert(users).values({ oidcSub: "viewer", name: "Vic", role: "viewer" }); const mk = async (name: string, ports: unknown[]) => (await db.insert(servers).values({ name, apiTokenHash: name, apiTokenPrefix: name, listeningPorts: JSON.stringify(ports) }).returning({ id: servers.id }))[0].id; const agent = (port: number, process: string) => ({ protocol: "tcp", port, address: "0.0.0.0", process }); ids.web = await mk("web", [agent(22, "sshd"), agent(8096, "docker-proxy"), agent(9999, "x")]); ids.db = await mk("db", [agent(5432, "docker-proxy")]); const a = express(); a.use(express.json()); a.use((req, _res, next) => { (req as unknown as { session: unknown }).session = { user: { sub: "viewer" } }; next(); }); a.use("/api/servers", requireAuth); a.use("/api/servers/:id/ports", serverPortsRouter); a.use("/api/ports", portsRouter); app = await new Promise((resolve) => { const s = a.listen(0, "127.0.0.1", () => resolve(s)); }); base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api`; }); after(() => { app.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); test("a refresh keeps each server's published ports under its own name", async () => { await cp.refreshContainerPorts(read()); assert.deepEqual(await namesFor(ids.web), ["tcp:8096=jellyfin"]); assert.deepEqual(await namesFor(ids.db), ["tcp:5432=postgres"]); }); test("the next refresh replaces them; a host with nothing published is cleared", async () => { await cp.refreshContainerPorts(read({ containers: [box(1, "emby", "running", [{ publicPort: 8920, protocol: "tcp" }]), box(2, "postgres", "exited", [{ publicPort: 5432, protocol: "tcp" }])] })); assert.deepEqual(await namesFor(ids.web), ["tcp:8920=emby"], "jellyfin is gone"); assert.deepEqual(await namesFor(ids.db), [], "postgres stopped"); await cp.refreshContainerPorts(read()); }); test("a host that could not be read keeps what it had; a refresh that covers nobody changes nothing", async () => { await cp.refreshContainerPorts(read({ unreachable: [{ environmentId: 2 }], containers: [box(1, "emby", "running", [{ publicPort: 8920, protocol: "tcp" }])] })); assert.deepEqual(await namesFor(ids.db), ["tcp:5432=postgres"]); assert.deepEqual(await namesFor(ids.web), ["tcp:8920=emby"]); await cp.refreshContainerPorts({ environments: [env(9, "elsewhere")], unreachable: [], containers: [box(9, "x", "running", [{ publicPort: 1, protocol: "tcp" }])] }); assert.deepEqual(await namesFor(ids.web), ["tcp:8920=emby"]); await cp.refreshContainerPorts(read()); }); test("a name not refreshed for a day is no longer used, and is cleared on the next pass", async () => { const later = Date.now() + cp.CONTAINER_PORTS_STALE_MS + 60_000; assert.deepEqual([...(await cp.containerPortNames(undefined, later)).keys()], [], "too old to trust"); assert.equal((await db.select().from(serverContainerPorts)).length, 2); await cp.purgeStaleContainerPorts(later); assert.equal((await db.select().from(serverContainerPorts)).length, 0); await cp.refreshContainerPorts(read()); assert.equal((await db.select().from(serverContainerPorts)).length, 2); }); test("a server's ports say which container publishes a port, and leave a note somebody wrote alone", async () => { await db.insert(serverPorts).values({ serverId: ids.web, port: 8096, protocol: "tcp", label: "Media", updatedAt: new Date().toISOString() }); const body = await json<{ ports: { port: number; label: string | null; container: { container: string; image: string } | null; service: string | null }[] }>(await fetch(`${base}/servers/${ids.web}/ports`)); const by = new Map(body.ports.map((p) => [p.port, p])); assert.deepEqual(by.get(8096)?.container, { container: "jellyfin", image: "img/jellyfin" }); assert.equal(by.get(8096)?.label, "Media", "the note is still the note"); assert.equal(by.get(22)?.container, null); assert.equal(by.get(9999)?.container, null); const [row] = await db.select().from(serverPorts).where(eq(serverPorts.serverId, ids.web)); assert.equal(row.label, "Media", "nothing was written onto the port notes"); }); test("the ports of every server say it too, only on the right server", async () => { const body = await json<{ ports: { serverName: string; port: number; container: { container: string } | null }[] }>(await fetch(`${base}/ports/agent`)); assert.deepEqual(body.ports.filter((p) => p.container).map((p) => `${p.serverName}:${p.port}=${p.container!.container}`).sort(), ["db:5432=postgres", "web:8096=jellyfin"]); }); test("a container's port is still news: it is named in the alert, not counted as expected", async () => { assert.equal(cp.containerLabel({ container: "jellyfin", image: "" }), "jellyfin (container)"); assert.equal(cp.containerLabel(null), null); assert.equal( newPortMessage("web", { protocol: "tcp", port: 8096, process: "docker-proxy", service: cp.containerLabel({ container: "jellyfin", image: "" }) }), "web: a new port is listening — 8096/tcp jellyfin (container) (docker-proxy). If it should be there, note it on the server's Ports card.", ); const expectedRows = await db.select().from(serverPorts).where(eq(serverPorts.serverId, ids.db)); assert.equal(expectedRows.length, 0, "no port note was made for the container's port"); }); test("the health pass keeps them from Dockhand and names the port in its new-port alert; with the container alerts off it still does; a Dockhand it can't read changes nothing", async () => { let published: { name: string; port: number }[] = [{ name: "jellyfin", port: 8096 }]; let broken = false; const dockhand = createHttpServer((req, res) => { const url = new URL(req.url ?? "", "http://x"); res.setHeader("content-type", "application/json"); if (broken) { res.statusCode = 500; return void res.end("{}"); } if (url.pathname === "/api/environments") return void res.end(JSON.stringify([{ id: 1, name: "pass-host", connectionType: "hawser" }])); if (url.pathname === "/api/containers/pending-updates") return void res.end(JSON.stringify({ pendingUpdates: [] })); if (url.pathname === "/api/containers") return void res.end(JSON.stringify(published.map((c) => ({ id: c.name, name: c.name, image: "img", state: "running", status: "Up 1 hour", ports: [{ IP: "0.0.0.0", PrivatePort: 80, PublicPort: c.port, Type: "tcp" }] })))); res.statusCode = 404; res.end("{}"); }); await new Promise((r) => dockhand.listen(0, "127.0.0.1", r)); const [integration] = await db.insert(integrations).values({ type: "dockhand", name: "Dockhand", baseUrl: "", config: JSON.stringify({ url: `http://127.0.0.1:${(dockhand.address() as AddressInfo).port}`, token: "t" }) }).returning({ id: integrations.id }); const listening = [8096, 8920].map((port) => ({ protocol: "tcp", port, address: "0.0.0.0", process: "docker-proxy" })); await db.insert(servers).values({ name: "pass-host", apiTokenHash: "p", apiTokenPrefix: "p", listeningPorts: JSON.stringify(listening), portBaseline: JSON.stringify(["tcp:22"]) }); const newPorts = async () => (await collectSnapshot()).snapshot.servers.find((s) => s.name === "pass-host")!.newPorts.map((p) => `${p.port} ${p.service ?? "-"}`); try { assert.deepEqual(await newPorts(), ["8096 jellyfin (container)", "8920 -"], "the port the container publishes is named; the other one has no name"); const checks = (await getSettings()).healthChecks; await updateSettings({ healthChecks: { ...checks, containerAlerts: false } }); published = [{ name: "emby", port: 8920 }]; const pass = await collectSnapshot(); assert.equal(pass.snapshot.docker, undefined, "no container alerts"); assert.deepEqual(await newPorts(), ["8096 -", "8920 emby (container)"], "but what the containers publish is still taken in"); await updateSettings({ healthChecks: { ...checks, containerAlerts: true } }); broken = true; const held = await collectSnapshot(); assert.ok(held.held.has(`dockhand:${integration.id}`), "an unreadable Dockhand holds its alerts"); assert.deepEqual(await newPorts(), ["8096 -", "8920 emby (container)"], "and what was kept is kept"); } finally { dockhand.close(); await db.delete(integrations).where(eq(integrations.id, integration.id)); await db.delete(servers).where(eq(servers.name, "pass-host")); } }); test("taking a server away takes its names with it", async () => { await db.delete(servers).where(eq(servers.id, ids.db)); assert.deepEqual(await namesFor(ids.db), []); assert.deepEqual(await namesFor(ids.web), ["tcp:8096=jellyfin"]); }); });