ci: 增加部署后发布验收脚本
This commit is contained in:
1 parent
85f37d2eae
commit
9c15a822e9
4 files changed
+125
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
function requiredEnvironment(name) {
|
||||
const value = process.env[name];
|
||||
if (!value) throw new Error(`${name} is required`);
|
||||
return value;
|
||||
}
|
||||
|
||||
async function requestJson(url, options = {}) {
|
||||
const response = await fetch(url, options);
|
||||
const body = await response.json().catch(() => null);
|
||||
assert.ok(
|
||||
response.ok && body,
|
||||
`${options.method ?? "GET"} ${url} failed with status ${response.status}`,
|
||||
);
|
||||
return body;
|
||||
}
|
||||
|
||||
const apiBaseUrl = requiredEnvironment("API_BASE_URL").replace(/\/$/, "");
|
||||
const brandSlug = requiredEnvironment("BRAND_SLUG");
|
||||
const username = requiredEnvironment("ADMIN_USERNAME");
|
||||
const password = requiredEnvironment("ADMIN_PASSWORD");
|
||||
|
||||
const health = await requestJson(`${apiBaseUrl}/health`);
|
||||
assert.equal(health.status, "ok");
|
||||
console.log(`health: ok (${health.service})`);
|
||||
|
||||
const session = await requestJson(`${apiBaseUrl}/admin/session/login`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
assert.equal(session.mustChangePassword, false, "admin must change the initial password");
|
||||
const token = session.token ?? session.accessToken;
|
||||
assert.equal(typeof token, "string", "login response does not contain a token");
|
||||
console.log("admin login: ok");
|
||||
|
||||
const readiness = await requestJson(`${apiBaseUrl}/admin/releases/readiness`, {
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
const missingChecks = readiness.checks.filter((check) => !check.passed);
|
||||
assert.deepEqual(
|
||||
missingChecks.map((check) => check.label),
|
||||
[],
|
||||
`release readiness failed: ${missingChecks.map((check) => check.label).join(", ")}`,
|
||||
);
|
||||
console.log(`release readiness: ok (${readiness.checks.length} checks)`);
|
||||
|
||||
const workspace = await requestJson(
|
||||
`${apiBaseUrl}/public/workspace?brandSlug=${encodeURIComponent(brandSlug)}`,
|
||||
);
|
||||
assert.equal(workspace.publication?.status, "published", "brand is not published");
|
||||
assert.ok(
|
||||
Array.isArray(workspace.offerings) && workspace.offerings.length > 0,
|
||||
"published workspace has no offerings",
|
||||
);
|
||||
console.log(
|
||||
`published workspace: ok (${brandSlug}, v${workspace.publication.version}, ${workspace.offerings.length} offerings)`,
|
||||
);
|
||||
@@ -0,0 +1,52 @@
|
||||
import test from "node:test";
|
||||
import { spawn } from "node:child_process";
|
||||
import { once } from "node:events";
|
||||
import { createServer } from "node:http";
|
||||
|
||||
function runScript(environment) {
|
||||
const child = spawn(process.execPath, ["scripts/deploy/verify-release.mjs"], {
|
||||
env: { ...process.env, ...environment },
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
return once(child, "close").then(([code]) => code);
|
||||
}
|
||||
|
||||
test("release verifier requires deployment variables", async () => {
|
||||
assertEqualExit(await runScript({}), 1);
|
||||
});
|
||||
|
||||
test("release verifier rejects an unpublished brand", async () => {
|
||||
const server = createServer((request, response) => {
|
||||
if (request.url === "/health") {
|
||||
response.writeHead(200, { "content-type": "application/json" });
|
||||
response.end(JSON.stringify({ status: "ok", service: "fixture" }));
|
||||
return;
|
||||
}
|
||||
if (request.url === "/admin/session/login") {
|
||||
response.writeHead(200, { "content-type": "application/json" });
|
||||
response.end(JSON.stringify({ token: "fixture-token", mustChangePassword: false }));
|
||||
return;
|
||||
}
|
||||
if (request.url === "/admin/releases/readiness") {
|
||||
response.writeHead(200, { "content-type": "application/json" });
|
||||
response.end(JSON.stringify({ checks: [{ key: "brand", passed: true }] }));
|
||||
return;
|
||||
}
|
||||
response.writeHead(200, { "content-type": "application/json" });
|
||||
response.end(JSON.stringify({ publication: { status: "unpublished" } }));
|
||||
});
|
||||
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const { port } = server.address();
|
||||
const exitCode = await runScript({
|
||||
API_BASE_URL: `http://127.0.0.1:${port}`,
|
||||
BRAND_SLUG: "fixture",
|
||||
ADMIN_USERNAME: "admin",
|
||||
ADMIN_PASSWORD: "fixture-password",
|
||||
});
|
||||
server.close();
|
||||
assertEqualExit(exitCode, 1);
|
||||
});
|
||||
|
||||
function assertEqualExit(actual, expected) {
|
||||
if (actual !== expected) throw new Error(`expected exit ${expected}, received ${actual}`);
|
||||
}
|
||||
Reference in new issue
Block a user