diff --git a/scripts/e2e/lib/docker-stats/assert-resource-ceiling.mjs b/scripts/e2e/lib/docker-stats/assert-resource-ceiling.mjs index 3738da60130d..5e102d838145 100644 --- a/scripts/e2e/lib/docker-stats/assert-resource-ceiling.mjs +++ b/scripts/e2e/lib/docker-stats/assert-resource-ceiling.mjs @@ -1,9 +1,9 @@ // Resource ceiling assertions for Docker E2E stats output. import fs from "node:fs"; -import { createInterface } from "node:readline"; const [statsFile, maxMemoryRaw, maxCpuRaw, label = "docker"] = process.argv.slice(2); const NON_NEGATIVE_DECIMAL_PATTERN = /^(?:0|[1-9]\d*)(?:\.\d+)?$/u; +const MAX_STATS_SAMPLE_LINE_BYTES = 1024 * 1024; function parseFiniteLimit(raw, name) { const text = String(raw ?? "").trim(); @@ -92,11 +92,55 @@ async function scanStatsFileLines(file, onLine) { return; } const input = fs.createReadStream(file, { encoding: "utf8" }); - const lines = createInterface({ crlfDelay: Infinity, input }); - for await (const line of lines) { + let pending = ""; + let pendingBytes = 0; + let skipLineFeedAfterCarriageReturn = false; + + const appendSegment = (segment) => { + if (!segment) { + return; + } + const segmentBytes = Buffer.byteLength(segment, "utf8"); + if (pendingBytes + segmentBytes > MAX_STATS_SAMPLE_LINE_BYTES) { + throw new Error( + `docker stats sample for ${label} exceeded ${MAX_STATS_SAMPLE_LINE_BYTES} bytes`, + ); + } + pending += segment; + pendingBytes += segmentBytes; + }; + const emitPendingLine = () => { + const line = pending.endsWith("\r") ? pending.slice(0, -1) : pending; + pending = ""; + pendingBytes = 0; if (line) { onLine(line); } + }; + + for await (const chunk of input) { + let start = 0; + for (let index = 0; index < chunk.length; index += 1) { + const code = chunk.charCodeAt(index); + if (skipLineFeedAfterCarriageReturn) { + skipLineFeedAfterCarriageReturn = false; + if (code === 10) { + start = index + 1; + continue; + } + } + if (code !== 10 && code !== 13) { + continue; + } + appendSegment(chunk.slice(start, index)); + emitPendingLine(); + skipLineFeedAfterCarriageReturn = code === 13; + start = index + 1; + } + appendSegment(chunk.slice(start)); + } + if (pending) { + emitPendingLine(); } } diff --git a/test/scripts/docker-stats-resource-ceiling.test.ts b/test/scripts/docker-stats-resource-ceiling.test.ts index 87985074fbce..921d8fdbdb3b 100644 --- a/test/scripts/docker-stats-resource-ceiling.test.ts +++ b/test/scripts/docker-stats-resource-ceiling.test.ts @@ -6,6 +6,7 @@ import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; const SCRIPT_PATH = "scripts/e2e/lib/docker-stats/assert-resource-ceiling.mjs"; +const MAX_STATS_SAMPLE_LINE_BYTES = 1024 * 1024; const tempRoots: string[] = []; function writeStats(contents: string): string { @@ -26,6 +27,14 @@ function runAssert(statsFile: string, maxMemoryMiB = "512", maxCpuPercent = "100 ); } +function validStatsLineWithBytes(byteLength: number): string { + const prefix = '{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%","padding":"'; + const suffix = '"}'; + const paddingLength = byteLength - Buffer.byteLength(prefix + suffix, "utf8"); + expect(paddingLength).toBeGreaterThan(0); + return `${prefix}${"x".repeat(paddingLength)}${suffix}`; +} + afterEach(() => { for (const root of tempRoots.splice(0)) { rmSync(root, { force: true, recursive: true }); @@ -101,10 +110,51 @@ describe("scripts/e2e/lib/docker-stats/assert-resource-ceiling.mjs", () => { const source = readFileSync(SCRIPT_PATH, "utf8"); expect(source).toContain("createReadStream"); + expect(source).toContain("MAX_STATS_SAMPLE_LINE_BYTES"); + expect(source).not.toContain("createInterface"); expect(source).not.toContain("readFileSync(statsFile"); expect(source).not.toContain("split(/\\r?\\n/u)"); }); + it("rejects oversized stats sample lines before parsing JSON", () => { + const result = runAssert(writeStats(`{"padding":"${"x".repeat(1024 * 1024)}"}\n`)); + + expect(result.status).not.toBe(0); + expect(result.stderr).toContain("exceeded 1048576 bytes"); + expect(result.stderr).not.toContain("was not valid JSON"); + }); + + it("accepts large stats sample lines within the line cap", () => { + const padding = "x".repeat(1024); + const result = runAssert( + writeStats(`{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%","padding":"${padding}"}\n`), + ); + + expect(result.status).toBe(0); + expect(result.stdout).toContain("samples=1"); + }); + + it("accepts CRLF stats sample lines whose content exactly matches the line cap", () => { + const line = validStatsLineWithBytes(MAX_STATS_SAMPLE_LINE_BYTES); + const result = runAssert(writeStats(`${line}\r\n`)); + + expect(Buffer.byteLength(line, "utf8")).toBe(MAX_STATS_SAMPLE_LINE_BYTES); + expect(result.status).toBe(0); + expect(result.stdout).toContain("samples=1"); + }); + + it("accepts stats sample lines separated by standalone carriage returns", () => { + const result = runAssert( + writeStats( + '{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%"}\r{"MemUsage":"64MiB / 2GiB","CPUPerc":"15.0%"}\r', + ), + ); + + expect(result.status).toBe(0); + expect(result.stdout).toContain("memory=128.0MiB"); + expect(result.stdout).toContain("samples=2"); + }); + it("accepts byte-unit Docker memory samples", () => { const result = runAssert(writeStats('{"MemUsage":"512B / 2GiB","CPUPerc":"0.5%"}\n'));