Files
clean-architecture-frontend…/tests/unit/product-ci-overlay.test.ts
T
DongHyeonka 92d5019c6d fix(ci): 제품 merge 게이트가 실패하면 같은 run의 release 단계를 멈춘다
문제
제품 overlay(config/ci/product-gates.json)의 mergeGates는 product_merge_gate
job으로 렌더링되는데, 다른 job이 이 job을 기다리지 않는다. release_gate는
merge_gate만 기다린다. 그래서 제품 검사(예: 짝 백엔드 계약 검사)가 실패해도
같은 run의 release·immutable_build·promotion·deployment는 그대로 돈다. 브랜치
필수 검사는 PR merge만 막는다. job 그래프는 계약이 고정해서(ci-gates.ts의
expectedJobGraph) 제품이 연결을 더할 방법도 없다.

고른 방법
- overlay에 게이트가 있으면 release_gate의 needs에 product_merge_gate를 더해
  렌더링한다(`needs: [merge_gate, product_merge_gate]`). release_gate는 release
  단계의 첫 job이고 뒤의 job은 모두 그것을 거쳐 기다린다. 템플릿 게이트가
  release를 막는 방식과 같다.
- 연결은 렌더링에서만 더한다. gates.json과 계약 그래프 검사는 그대로라 템플릿
  job 그래프와 check:ci의 엄격함은 바뀌지 않는다. 게이트가 없으면 workflow
  바이트가 같다(.gitea/workflows/quality-gates.yml 변경 없음).
- workflowGateExecutionFailures(check:ci가 부름)가 제품 게이트가 있는데
  release_gate가 product_merge_gate를 기다리지 않는 workflow를 거절한다.
  "실행된다"와 "막는다"를 따로 본다.
- 문서(ci-quality-gates.md, README)에 release 단계가 제품 job을 기다린다고
  적었다.
- 버린 대안: (a) 문서에만 "막지 않는다"고 적기. 제품이 막을 방법이 계속 없다.
  (c) 게이트마다 blocksRelease 칸. 선택권은 주지만 칸이 늘고, 불안정한 제품
  검사가 생기기 전에는 필요가 없다. 계약 그래프 검사에 overlay 예외를 넣는 것:
  템플릿 계약이 제품 overlay를 알아야 한다.

위험·한계
- 불안정한 제품 게이트는 이제 release도 멈춘다. 그때는 (c)로 간다.
- Gitea가 needs와 기본 success() 조건을 GitHub Actions처럼 처리한다고 본다.
  실제 runner에서는 확인하지 못했다(Actions 꺼짐).
- 제품 게이트를 이미 쓰는 다운스트림은 generate:ci-workflow를 다시 돌려야
  check:ci를 통과한다(생성 YAML의 release_gate needs 한 줄이 바뀐다).

검증
- desktop batch에서 확인: node scripts/run-vitest.ts run
  tests/unit/product-ci-overlay.test.ts tests/unit/ci-workflow-generation.test.ts,
  corepack pnpm check:ci.
- RED 기대: "holds the release stage until the product merge gates pass",
  "runs product merge gates in a job of their own, apart from the template gates".

Refs: 1105/13-02
2026-11-05 22:41:50 +09:00

222 lines
9.5 KiB
TypeScript

import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { loadCiGateContract } from "../../scripts/contracts/ci-gates.ts";
import {
loadProductCiOverlay,
parseProductCiOverlay,
PRODUCT_CI_OVERLAY_PATH,
} from "../../scripts/contracts/product-ci-overlay.ts";
import { renderCiWorkflow, workflowGateExecutionFailures } from "../../scripts/generate-ci-workflow.ts";
/**
* 1103/12-07. A product attached its deployment and its own merge checks by
* editing the template-owned config/ci/gates.json (3,000 lines) and the
* generated workflow, and met them again in every template sync. The product
* now writes them in config/ci/product-gates.json, which the template ships
* empty and never changes.
*/
const roots: string[] = [];
afterEach(async () => {
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
});
async function productRoot(
overlay: unknown,
extraScripts: Readonly<Record<string, string>> = {},
): Promise<string> {
const root = await mkdtemp(path.join(tmpdir(), "product-ci-overlay-"));
roots.push(root);
await mkdir(path.join(root, "config/ci"), { recursive: true });
for (const file of ["config/ci/gates.json", "config/ci/contract-baseline.json"]) {
await writeFile(path.join(root, file), await readFile(file));
}
const packageDocument = JSON.parse(await readFile("package.json", "utf8")) as {
scripts: Record<string, string>;
};
Object.assign(packageDocument.scripts, extraScripts);
await writeFile(path.join(root, "package.json"), JSON.stringify(packageDocument, null, 2));
await writeFile(path.join(root, PRODUCT_CI_OVERLAY_PATH), JSON.stringify(overlay, null, 2));
return root;
}
const DEPLOYMENT = {
script: "deploy:desktop",
environment: [
{ name: "DEPLOY_HOST", value: "${{ vars.DEPLOY_HOST }}" },
{ name: "DEPLOY_SSH_KEY", value: "${{ secrets.DEPLOY_SSH_KEY }}" },
],
};
const DEPLOY_SCRIPTS = { "deploy:desktop": "node scripts/deploy-desktop.ts" };
const GATE = { id: "PRODUCT-GATE-001", name: "backend-contract", script: "check:backend-contract" };
const GATE_SCRIPTS = { "check:backend-contract": "node scripts/check-backend-contract.ts" };
const RELEASE_HELD_BY_TEMPLATE = " needs: merge_gate\n";
const RELEASE_HELD_BY_PRODUCT = " needs: [merge_gate, product_merge_gate]\n";
describe("product CI overlay (1103/12-07)", () => {
it("ships empty, so the template's own workflow is unchanged", async () => {
expect(await loadProductCiOverlay(process.cwd())).toEqual({
schemaVersion: 1,
deployment: null,
mergeGates: [],
});
});
it("fills the deployment slot with the promoted candidate and the product's script", async () => {
const root = await productRoot(
{ schemaVersion: 1, deployment: DEPLOYMENT, mergeGates: [] },
DEPLOY_SCRIPTS,
);
// The template baseline (config/ci/contract-baseline.json) is unchanged.
const contract = await loadCiGateContract(root);
const slot = contract.jobs.find(({ id }) => id === "deployment");
expect(slot?.steps.map(({ kind }) => kind)).toEqual([
"checkout",
"setup-node",
"frozen-install",
"download",
"extract",
"deploy",
]);
expect(slot?.steps).toContainEqual(expect.objectContaining({ kind: "download", transferId: "promoted-release" }));
expect(slot?.steps.at(-1)).toEqual({ kind: "deploy", script: "deploy:desktop" });
expect(slot?.environment).toEqual([
{ name: "CANDIDATE_ARCHIVE_SHA256", value: "${{ needs.immutable_build.outputs.archive_sha256 }}" },
...DEPLOYMENT.environment,
]);
const workflow = renderCiWorkflow(contract);
const job = workflow.slice(workflow.indexOf(" deployment:\n"), workflow.indexOf(" production_gate:\n"));
expect(job).toContain("corepack pnpm deploy:desktop");
expect(job.match(/secrets\./gu)).toHaveLength(1);
});
it("refuses a slot filled in the template contract and in the overlay", async () => {
const root = await productRoot(
{ schemaVersion: 1, deployment: DEPLOYMENT, mergeGates: [] },
DEPLOY_SCRIPTS,
);
const gatesPath = path.join(root, "config/ci/gates.json");
const gates = JSON.parse(await readFile(gatesPath, "utf8")) as {
jobs: { id: string; steps: unknown[]; environment: unknown[] }[];
};
const slot = gates.jobs.find(({ id }) => id === "deployment")!;
slot.environment = [
{ name: "CANDIDATE_ARCHIVE_SHA256", value: "${{ needs.immutable_build.outputs.archive_sha256 }}" },
];
slot.steps = [
{ kind: "download", transferId: "promoted-release", path: ".release/promoted" },
{
kind: "extract",
archivePath: ".release/promoted/release-candidate.tar.gz",
targetRoot: ".release/promoted-release",
},
{ kind: "deploy", script: "deploy:desktop" },
];
await writeFile(gatesPath, JSON.stringify(gates));
await expect(loadCiGateContract(root)).rejects.toThrow(/product-gates\.json/u);
});
it("refuses an overlay script that package.json does not define", async () => {
const root = await productRoot({ schemaVersion: 1, deployment: DEPLOYMENT, mergeGates: [GATE] });
await expect(loadCiGateContract(root)).rejects.toThrow(
/missing package scripts: deploy:desktop, check:backend-contract/u,
);
});
it("runs product merge gates in a job of their own, apart from the template gates", async () => {
const root = await productRoot(
{ schemaVersion: 1, deployment: null, mergeGates: [GATE] },
GATE_SCRIPTS,
);
const contract = await loadCiGateContract(root);
const { mergeGates } = await loadProductCiOverlay(root);
const workflow = renderCiWorkflow(contract, mergeGates);
const job = workflow.slice(workflow.indexOf(" product_merge_gate:\n"));
expect(job).toContain(' - { gate: PRODUCT-GATE-001, name: backend-contract, script: "check:backend-contract" }');
expect(job).toContain(" run: corepack pnpm ${{ matrix.script }}");
expect(job).toContain(` if: ${"${{ gitea.event_name != 'workflow_dispatch' || inputs.stage != 'documentation' }}"}`);
expect(workflowGateExecutionFailures(contract, workflow, mergeGates)).toEqual([]);
// The template part of the workflow is the same bytes with or without the
// overlay, except that the release stage waits for the product job (1105/13-02).
expect(
workflow.replace(RELEASE_HELD_BY_PRODUCT, RELEASE_HELD_BY_TEMPLATE).startsWith(renderCiWorkflow(contract).trimEnd()),
).toBe(true);
expect(workflowGateExecutionFailures(contract, renderCiWorkflow(contract), mergeGates)).toEqual([
"product gate is not executed by the product_merge_gate job: PRODUCT-GATE-001",
"product gates do not hold the release stage: release_gate does not need product_merge_gate",
]);
});
// 1105/13-02. A failing product gate stopped only a PR merge (as a required
// check): the same run went on to release, promote and deploy, and the fixed
// job graph left the product no way to link them.
it("holds the release stage until the product merge gates pass", async () => {
const root = await productRoot(
{ schemaVersion: 1, deployment: DEPLOYMENT, mergeGates: [GATE] },
{ ...DEPLOY_SCRIPTS, ...GATE_SCRIPTS },
);
const contract = await loadCiGateContract(root);
const { mergeGates } = await loadProductCiOverlay(root);
const releaseJob = (workflow: string) =>
workflow.slice(workflow.indexOf(" release_gate:\n"), workflow.indexOf(" immutable_build:\n"));
const workflow = renderCiWorkflow(contract, mergeGates);
expect(releaseJob(workflow)).toContain(RELEASE_HELD_BY_PRODUCT);
// immutable_build, and through it every later release-stage job
// (deployment included), needs release_gate.
expect(contract.jobs.find(({ id }) => id === "immutable_build")?.needs).toEqual(["release_gate"]);
// Without product gates the template's own graph is rendered.
expect(releaseJob(renderCiWorkflow(contract))).toContain(RELEASE_HELD_BY_TEMPLATE);
// check:ci refuses a workflow that runs the product gates without the link.
expect(
workflowGateExecutionFailures(
contract,
workflow.replace(RELEASE_HELD_BY_PRODUCT, RELEASE_HELD_BY_TEMPLATE),
mergeGates,
),
).toEqual(["product gates do not hold the release stage: release_gate does not need product_merge_gate"]);
});
it.each([
["a template gate id", { mergeGates: [{ ...GATE, id: "FE-GATE-030" }] }, /PRODUCT-GATE-NNN/u],
["a duplicate gate id", { mergeGates: [GATE, { ...GATE, name: "other" }] }, /duplicate product gate/u],
[
"a binding to another job's output",
{
deployment: {
...DEPLOYMENT,
environment: [{ name: "NONCE", value: "${{ needs.promotion.outputs.staging_root }}" }],
},
},
/vars\.\* or secrets\.\*/u,
],
[
"the candidate digest the template binds",
{
deployment: {
...DEPLOYMENT,
environment: [{ name: "CANDIDATE_ARCHIVE_SHA256", value: "${{ vars.DIGEST }}" }],
},
},
/CANDIDATE_ARCHIVE_SHA256/u,
],
["a script that is not a plain name", { deployment: { ...DEPLOYMENT, script: "deploy; rm -rf ." } }, /script/u],
["an unknown field", { gates: [] }, /unrecognized|Unrecognized/u],
] as const)("refuses %s", (_name, change, diagnostic) => {
expect(() =>
parseProductCiOverlay({ schemaVersion: 1, deployment: null, mergeGates: [], ...change }),
).toThrow(diagnostic);
});
});