diff --git a/packages/mongodb-runner/README.md b/packages/mongodb-runner/README.md index 17aef942..03954e19 100644 --- a/packages/mongodb-runner/README.md +++ b/packages/mongodb-runner/README.md @@ -37,6 +37,7 @@ Options: --version MongoDB server version to use [string] --logDir Directory to store server log files in [string] --binDir Directory containing mongod/mongos binaries [string] + --downloadDir Directory to store downloaded MongoDB versions in [string] --tmpDir Directory for temporary files [string] [default: "/tmp"] --runnerDir Directory for storing cluster metadata [string] [default: "/home/addaleax/.mongodb/runner2"] diff --git a/packages/mongodb-runner/src/cli.ts b/packages/mongodb-runner/src/cli.ts index 0e7c23e2..6fdf2c6f 100644 --- a/packages/mongodb-runner/src/cli.ts +++ b/packages/mongodb-runner/src/cli.ts @@ -49,6 +49,11 @@ import * as utilities from './index'; default: os.tmpdir(), describe: 'Directory for temporary files', }) + .option('downloadDir', { + type: 'string', + describe: + 'Directory for downloading and caching MongoDB binaries (uses tmpDir if not specified)', + }) .option('runnerDir', { type: 'string', default: defaultRunnerDir, diff --git a/packages/mongodb-runner/src/mongocluster.spec.ts b/packages/mongodb-runner/src/mongocluster.spec.ts index 156b3ea7..9447a979 100644 --- a/packages/mongodb-runner/src/mongocluster.spec.ts +++ b/packages/mongodb-runner/src/mongocluster.spec.ts @@ -4,6 +4,7 @@ import { promises as fs } from 'fs'; import path from 'path'; import os from 'os'; import createDebug from 'debug'; +import sinon from 'sinon'; if (process.env.CI) { createDebug.enable('mongodb-runner,mongodb-downloader'); @@ -36,6 +37,39 @@ describe('MongoCluster', function () { afterEach(async function () { await cluster?.close(); + sinon.restore(); + }); + + it('can use custom downloadDir option for binary downloads', async function () { + const customDownloadDir = path.join(tmpDir, 'custom-downloads'); + + sinon + .stub(MongoCluster, 'downloadMongoDb' as any) + .resolves(customDownloadDir); + + try { + cluster = await MongoCluster.start({ + version: '6.x', + topology: 'standalone', + tmpDir, + downloadDir: customDownloadDir, + downloadOptions: { + platform: 'linux', + arch: 'x64', + }, + }); + } catch (err) { + // This will error because no actual binary gets downloaded + } + + expect(MongoCluster['downloadMongoDb']).to.have.been.calledWith( + customDownloadDir, + '6.x', + { + platform: 'linux', + arch: 'x64', + }, + ); }); it('can spawn a 6.x standalone mongod', async function () { diff --git a/packages/mongodb-runner/src/mongocluster.ts b/packages/mongodb-runner/src/mongocluster.ts index 406377f0..4d698087 100644 --- a/packages/mongodb-runner/src/mongocluster.ts +++ b/packages/mongodb-runner/src/mongocluster.ts @@ -17,6 +17,7 @@ export interface MongoClusterOptions secondaries?: number; shards?: number; version?: string; + downloadDir?: string; downloadOptions?: DownloadOptions; } @@ -30,6 +31,14 @@ export class MongoCluster { /* see .start() */ } + private static downloadMongoDb( + tmpdir: string, + targetVersionSemverSpecifier?: string | undefined, + options?: DownloadOptions | undefined, + ): Promise { + return downloadMongoDb(tmpdir, targetVersionSemverSpecifier, options); + } + serialize(): unknown /* JSON-serializable */ { return { topology: this.topology, @@ -84,8 +93,8 @@ export class MongoCluster { const cluster = new MongoCluster(); cluster.topology = options.topology; if (!options.binDir) { - options.binDir = await downloadMongoDb( - options.tmpDir, + options.binDir = await this.downloadMongoDb( + options.downloadDir ?? options.tmpDir, options.version, options.downloadOptions, );