Skip to content

Commit a38b9aa

Browse files
committed
feat(mongodb-runner): add downloadDir option
There are cases where the place we want to manage the place to download and store binaries ourselves. For example in mongosh this is useful in making sure each `mongodb-runner` has its own temporary directory for all other files but refers to only 1 download directory for the database binaries.
1 parent 0aaf676 commit a38b9aa

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

packages/mongodb-runner/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Options:
3737
--version MongoDB server version to use [string]
3838
--logDir Directory to store server log files in [string]
3939
--binDir Directory containing mongod/mongos binaries [string]
40+
--downloadDir Directory to store downloaded MongoDB versions in [string]
4041
--tmpDir Directory for temporary files [string] [default: "/tmp"]
4142
--runnerDir Directory for storing cluster metadata
4243
[string] [default: "/home/addaleax/.mongodb/runner2"]

packages/mongodb-runner/src/cli.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ import * as utilities from './index';
4949
default: os.tmpdir(),
5050
describe: 'Directory for temporary files',
5151
})
52+
.option('downloadDir', {
53+
type: 'string',
54+
describe:
55+
'Directory for downloading and caching MongoDB binaries (uses tmpDir if not specified)',
56+
})
5257
.option('runnerDir', {
5358
type: 'string',
5459
default: defaultRunnerDir,

packages/mongodb-runner/src/mongocluster.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { promises as fs } from 'fs';
44
import path from 'path';
55
import os from 'os';
66
import createDebug from 'debug';
7+
import sinon from 'sinon';
78

89
if (process.env.CI) {
910
createDebug.enable('mongodb-runner,mongodb-downloader');
@@ -36,6 +37,40 @@ describe('MongoCluster', function () {
3637

3738
afterEach(async function () {
3839
await cluster?.close();
40+
sinon.restore();
41+
});
42+
43+
it('can use custom downloadDir option for binary downloads', async function () {
44+
const customDownloadDir = path.join(tmpDir, 'custom-downloads');
45+
await fs.mkdir(customDownloadDir, { recursive: true });
46+
47+
sinon
48+
.stub(MongoCluster, 'downloadMongoDb' as any)
49+
.resolves(customDownloadDir);
50+
51+
try {
52+
cluster = await MongoCluster.start({
53+
version: '6.x',
54+
topology: 'standalone',
55+
tmpDir,
56+
downloadDir: customDownloadDir,
57+
downloadOptions: {
58+
platform: 'linux',
59+
arch: 'x64',
60+
},
61+
});
62+
} catch (err) {
63+
// This will error because no actual binary gets downloaded
64+
}
65+
66+
expect(MongoCluster['downloadMongoDb']).to.have.been.calledWith(
67+
customDownloadDir,
68+
'6.x',
69+
{
70+
platform: 'linux',
71+
arch: 'x64',
72+
},
73+
);
3974
});
4075

4176
it('can spawn a 6.x standalone mongod', async function () {

packages/mongodb-runner/src/mongocluster.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface MongoClusterOptions
1717
secondaries?: number;
1818
shards?: number;
1919
version?: string;
20+
downloadDir?: string;
2021
downloadOptions?: DownloadOptions;
2122
}
2223

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

34+
private static downloadMongoDb(
35+
tmpdir: string,
36+
targetVersionSemverSpecifier?: string | undefined,
37+
options?: DownloadOptions | undefined,
38+
): Promise<string> {
39+
return downloadMongoDb(tmpdir, targetVersionSemverSpecifier, options);
40+
}
41+
3342
serialize(): unknown /* JSON-serializable */ {
3443
return {
3544
topology: this.topology,
@@ -84,8 +93,8 @@ export class MongoCluster {
8493
const cluster = new MongoCluster();
8594
cluster.topology = options.topology;
8695
if (!options.binDir) {
87-
options.binDir = await downloadMongoDb(
88-
options.tmpDir,
96+
options.binDir = await MongoCluster.downloadMongoDb(
97+
options.downloadDir ?? options.tmpDir,
8998
options.version,
9099
options.downloadOptions,
91100
);

0 commit comments

Comments
 (0)