Skip to content
Merged
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
50 changes: 50 additions & 0 deletions packages/mongodb-runner/src/mongocluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,34 @@ describe('MongoCluster', function () {
expect(+hello.passives.length + +hello.hosts.length).to.equal(3);
});

it('can spawn a 8.x replset with a pre-specified port for the primary', async function () {
cluster = await MongoCluster.start({
version: '8.x',
topology: 'replset',
tmpDir,
args: ['--port', '50080'],
});
expect(cluster.connectionString).to.include(`:50080`);
const hello = await cluster.withClient(async (client) => {
return await client.db('admin').command({ hello: 1 });
});
expect(+hello.passives.length + +hello.hosts.length).to.equal(3);
});

it('can spawn a 8.x replset with a pre-specified --port= for the primary', async function () {
cluster = await MongoCluster.start({
version: '8.x',
topology: 'replset',
tmpDir,
args: ['--port=50081'],
});
expect(cluster.connectionString).to.include(`:50081`);
const hello = await cluster.withClient(async (client) => {
return await client.db('admin').command({ hello: 1 });
});
expect(+hello.passives.length + +hello.hosts.length).to.equal(3);
});

it('can spawn a 8.x replset with specific number of arbiters and secondaries', async function () {
cluster = await MongoCluster.start({
version: '8.x',
Expand Down Expand Up @@ -699,6 +727,28 @@ describe('MongoCluster', function () {
});
});

it('can spawn a sharded cluster with a pre-specified port for mongos (--port x)', async function () {
cluster = await MongoCluster.start({
version: '8.x',
topology: 'sharded',
tmpDir,
secondaries: 0,
mongosArgs: [['--port', '50082']],
});
expect(cluster.connectionString).to.include(`:50082`);
});

it('can spawn a sharded cluster with a pre-specified port for mongos (--port=x)', async function () {
cluster = await MongoCluster.start({
version: '8.x',
topology: 'sharded',
tmpDir,
secondaries: 0,
mongosArgs: [['--port=50083']],
});
expect(cluster.connectionString).to.include(`:50083`);
});

it('can add authentication options and verify them after serialization', async function () {
cluster = await MongoCluster.start({
version: '8.x',
Expand Down
10 changes: 2 additions & 8 deletions packages/mongodb-runner/src/mongocluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
debugVerbose,
makeConnectionString,
safePromiseAll,
hasPortArg,
} from './util';
import { OIDCMockProviderProcess } from './oidc';
import { EventEmitter } from 'events';
Expand Down Expand Up @@ -166,7 +167,7 @@ export type MongoClusterEvents = {
function removePortArg([...args]: string[]): string[] {
let portArgIndex = -1;
if ((portArgIndex = args.indexOf('--port')) !== -1) {
args.splice(portArgIndex + 1, 1);
args.splice(portArgIndex, 2);
} else if (
Comment on lines 167 to 171

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anyone uses that.

(portArgIndex = args.findIndex((arg) => arg.startsWith('--port='))) !== -1
) {
Expand All @@ -175,13 +176,6 @@ function removePortArg([...args]: string[]): string[] {
return args;
}

function hasPortArg(args: string[] | undefined): boolean {
if (!args) return false;
return (
args.includes('--port') || args.some((arg) => arg.startsWith('--port='))
);
}

function processRSMembers(options: MongoClusterOptions): {
rsMembers: RSMemberOptions[];
replSetName: string;
Expand Down
5 changes: 3 additions & 2 deletions packages/mongodb-runner/src/mongoserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
makeConnectionString,
sleep,
eventually,
hasPortArg,
} from './util';

/**
Expand Down Expand Up @@ -187,7 +188,7 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
}

static async start({ ...options }: MongoServerOptions): Promise<MongoServer> {
if (options.binary === 'mongos' && !options.args?.includes('--port')) {
if (options.binary === 'mongos' && !hasPortArg(options.args)) {
// SERVER-78384: mongos before 7.1.0 does not understand `--port 0` ...
// Just pick a random port in [1024, 49152), try to listen, and continue until
// we find a free one.
Expand Down Expand Up @@ -265,7 +266,7 @@ export class MongoServer extends EventEmitter<MongoServerEvents> {
}

commandline.push(...(options.args ?? []));
if (!options.args?.includes('--port')) commandline.push('--port', '0');
if (!hasPortArg(options.args)) commandline.push('--port', '0');
if (!options.args?.includes('--dbpath') && options.binary === 'mongod')
commandline.push('--dbpath', options.docker ? '/tmp' : srv.dbPath!);
if (
Expand Down
5 changes: 5 additions & 0 deletions packages/mongodb-runner/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const sleep = (ms: number): Promise<void> =>
new Promise((r) => setTimeout(r, ms));
export const range = (n: number): number[] => [...Array(n).keys()];

export function hasPortArg(args: string[] | undefined): boolean {
if (!args) return false;
return args.some((arg) => arg === '--port' || arg.startsWith('--port='));
}

/**
* This function iterates `iterable` and applies `fn` to each item that
* `iterable` produces. `fn` is not awaited on each iteration of `iterable`.
Expand Down
Loading