Files
mtusi2026lab3/student-tests/k3s-model.test.ts
T

289 lines
7.9 KiB
TypeScript

import { test, expect } from "bun:test";
import {
settings,
assertOwned,
guardedOperator,
pinnedConfigID,
assertScopedArgs,
assertNoGame,
assertPartialClaim,
assertNoRunningContainers,
withCleanup,
assertImageBudget,
} from "../scripts/k3s-model";
test("rejects an image API budget below the verified decoder limit", () => {
expect(() => assertImageBudget(4, 512, "384MiB")).toThrow();
});
test("accepts the verified image API budget", () => {
expect(() => assertImageBudget(4, 2048, "1536MiB")).not.toThrow();
});
test("allows smaller text-only API research budgets", () => {
expect(() => assertImageBudget(3, 512, "384MiB")).not.toThrow();
});
test("cleanup failure preserves the original operation error", async () => {
const primary = new Error("primary operation");
try {
await withCleanup({
perform: async () => {
throw primary;
},
cleanup: async () => {
throw new Error("cleanup");
},
});
throw new Error("unexpected success");
} catch (error) {
expect((error as Error).cause).toBe(primary);
}
});
test("cleanup failure cannot publish a successful operation", async () => {
await expect(
withCleanup({
perform: async () => 42,
cleanup: async () => {
throw new Error("cleanup");
},
}),
).rejects.toThrow("cleanup");
});
test("accepts the canonical Docker Hub form of a pinned repository", () => {
expect(
pinnedConfigID(
{
Id: "sha256:config",
RepoDigests: ["docker.io/library/postgres@sha256:manifest"],
},
"postgres:tag@sha256:manifest",
),
).toBe("sha256:config");
});
test.each([
{ name: "platform" },
{ name: "vp-lab4-medvedev" },
{ name: "../vp-lab3" },
{ name: "vp-lab3;echo" },
])("rejects foreign or malformed cluster $name", ({ name }) => {
expect(() => settings(3, { VP_CLUSTER: name })).toThrow();
});
test.each([
{ port: "0" },
{ port: "65536" },
{ port: "NaN" },
{ port: "28203;echo" },
])("rejects invalid public port $port", ({ port }) => {
expect(() => settings(3, { PUBLIC_PORT: port })).toThrow();
});
test("accepts default and isolated experiment names", () => {
expect(settings(3).cluster).toBe("vp-lab3-medvedev");
expect(settings(3, { VP_CLUSTER: "vp-lab3-r-1234abcd" }).cluster).toBe(
"vp-lab3-r-1234abcd",
);
});
const expected = { lab: 3, cluster: "vp-lab3-medvedev" };
const receipt = {
lab: 3,
cluster: expected.cluster,
container: "owned-container",
namespaceUID: "owned-namespace",
};
test("rejects an unclaimed cluster", () => {
expect(() =>
assertOwned(undefined, { container: "owned-container" }, expected),
).toThrow();
});
test("rejects a recreated foreign container", () => {
expect(() =>
assertOwned(
receipt,
{ container: "foreign-container", namespaceUID: "owned-namespace" },
expected,
),
).toThrow();
});
test("rejects a replaced namespace", () => {
expect(() =>
assertOwned(
receipt,
{ container: "owned-container", namespaceUID: "foreign-namespace" },
expected,
),
).toThrow();
});
test("accepts the exact claimed cluster and namespace", () => {
expect(() =>
assertOwned(
receipt,
{ container: "owned-container", namespaceUID: "owned-namespace" },
expected,
),
).not.toThrow();
});
test("foreign cluster never reaches the mutation port", async () => {
let dispatched = 0;
const operator = guardedOperator(expected, {
receipt: async () => receipt,
actual: async () => ({
container: "foreign-container",
namespaceUID: "owned-namespace",
}),
dispatch: async () => {
dispatched++;
},
});
await expect(operator.run("delete")).rejects.toThrow();
expect(dispatched).toBe(0);
});
test("inventory failure never reaches the mutation port", async () => {
let dispatched = 0;
const operator = guardedOperator(expected, {
receipt: async () => receipt,
actual: async () => {
throw new Error("inventory unavailable");
},
dispatch: async () => {
dispatched++;
},
});
await expect(operator.run("delete")).rejects.toThrow("inventory unavailable");
expect(dispatched).toBe(0);
});
test("claimed cluster dispatches the requested action once", async () => {
const actions: string[] = [];
const operator = guardedOperator(expected, {
receipt: async () => receipt,
actual: async () => ({
container: receipt.container,
namespaceUID: receipt.namespaceUID,
}),
dispatch: async (action) => {
actions.push(action);
},
});
await operator.run("restart");
expect(actions).toEqual(["restart"]);
});
test("accepts pinned manifest identity when image config ID differs", () => {
expect(
pinnedConfigID(
{ Id: "sha256:config", RepoDigests: ["example/image@sha256:manifest"] },
"example/image:tag@sha256:manifest",
),
).toBe("sha256:config");
});
test("rejects an unpinned image", () => {
expect(() =>
pinnedConfigID(
{ Id: "sha256:config", RepoDigests: ["example/image@sha256:other"] },
"example/image:tag@sha256:manifest",
),
).toThrow();
});
test.each([
{ args: ["get", "pods", "-nforeign"] },
{ args: ["get", "pods", "--cluster=foreign"] },
{ args: ["get", "nodes"] },
{ args: ["get", "secrets"] },
])("rejects target or resource override $args", ({ args }) => {
expect(() => assertScopedArgs(args)).toThrow();
});
test("allows flags belonging to the executed application", () => {
expect(() =>
assertScopedArgs([
"exec",
"deployment/postgres",
"--",
"psql",
"--user",
"messenger",
]),
).not.toThrow();
});
test("allows stdin for scoped PostgreSQL execution", () => {
expect(() =>
assertScopedArgs(["exec", "-i", "deployment/postgres", "--", "psql"]),
).not.toThrow();
});
test("rejects a second resource that discloses a Secret", () => {
expect(() =>
assertScopedArgs([
"get",
"pods/existing-api",
"secrets/app-config",
"-o",
"json",
]),
).toThrow();
});
test("rejects the short server override", () => {
expect(() =>
assertScopedArgs(["get", "pods", "-shttps://foreign"]),
).toThrow();
});
test.each([{ code: 0 }, { code: 2 }, { code: -1 }])(
"refuses measurement for active or unknown game status $code",
({ code }) => {
expect(() => assertNoGame(code)).toThrow();
},
);
test("accepts measurement only for confirmed inactive game", () => {
expect(() => assertNoGame(1)).not.toThrow();
});
test("rejects extra resources after delimiter outside exec", () => {
expect(() =>
assertScopedArgs([
"get",
"pods/existing",
"-o",
"json",
"--",
"secrets/app-config",
]),
).toThrow();
});
test("partial cleanup accepts only the exclusive server owner nonce", () => {
expect(() =>
assertPartialClaim(
{ ...receipt, state: "claimed", nonce: "ours" },
{ container: receipt.container, owner: "foreign" },
expected,
),
).toThrow();
});
test("partial cleanup never weakens a ready namespace guard", () => {
expect(() =>
assertPartialClaim(
{ ...receipt, state: "ready", nonce: "ours" },
{ container: receipt.container, owner: "ours" },
expected,
),
).toThrow();
});
test("partial cleanup accepts an owned server before namespace creation", () => {
expect(() =>
assertPartialClaim(
{ lab: 3, cluster: expected.cluster, state: "claimed", nonce: "ours" },
{ container: "fresh-server", owner: "ours" },
expected,
),
).not.toThrow();
});
test("rejects a running replacement process despite stale Kubernetes status", () => {
expect(() =>
assertNoRunningContainers(
[{ id: "new-process", labels: { "io.kubernetes.pod.uid": "victim" } }],
"victim",
),
).toThrow();
});
test("accepts native absence of the victim process", () => {
expect(() =>
assertNoRunningContainers(
[{ id: "survivor", labels: { "io.kubernetes.pod.uid": "other" } }],
"victim",
),
).not.toThrow();
});