Public: anyone can read this; the crew can change it.
test/snapshot-hosted-inside.test.ts fits its 2 s budget by building its fixture once #15152
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/15147-test-snapshot-hosted-inside-test-ts-fits → main 1 commit fork
1 file
changed +93
−61
test/snapshot-hosted-inside.test.ts +93 −61
| @@ -124,6 +124,85 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 124 | 124 | if (!c.ok) throw new Error(JSON.stringify(c.error)); |
| 125 | 125 | }; |
| 126 | 126 | |
| 127 | /** | |
| 128 | * Every hosted forge a test reads, built once before any test runs: seeding (a user, commits, | |
| 129 | * links laid in its directory) is the fixture's cost, not the test's. Each test reads only its | |
| 130 | * own forge, by name; what a test changes is its own forge's and no other test reads it. | |
| 131 | */ | |
| 132 | type Hosted = Awaited<ReturnType<typeof hostForge>>; | |
| 133 | const forges = new Map<string, Hosted>(); | |
| 134 | const forge = (name: string): Hosted => { | |
| 135 | const f = forges.get(name); | |
| 136 | if (f === undefined) throw new Error(`no hosted forge ${name} was built`); | |
| 137 | return f; | |
| 138 | }; | |
| 139 | /** A forge built and closed, its repositories committed, then laid out by `then`. */ | |
| 140 | const closed = | |
| 141 | (repos: readonly string[], then: (f: Hosted) => void = () => {}, before: (f: Hosted) => void = () => {}) => | |
| 142 | async (name: string): Promise<Hosted> => { | |
| 143 | const f = await hostForge(name, `body-${name}`); | |
| 144 | for (const repo of repos) await commit(f, repo); | |
| 145 | before(f); | |
| 146 | f.open.close(); | |
| 147 | then(f); | |
| 148 | return f; | |
| 149 | }; | |
| 150 | const BUILD: Record<string, (name: string) => Promise<Hosted>> = { | |
| 151 | links: closed([], (f) => { | |
| 152 | const dir = f.source.dir; | |
| 153 | const outside = join(root, "outside.txt"); | |
| 154 | symlinkSync(outside, join(dir, "ssh", "linked")); | |
| 155 | symlinkSync(outside, join(dir, "apps", "a1", "production", "data", "linked.txt")); | |
| 156 | symlinkSync(join(root, "outside-dir"), join(dir, "apps", "a1", "production", "data", "linked-dir")); | |
| 157 | // A whole directory of its own that is a link, and a link where another of its files is read. | |
| 158 | mkdirSync(join(dir, "apps", "a2", "production"), { recursive: true }); | |
| 159 | symlinkSync(join(root, "outside-dir"), join(dir, "apps", "a2", "production", "data")); | |
| 160 | rmSync(join(dir, "secret.key")); | |
| 161 | symlinkSync(outside, join(dir, "secret.key")); | |
| 162 | }), | |
| 163 | swapped: closed([], (f) => { | |
| 164 | for (const name of ["forge.db", "forge.db-wal", "forge.db-shm"]) rmSync(join(f.source.dir, name), { force: true }); | |
| 165 | symlinkSync(join(root, "outside.db"), join(f.source.dir, "forge.db")); | |
| 166 | }), | |
| 167 | stacks: closed(["fine", "borrow", "trap", "linked"], (f) => { | |
| 168 | const repos = join(f.source.dir, "repos"); | |
| 169 | // A link inside a repository: git would follow it wherever it names. | |
| 170 | symlinkSync(join(root, "outside-dir"), join(repoDir(repos, "stacks", "linked"), "objects", "elsewhere")); | |
| 171 | // Configuration that names a command, and objects borrowed from a directory outside the forge | |
| 172 | // (in the quoted form git also reads). | |
| 173 | const trap = repoDir(repos, "stacks", "trap"); | |
| 174 | writeFileSync(join(trap, "config"), `${readFileSync(join(trap, "config"), "utf8")}\n[core]\n\tfsmonitor = /bin/true\n`); | |
| 175 | writeFileSync(join(repoDir(repos, "stacks", "borrow"), "objects", "info", "alternates"), `"${join(root, "outside-dir")}"\n`); | |
| 176 | }), | |
| 177 | ledgers: closed( | |
| 178 | ["fine", "gone", "odd"], | |
| 179 | (f) => { | |
| 180 | const repos = join(f.source.dir, "repos"); | |
| 181 | for (const name of ["gone", "odd"]) rmSync(repoDir(repos, "ledgers", name), { recursive: true }); | |
| 182 | }, | |
| 183 | // Its database names one its disk no longer holds, and one by a name no path may carry. | |
| 184 | (f) => f.open.db.run("UPDATE repo SET name = 'no such/../name' WHERE name = 'odd'"), | |
| 185 | ), | |
| 186 | shifty: closed(["fine", "swap"]), | |
| 187 | parent: closed(["moved"]), | |
| 188 | many: closed([], (f) => { | |
| 189 | for (const n of [1, 2, 3]) writeFileSync(join(f.source.dir, "ssh", `extra-${n}`), `${n}`); | |
| 190 | }), | |
| 191 | views: closed(["work"], (f) => { | |
| 192 | // `repo` is now a view the forge wrote: whatever it selects, nothing of the snapshot runs it. | |
| 193 | const db = new Database(join(f.source.dir, "forge.db")); | |
| 194 | db.run("PRAGMA foreign_keys = OFF"); | |
| 195 | db.run("ALTER TABLE repo RENAME TO repo_kept"); | |
| 196 | db.run("CREATE VIEW repo AS SELECT * FROM repo_kept"); | |
| 197 | db.close(); | |
| 198 | }), | |
| 199 | // Open: the test kills it, and writes to it while it is copied. | |
| 200 | asleep: (name) => hostForge(name, `body-${name}`), | |
| 201 | awake: (name) => hostForge(name, `body-${name}`), | |
| 202 | follower: closed(["forge"]), | |
| 203 | late: closed([]), | |
| 204 | }; | |
| 205 | ||
| 127 | 206 | beforeAll(async () => { |
| 128 | 207 | root = mkdtempSync(join(tmpdir(), "forge-hosted-inside-")); |
| 129 | 208 | host = createApp({ dataDir: join(root, "host"), port: 0 }); |
| @@ -138,6 +217,8 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 138 | 217 | other.run("CREATE TABLE note (body TEXT)"); |
| 139 | 218 | other.run("INSERT INTO note VALUES (?)", [OUTSIDE]); |
| 140 | 219 | other.close(); |
| 220 | const built = await Promise.all(Object.entries(BUILD).map(async ([name, build]) => [name, await build(name)] as const)); | |
| 221 | for (const [name, f] of built) forges.set(name, f); | |
| 141 | 222 | }); |
| 142 | 223 | afterAll(async () => { |
| 143 | 224 | host.close(); |
| @@ -145,18 +226,6 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 145 | 226 | }); |
| 146 | 227 | |
| 147 | 228 | test("a link in a forge's directory is not followed, wherever its files are read", async () => { |
| 148 | const f = await hostForge("links", "body-links"); | |
| 149 | f.open.close(); | |
| 150 | const dir = f.source.dir; | |
| 151 | const outside = join(root, "outside.txt"); | |
| 152 | symlinkSync(outside, join(dir, "ssh", "linked")); | |
| 153 | symlinkSync(outside, join(dir, "apps", "a1", "production", "data", "linked.txt")); | |
| 154 | symlinkSync(join(root, "outside-dir"), join(dir, "apps", "a1", "production", "data", "linked-dir")); | |
| 155 | // A whole directory of its own that is a link, and a link where another of its files is read. | |
| 156 | mkdirSync(join(dir, "apps", "a2", "production"), { recursive: true }); | |
| 157 | symlinkSync(join(root, "outside-dir"), join(dir, "apps", "a2", "production", "data")); | |
| 158 | rmSync(join(dir, "secret.key")); | |
| 159 | symlinkSync(outside, join(dir, "secret.key")); | |
| 160 | 229 | const { store, manifest } = await snap(["links"]); |
| 161 | 230 | expect(manifest.hosted).toMatchObject([{ name: "links", key: "none" }]); |
| 162 | 231 | expect(manifest.hosted?.[0]?.why).toBeUndefined(); |
| @@ -167,10 +236,7 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 167 | 236 | }); |
| 168 | 237 | |
| 169 | 238 | test("a forge whose database is not a file of its own is left out, by name, and the snapshot still lands", async () => { |
| 170 | const f = await hostForge("swapped", "body-swapped"); | |
| 171 | f.open.close(); | |
| 172 | for (const name of ["forge.db", "forge.db-wal", "forge.db-shm"]) rmSync(join(f.source.dir, name), { force: true }); | |
| 173 | symlinkSync(join(root, "outside.db"), join(f.source.dir, "forge.db")); | |
| 239 | const f = forge("swapped"); | |
| 174 | 240 | // Still one of the forges this host is to keep: it is named, not passed over. |
| 175 | 241 | writeFileSync(join(host.config.dataDir, "spawn", "swapped.json"), f.source.ledger); |
| 176 | 242 | expect(hostedSources(host.config.dataDir).map((s) => s.name)).toContain("swapped"); |
| @@ -184,18 +250,9 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 184 | 250 | }); |
| 185 | 251 | |
| 186 | 252 | test("a repository git would run something of, that holds a link, or that borrows from outside its forge is left out by name in the manifest", async () => { |
| 187 | const f = await hostForge("stacks", "body-stacks"); | |
| 188 | for (const name of ["fine", "borrow", "trap", "linked"]) await commit(f, name); | |
| 189 | f.open.close(); | |
| 190 | const repos = join(f.source.dir, "repos"); | |
| 191 | // A link inside a repository: git would follow it wherever it names. | |
| 192 | symlinkSync(join(root, "outside-dir"), join(repoDir(repos, "stacks", "linked"), "objects", "elsewhere")); | |
| 193 | // Configuration that names a command, and objects borrowed from a directory outside the forge | |
| 194 | // (in the quoted form git also reads). | |
| 195 | const trap = repoDir(repos, "stacks", "trap"); | |
| 196 | writeFileSync(join(trap, "config"), `${readFileSync(join(trap, "config"), "utf8")}\n[core]\n\tfsmonitor = /bin/true\n`); | |
| 197 | writeFileSync(join(repoDir(repos, "stacks", "borrow"), "objects", "info", "alternates"), `"${join(root, "outside-dir")}"\n`); | |
| 198 | const { store, manifest } = await snap(["stacks"], { passphrase: PASS }); | |
| 253 | // No passphrase: the key is not what this proves (the views, WAL and awake tests wrap one), and | |
| 254 | // each wrap is a scrypt. | |
| 255 | const { store, manifest } = await snap(["stacks"]); | |
| 199 | 256 | const entry = manifest.hosted![0]!; |
| 200 | 257 | expect(entry.why).toBeUndefined(); |
| 201 | 258 | expect(entry.repos).toBe(1); |
| @@ -207,19 +264,12 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 207 | 264 | expect(filesOf(manifest, "stacks").filter((p) => p.startsWith("repos/"))).toEqual(["repos/stacks/fine.bundle"]); |
| 208 | 265 | // The one that is in comes back whole. |
| 209 | 266 | const back = fresh("repos-back"); |
| 210 | const done = await restoreHosted(store, manifest.name, "stacks", back, { passphrase: PASS }); | |
| 267 | const done = await restoreHosted(store, manifest.name, "stacks", back); | |
| 211 | 268 | expect(done.repos).toBe(1); |
| 212 | 269 | expect((await runGit(["fsck", "--no-dangling"], { cwd: repoDir(join(back, "repos"), "stacks", "fine") })).ok).toBe(true); |
| 213 | 270 | }); |
| 214 | 271 | |
| 215 | 272 | test("a repository its database lists and its disk lacks, and one whose name cannot stand in a snapshot, are named and not passed over", async () => { |
| 216 | const f = await hostForge("ledgers", "body-ledgers"); | |
| 217 | for (const name of ["fine", "gone", "odd"]) await commit(f, name); | |
| 218 | // Its database names one its disk no longer holds, and one by a name no path may carry. | |
| 219 | f.open.db.run("UPDATE repo SET name = 'no such/../name' WHERE name = 'odd'"); | |
| 220 | f.open.close(); | |
| 221 | const repos = join(f.source.dir, "repos"); | |
| 222 | for (const name of ["gone", "odd"]) rmSync(repoDir(repos, "ledgers", name), { recursive: true }); | |
| 223 | 273 | const { manifest } = await snap(["ledgers"]); |
| 224 | 274 | const entry = manifest.hosted![0]!; |
| 225 | 275 | expect([entry.why, entry.repos]).toEqual([undefined, 1]); |
| @@ -233,9 +283,7 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 233 | 283 | }); |
| 234 | 284 | |
| 235 | 285 | test("a repository that changed into something else while git read it is not kept", async () => { |
| 236 | const f = await hostForge("shifty", "body-shifty"); | |
| 237 | for (const name of ["fine", "swap"]) await commit(f, name); | |
| 238 | f.open.close(); | |
| 286 | const f = forge("shifty"); | |
| 239 | 287 | const swap = repoDir(join(f.source.dir, "repos"), "shifty", "swap"); |
| 240 | 288 | const { manifest } = await snap(["shifty"], { |
| 241 | 289 | // The forge is awake: the moment after git has made its bundle, the repository holds a link. |
| @@ -252,9 +300,7 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 252 | 300 | }); |
| 253 | 301 | |
| 254 | 302 | test("a repository whose parent directory is swapped for a link while git reads it is not kept, and the path is named", async () => { |
| 255 | const f = await hostForge("parent", "body-parent"); | |
| 256 | await commit(f, "moved"); | |
| 257 | f.open.close(); | |
| 303 | const f = forge("parent"); | |
| 258 | 304 | const repos = join(f.source.dir, "repos"); |
| 259 | 305 | const moved = repoDir(repos, "parent", "moved"); |
| 260 | 306 | let swapped = false; |
| @@ -288,9 +334,7 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 288 | 334 | }); |
| 289 | 335 | |
| 290 | 336 | test("a forge that holds more files than one copy reads is left out, by name", async () => { |
| 291 | const f = await hostForge("many", "body-many"); | |
| 292 | f.open.close(); | |
| 293 | for (const n of [1, 2, 3]) writeFileSync(join(f.source.dir, "ssh", `extra-${n}`), `${n}`); | |
| 337 | const f = forge("many"); | |
| 294 | 338 | const { store, manifest } = await snap(["many"], { hostedFilesMax: 3 }); |
| 295 | 339 | expect(manifest.hosted).toEqual([{ name: "many", at: manifest.hosted![0]!.at, repos: 0, bytes: 0, key: "none", why: "it holds more than 3 files to copy" }]); |
| 296 | 340 | expect(filesOf(manifest, "many")).toEqual([]); |
| @@ -303,15 +347,6 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 303 | 347 | }); |
| 304 | 348 | |
| 305 | 349 | test("the forge's own lists are read in the copy's process: a view under a table's name is not run", async () => { |
| 306 | const f = await hostForge("views", "body-views"); | |
| 307 | await commit(f, "work"); | |
| 308 | f.open.close(); | |
| 309 | // `repo` is now a view the forge wrote: whatever it selects, nothing of the snapshot runs it. | |
| 310 | const db = new Database(join(f.source.dir, "forge.db")); | |
| 311 | db.run("PRAGMA foreign_keys = OFF"); | |
| 312 | db.run("ALTER TABLE repo RENAME TO repo_kept"); | |
| 313 | db.run("CREATE VIEW repo AS SELECT * FROM repo_kept"); | |
| 314 | db.close(); | |
| 315 | 350 | const { manifest } = await snap(["views"], { passphrase: PASS, hostedCopy: undefined }); |
| 316 | 351 | // Its repositories are then the ones on its disk. |
| 317 | 352 | expect(manifest.hosted).toMatchObject([{ name: "views", repos: 1, key: "wrapped" }]); |
| @@ -322,7 +357,7 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 322 | 357 | test("the copy holds what a sleeping forge left in its WAL", async () => { |
| 323 | 358 | // Asleep: killed, not closed. Its last commit is in its WAL, and the database file alone lacks |
| 324 | 359 | // it. |
| 325 | const asleep = await hostForge("asleep", "body-asleep"); | |
| 360 | const asleep = forge("asleep"); | |
| 326 | 361 | asleep.open.db.run("PRAGMA wal_autocheckpoint = 0"); |
| 327 | 362 | asleep.open.db.run("CREATE TABLE last_words (n INTEGER)"); |
| 328 | 363 | asleep.open.db.run("INSERT INTO last_words VALUES (7)"); |
| @@ -348,7 +383,7 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 348 | 383 | |
| 349 | 384 | test("the copy is one consistent moment while the forge's own process goes on writing", async () => { |
| 350 | 385 | // Awake: its own process commits before, during and after its copy is made. |
| 351 | const awake = await hostForge("awake", "body-awake"); | |
| 386 | const awake = forge("awake"); | |
| 352 | 387 | const db = awake.open.db; |
| 353 | 388 | db.run("CREATE TABLE pair_a (id INTEGER PRIMARY KEY)"); |
| 354 | 389 | db.run("CREATE TABLE pair_b (id INTEGER PRIMARY KEY)"); |
| @@ -397,9 +432,7 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 397 | 432 | }); |
| 398 | 433 | |
| 399 | 434 | test("a fork that only fetched its parent's code is the same bundle: the refs it mirrors are left out, and the next fetch brings them back", async () => { |
| 400 | const f = await hostForge("follower", "body-follower"); | |
| 401 | await commit(f, "forge"); | |
| 402 | f.open.close(); | |
| 435 | const f = forge("follower"); | |
| 403 | 436 | const fork = repoDir(join(f.source.dir, "repos"), "follower", "forge"); |
| 404 | 437 | const git = async (args: string[]): Promise<string> => { |
| 405 | 438 | const r = await runGit(args, { cwd: fork }); |
| @@ -440,8 +473,7 @@ describe("a host's snapshot of the forges it hosts", () => { | ||
| 440 | 473 | }); |
| 441 | 474 | |
| 442 | 475 | test("a copy that does not end in its time is ended, and says so", async () => { |
| 443 | const f = await hostForge("late", "body-late"); | |
| 444 | f.open.close(); | |
| 476 | const f = forge("late"); | |
| 445 | 477 | const scratch = fresh("late"); |
| 446 | 478 | mkdirSync(scratch); |
| 447 | 479 | const made = await copyInProcess({ from: join(f.source.dir, "forge.db"), to: join(scratch, "copy.db"), root: f.source.dir }, 1); |
new above
offline