|
| 1 | +import { readFile, writeFile } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
| 6 | +import { captureEnv } from "../test-utils/env.js"; |
| 7 | +import { startGatewayTailscaleExposure } from "./server-tailscale.js"; |
| 8 | + |
| 9 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 10 | + |
| 11 | +describe("managed Tailscale upgrade", () => { |
| 12 | + const legacyRoute = (funnel = false, proxyPort = 18789) => { |
| 13 | + const host = "fixture.tailnet.ts.net:443"; |
| 14 | + return { |
| 15 | + TCP: { "443": { HTTPS: true } }, |
| 16 | + Web: { [host]: { Handlers: { "/": { Proxy: `http://127.0.0.1:${proxyPort}/` } } } }, |
| 17 | + ...(funnel ? { AllowFunnel: { [host]: true } } : {}), |
| 18 | + }; |
| 19 | + }; |
| 20 | + |
| 21 | + const installFixture = async (config: object, mode: "serve" | "funnel") => { |
| 22 | + const fixture = fileURLToPath( |
| 23 | + new URL("../../test/fixtures/tailscale-legacy-route-fixture.mjs", import.meta.url), |
| 24 | + ); |
| 25 | + const marker = path.join(tempDirs.make("openclaw-tailscale-upgrade-"), "state"); |
| 26 | + await writeFile(marker, JSON.stringify(config)); |
| 27 | + process.env.OPENCLAW_TEST_TAILSCALE_BINARY = fixture; |
| 28 | + process.env.OPENCLAW_TEST_TAILSCALE_FIXTURE_MARKER = marker; |
| 29 | + process.env.OPENCLAW_TEST_TAILSCALE_FIXTURE_MODE = mode; |
| 30 | + process.env.VITEST ??= "true"; |
| 31 | + return marker; |
| 32 | + }; |
| 33 | + |
| 34 | + it.each(["serve", "funnel"] as const)( |
| 35 | + "does not infer ownership from a matching persistent %s route", |
| 36 | + async (mode) => { |
| 37 | + const env = captureEnv([ |
| 38 | + "OPENCLAW_TEST_TAILSCALE_BINARY", |
| 39 | + "OPENCLAW_TEST_TAILSCALE_FIXTURE_MARKER", |
| 40 | + "OPENCLAW_TEST_TAILSCALE_FIXTURE_MODE", |
| 41 | + "VITEST", |
| 42 | + ]); |
| 43 | + const marker = await installFixture(legacyRoute(mode === "funnel"), mode); |
| 44 | + const before = await readFile(marker, "utf8"); |
| 45 | + |
| 46 | + try { |
| 47 | + await expect( |
| 48 | + startGatewayTailscaleExposure({ |
| 49 | + tailscaleMode: mode, |
| 50 | + port: 18789, |
| 51 | + backend: { host: "127.0.0.1", port: 19000 }, |
| 52 | + logTailscale: { info: () => undefined, warn: () => undefined }, |
| 53 | + }), |
| 54 | + ).rejects.toThrow("ownership OpenClaw cannot prove; it was not modified"); |
| 55 | + expect(await readFile(marker, "utf8")).toBe(before); |
| 56 | + } finally { |
| 57 | + env.restore(); |
| 58 | + } |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + it("does not mutate an independent Tailscale Service", async () => { |
| 63 | + const env = captureEnv([ |
| 64 | + "OPENCLAW_TEST_TAILSCALE_BINARY", |
| 65 | + "OPENCLAW_TEST_TAILSCALE_FIXTURE_MARKER", |
| 66 | + "OPENCLAW_TEST_TAILSCALE_FIXTURE_MODE", |
| 67 | + "VITEST", |
| 68 | + ]); |
| 69 | + const marker = await installFixture({ Services: { "svc:other": legacyRoute() } }, "serve"); |
| 70 | + const before = await readFile(marker, "utf8"); |
| 71 | + |
| 72 | + try { |
| 73 | + const cleanup = await startGatewayTailscaleExposure({ |
| 74 | + tailscaleMode: "serve", |
| 75 | + port: 18789, |
| 76 | + backend: { host: "127.0.0.1", port: 19000 }, |
| 77 | + logTailscale: { info: () => undefined, warn: () => undefined }, |
| 78 | + }); |
| 79 | + |
| 80 | + expect(await readFile(marker, "utf8")).toBe(before); |
| 81 | + await cleanup?.(); |
| 82 | + } finally { |
| 83 | + env.restore(); |
| 84 | + } |
| 85 | + }); |
| 86 | +}); |
0 commit comments