mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
test(environment): verify registry pins in the exported bundle (#11870)
This commit is contained in:
1 parent
6f0f9a122d
commit
e9cc643198
3 files changed
+39
-129
No files matched your search
@@ -1,4 +1,3 @@
|
||||
import { isPortableBundleName } from "../../domain/portableBundleName.js";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { timedCaptureEvidence } from "../../../tests/fixtures/analysisView.js";
|
||||
@@ -110,34 +109,6 @@ describe("planEnvironmentBundle", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("pins the registry package to this build's exact version in the Dockerfile", () => {
|
||||
const dockerfile = file(plan(), "envs/grafana-example/Dockerfile");
|
||||
const lines = dockerfile.trimEnd().split("\n");
|
||||
expect(lines[0]).toMatch(/^FROM node:\d+\.\d+\.\d+-bookworm-slim$/u);
|
||||
expect(lines).toContain("ENV MCP_HOST=0.0.0.0");
|
||||
expect(lines).toContain("ENV MCP_PORT=18765");
|
||||
expect(lines).toContain(
|
||||
`ENV MCP_ALLOWED_HOSTS=127.0.0.1,localhost,grafana-example`,
|
||||
);
|
||||
expect(lines).toContain(
|
||||
"RUN apt-get update && apt-get install -y --no-install-recommends python3 && rm -rf /var/lib/apt/lists/*",
|
||||
);
|
||||
expect(lines).toContain(
|
||||
`RUN npm install -g --ignore-scripts --no-fund --no-audit rea-agents@${PACKAGE_METADATA.version}`,
|
||||
);
|
||||
expect(lines).toContain(
|
||||
"COPY --chown=node:node manifest.json evidence.json /env/",
|
||||
);
|
||||
expect(lines).toContain("USER node");
|
||||
expect(lines.at(-1)).toBe(
|
||||
'CMD ["rea","environment-serve","/env/manifest.json","--evidence","/env/evidence.json"]',
|
||||
);
|
||||
expect(dockerfile).not.toContain("rea-agents.tgz");
|
||||
expect(dockerfile.indexOf("RUN npm install")).toBeLessThan(
|
||||
dockerfile.indexOf("USER node"),
|
||||
);
|
||||
});
|
||||
|
||||
it("copies and installs the tarball instead of the registry package when one is given", () => {
|
||||
const planned = plan({ tarball: "/packed/rea-agents-1.2.3.tgz" });
|
||||
expect(planned.copies.tarball).toBe("envs/grafana-example/rea-agents.tgz");
|
||||
@@ -147,9 +118,6 @@ describe("planEnvironmentBundle", () => {
|
||||
"RUN npm install -g --ignore-scripts --no-fund --no-audit /tmp/rea-agents.tgz && rm /tmp/rea-agents.tgz\n",
|
||||
);
|
||||
expect(dockerfile).not.toContain(`rea-agents@${PACKAGE_METADATA.version}`);
|
||||
expect(file(plan(), "envs/grafana-example/Dockerfile")).not.toBe(
|
||||
dockerfile,
|
||||
);
|
||||
});
|
||||
|
||||
it("writes a verifier that checks the served tool count and the status the server answers the first parameterless GET tool with", () => {
|
||||
@@ -221,7 +189,6 @@ describe("planEnvironmentBundle", () => {
|
||||
expect(readme).toContain("```sh\nagent-env run .\n```");
|
||||
expect(readme).not.toContain("agent-env run grafana-example");
|
||||
expect(readme).toContain("## What the environment serves");
|
||||
expect(readme).not.toContain("What replay means");
|
||||
expect(readme).toContain(
|
||||
"write tools are served only where the capture's later reads confirmed\ntheir effect",
|
||||
);
|
||||
@@ -239,34 +206,24 @@ describe("planEnvironmentBundle", () => {
|
||||
});
|
||||
|
||||
describe("planEnvironmentBundle portable names", () => {
|
||||
it.each(["4294967296", "123", "0xffffffff"])(
|
||||
"refuses bundle name %s when HTTP cannot preserve its host identity",
|
||||
(name) => {
|
||||
expect(nameRefusal(name)).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
operation: "environment-export",
|
||||
issues: [{ path: ["name"], reason: "invalid_value" }],
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
it("refuses a hexadecimal IP spelling before preparing state", () => {
|
||||
expect(nameRefusal("0xffffffff")).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
operation: "environment-export",
|
||||
issues: [{ path: ["name"], reason: "invalid_value" }],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps addressable numeric prefixes and admits the DNS label boundary", () => {
|
||||
for (const name of ["123-replay", "0xffffffff-replay", "a".repeat(63)]) {
|
||||
expect(isPortableBundleName(name)).toBe(true);
|
||||
const planned = plan({ name });
|
||||
expect(planned.name).toBe(name);
|
||||
expect(file(planned, `envs/${name}/Dockerfile`)).toContain(
|
||||
`ENV MCP_ALLOWED_HOSTS=127.0.0.1,localhost,${name}`,
|
||||
);
|
||||
}
|
||||
expect(isPortableBundleName("a".repeat(64))).toBe(false);
|
||||
expect(nameRefusal("a".repeat(64))).toMatchObject({
|
||||
code: "invalid_request",
|
||||
});
|
||||
for (const name of ["123", "4294967296", "0xffffffff"])
|
||||
expect(isPortableBundleName(name)).toBe(false);
|
||||
});
|
||||
|
||||
it("accepts a kebab-case name override and refuses any other name as invalid input", () => {
|
||||
|
||||
@@ -11,6 +11,7 @@ import { join, sep } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { supportsNodeVersion } from "../../../src/domain/runtimeVersion.js";
|
||||
import {
|
||||
analysisViewCaptureEvidence,
|
||||
analysisViewCaptureFixture,
|
||||
@@ -79,7 +80,11 @@ cliTest(
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-bundle-hostname-cli-");
|
||||
const files = await writeEnvironmentServeFiles(root);
|
||||
for (const name of ["4294967296", "123", "a".repeat(64)]) {
|
||||
for (const [name, reason] of [
|
||||
["4294967296", "invalid_value"],
|
||||
["123", "invalid_value"],
|
||||
["a".repeat(64), "invalid_format"],
|
||||
] as const) {
|
||||
const directory = join(root, `bundle-${name}`);
|
||||
const result = await cli.run({
|
||||
arguments: [
|
||||
@@ -98,7 +103,7 @@ cliTest(
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
operation: "environment-export",
|
||||
issues: [{ path: ["name"] }],
|
||||
issues: [{ path: ["name"], reason }],
|
||||
},
|
||||
});
|
||||
await expect(stat(directory)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
@@ -172,10 +177,30 @@ cliTest(
|
||||
const pinnedNodeVersion = (
|
||||
await readFile(new URL("../../../.nvmrc", import.meta.url), "utf8")
|
||||
).trim();
|
||||
expect(dockerfile.split("\n")[0]).toBe(
|
||||
`FROM node:${pinnedNodeVersion}-bookworm-slim`,
|
||||
const packageManifest = JSON.parse(
|
||||
await readFile(new URL("../../../package.json", import.meta.url), "utf8"),
|
||||
) as { name: string; version: string };
|
||||
const lines = dockerfile.trimEnd().split("\n");
|
||||
expect(lines[0]).toBe(`FROM node:${pinnedNodeVersion}-bookworm-slim`);
|
||||
expect(supportsNodeVersion(pinnedNodeVersion)).toBe(true);
|
||||
expect(lines).toContain("ENV MCP_HOST=0.0.0.0");
|
||||
expect(lines).toContain("ENV MCP_PORT=18765");
|
||||
expect(lines).toContain(
|
||||
"ENV MCP_ALLOWED_HOSTS=127.0.0.1,localhost,grafana-example",
|
||||
);
|
||||
expect(dockerfile).toContain(
|
||||
expect(lines).toContain(
|
||||
"RUN apt-get update && apt-get install -y --no-install-recommends python3 && rm -rf /var/lib/apt/lists/*",
|
||||
);
|
||||
const install = `RUN npm install -g --ignore-scripts --no-fund --no-audit ${packageManifest.name}@${packageManifest.version}`;
|
||||
expect(
|
||||
lines.filter((line) => line.startsWith("RUN npm install -g")),
|
||||
).toEqual([install]);
|
||||
expect(lines).toContain(
|
||||
"COPY --chown=node:node manifest.json evidence.json /env/",
|
||||
);
|
||||
expect(lines.indexOf("USER node")).toBeGreaterThan(lines.indexOf(install));
|
||||
expect(dockerfile).not.toContain("rea-agents.tgz");
|
||||
expect(lines.at(-1)).toBe(
|
||||
'CMD ["rea","environment-serve","/env/manifest.json","--evidence","/env/evidence.json"]',
|
||||
);
|
||||
const verifier = await readFile(
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import {
|
||||
planEnvironmentBundle,
|
||||
registryPackageSource,
|
||||
} from "../../../src/application/environment/environmentBundle.js";
|
||||
import {
|
||||
environmentServeFixture,
|
||||
environmentServeServed,
|
||||
} from "../../fixtures/environmentServe.js";
|
||||
import { PRODUCT_IDENTITY } from "../../../src/identity.js";
|
||||
import { supportsNodeVersion } from "../../../src/domain/runtimeVersion.js";
|
||||
|
||||
const plannedDockerfile = (): string => {
|
||||
const { source, served } = environmentServeServed(environmentServeFixture());
|
||||
const planned = planEnvironmentBundle(source, {
|
||||
packageSource: registryPackageSource(),
|
||||
served: () => served,
|
||||
});
|
||||
if (!planned.ok) throw planned.error;
|
||||
const dockerfile = planned.value.files.find(({ path }) =>
|
||||
path.endsWith("/Dockerfile"),
|
||||
);
|
||||
if (dockerfile === undefined)
|
||||
throw new Error("Expected a planned Dockerfile");
|
||||
return dockerfile.content;
|
||||
};
|
||||
|
||||
/**
|
||||
* The exported Dockerfile installs the CLI that produced the bundle, so the
|
||||
* pin must be the exact package version of this build, read from the package
|
||||
* manifest rather than restated anywhere in source.
|
||||
*/
|
||||
describe("environment bundle package pin", () => {
|
||||
it("exports the pinned supported Node runtime in the Dockerfile", async () => {
|
||||
const pinned = (
|
||||
await readFile(new URL("../../../.nvmrc", import.meta.url), "utf8")
|
||||
).trim();
|
||||
const dockerfile = plannedDockerfile();
|
||||
const nodeVersion = /^FROM node:(\d+\.\d+\.\d+)(?:[-@][^\s]+)?$/u.exec(
|
||||
dockerfile.split("\n")[0] ?? "",
|
||||
)?.[1];
|
||||
expect(nodeVersion).toBe(pinned);
|
||||
expect(supportsNodeVersion(nodeVersion ?? "")).toBe(true);
|
||||
});
|
||||
|
||||
it("pins the registry install to the package.json version", async () => {
|
||||
const manifest = z
|
||||
.object({ name: z.literal("rea-agents"), version: z.string().min(1) })
|
||||
.parse(
|
||||
JSON.parse(
|
||||
await readFile(
|
||||
new URL("../../../package.json", import.meta.url),
|
||||
"utf8",
|
||||
),
|
||||
),
|
||||
);
|
||||
const dockerfile = plannedDockerfile();
|
||||
const install = dockerfile
|
||||
.split("\n")
|
||||
.find((line) => line.startsWith("RUN npm install -g"));
|
||||
expect(install).toBeDefined();
|
||||
expect(install?.endsWith(` ${manifest.name}@${manifest.version}`)).toBe(
|
||||
true,
|
||||
);
|
||||
expect(PRODUCT_IDENTITY.packageVersion).toBe(manifest.version);
|
||||
expect(dockerfile).not.toMatch(/rea-agents@(?:latest|\^|~)/u);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user