Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion scripts/darwin-ci/lib/guest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const sshOptions = [
"ServerAliveInterval=30",
];

// The guest gets the working tree only. Nothing in it reads the checkout's .git (the runner takes the commit from
// the forwarded BUILDKITE_* env), and the `git gc --auto` the agent's fetch leaves running rewrites .git while
// rsync reads it: a vanished .git/gc.pid fails the sync with exit 23. Anchored, so a nested repo still copies whole.
export const checkoutExcludes = ["--exclude=/.git"];

export async function ensureHostKey(): Promise<string> {
if (!(await Bun.file(hostKey).exists())) {
await $`ssh-keygen -q -t ed25519 -N "" -C ${`${process.env.USER}@darwin-ci`} -f ${hostKey}`;
Expand Down Expand Up @@ -50,7 +55,7 @@ export function guest(ip: string) {
push: (local: string, remote: string) => $`scp ${sshOptions} -q ${local} ${target}:${remote}`.quiet(),

syncTo: (localDir: string, remoteDir: string) =>
$`rsync -a --delete -e ${rsh} ${localDir}/ ${target}:${remoteDir}/`.quiet(),
$`rsync -a --delete ${checkoutExcludes} -e ${rsh} ${localDir}/ ${target}:${remoteDir}/`.quiet(),

collectReports: (remoteDir: string, localDir: string) =>
$`rsync -a -e ${rsh} --include=*/ --include=*.xml --include=*.junit --exclude=* --prune-empty-dirs ${target}:${remoteDir}/ ${localDir}/`
Expand Down
65 changes: 65 additions & 0 deletions test/internal/darwin-ci-guest-sync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* The darwin tart command hook (scripts/darwin-ci/hooks/command.ts) rsyncs the agent's checkout into a fresh
* macOS guest with guest().syncTo() from scripts/darwin-ci/lib/guest.ts. The checkout's own .git must stay out
* of that copy: the `git gc --auto` the agent's fetch leaves running rewrites .git while rsync reads it, and a
* file that vanishes mid-transfer (.git/gc.pid) fails the sync, and the job with it, before any test runs.
*
* syncTo runs the real rsync over `-e ssh`. A fake `ssh` on PATH drops the ssh options and runs the rsync server
* side in a local "guest home" instead, so the command under test is the one the hook runs.
*/
import { expect, test } from "bun:test";
import { isWindows, tempDir } from "harness";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";

import { guest } from "../../scripts/darwin-ci/lib/guest.ts";

const rsync = Bun.which("rsync");

test.skipIf(isWindows || !rsync)("syncTo sends the working tree, not the checkout's .git", async () => {
using dir = tempDir("darwin-ci-sync", {
"checkout/package.json": `{ "name": "bun" }`,
"checkout/.gitignore": "node_modules\n",
"checkout/src/index.ts": "export {};\n",
"checkout/.git/HEAD": "ref: refs/heads/main\n",
"checkout/.git/gc.pid": "12345 host\n",
"checkout/.git/objects/pack/pack-a.pack": "PACK",
// a vendor clone is its own repository; only the checkout root's .git stays behind
"checkout/vendor/elysia/.git/HEAD": "ref: refs/heads/main\n",
"checkout/vendor/elysia/package.json": `{ "name": "elysia" }`,
// the baked image has an empty ~/work; a leftover must still be mirrored away
"guest/work/stale.txt": "from a previous sync\n",
});
const root = String(dir);
const guestHome = join(root, "guest");

mkdirSync(join(root, "bin"));
writeFileSync(
join(root, "bin", "ssh"),
[
"#!/bin/bash",
"# stands in for ssh: drop the options (rsync passes the user as -l) and the host, then run the rsync server",
"while [[ $1 == -* ]]; do case $1 in -i | -o | -l) shift 2 ;; *) shift ;; esac; done",
"shift",
`cd ${JSON.stringify(guestHome)} && exec "$@"`,
"",
].join("\n"),
{ mode: 0o755 },
);

const path = process.env.PATH;
process.env.PATH = `${join(root, "bin")}:${path}`;
try {
await guest("192.0.2.1").syncTo(join(root, "checkout"), "work");
} finally {
process.env.PATH = path;
}

const work = join(guestHome, "work");
expect(existsSync(join(work, ".git"))).toBe(false);
expect(existsSync(join(work, "stale.txt"))).toBe(false);
expect(readFileSync(join(work, "package.json"), "utf8")).toBe(`{ "name": "bun" }`);
expect(readFileSync(join(work, "src", "index.ts"), "utf8")).toBe("export {};\n");
expect(readFileSync(join(work, ".gitignore"), "utf8")).toBe("node_modules\n");
expect(readFileSync(join(work, "vendor", "elysia", ".git", "HEAD"), "utf8")).toBe("ref: refs/heads/main\n");
});
Loading