Skip to content

Commit

Permalink
fix(build): fix hash file parsing (#1950)
Browse files Browse the repository at this point in the history
In 2da3c31, we moved file hash computation for our produced artifacts
from the JSON feed generation step to the newly introduced "download artifacts"
step of the `release_publish` tasks (because we need to download artifacts
before we can submit them to the papertrail service). We used the standard-ish
SHASUMS format (output of `sha256sum`/input of `sha256sum -c`) for that, but
failed to parse it properly when consuming it.

This fixes that and extends the download center config tests to also perform
the full "download artifacts" step as part of the test setup, to ensure that
the two parts work together properly.
  • Loading branch information
addaleax committed Apr 15, 2024
1 parent ffb9fa0 commit 136eb91
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
58 changes: 35 additions & 23 deletions packages/build/src/download-center/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { DownloadCenterConfig } from '@mongodb-js/dl-center/dist/download-center-config';
import { getPackageFile, type PackageInformationProvider } from '../packaging';
import { type PackageInformationProvider } from '../packaging';
import { expect } from 'chai';
import sinon from 'sinon';
import { ALL_PACKAGE_VARIANTS, type PackageVariant } from '../config';
import type { Config } from '../config';
import { type PackageVariant } from '../config';
import {
createVersionConfig,
createDownloadCenterConfig,
Expand All @@ -13,6 +14,10 @@ import {
import { promises as fs } from 'fs';
import path from 'path';
import fetch from 'node-fetch';
import { createServer } from 'http';
import { once } from 'events';
import { runDownloadAndListArtifacts } from '../run-download-and-list-artifacts';
import type { AddressInfo } from 'net';

const packageInformation = (version: string) =>
((packageVariant: PackageVariant) => {
Expand All @@ -33,7 +38,7 @@ const packageInformation = (version: string) =>

describe('DownloadCenter config', function () {
let outputDir: string;
beforeEach(async function () {
before(async function () {
outputDir = path.join(
__dirname,
'..',
Expand All @@ -45,26 +50,32 @@ describe('DownloadCenter config', function () {
);
await fs.mkdir(outputDir, { recursive: true });

const testVersions = ['2.0.1', '2.0.0', '1.2.2'];
const allFiles = testVersions
.flatMap((version) =>
ALL_PACKAGE_VARIANTS.map(
(packageVariant) =>
getPackageFile(packageVariant, packageInformation(version)).path
)
)
.flatMap((file) => [file, `${file}.sig`]);
await fs.writeFile(
path.join(outputDir, 'SHASUMS1.txt'),
allFiles.map((f) => `ghjklm ${f}`).join('\n')
);
await fs.writeFile(
path.join(outputDir, 'SHASUMS256.txt'),
allFiles.map((f) => `abcdef ${f}`).join('\n')
);
const httpServer = createServer((req, res) => {
res.end(req.url);
});
httpServer.listen(0);
await once(httpServer, 'listening');
try {
const testVersions = ['2.0.1', '2.0.0', '1.2.2'];

for (const version of testVersions) {
const config: Partial<Config> = {
outputDir,
packageInformation: packageInformation(version),
};
await runDownloadAndListArtifacts(
config as Config,
`http://localhost:${(httpServer.address() as AddressInfo).port}/`,
'append-to-hash-file-for-testing'
);
}
} finally {
httpServer.close();
await once(httpServer, 'close');
}
});

afterEach(async function () {
after(async function () {
await fs.rm(outputDir, { recursive: true });
});

Expand Down Expand Up @@ -376,8 +387,9 @@ describe('DownloadCenter config', function () {
archive: {
type: 'zip',
url: `${baseUrl}mongosh-1.2.2-darwin-x64.zip`,
sha256: 'abcdef',
sha1: 'ghjklm',
sha256:
'ed8922ef572c307634150bf78ea02338349949f29245123884b1318cdc402dd9',
sha1: '4f1b987549703c58940be9f8582f4032374b1074',
},
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/build/src/download-center/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ async function getHashes(
);
const line = content
.split('\n')
.find((line) => line.endsWith(packagedFilename));
.find((line) => line.trim().startsWith(packagedFilename));
if (!line) {
throw new Error(
`Could not find entry for ${packagedFilename} in ${filename}`
);
}
return [hash, line.trim().split(/\s/)[0]] as const;
return [hash, line.trim().split(/\s+/)[1]] as const;
})
)
) as { sha1: string; sha256: string };
Expand Down
8 changes: 6 additions & 2 deletions packages/build/src/run-download-and-list-artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const hashListFiles = [

export async function runDownloadAndListArtifacts(
config: Config,
publicArtifactBaseUrl: string = ARTIFACTS_URL_PUBLIC_BASE
publicArtifactBaseUrl: string = ARTIFACTS_URL_PUBLIC_BASE,
hashFileWriteMode: 'normal' | 'append-to-hash-file-for-testing' = 'normal'
): Promise<void> {
const requiredConfigKeys: (keyof Config)[] = ['outputDir'];
for (const key of requiredConfigKeys) {
Expand Down Expand Up @@ -66,7 +67,10 @@ export async function runDownloadAndListArtifacts(
.filter(Boolean)
.map((file) => `${file?.filename} ${file?.[hash]}`)
.join('\n') + '\n';
await fs.writeFile(filepath, contents);
await (hashFileWriteMode === 'normal' ? fs.writeFile : fs.appendFile)(
filepath,
contents
);
console.log('wrote hash list to', filepath);
}
}
Expand Down

0 comments on commit 136eb91

Please sign in to comment.