返回 CodeWhale
verify-release-inventory.test.js
根目录 / scripts / release / verify-release-inventory.test.js
1 #!/usr/bin/env node
2
3 const assert = require("node:assert/strict");
4 const crypto = require("node:crypto");
5 const fs = require("node:fs");
6 const os = require("node:os");
7 const path = require("node:path");
8 const test = require("node:test");
9
10 const { allReleaseAssetNames, CHECKSUM_MANIFEST, CHECKSUM_SIGNATURE } = require("../../npm/codewhale/scripts/artifacts");
11 const compiledHosts = require("../../npm/codewhale/scripts/compiled-hosts");
12 const { run } = require("./verify-release-inventory");
13
14 const REPO = "codewhale-hq/CodeWhale";
15 const TAG = "v0.10.1";
16
17 function localAssets(t) {
18 const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cw-release-inventory-"));
19 t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
20 for (const [index, name] of allReleaseAssetNames().entries()) fs.writeFileSync(path.join(dir, name), "x".repeat(index + 1));
21 return dir;
22 }
23
24 function uploaded(names, sizeOf = (index) => index + 1) {
25 return names.map((name, index) => ({ name, state: "uploaded", size: sizeOf(index),
26 digest: `sha256:${crypto.hash("sha256", "x".repeat(sizeOf(index)))}` }));
27 }
28
29 /** A gh double over one repository's releases; records every call. */
30 function fakeGh(releases, { manifest = "", pages = [releases] } = {}) {
31 const calls = [];
32 const gh = (args) => {
33 calls.push(args.join(" "));
34 if (args[0] === "release" && args[1] === "download") return manifest;
35 const [, method, endpoint] = args[1] === "-X" ? [null, args[2], args[3]] : [null, "GET", args[1]];
36 if (method === "PATCH") {
37 const id = Number(endpoint.split("/").pop());
38 for (const release of releases) if (release.id === id) release.draft = false;
39 return "{}";
40 }
41 if (endpoint.startsWith(`repos/${REPO}/releases/tags/`)) {
42 const found = releases.find((release) => release.tag_name === TAG && !release.draft);
43 if (!found) throw new Error("gh api failed: HTTP 404");
44 return JSON.stringify(found);
45 }
46 if (endpoint.startsWith(`repos/${REPO}/releases?`)) {
47 assert.ok(args.includes("--paginate") && args.includes("--slurp"));
48 return JSON.stringify(pages);
49 }
50 throw new Error(`unexpected gh call: ${args.join(" ")}`);
51 };
52 return { gh, calls };
53 }
54
55 test("a complete draft is published at once, only after its inventory verifies", (t) => {
56 const dir = localAssets(t);
57 const releases = [{ id: 7, tag_name: TAG, draft: true, assets: uploaded(allReleaseAssetNames()) }];
58 const { gh, calls } = fakeGh(releases);
59 run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], gh, () => {});
60 assert.equal(releases[0].draft, false);
61 const patch = calls.findIndex((call) => call.startsWith("api -X PATCH"));
62 assert.ok(patch > 0, "publishing follows the draft lookup");
63 assert.equal(calls[patch], `api -X PATCH repos/${REPO}/releases/7 -F draft=false`);
64 });
65
66 test("a partial, unfinished or resized draft is never published", (t) => {
67 const dir = localAssets(t);
68 const names = allReleaseAssetNames();
69 const cases = [
70 ["missing", uploaded(names.slice(0, -1)), /missing: /],
71 ["starter", uploaded(names).map((asset, index) => (index === 3 ? { ...asset, state: "starter" } : asset)), /not fully uploaded/],
72 ["size", uploaded(names, () => 1), /bytes; the verified local asset has/],
73 ["unexpected", [...uploaded(names), { name: "stray.txt", state: "uploaded", size: 1 }], /unexpected: stray\.txt/],
74 ];
75 for (const [label, assets, message] of cases) {
76 const releases = [{ id: 7, tag_name: TAG, draft: true, assets }];
77 const { gh, calls } = fakeGh(releases);
78 assert.throws(() => run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], gh, () => {}), message, label);
79 assert.equal(releases[0].draft, true, label);
80 assert.equal(calls.some((call) => call.includes("PATCH")), false, label);
81 }
82 });
83
84 test("zero or several drafts for the tag need a maintainer, not a guess", () => {
85 for (const drafts of [[], [1, 2]]) {
86 const releases = drafts.map((id) => ({ id, tag_name: TAG, draft: true, assets: uploaded(allReleaseAssetNames()) }));
87 const { gh } = fakeGh(releases);
88 assert.throws(() => run(["--draft", REPO, TAG], gh, () => {}), /Expected exactly one draft release/);
89 }
90 });
91
92 test("republish derives only from a release carrying exactly its own checksum manifest", () => {
93 const names = ["codewhale-linux-x64", "codewhale-macos-arm64"];
94 const manifest = names.map((name, index) => `${crypto.hash("sha256", "x".repeat(index + 1))} ${name}`).join("\n");
95 const complete = [{ id: 3, tag_name: TAG, draft: false, assets: uploaded([...names, CHECKSUM_MANIFEST]) }];
96 assert.doesNotThrow(() => run(["--manifest", REPO, TAG], fakeGh(complete, { manifest }).gh, () => {}));
97 const partial = [{ id: 3, tag_name: TAG, draft: false, assets: uploaded([names[0], CHECKSUM_MANIFEST]) }];
98 assert.throws(() => run(["--manifest", REPO, TAG], fakeGh(partial, { manifest }).gh, () => {}), /missing: codewhale-macos-arm64/);
99 });
100
101 test("usage refuses publish without draft and manifest with a local directory", () => {
102 assert.throws(() => run(["--publish", REPO, TAG], () => "[]", () => {}), /Usage/);
103 assert.throws(() => run(["--draft", "--publish", REPO, TAG], () => "[]", () => {}), /Usage/);
104 assert.throws(() => run(["--manifest", "--asset-dir", "x", REPO, TAG], () => "[]", () => {}), /Usage/);
105 });
106
107 test("same-size stale bytes and missing digests cannot publish a draft", (t) => {
108 const dir = localAssets(t);
109 for (const digest of [`sha256:${crypto.hash("sha256", "y")}`, null]) {
110 const assets = uploaded(allReleaseAssetNames());
111 assets[0].digest = digest;
112 const releases = [{ id: 7, tag_name: TAG, draft: true, assets }];
113 const { gh, calls } = fakeGh(releases);
114 assert.throws(() => run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], gh, () => {}), /SHA-256 digest/);
115 assert.equal(releases[0].draft, true);
116 assert.equal(calls.some((call) => call.includes("PATCH")), false);
117 }
118 });
119
120 test("drafts beyond the first release page are verified and duplicates refused", (t) => {
121 const dir = localAssets(t);
122 const draft = { id: 7, tag_name: TAG, draft: true, assets: uploaded(allReleaseAssetNames()) };
123 const older = Array.from({ length: 100 }, (_, id) => ({ id: 100 + id, tag_name: `v0.9.${id}`, draft: false }));
124 const { gh } = fakeGh([draft], { pages: [older, [draft]] });
125 run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], gh, () => {});
126 assert.equal(draft.draft, false);
127 draft.draft = true;
128 const duplicate = { ...draft, id: 8 };
129 const second = fakeGh([draft, duplicate], { pages: [[draft], [duplicate]] });
130 assert.throws(() => run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], second.gh, () => {}), /found 2/);
131 assert.equal(second.calls.some((call) => call.includes("PATCH")), false);
132 });
133
134 function uploadedFromDir(dir, names) {
135 return names.map((name) => {
136 const bytes = fs.readFileSync(path.join(dir, name));
137 return {
138 name,
139 state: "uploaded",
140 size: bytes.length,
141 digest: `sha256:${crypto.hash("sha256", bytes)}`,
142 };
143 });
144 }
145
146 function stageCompiledHostCatalog(dir, version = "0.10.1") {
147 const target = "linux-x64";
148 const names = compiledHosts.names(target);
149 const payloads = {
150 [names.binary]: Buffer.from("tested-image"),
151 [names.notices]: Buffer.from("notices-fixture"),
152 [names.source]: Buffer.from("source-fixture"),
153 };
154 const source = "b".repeat(40);
155 const bundle = "a".repeat(64);
156 const catalog = {
157 schema: 1,
158 version,
159 source_sha: source,
160 bundle_sha256: bundle,
161 hosts: [
162 {
163 target,
164 asset: names.binary,
165 sha256: compiledHosts.sha256(payloads[names.binary]),
166 notices_asset: names.notices,
167 notices_sha256: compiledHosts.sha256(payloads[names.notices]),
168 source_asset: names.source,
169 source_sha256: compiledHosts.sha256(payloads[names.source]),
170 runtime_version: "1.4.0",
171 runtime_revision: "c".repeat(40),
172 runtime_sha256: "d".repeat(64),
173 webkit_revision: "e".repeat(40),
174 bundle_sha256: bundle,
175 source_commit: source,
176 native_platform: compiledHosts.TARGETS[target][0],
177 native_arch: compiledHosts.TARGETS[target][1],
178 libc: "glibc",
179 passed: compiledHosts.compiledMinimum(target),
180 failed: 0,
181 skipped: 0,
182 test_log_sha256: "f".repeat(64),
183 native_passed: compiledHosts.nativeMinimum(target),
184 native_failed: 0,
185 native_skipped: 0,
186 native_log_sha256: "1".repeat(64),
187 license_closure: "complete",
188 relink_source: "complete",
189 },
190 ],
191 };
192 for (const [name, bytes] of Object.entries(payloads)) {
193 fs.writeFileSync(path.join(dir, name), bytes);
194 }
195 fs.writeFileSync(path.join(dir, compiledHosts.HOST_CATALOG), JSON.stringify(catalog));
196 return catalog;
197 }
198
199 test("missing remote signature fails verification when staged locally", (t) => {
200 const dir = localAssets(t);
201 fs.writeFileSync(path.join(dir, CHECKSUM_SIGNATURE), "signature-bytes");
202 const releases = [{ id: 7, tag_name: TAG, draft: true, assets: uploadedFromDir(dir, allReleaseAssetNames()) }];
203 const { gh, calls } = fakeGh(releases);
204 assert.throws(
205 () => run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], gh, () => {}),
206 new RegExp(`missing: .*${CHECKSUM_SIGNATURE.replace(/\./g, "\\.")}`),
207 );
208 assert.equal(releases[0].draft, true);
209 assert.equal(calls.some((call) => call.includes("PATCH")), false);
210
211 const signed = [{
212 id: 8,
213 tag_name: TAG,
214 draft: true,
215 assets: uploadedFromDir(dir, [...allReleaseAssetNames(), CHECKSUM_SIGNATURE]),
216 }];
217 run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], fakeGh(signed).gh, () => {});
218 assert.equal(signed[0].draft, false);
219 });
220
221 test("missing remote compiled-host catalog fails verification when staged locally", (t) => {
222 const dir = localAssets(t);
223 const catalog = stageCompiledHostCatalog(dir);
224 const releases = [{ id: 9, tag_name: TAG, draft: true, assets: uploadedFromDir(dir, allReleaseAssetNames()) }];
225 const { gh, calls } = fakeGh(releases);
226 assert.throws(
227 () => run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], gh, () => {}),
228 new RegExp(`missing: .*${compiledHosts.HOST_CATALOG.replace(/\./g, "\\.")}`),
229 );
230 assert.equal(releases[0].draft, true);
231 assert.equal(calls.some((call) => call.includes("PATCH")), false);
232
233 const complete = [{
234 id: 10,
235 tag_name: TAG,
236 draft: true,
237 assets: uploadedFromDir(dir, allReleaseAssetNames(catalog)),
238 }];
239 run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], fakeGh(complete).gh, () => {});
240 assert.equal(complete[0].draft, false);
241 });
242
243 test("unexpected remote signature fails verification when absent locally", (t) => {
244 const dir = localAssets(t);
245 const assets = [
246 ...uploadedFromDir(dir, allReleaseAssetNames()),
247 {
248 name: CHECKSUM_SIGNATURE,
249 state: "uploaded",
250 size: 3,
251 digest: `sha256:${crypto.hash("sha256", "sig")}`,
252 },
253 ];
254 const releases = [{ id: 11, tag_name: TAG, draft: true, assets }];
255 const { gh, calls } = fakeGh(releases);
256 assert.throws(
257 () => run(["--draft", "--asset-dir", dir, "--publish", REPO, TAG], gh, () => {}),
258 new RegExp(`unexpected: .*${CHECKSUM_SIGNATURE.replace(/\./g, "\\.")}`),
259 );
260 assert.equal(releases[0].draft, true);
261 assert.equal(calls.some((call) => call.includes("PATCH")), false);
262 });
263
263 lines JAVASCRIPT