Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions packages/mongodb-runner/src/mongocluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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({
Expand Down
8 changes: 7 additions & 1 deletion packages/mongodb-runner/src/mongocluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down
13 changes: 12 additions & 1 deletion packages/mongodb-runner/src/mongoserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MongoClientOptions>;
/** Internal option -- if this is an arbiter, it does not understand user auth */
Expand All @@ -55,6 +57,7 @@ interface SerializedServerProperties {
_id: string;
pid?: number;
port?: number;
host?: string;
dbPath?: string;
defaultConnectionOptions?: Partial<MongoClientOptions>;
startTime: string;
Expand Down Expand Up @@ -84,6 +87,7 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
private buildInfo?: Document;
private childProcess?: ChildProcess;
private pid?: number;
private host = '127.0.0.1';
private port?: number;
private dbPath?: string;
private closing = false;
Expand All @@ -109,6 +113,7 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
_id: this.uuid,
pid: this.pid,
port: this.port,
host: this.host,
dbPath: this.dbPath,
startTime: this.startTime,
hasInsertedMetadataCollEntry: this.hasInsertedMetadataCollEntry,
Expand All @@ -126,6 +131,9 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
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;
Expand All @@ -144,7 +152,7 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
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
Expand Down Expand Up @@ -204,6 +212,9 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
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');
Expand Down
Loading