Public: anyone can read this; the crew can change it.
Red after landing: #17015 broke test/big-clone.test.ts #17219
Merged 43h 30m ago, live in production
scout/forge:scout/17116-red-after-landing-17015-broke-test-big-c → main 0 commits fork 1 thread · 1 unresolved
3 files
changed +142
−122
test/big-clone-by-id.test.ts added +93
| @@ -0,0 +1,93 @@ | ||
| 1 | /** | |
| 2 | * The object-id half of test/big-clone.test.ts, split from it to keep each file under the file | |
| 3 | * budget (#17116). A fetch by object id passes the same read check as a clone: a stranger cannot | |
| 4 | * reach a private repository's blob, and a reader's partial clone fetches it lazily. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createToken } from "../src/core/tokens.ts"; | |
| 13 | import { runGit } from "../src/git/env.ts"; | |
| 14 | import { repoDir } from "../src/git/repos.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 17 | import { git, missing, ok } from "./helpers/git-run.ts"; | |
| 18 | import { unwrap } from "../src/kernel/result.ts"; | |
| 19 | ||
| 20 | describe("a fetch by object id is a read like any other", () => { | |
| 21 | let dataDir: string; | |
| 22 | let work: string; | |
| 23 | let app: App; | |
| 24 | let servers: Servers; | |
| 25 | let token: string; | |
| 26 | let secretOid: string; | |
| 27 | ||
| 28 | beforeAll(async () => { | |
| 29 | dataDir = await mkdtemp(join(tmpdir(), "forge-bigclone-app-")); | |
| 30 | work = await mkdtemp(join(tmpdir(), "forge-bigclone-app-work-")); | |
| 31 | app = createApp({ dataDir, port: 0 }); | |
| 32 | servers = await serve(app); | |
| 33 | const owner = await seedAdmin(app, { | |
| 34 | username: "ada", | |
| 35 | email: "[email protected]", | |
| 36 | password: "owner-passphrase", | |
| 37 | }); | |
| 38 | if (!owner.ok) throw new Error(owner.error); | |
| 39 | token = createToken(app.db, owner.value.id, "t", "ro").token; | |
| 40 | const vault = await createRepo(app, owner.value, { name: "vault" }); | |
| 41 | const open = await createRepo(app, owner.value, { name: "open", visibility: "public" }); | |
| 42 | if (!vault.ok || !open.ok) throw new Error("repo creation failed"); | |
| 43 | await commitToBranch(app, owner.value, vault.value, { | |
| 44 | message: "seed", | |
| 45 | files: [{ path: "secret", content: "the vault's secret\n" }], | |
| 46 | }); | |
| 47 | await commitToBranch(app, owner.value, open.value, { | |
| 48 | message: "seed", | |
| 49 | files: [{ path: "README.md", content: "# open\n" }], | |
| 50 | }); | |
| 51 | secretOid = unwrap( | |
| 52 | await runGit(["rev-parse", "main:secret"], { cwd: repoDir(app.reposRoot, "ada", "vault") }), | |
| 53 | ); | |
| 54 | }); | |
| 55 | ||
| 56 | afterAll(async () => { | |
| 57 | servers.stop(); | |
| 58 | app.close(); | |
| 59 | await rm(dataDir, { recursive: true, force: true }); | |
| 60 | await rm(work, { recursive: true, force: true }); | |
| 61 | }); | |
| 62 | ||
| 63 | const url = (name: string, auth = ""): string => | |
| 64 | `${servers.url.replace("://", `://${auth}`)}/ada/${name}.git`; | |
| 65 | ||
| 66 | test("a stranger cannot fetch a private repository's blob by its id", async () => { | |
| 67 | const dir = join(work, "stranger"); | |
| 68 | await ok(["init", "-q", dir], work); | |
| 69 | for (const version of [0, 2]) { | |
| 70 | const run = await git(["-c", `protocol.version=${version}`, "fetch", url("vault"), secretOid], dir); | |
| 71 | expect(run.code).not.toBe(0); | |
| 72 | } | |
| 73 | const lookup = await git(["cat-file", "-e", secretOid], dir); | |
| 74 | expect(lookup.code).not.toBe(0); | |
| 75 | }); | |
| 76 | ||
| 77 | test("a partial clone of a public repository cannot reach into a private one", async () => { | |
| 78 | const run = await git(["clone", "-q", "--filter=blob:none", url("open"), "open"], work); | |
| 79 | expect(run.code).toBe(0); | |
| 80 | const lazy = await git(["cat-file", "-p", secretOid], join(work, "open")); | |
| 81 | expect(lazy.code).not.toBe(0); | |
| 82 | expect(lazy.out).not.toContain("secret"); | |
| 83 | }); | |
| 84 | ||
| 85 | test("a reader's partial clone of the private repository fetches the blob lazily", async () => { | |
| 86 | const dir = join(work, "reader"); | |
| 87 | const auth = `ada:${token}@`; | |
| 88 | const run = await git(["clone", "-q", "--no-checkout", "--filter=blob:none", url("vault", auth), dir], work); | |
| 89 | expect(run.code).toBe(0); | |
| 90 | expect(await missing(dir)).toContain(secretOid); | |
| 91 | expect(await ok(["cat-file", "-p", secretOid], dir)).toBe("the vault's secret"); | |
| 92 | }); | |
| 93 | }); |
test/big-clone.test.ts +4 −122
| @@ -10,15 +10,14 @@ | ||
| 10 | 10 | * hundreds of megabytes; here the window is one second and the pack is 4 MiB |
| 11 | 11 | * through a 1 MiB/s link, so the clone lasts several windows, longer than |
| 12 | 12 | * the wall clock it is also run against. |
| 13 | * | |
| 14 | * The object-id reads live in test/big-clone-by-id.test.ts, split off to keep each file under the | |
| 15 | * file budget (#17116). | |
| 13 | 16 | */ |
| 14 | 17 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 15 | 18 | import { mkdtemp, rm } from "node:fs/promises"; |
| 16 | 19 | import { tmpdir } from "node:os"; |
| 17 | 20 | import { join } from "node:path"; |
| 18 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 19 | import { createApp, type App } from "../src/app.ts"; | |
| 20 | import { createToken } from "../src/core/tokens.ts"; | |
| 21 | import { runGit } from "../src/git/env.ts"; | |
| 22 | 21 | import { metered, watchProgress, type Stall } from "../src/git/progress.ts"; |
| 23 | 22 | import { initBareRepo, repoDir } from "../src/git/repos.ts"; |
| 24 | 23 | import { |
| @@ -27,50 +26,8 @@ import { | ||
| 27 | 26 | serviceRpc, |
| 28 | 27 | type SmartHttpOptions, |
| 29 | 28 | } from "../src/git/smart-http.ts"; |
| 30 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 31 | 29 | import { unwrap } from "../src/kernel/result.ts"; |
| 32 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 33 | import { gitClientEnv } from "./helpers/git-client.ts"; | |
| 34 | ||
| 35 | /** | |
| 36 | * One git client run. A clone expected to succeed is matched whole | |
| 37 | * (`toMatchObject({ code: 0 })`), so a red prints git's stderr, not a bare 128. | |
| 38 | */ | |
| 39 | interface Run { | |
| 40 | code: number; | |
| 41 | out: string; | |
| 42 | err: string; | |
| 43 | } | |
| 44 | ||
| 45 | async function git(args: string[], cwd: string): Promise<Run> { | |
| 46 | const proc = Bun.spawn(["git", ...args], { | |
| 47 | cwd, | |
| 48 | env: gitClientEnv(), | |
| 49 | stdout: "pipe", | |
| 50 | stderr: "pipe", | |
| 51 | }); | |
| 52 | const [out, err, code] = await Promise.all([ | |
| 53 | new Response(proc.stdout).text(), | |
| 54 | new Response(proc.stderr).text(), | |
| 55 | proc.exited, | |
| 56 | ]); | |
| 57 | return { code, out: out.trim(), err: err.trim() }; | |
| 58 | } | |
| 59 | ||
| 60 | async function ok(args: string[], cwd: string): Promise<string> { | |
| 61 | const run = await git(args, cwd); | |
| 62 | if (run.code !== 0) throw new Error(`git ${args.join(" ")}: ${run.err}`); | |
| 63 | return run.out; | |
| 64 | } | |
| 65 | ||
| 66 | /** Object ids a partial clone does not have, without fetching them. */ | |
| 67 | async function missing(dir: string): Promise<string[]> { | |
| 68 | const out = await ok(["rev-list", "--objects", "--missing=print", "--all"], dir); | |
| 69 | return out | |
| 70 | .split("\n") | |
| 71 | .filter((line) => line.startsWith("?")) | |
| 72 | .map((line) => line.slice(1)); | |
| 73 | } | |
| 30 | import { git, missing, ok } from "./helpers/git-run.ts"; | |
| 74 | 31 | |
| 75 | 32 | function randomBytes(size: number): Uint8Array { |
| 76 | 33 | const out = new Uint8Array(size); |
| @@ -337,78 +294,3 @@ describe("a big repository over smart http", () => { | ||
| 337 | 294 | }, 180_000); |
| 338 | 295 | } |
| 339 | 296 | }); |
| 340 | ||
| 341 | describe("a fetch by object id is a read like any other", () => { | |
| 342 | let dataDir: string; | |
| 343 | let work: string; | |
| 344 | let app: App; | |
| 345 | let servers: Servers; | |
| 346 | let token: string; | |
| 347 | let secretOid: string; | |
| 348 | ||
| 349 | beforeAll(async () => { | |
| 350 | dataDir = await mkdtemp(join(tmpdir(), "forge-bigclone-app-")); | |
| 351 | work = await mkdtemp(join(tmpdir(), "forge-bigclone-app-work-")); | |
| 352 | app = createApp({ dataDir, port: 0 }); | |
| 353 | servers = await serve(app); | |
| 354 | const owner = await seedAdmin(app, { | |
| 355 | username: "ada", | |
| 356 | email: "[email protected]", | |
| 357 | password: "owner-passphrase", | |
| 358 | }); | |
| 359 | if (!owner.ok) throw new Error(owner.error); | |
| 360 | token = createToken(app.db, owner.value.id, "t", "ro").token; | |
| 361 | const vault = await createRepo(app, owner.value, { name: "vault" }); | |
| 362 | const open = await createRepo(app, owner.value, { name: "open", visibility: "public" }); | |
| 363 | if (!vault.ok || !open.ok) throw new Error("repo creation failed"); | |
| 364 | await commitToBranch(app, owner.value, vault.value, { | |
| 365 | message: "seed", | |
| 366 | files: [{ path: "secret", content: "the vault's secret\n" }], | |
| 367 | }); | |
| 368 | await commitToBranch(app, owner.value, open.value, { | |
| 369 | message: "seed", | |
| 370 | files: [{ path: "README.md", content: "# open\n" }], | |
| 371 | }); | |
| 372 | secretOid = unwrap( | |
| 373 | await runGit(["rev-parse", "main:secret"], { cwd: repoDir(app.reposRoot, "ada", "vault") }), | |
| 374 | ); | |
| 375 | }); | |
| 376 | ||
| 377 | afterAll(async () => { | |
| 378 | servers.stop(); | |
| 379 | app.close(); | |
| 380 | await rm(dataDir, { recursive: true, force: true }); | |
| 381 | await rm(work, { recursive: true, force: true }); | |
| 382 | }); | |
| 383 | ||
| 384 | const url = (name: string, auth = ""): string => | |
| 385 | `${servers.url.replace("://", `://${auth}`)}/ada/${name}.git`; | |
| 386 | ||
| 387 | test("a stranger cannot fetch a private repository's blob by its id", async () => { | |
| 388 | const dir = join(work, "stranger"); | |
| 389 | await ok(["init", "-q", dir], work); | |
| 390 | for (const version of [0, 2]) { | |
| 391 | const run = await git(["-c", `protocol.version=${version}`, "fetch", url("vault"), secretOid], dir); | |
| 392 | expect(run.code).not.toBe(0); | |
| 393 | } | |
| 394 | const lookup = await git(["cat-file", "-e", secretOid], dir); | |
| 395 | expect(lookup.code).not.toBe(0); | |
| 396 | }); | |
| 397 | ||
| 398 | test("a partial clone of a public repository cannot reach into a private one", async () => { | |
| 399 | const run = await git(["clone", "-q", "--filter=blob:none", url("open"), "open"], work); | |
| 400 | expect(run.code).toBe(0); | |
| 401 | const lazy = await git(["cat-file", "-p", secretOid], join(work, "open")); | |
| 402 | expect(lazy.code).not.toBe(0); | |
| 403 | expect(lazy.out).not.toContain("secret"); | |
| 404 | }); | |
| 405 | ||
| 406 | test("a reader's partial clone of the private repository fetches the blob lazily", async () => { | |
| 407 | const dir = join(work, "reader"); | |
| 408 | const auth = `ada:${token}@`; | |
| 409 | const run = await git(["clone", "-q", "--no-checkout", "--filter=blob:none", url("vault", auth), dir], work); | |
| 410 | expect(run.code).toBe(0); | |
| 411 | expect(await missing(dir)).toContain(secretOid); | |
| 412 | expect(await ok(["cat-file", "-p", secretOid], dir)).toBe("the vault's secret"); | |
| 413 | }); | |
| 414 | }); |
test/helpers/git-run.ts added +45
| @@ -0,0 +1,45 @@ | ||
| 1 | /** | |
| 2 | * One real git client run for a test, shared by the big-clone files: `git` returns its code and | |
| 3 | * output, `ok` throws on a nonzero code, `missing` lists the objects a partial clone lacks. | |
| 4 | */ | |
| 5 | import { gitClientEnv } from "./git-client.ts"; | |
| 6 | ||
| 7 | /** | |
| 8 | * One git client run. A clone expected to succeed is matched whole | |
| 9 | * (`toMatchObject({ code: 0 })`), so a red prints git's stderr, not a bare 128. | |
| 10 | */ | |
| 11 | export interface Run { | |
| 12 | code: number; | |
| 13 | out: string; | |
| 14 | err: string; | |
| 15 | } | |
| 16 | ||
| 17 | export async function git(args: string[], cwd: string): Promise<Run> { | |
| 18 | const proc = Bun.spawn(["git", ...args], { | |
| 19 | cwd, | |
| 20 | env: gitClientEnv(), | |
| 21 | stdout: "pipe", | |
| 22 | stderr: "pipe", | |
| 23 | }); | |
| 24 | const [out, err, code] = await Promise.all([ | |
| 25 | new Response(proc.stdout).text(), | |
| 26 | new Response(proc.stderr).text(), | |
| 27 | proc.exited, | |
| 28 | ]); | |
| 29 | return { code, out: out.trim(), err: err.trim() }; | |
| 30 | } | |
| 31 | ||
| 32 | export async function ok(args: string[], cwd: string): Promise<string> { | |
| 33 | const run = await git(args, cwd); | |
| 34 | if (run.code !== 0) throw new Error(`git ${args.join(" ")}: ${run.err}`); | |
| 35 | return run.out; | |
| 36 | } | |
| 37 | ||
| 38 | /** Object ids a partial clone does not have, without fetching them. */ | |
| 39 | export async function missing(dir: string): Promise<string[]> { | |
| 40 | const out = await ok(["rev-list", "--objects", "--missing=print", "--all"], dir); | |
| 41 | return out | |
| 42 | .split("\n") | |
| 43 | .filter((line) => line.startsWith("?")) | |
| 44 | .map((line) => line.slice(1)); | |
| 45 | } |
new above
offline
Run, git, ok and missing are copied from test/big-clone.test.ts, which still defines them. Put them in one test/helpers/ module and import them in both files.