Skip to content

Commit 02d68d5

Browse files
authored
feat(mongodb-runner): add downloadDir option MONGOSH-1875 (#574)
1 parent 90b89c5 commit 02d68d5

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-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: 34 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,39 @@ 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+
46+
sinon
47+
.stub(MongoCluster, 'downloadMongoDb' as any)
48+
.resolves(customDownloadDir);
49+
50+
try {
51+
cluster = await MongoCluster.start({
52+
version: '6.x',
53+
topology: 'standalone',
54+
tmpDir,
55+
downloadDir: customDownloadDir,
56+
downloadOptions: {
57+
platform: 'linux',
58+
arch: 'x64',
59+
},
60+
});
61+
} catch (err) {
62+
// This will error because no actual binary gets downloaded
63+
}
64+
65+
expect(MongoCluster['downloadMongoDb']).to.have.been.calledWith(
66+
customDownloadDir,
67+
'6.x',
68+
{
69+
platform: 'linux',
70+
arch: 'x64',
71+
},
72+
);
3973
});
4074

4175
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 this.downloadMongoDb(
97+
options.downloadDir ?? options.tmpDir,
8998
options.version,
9099
options.downloadOptions,
91100
);

0 commit comments

Comments
 (0)