peppa/packages/testkit/test/act-runner.test.ts

98 lines
5.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-205: the checks executor (docker container) is restarted by the watchdog when it is down.
import assert from "node:assert/strict";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { after, beforeEach, describe, test } from "node:test";
import { db, runner, teardown } from "./helpers.ts";
import { core } from "./helpers.ts";
const ROOT = resolve(fileURLToPath(import.meta.url), "../../../..");
const state = mkdtempSync(join(tmpdir(), "mock-docker-act-"));
mkdirSync(join(state, "containers"), { recursive: true });
process.env.PEPPA_DOCKER_BIN = join(ROOT, "packages/testkit/bin/mock-docker");
process.env.MOCK_DOCKER_STATE = state;
process.env.PEPPA_ACT_RUNNER_CONTAINER = "mock-act-runner";
process.env.PEPPA_SECRET = "mock-secret-for-tests";
after(teardown);
const set = (v: string) => writeFileSync(join(state, "containers/mock-act-runner"), `${v}\n`);
const restarts = () => (existsSync(join(state, "restarted")) ? readFileSync(join(state, "restarted"), "utf8").trim().split("\n").filter(Boolean) : []);
beforeEach(async () => {
const { settings } = await import("@peppa/db");
const { eq } = await import("drizzle-orm");
await db.delete(settings).where(eq(settings.key, "actrunner.restarted"));
});
describe("Исполнитель проверок под присмотром", () => {
test("Работает — не трогаем", async () => {
set("running");
assert.equal(await runner.actRunnerTick(db), null);
assert.equal(restarts().length, 0);
});
test("Остановлен — перезапущен, повтор не чаще раза в минуту", async () => {
set("exited");
const now = new Date();
assert.equal(await runner.actRunnerTick(db, { now }), "down");
assert.equal(restarts().length, 1);
set("exited");
assert.equal(await runner.actRunnerTick(db, { now: new Date(now.getTime() + 10_000) }), null, "not again at once");
assert.equal(restarts().length, 1);
});
test("Нет такого контейнера на этой машине — молчим", async () => {
const { rmSync } = await import("node:fs");
rmSync(join(state, "containers/mock-act-runner"), { force: true });
assert.equal(await runner.actRunnerTick(db), null);
});
test("Работает, но очередь стоит без единого задания — один перезапуск", async () => {
set("running");
const now = Date.now();
await core.setSetting(db, "checks.wait:999", { sha: "a", since: now - 20 * 60_000, stuck: true, told: true, cause: "no_job" });
assert.equal(await runner.actRunnerTick(db, { now: new Date(now) }), "hung");
const n = restarts().length;
assert.equal(await runner.actRunnerTick(db, { now: new Date(now + 5 * 60_000) }), null, "not more often than every half hour");
assert.equal(restarts().length, n);
await core.deleteSetting(db, "checks.wait:999");
});
// PEP-233
test("Задания идут — исполнитель не перезапускается", async () => {
set("running");
const now = Date.now();
writeFileSync(join(state, "ps"), "abc123\tGITEA-ACTIONS-TASK-41_WORKFLOW-deploy_JOB-deploy\t\t\n");
await core.setSetting(db, "checks.wait:999", { sha: "a", since: now - 20 * 60_000, stuck: true, told: true, cause: "no_job" });
const n = restarts().length;
assert.equal(await runner.actRunnerTick(db, { now: new Date(now) }), null);
assert.equal(restarts().length, n, "running jobs are not cut");
const { rmSync } = await import("node:fs");
rmSync(join(state, "ps"), { force: true });
assert.equal(await runner.actRunnerTick(db, { now: new Date(now) }), "hung", "no jobs — restarted");
await core.deleteSetting(db, "checks.wait:999");
});
test("Та же стоящая голова — один перезапуск; новая голова — снова", async () => {
set("running");
const now = Date.now();
await core.setSetting(db, "checks.wait:999", { sha: "a", since: now - 20 * 60_000, stuck: true, told: true, cause: "no_job" });
assert.equal(await runner.actRunnerTick(db, { now: new Date(now) }), "hung");
const n = restarts().length;
assert.equal(await runner.actRunnerTick(db, { now: new Date(now + 60 * 60_000) }), null, "the same head after an hour — no second restart");
assert.equal(restarts().length, n);
await core.setSetting(db, "checks.wait:998", { sha: "b", since: now + 40 * 60_000, stuck: true, told: true, cause: "no_job" });
assert.equal(await runner.actRunnerTick(db, { now: new Date(now + 61 * 60_000) }), "hung", "a new head without a job — a new sign");
await core.deleteSetting(db, "checks.wait:999");
await core.deleteSetting(db, "checks.wait:998");
});
test("Голова без проверок — не повод для перезапуска", async () => {
set("running");
const now = Date.now();
await core.setSetting(db, "checks.wait:999", { sha: "a", since: now - 20 * 60_000, stuck: true, told: true, cause: "no_workflow", orphan: true });
assert.equal(await runner.actRunnerTick(db, { now: new Date(now) }), null);
await core.deleteSetting(db, "checks.wait:999");
});
});