Skip to content

Commit e6d9c5e

Browse files
authored
feat(mongodb-runner): allow the server host to be specified DRIVERS-3335 (#616)
1 parent bb339a3 commit e6d9c5e

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe('MongoCluster', function () {
8888
tmpDir,
8989
});
9090
expect(cluster.connectionString).to.be.a('string');
91+
expect(cluster.connectionString).to.include('127.0.0.1');
9192
expect(cluster.serverVersion).to.match(/^6\./);
9293
expect(cluster.serverVariant).to.equal('community');
9394
const { ok } = await cluster.withClient(async (client) => {
@@ -179,6 +180,18 @@ describe('MongoCluster', function () {
179180
).to.equal(1);
180181
});
181182

183+
it('can use localhost as the host', async function () {
184+
cluster = await MongoCluster.start({
185+
host: 'localhost',
186+
topology: 'standalone',
187+
tmpDir,
188+
});
189+
expect(cluster.connectionString).to.be.a('string');
190+
expect(cluster.connectionString).to.include('localhost');
191+
cluster = await MongoCluster.deserialize(cluster.serialize());
192+
expect(cluster.connectionString).to.include('localhost');
193+
});
194+
182195
context('TLS', function () {
183196
it('can spawn a 8.x standalone mongod with TLS enabled and get build info (automatically added client key)', async function () {
184197
cluster = await MongoCluster.start({

packages/mongodb-runner/src/mongocluster.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ export type ShardedOptions = {
143143

144144
export type MongoClusterOptions = Pick<
145145
MongoServerOptions,
146-
'logDir' | 'tmpDir' | 'args' | 'binDir' | 'docker' | 'internalClientOptions'
146+
| 'logDir'
147+
| 'tmpDir'
148+
| 'args'
149+
| 'binDir'
150+
| 'docker'
151+
| 'internalClientOptions'
152+
| 'host'
147153
> &
148154
CommonOptions &
149155
RSOptions &

packages/mongodb-runner/src/mongoserver.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export interface MongoServerOptions {
3939
args?: string[];
4040
/** Docker image or options to run the MongoDB binary in a container. */
4141
docker?: string | string[];
42+
/** The host address of the servers (default: '127.0.0.1') */
43+
host?: string;
4244
/** Internal options for the MongoDB client used by this server instance. */
4345
internalClientOptions?: Partial<MongoClientOptions>;
4446
/** Internal option -- if this is an arbiter, it does not understand user auth */
@@ -55,6 +57,7 @@ interface SerializedServerProperties {
5557
_id: string;
5658
pid?: number;
5759
port?: number;
60+
host?: string;
5861
dbPath?: string;
5962
defaultConnectionOptions?: Partial<MongoClientOptions>;
6063
startTime: string;
@@ -84,6 +87,7 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
8487
private buildInfo?: Document;
8588
private childProcess?: ChildProcess;
8689
private pid?: number;
90+
private host = '127.0.0.1';
8791
private port?: number;
8892
private dbPath?: string;
8993
private closing = false;
@@ -109,6 +113,7 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
109113
_id: this.uuid,
110114
pid: this.pid,
111115
port: this.port,
116+
host: this.host,
112117
dbPath: this.dbPath,
113118
startTime: this.startTime,
114119
hasInsertedMetadataCollEntry: this.hasInsertedMetadataCollEntry,
@@ -126,6 +131,9 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
126131
const srv = new MongoServer();
127132
srv.uuid = serialized._id;
128133
srv.port = serialized.port;
134+
if (serialized.host) {
135+
srv.host = serialized.host;
136+
}
129137
srv.defaultConnectionOptions = serialized.defaultConnectionOptions;
130138
srv.closing = !!(await srv._populateBuildInfo('restore-check'));
131139
srv.isArbiter = !!serialized.isArbiter;
@@ -144,7 +152,7 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
144152
if (this.port === undefined) {
145153
throw new Error('Cannot get host/port for closed server');
146154
}
147-
return `127.0.0.1:${this.port}`;
155+
return `${this.host}:${this.port}`;
148156
}
149157

150158
// Returns the version reported in the server log output
@@ -204,6 +212,9 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
204212
srv.isArbiter = !!options.isArbiter;
205213
srv.isMongos = options.binary === 'mongos';
206214
srv.isConfigSvr = !!options.args?.includes('--configsvr');
215+
if (options.host) {
216+
srv.host = options.host;
217+
}
207218
const keyFilePath = getKeyFileOption(options.args);
208219
if (keyFilePath) {
209220
srv.keyFileContents = await fs.readFile(keyFilePath, 'utf8');

0 commit comments

Comments
 (0)