185 lines
6.2 KiB
TypeScript
185 lines
6.2 KiB
TypeScript
import { describe, expect, test, vi } from "vitest";
|
|
import { runWorkerDeployHealthCli } from "./deploy-health.js";
|
|
|
|
function createIo() {
|
|
let stderr = "";
|
|
let stdout = "";
|
|
return {
|
|
io: {
|
|
stderr: {
|
|
write(chunk: string) {
|
|
stderr += chunk;
|
|
return true;
|
|
},
|
|
},
|
|
stdout: {
|
|
write(chunk: string) {
|
|
stdout += chunk;
|
|
return true;
|
|
},
|
|
},
|
|
},
|
|
readStderr: () => stderr,
|
|
readStdout: () => stdout,
|
|
};
|
|
}
|
|
|
|
function readyResponse(group = "general", overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
status: 200,
|
|
json: vi.fn(async () => ({
|
|
status: "ready",
|
|
workerGroup: group,
|
|
checks: { redis: "ok", database: "ok" },
|
|
...overrides,
|
|
})),
|
|
};
|
|
}
|
|
|
|
function enabledEnv(revision: string, group = "general") {
|
|
return {
|
|
APP_REVISION: revision,
|
|
WORKER_ENABLED: "true",
|
|
WORKER_HEALTH_PORT: "9101",
|
|
WORKER_QUEUE_GROUP: group,
|
|
};
|
|
}
|
|
|
|
describe("worker deployment health", () => {
|
|
test("probes the live worker readiness endpoint for the exact baked revision and group", async () => {
|
|
const revision = "a".repeat(40);
|
|
const fetchReadiness = vi.fn(async () => readyResponse());
|
|
const { io, readStderr, readStdout } = createIo();
|
|
|
|
const exitCode = await runWorkerDeployHealthCli(revision, "general", enabledEnv(revision), io, {
|
|
fetchReadiness,
|
|
readImageRevision: () => revision,
|
|
timeoutSignal: () => new AbortController().signal,
|
|
});
|
|
|
|
expect(exitCode).toBe(0);
|
|
expect(JSON.parse(readStdout())).toEqual({ status: "ready", revision, workerGroup: "general" });
|
|
expect(readStderr()).toBe("");
|
|
expect(fetchReadiness).toHaveBeenCalledWith(
|
|
"http://127.0.0.1:9101/readyz",
|
|
expect.objectContaining({ headers: { Accept: "application/json" }, signal: expect.any(AbortSignal) })
|
|
);
|
|
});
|
|
|
|
test("rejects an environment override that disagrees with the baked image revision", async () => {
|
|
const expectedRevision = "b".repeat(40);
|
|
const fetchReadiness = vi.fn();
|
|
const { io, readStderr, readStdout } = createIo();
|
|
|
|
const exitCode = await runWorkerDeployHealthCli(expectedRevision, "general", enabledEnv(expectedRevision), io, {
|
|
fetchReadiness,
|
|
readImageRevision: () => "a".repeat(40),
|
|
});
|
|
|
|
expect(exitCode).toBe(1);
|
|
expect(readStdout()).toBe("");
|
|
expect(readStderr()).toMatch(/revision does not match/);
|
|
expect(readStderr()).not.toContain(expectedRevision);
|
|
expect(fetchReadiness).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("rejects a disabled worker before probing the runtime", async () => {
|
|
const revision = "a".repeat(40);
|
|
const fetchReadiness = vi.fn();
|
|
const { io, readStderr, readStdout } = createIo();
|
|
|
|
const exitCode = await runWorkerDeployHealthCli(
|
|
revision,
|
|
"general",
|
|
{ ...enabledEnv(revision), WORKER_ENABLED: "false" },
|
|
io,
|
|
{ fetchReadiness, readImageRevision: () => revision }
|
|
);
|
|
|
|
expect(exitCode).toBe(1);
|
|
expect(readStdout()).toBe("");
|
|
expect(readStderr()).toMatch(/processing to be enabled/);
|
|
expect(fetchReadiness).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("rejects a resource assigned to the wrong queue group", async () => {
|
|
const revision = "a".repeat(40);
|
|
const fetchReadiness = vi.fn();
|
|
const { io, readStderr, readStdout } = createIo();
|
|
|
|
const exitCode = await runWorkerDeployHealthCli(revision, "mail", enabledEnv(revision, "general"), io, {
|
|
fetchReadiness,
|
|
readImageRevision: () => revision,
|
|
});
|
|
|
|
expect(exitCode).toBe(1);
|
|
expect(readStdout()).toBe("");
|
|
expect(readStderr()).toMatch(/group does not match/);
|
|
expect(readStderr()).not.toContain("mail");
|
|
expect(readStderr()).not.toContain("general");
|
|
expect(fetchReadiness).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("rejects a readiness response for a different runtime group", async () => {
|
|
const revision = "a".repeat(40);
|
|
const { io, readStderr, readStdout } = createIo();
|
|
|
|
const exitCode = await runWorkerDeployHealthCli(revision, "general", enabledEnv(revision), io, {
|
|
fetchReadiness: vi.fn(async () => readyResponse("mail")),
|
|
readImageRevision: () => revision,
|
|
timeoutSignal: () => new AbortController().signal,
|
|
});
|
|
|
|
expect(exitCode).toBe(1);
|
|
expect(readStdout()).toBe("");
|
|
expect(readStderr()).toMatch(/response does not match/);
|
|
});
|
|
|
|
test("rejects unhealthy, malformed, or unreachable readiness responses", async () => {
|
|
const revision = "a".repeat(40);
|
|
const scenarios = [
|
|
vi.fn(async () => ({ status: 503, json: vi.fn() })),
|
|
vi.fn(async () => readyResponse("general", { checks: { redis: "ok", database: "error" } })),
|
|
vi.fn(async () => readyResponse("general", { checks: { first: "ok", second: "ok" } })),
|
|
vi.fn(async () => readyResponse("general", { checks: ["ok", "ok"] })),
|
|
vi.fn(async () => {
|
|
throw new Error("connection failed with secret details");
|
|
}),
|
|
];
|
|
|
|
for (const fetchReadiness of scenarios) {
|
|
const { io, readStderr, readStdout } = createIo();
|
|
const exitCode = await runWorkerDeployHealthCli(revision, "general", enabledEnv(revision), io, {
|
|
fetchReadiness,
|
|
readImageRevision: () => revision,
|
|
timeoutSignal: () => new AbortController().signal,
|
|
});
|
|
|
|
expect(exitCode).toBe(1);
|
|
expect(readStdout()).toBe("");
|
|
expect(readStderr()).not.toContain("secret details");
|
|
}
|
|
});
|
|
|
|
test("rejects malformed revision, group, and health port arguments", async () => {
|
|
const revision = "a".repeat(40);
|
|
const cases = [
|
|
["not-a-sha", "general", enabledEnv(revision)],
|
|
[revision, "unknown-group", enabledEnv(revision)],
|
|
[revision, "general", { ...enabledEnv(revision), WORKER_HEALTH_PORT: "70000" }],
|
|
] as const;
|
|
|
|
for (const [expectedRevision, expectedGroup, env] of cases) {
|
|
const { io, readStderr, readStdout } = createIo();
|
|
const exitCode = await runWorkerDeployHealthCli(expectedRevision, expectedGroup, env, io, {
|
|
readImageRevision: () => revision,
|
|
});
|
|
|
|
expect(exitCode).toBe(1);
|
|
expect(readStdout()).toBe("");
|
|
expect(readStderr()).not.toContain("not-a-sha");
|
|
expect(readStderr()).not.toContain("unknown-group");
|
|
}
|
|
});
|
|
});
|