Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mongodb-runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 5 additions & 0 deletions packages/mongodb-runner/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions packages/mongodb-runner/src/mongocluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 () {
Expand Down
13 changes: 11 additions & 2 deletions packages/mongodb-runner/src/mongocluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface MongoClusterOptions
secondaries?: number;
shards?: number;
version?: string;
downloadDir?: string;
downloadOptions?: DownloadOptions;
}

Expand All @@ -30,6 +31,14 @@ export class MongoCluster {
/* see .start() */
}

private static downloadMongoDb(
tmpdir: string,
targetVersionSemverSpecifier?: string | undefined,
options?: DownloadOptions | undefined,
): Promise<string> {
return downloadMongoDb(tmpdir, targetVersionSemverSpecifier, options);
}

serialize(): unknown /* JSON-serializable */ {
return {
topology: this.topology,
Expand Down Expand Up @@ -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 MongoCluster.downloadMongoDb(
options.downloadDir ?? options.tmpDir,
options.version,
options.downloadOptions,
);
Expand Down
Loading