diff --git a/packages/mongodb-runner/src/mongocluster.spec.ts b/packages/mongodb-runner/src/mongocluster.spec.ts index d6dc855b..c82ef9c3 100644 --- a/packages/mongodb-runner/src/mongocluster.spec.ts +++ b/packages/mongodb-runner/src/mongocluster.spec.ts @@ -88,6 +88,7 @@ describe('MongoCluster', function () { tmpDir, }); expect(cluster.connectionString).to.be.a('string'); + expect(cluster.connectionString).to.include('127.0.0.1'); expect(cluster.serverVersion).to.match(/^6\./); expect(cluster.serverVariant).to.equal('community'); const { ok } = await cluster.withClient(async (client) => { @@ -179,6 +180,18 @@ describe('MongoCluster', function () { ).to.equal(1); }); + it('can use localhost as the host', async function () { + cluster = await MongoCluster.start({ + host: 'localhost', + topology: 'standalone', + tmpDir, + }); + expect(cluster.connectionString).to.be.a('string'); + expect(cluster.connectionString).to.include('localhost'); + cluster = await MongoCluster.deserialize(cluster.serialize()); + expect(cluster.connectionString).to.include('localhost'); + }); + context('TLS', function () { it('can spawn a 8.x standalone mongod with TLS enabled and get build info (automatically added client key)', async function () { cluster = await MongoCluster.start({ diff --git a/packages/mongodb-runner/src/mongocluster.ts b/packages/mongodb-runner/src/mongocluster.ts index 533ec26b..d8d97388 100644 --- a/packages/mongodb-runner/src/mongocluster.ts +++ b/packages/mongodb-runner/src/mongocluster.ts @@ -143,7 +143,13 @@ export type ShardedOptions = { export type MongoClusterOptions = Pick< MongoServerOptions, - 'logDir' | 'tmpDir' | 'args' | 'binDir' | 'docker' | 'internalClientOptions' + | 'logDir' + | 'tmpDir' + | 'args' + | 'binDir' + | 'docker' + | 'internalClientOptions' + | 'host' > & CommonOptions & RSOptions & diff --git a/packages/mongodb-runner/src/mongoserver.ts b/packages/mongodb-runner/src/mongoserver.ts index a19da349..64d8bc75 100644 --- a/packages/mongodb-runner/src/mongoserver.ts +++ b/packages/mongodb-runner/src/mongoserver.ts @@ -39,6 +39,8 @@ export interface MongoServerOptions { args?: string[]; /** Docker image or options to run the MongoDB binary in a container. */ docker?: string | string[]; + /** The host address of the servers (default: '127.0.0.1') */ + host?: string; /** Internal options for the MongoDB client used by this server instance. */ internalClientOptions?: Partial; /** Internal option -- if this is an arbiter, it does not understand user auth */ @@ -55,6 +57,7 @@ interface SerializedServerProperties { _id: string; pid?: number; port?: number; + host?: string; dbPath?: string; defaultConnectionOptions?: Partial; startTime: string; @@ -84,6 +87,7 @@ export class MongoServer extends EventEmitter { private buildInfo?: Document; private childProcess?: ChildProcess; private pid?: number; + private host = '127.0.0.1'; private port?: number; private dbPath?: string; private closing = false; @@ -109,6 +113,7 @@ export class MongoServer extends EventEmitter { _id: this.uuid, pid: this.pid, port: this.port, + host: this.host, dbPath: this.dbPath, startTime: this.startTime, hasInsertedMetadataCollEntry: this.hasInsertedMetadataCollEntry, @@ -126,6 +131,9 @@ export class MongoServer extends EventEmitter { const srv = new MongoServer(); srv.uuid = serialized._id; srv.port = serialized.port; + if (serialized.host) { + srv.host = serialized.host; + } srv.defaultConnectionOptions = serialized.defaultConnectionOptions; srv.closing = !!(await srv._populateBuildInfo('restore-check')); srv.isArbiter = !!serialized.isArbiter; @@ -144,7 +152,7 @@ export class MongoServer extends EventEmitter { if (this.port === undefined) { throw new Error('Cannot get host/port for closed server'); } - return `127.0.0.1:${this.port}`; + return `${this.host}:${this.port}`; } // Returns the version reported in the server log output @@ -204,6 +212,9 @@ export class MongoServer extends EventEmitter { srv.isArbiter = !!options.isArbiter; srv.isMongos = options.binary === 'mongos'; srv.isConfigSvr = !!options.args?.includes('--configsvr'); + if (options.host) { + srv.host = options.host; + } const keyFilePath = getKeyFileOption(options.args); if (keyFilePath) { srv.keyFileContents = await fs.readFile(keyFilePath, 'utf8');