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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/15147-test-snapshot-hosted-inside-test-ts-fits → main 1 commit fork

  • Merged. torque/15147-test-snapshot-hosted-inside-test-ts-fits was merged into main as 706c9c0.

Review

Sign in to review this change.
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", () => {
124124 if (!c.ok) throw new Error(JSON.stringify(c.error));
125125 };
126126
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
127206 beforeAll(async () => {
128207 root = mkdtempSync(join(tmpdir(), "forge-hosted-inside-"));
129208 host = createApp({ dataDir: join(root, "host"), port: 0 });
@@ -138,6 +217,8 @@ describe("a host's snapshot of the forges it hosts", () => {
138217 other.run("CREATE TABLE note (body TEXT)");
139218 other.run("INSERT INTO note VALUES (?)", [OUTSIDE]);
140219 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);
141222 });
142223 afterAll(async () => {
143224 host.close();
@@ -145,18 +226,6 @@ describe("a host's snapshot of the forges it hosts", () => {
145226 });
146227
147228 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"));
160229 const { store, manifest } = await snap(["links"]);
161230 expect(manifest.hosted).toMatchObject([{ name: "links", key: "none" }]);
162231 expect(manifest.hosted?.[0]?.why).toBeUndefined();
@@ -167,10 +236,7 @@ describe("a host's snapshot of the forges it hosts", () => {
167236 });
168237
169238 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");
174240 // Still one of the forges this host is to keep: it is named, not passed over.
175241 writeFileSync(join(host.config.dataDir, "spawn", "swapped.json"), f.source.ledger);
176242 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", () => {
184250 });
185251
186252 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"]);
199256 const entry = manifest.hosted![0]!;
200257 expect(entry.why).toBeUndefined();
201258 expect(entry.repos).toBe(1);
@@ -207,19 +264,12 @@ describe("a host's snapshot of the forges it hosts", () => {
207264 expect(filesOf(manifest, "stacks").filter((p) => p.startsWith("repos/"))).toEqual(["repos/stacks/fine.bundle"]);
208265 // The one that is in comes back whole.
209266 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);
211268 expect(done.repos).toBe(1);
212269 expect((await runGit(["fsck", "--no-dangling"], { cwd: repoDir(join(back, "repos"), "stacks", "fine") })).ok).toBe(true);
213270 });
214271
215272 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 });
223273 const { manifest } = await snap(["ledgers"]);
224274 const entry = manifest.hosted![0]!;
225275 expect([entry.why, entry.repos]).toEqual([undefined, 1]);
@@ -233,9 +283,7 @@ describe("a host's snapshot of the forges it hosts", () => {
233283 });
234284
235285 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");
239287 const swap = repoDir(join(f.source.dir, "repos"), "shifty", "swap");
240288 const { manifest } = await snap(["shifty"], {
241289 // 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", () => {
252300 });
253301
254302 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");
258304 const repos = join(f.source.dir, "repos");
259305 const moved = repoDir(repos, "parent", "moved");
260306 let swapped = false;
@@ -288,9 +334,7 @@ describe("a host's snapshot of the forges it hosts", () => {
288334 });
289335
290336 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");
294338 const { store, manifest } = await snap(["many"], { hostedFilesMax: 3 });
295339 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" }]);
296340 expect(filesOf(manifest, "many")).toEqual([]);
@@ -303,15 +347,6 @@ describe("a host's snapshot of the forges it hosts", () => {
303347 });
304348
305349 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();
315350 const { manifest } = await snap(["views"], { passphrase: PASS, hostedCopy: undefined });
316351 // Its repositories are then the ones on its disk.
317352 expect(manifest.hosted).toMatchObject([{ name: "views", repos: 1, key: "wrapped" }]);
@@ -322,7 +357,7 @@ describe("a host's snapshot of the forges it hosts", () => {
322357 test("the copy holds what a sleeping forge left in its WAL", async () => {
323358 // Asleep: killed, not closed. Its last commit is in its WAL, and the database file alone lacks
324359 // it.
325 const asleep = await hostForge("asleep", "body-asleep");
360 const asleep = forge("asleep");
326361 asleep.open.db.run("PRAGMA wal_autocheckpoint = 0");
327362 asleep.open.db.run("CREATE TABLE last_words (n INTEGER)");
328363 asleep.open.db.run("INSERT INTO last_words VALUES (7)");
@@ -348,7 +383,7 @@ describe("a host's snapshot of the forges it hosts", () => {
348383
349384 test("the copy is one consistent moment while the forge's own process goes on writing", async () => {
350385 // 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");
352387 const db = awake.open.db;
353388 db.run("CREATE TABLE pair_a (id INTEGER PRIMARY KEY)");
354389 db.run("CREATE TABLE pair_b (id INTEGER PRIMARY KEY)");
@@ -397,9 +432,7 @@ describe("a host's snapshot of the forges it hosts", () => {
397432 });
398433
399434 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");
403436 const fork = repoDir(join(f.source.dir, "repos"), "follower", "forge");
404437 const git = async (args: string[]): Promise<string> => {
405438 const r = await runGit(args, { cwd: fork });
@@ -440,8 +473,7 @@ describe("a host's snapshot of the forges it hosts", () => {
440473 });
441474
442475 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");
445477 const scratch = fresh("late");
446478 mkdirSync(scratch);
447479 const made = await copyInProcess({ from: join(f.source.dir, "forge.db"), to: join(scratch, "copy.db"), root: f.source.dir }, 1);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs