Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
550 changes: 547 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

518 changes: 510 additions & 8 deletions dist/post/index.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const config = {
testEnvironment: "node",
moduleFileExtensions: ["ts", "js"],
transform: {
"^.+\\.ts$": "ts-jest",
// The @buf packages ship ES modules only; ts-jest compiles them to
// CommonJS alongside our sources.
"^.+\\.[tj]s$": ["ts-jest", { tsconfig: { allowJs: true } }],
},
transformIgnorePatterns: ["/node_modules/(?!@buf/)"],
testMatch: ["**/__tests__/**/*.test.ts"],
};

Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
"dependencies": {
"@actions/core": "^1.10.1",
"@buf/blacksmith_vm-agent.bufbuild_es": "1.5.0-20260904133914-c6beb298c70c.2",
"@buf/blacksmith_vm-agent.connectrpc_es": "^1.6.1-20260904133914-c6beb298c70c.2",
"@buf/blacksmith_vm-agent.bufbuild_es": "1.5.0-20261023032817-36eedc7ae378.2",
"@buf/blacksmith_vm-agent.connectrpc_es": "1.6.1-20261023032817-36eedc7ae378.2",
"@bufbuild/connect": "^0.13.0",
"@bufbuild/connect-web": "^0.13.0",
"@bufbuild/protobuf": "^1.4.2",
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/mount-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const report: MountReport = {
format_ms: 900,
mount_ms: 25,
unmount_ms: 130,
fstrim_outcome: "ok",
fstrim_ms: 40,
fstrim_bytes: 1073741824,
};

interface Received {
Expand Down
135 changes: 135 additions & 0 deletions src/__tests__/mount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {
findMountedDevice,
getFilesystemUsedField,
parseMountSource,
parseFstrimMinExtentBytes,
parseFstrimTimeoutSecs,
parseTrimmedBytes,
trimFilesystem,
unmount,
} from "../mount";

Expand Down Expand Up @@ -74,6 +78,137 @@ describe("mount helpers", () => {
expect(await findMountedDevice("/mnt/cache/nested", run)).toBeNull();
});

it("runs fstrim under sudo timeout with the path as one argv element when not root", async () => {
const { run, calls } = recordingRunner({
sudo: { stdout: `${hostilePath}: 1 GiB (1073741824 bytes) trimmed\n` },
});

const result = await trimFilesystem(hostilePath, 5, 0, run, 1001);

expect(calls).toEqual([
{
file: "sudo",
args: ["timeout", "5", "fstrim", "-v", "--", hostilePath],
},
]);
expect(result.outcome).toBe("ok");
expect(result.trimmedBytes).toBe(1073741824);
expect(result.detail).toBe("");
});

it("runs timeout and fstrim directly when already root", async () => {
const { run, calls } = recordingRunner({
timeout: { stdout: `${hostilePath}: 0 B (0 bytes) trimmed\n` },
});

const result = await trimFilesystem(hostilePath, 5, 0, run, 0);

expect(calls).toEqual([
{ file: "timeout", args: ["5", "fstrim", "-v", "--", hostilePath] },
]);
expect(result.outcome).toBe("ok");
});

it("passes a positive minimum extent as fstrim -m and omits it otherwise", async () => {
const { run, calls } = recordingRunner({
sudo: { stdout: `${hostilePath}: 0 B (0 bytes) trimmed\n` },
});

await trimFilesystem(hostilePath, 5, 4194304, run, 1001);
await trimFilesystem(hostilePath, 5, 0, run, 1001);

expect(calls.map((c) => c.args)).toEqual([
["timeout", "5", "fstrim", "-v", "-m", "4194304", "--", hostilePath],
["timeout", "5", "fstrim", "-v", "--", hostilePath],
]);
});

it("parses the host fstrim minimum extent from the RPC bigint or saved state", () => {
expect(parseFstrimMinExtentBytes(BigInt(65536))).toBe(65536);
expect(parseFstrimMinExtentBytes("4194304")).toBe(4194304);
expect(parseFstrimMinExtentBytes(BigInt(0))).toBe(0);
expect(parseFstrimMinExtentBytes(-1)).toBe(0);
expect(parseFstrimMinExtentBytes("")).toBe(0);
expect(parseFstrimMinExtentBytes(undefined)).toBe(0);
});

it("parses the host fstrim budget from the RPC bigint or saved state", () => {
expect(parseFstrimTimeoutSecs(BigInt(5))).toBe(5);
expect(parseFstrimTimeoutSecs(5)).toBe(5);
expect(parseFstrimTimeoutSecs("5")).toBe(5);
expect(parseFstrimTimeoutSecs("2.9")).toBe(2);
expect(parseFstrimTimeoutSecs(BigInt(0))).toBe(0);
expect(parseFstrimTimeoutSecs(-1)).toBe(0);
expect(parseFstrimTimeoutSecs("")).toBe(0);
expect(parseFstrimTimeoutSecs(undefined)).toBe(0);
expect(parseFstrimTimeoutSecs("nope")).toBe(0);
});

it("parses the byte count from fstrim -v across util-linux output shapes", () => {
expect(parseTrimmedBytes("/mnt: 0 B (0 bytes) trimmed\n")).toBe(0);
expect(
parseTrimmedBytes(
"/mnt: 1.2 GiB (1288490188 bytes) trimmed on /dev/vdb\n",
),
).toBe(1288490188);
expect(parseTrimmedBytes("")).toBeNull();
expect(parseTrimmedBytes("fstrim: /mnt: FITRIM ioctl failed")).toBeNull();
});

it("never throws from trimFilesystem and classifies the failure", async () => {
const unsupported = Object.assign(new Error("Command failed"), {
code: 1,
stderr: "fstrim: /mnt/cache: the discard operation is not supported\n",
});
expect(
await trimFilesystem(
"/mnt/cache",
5,
0,
recordingRunner({ sudo: unsupported }).run,
1001,
),
).toMatchObject({
outcome: "unsupported",
trimmedBytes: null,
detail: "fstrim: /mnt/cache: the discard operation is not supported",
});

const timedOut = Object.assign(new Error("Command failed"), {
code: 124,
stderr: "",
});
expect(
await trimFilesystem(
"/mnt/cache",
5,
0,
recordingRunner({ sudo: timedOut }).run,
1001,
),
).toMatchObject({
outcome: "timeout",
trimmedBytes: null,
detail: "timed out after 5s",
});

const missing = Object.assign(new Error("spawn timeout ENOENT"), {
code: "ENOENT",
});
expect(
await trimFilesystem(
"/mnt/cache",
5,
0,
recordingRunner({ sudo: missing }).run,
1001,
),
).toMatchObject({
outcome: "unsupported",
detail: "spawn timeout ENOENT",
});
});

const itOnLinux = process.platform === "linux" ? it : it.skip;

itOnLinux("runs the real binaries without a shell", async () => {
Expand Down
100 changes: 98 additions & 2 deletions src/__tests__/post.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from "@actions/core";
import { findMountedDevice, unmount } from "../mount";
import { findMountedDevice, trimFilesystem, unmount } from "../mount";
import { MountReport, sendMountReport } from "../mount-report";
import { createStickyDiskClient } from "../utils";

Expand All @@ -13,6 +13,9 @@ jest.mock("@actions/core", () => ({
jest.mock("../mount", () => ({
findMountedDevice: jest.fn(),
getFilesystemUsedField: jest.fn(),
parseFstrimMinExtentBytes: jest.fn((v) => Number(v) || 0),
parseFstrimTimeoutSecs: jest.fn((v) => Number(v) || 0),
trimFilesystem: jest.fn(),
unmount: jest.fn(),
}));
jest.mock("../utils", () => ({ createStickyDiskClient: jest.fn() }));
Expand All @@ -21,8 +24,12 @@ jest.mock("../mount-report", () => ({
sendMountReport: jest.fn(),
}));

async function runPost(state: Record<string, string>): Promise<MountReport> {
async function runPost(
state: Record<string, string>,
jobStatus = "success",
): Promise<MountReport> {
jest.mocked(core.getState).mockImplementation((key) => state[key] || "");
jest.mocked(core.getInput).mockReturnValue(jobStatus);
const finished = new Promise<MountReport>((resolve) => {
jest.mocked(sendMountReport).mockImplementation(async (report) => {
resolve(report);
Expand Down Expand Up @@ -89,3 +96,92 @@ describe("post mount ownership", () => {
});
});
});

describe("post fstrim before unmount", () => {
const mounted = {
STICKYDISK_PATH: "/mnt/cache",
STICKYDISK_KEY: "cache",
STICKYDISK_EXPOSE_ID: "expose-1",
STICKYDISK_FSTRIM_TIMEOUT_SECS: "5",
STICKYDISK_FSTRIM_MIN_EXTENT_BYTES: "4194304",
};

beforeEach(() => {
jest.clearAllMocks();
jest.mocked(findMountedDevice).mockResolvedValue("/dev/vdh");
jest.mocked(trimFilesystem).mockResolvedValue({
outcome: "ok",
durationMs: 40,
trimmedBytes: 1073741824,
detail: "",
});
jest.mocked(createStickyDiskClient).mockReturnValue({
commitStickyDisk: jest.fn(async () => ({})),
cleanupStickyDisk: jest.fn(async () => ({})),
} as never);
});

it("trims with the host budget before unmount when the job succeeded", async () => {
const order: string[] = [];
jest.mocked(trimFilesystem).mockImplementation(async () => {
order.push("fstrim");
return {
outcome: "ok",
durationMs: 40,
trimmedBytes: 1073741824,
detail: "",
};
});
jest.mocked(unmount).mockImplementation(async () => {
order.push("umount");
});

const report = await runPost(mounted);

expect(trimFilesystem).toHaveBeenCalledWith("/mnt/cache", 5, 4194304);
expect(order).toEqual(["fstrim", "umount"]);
expect(report).toMatchObject({
skip_reason: "",
fstrim_outcome: "ok",
fstrim_ms: 40,
fstrim_bytes: 1073741824,
});
});

it("skips the trim when the job failed, like the commit", async () => {
const report = await runPost(mounted, "failure");

expect(trimFilesystem).not.toHaveBeenCalled();
expect(unmount).toHaveBeenCalled();
expect(report).toMatchObject({
skip_reason: "prior_step_failure",
fstrim_outcome: "skipped",
});
});

it("skips the trim without a host budget or on a read-only mount", async () => {
await runPost({ ...mounted, STICKYDISK_FSTRIM_TIMEOUT_SECS: "0" });
await runPost({ ...mounted, STICKYDISK_COMMIT_MODE: "false" });

expect(trimFilesystem).not.toHaveBeenCalled();
expect(unmount).toHaveBeenCalledTimes(2);
});

it("reports a failed trim and still unmounts and commits", async () => {
jest.mocked(trimFilesystem).mockResolvedValue({
outcome: "unsupported",
durationMs: 3,
trimmedBytes: null,
detail: "the discard operation is not supported",
});

const report = await runPost(mounted);

expect(unmount).toHaveBeenCalled();
expect(report).toMatchObject({
skip_reason: "",
fstrim_outcome: "unsupported",
fstrim_bytes: 0,
});
});
});
Loading
Loading