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
127 changes: 127 additions & 0 deletions packages/mongodb-runner/src/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { expect } from 'chai';
import { promises as fs } from 'fs';
import path from 'path';
import os from 'os';
import { promisify } from 'util';
import { execFile } from 'child_process';
import createDebug from 'debug';
import sinon from 'sinon';
import { MongoClient } from 'mongodb';

if (process.env.CI) {
createDebug.enable('mongodb-runner,mongodb-downloader');
}

const execFileAsync = promisify(execFile);
const tmpDir = path.join(os.tmpdir(), `runner-cli-tests-${Date.now()}`);

async function runCli(args: string[]): Promise<string> {
const { stdout } = await execFileAsync('mongodb-runner', args);
return stdout;
}

describe('cli', function () {
this.timeout(1_000_000); // Downloading Windows binaries can take a very long time...

before(async function () {
await fs.mkdir(tmpDir, { recursive: true });
});

after(async function () {
await fs.rm(tmpDir, {
recursive: true,
maxRetries: 100,
});
});

afterEach(function () {
sinon.restore();
});

it('can manage a standalone cluster with command line args', async function () {
// Start the CLI with arguments and capture stdout.
const stdout = await runCli(['start', '--topology', 'standalone']);

// stdout is JUST the connection string.
const connectionString = stdout.trim();
expect(connectionString).to.match(/^mongodb(\+srv)?:\/\//);

// Connect to the cluster.
const client = new MongoClient(connectionString);
const result = await client.db('admin').command({ ping: 1 });
await client.close();
expect(result.ok).to.eq(1);

// Call `stop` on the CLI
await runCli(['stop', '--all']);
});
it('can manage a replset cluster with command line args', async function () {
const stdout = await runCli([
'start',
'--topology',
'replset',
'--secondaries',
'2',
'--arbiters',
'1',
'--version',
'8.0.x',
'--',
'--replSet',
'repl0',
]);
const connectionString = stdout.trim();
expect(/repl0/.test(connectionString)).to.be.true;

// Connect to the cluster.
const client = new MongoClient(connectionString);
const result = await client.db('admin').command({ ping: 1 });
await client.close();
expect(result.ok).to.eq(1);

// Call `stop` on the CLI
await runCli(['stop', '--all']);
});
it('can manage a sharded cluster with command line args', async function () {
const stdout = await runCli([
'start',
'--topology',
'sharded',
'--shards',
'2',
'--version',
'7.0.x',
]);
const connectionString = stdout.trim();

// Connect to the cluster.
const client = new MongoClient(connectionString);
const result = await client.db('admin').command({ ping: 1 });
await client.close();
expect(result.ok).to.eq(1);

// Call `stop` on the CLI
await runCli(['stop', '--all']);
});
it('can manage a cluster with a config file', async function () {
const configFile = path.resolve(
__dirname,
'..',
'test',
'fixtures',
'config.json',
);
const stdout = await runCli(['start', '--config', configFile]);
const connectionString = stdout.trim();
expect(/repl0/.test(connectionString)).to.be.true;

// Connect to the cluster.
const client = new MongoClient(connectionString);
const result = await client.db('admin').command({ ping: 1 });
await client.close();
expect(result.ok).to.eq(1);

// Call `stop` on the CLI
await runCli(['stop', '--all']);
});
});
21 changes: 16 additions & 5 deletions packages/mongodb-runner/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ import type { MongoClientOptions } from 'mongodb';
.demandCommand(1, 'A command needs to be provided')
.help().argv;
const [command, ...args] = argv._.map(String);
// Allow args to be provided by the config file.
if (Array.isArray(argv.args)) {
args.push(...argv.args.map(String));
}
if (argv.debug || argv.verbose) {
createDebug.enable('mongodb-runner');
}
Expand All @@ -111,22 +115,29 @@ import type { MongoClientOptions } from 'mongodb';
async function start() {
const { cluster, id } = await utilities.start(argv, args);
const cs = new ConnectionString(cluster.connectionString);
console.log(`Server started and running at ${cs.toString()}`);
// Only the connection string should print to stdout so it can be captured
// by a calling process.
console.error(`Server started and running at ${cs.toString()}`);
if (cluster.oidcIssuer) {
cs.typedSearchParams<MongoClientOptions>().set(
'authMechanism',
'MONGODB-OIDC',
);
console.log(`OIDC provider started and running at ${cluster.oidcIssuer}`);
console.log(`Server connection string with OIDC auth: ${cs.toString()}`);
console.error(
`OIDC provider started and running at ${cluster.oidcIssuer}`,
);
console.error(
`Server connection string with OIDC auth: ${cs.toString()}`,
);
}
console.log('Run the following command to stop the instance:');
console.log(
console.error('Run the following command to stop the instance:');
console.error(
`${argv.$0} stop --id=${id}` +
(argv.runnerDir !== defaultRunnerDir
? `--runnerDir=${argv.runnerDir}`
: ''),
);
console.log(cs.toString());
cluster.unref();
}

Expand Down
47 changes: 47 additions & 0 deletions packages/mongodb-runner/src/mongocluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,51 @@ describe('MongoCluster', function () {
{ user: 'testuser', db: 'admin' },
]);
});
it('can use a keyFile', async function () {
const keyFile = path.join(tmpDir, 'keyFile');
await fs.writeFile(keyFile, 'secret', { mode: 0o400 });
cluster = await MongoCluster.start({
version: '8.x',
topology: 'replset',
tmpDir,
secondaries: 1,
arbiters: 1,
args: ['--keyFile', keyFile],
users: [
{
username: 'testuser',
password: 'testpass',
roles: [
{ role: 'userAdminAnyDatabase', db: 'admin' },
{ role: 'clusterAdmin', db: 'admin' },
],
},
],
});
expect(cluster.connectionString).to.be.a('string');
expect(cluster.serverVersion).to.match(/^8\./);
expect(cluster.connectionString).to.include('testuser:testpass@');
cluster = await MongoCluster.deserialize(cluster.serialize());
expect(cluster.connectionString).to.include('testuser:testpass@');
});
it('can support requireApiVersion', async function () {
cluster = await MongoCluster.start({
version: '8.x',
topology: 'sharded',
tmpDir,
secondaries: 1,
shards: 1,
requireApiVersion: 1,
args: ['--setParameter', 'enableTestCommands=1'],
});
expect(cluster.connectionString).to.be.a('string');
expect(cluster.serverVersion).to.match(/^8\./);
await cluster.withClient((client) => {
expect(client.serverApi?.version).to.eq('1');
});
cluster = await MongoCluster.deserialize(cluster.serialize());
await cluster.withClient((client) => {
expect(client.serverApi?.version).to.eq('1');
});
});
});
33 changes: 33 additions & 0 deletions packages/mongodb-runner/src/mongocluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export interface CommonOptions {
*/
tlsAddClientKey?: boolean;

/**
* Whether to require an API version for commands.
*/
requireApiVersion?: number;

/**
* Topology of the cluster.
*/
Expand Down Expand Up @@ -488,6 +493,7 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
...options,
...s,
topology: 'replset',
requireApiVersion: undefined,
users: isConfig ? undefined : options.users, // users go on the mongos/config server only for the config set
});
return [cluster, isConfig] as const;
Expand Down Expand Up @@ -528,6 +534,7 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
}

await cluster.addAuthIfNeeded();
await cluster.addRequireApiVersionIfNeeded(options);
return cluster;
}

Expand All @@ -536,6 +543,32 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
yield* this.shards;
}

async addRequireApiVersionIfNeeded({
...options
}: MongoClusterOptions): Promise<void> {
// Set up requireApiVersion if requested.
if (options.requireApiVersion === undefined) {
return;
}
if (options.topology === 'replset') {
throw new Error(
'requireApiVersion is not supported for replica sets, see SERVER-97010',
);
}
await Promise.all(
[...this.servers].map(
async (child) =>
await child.withClient(async (client) => {
const admin = client.db('admin');
await admin.command({ setParameter: 1, requireApiVersion: true });
}),
),
);
await this.updateDefaultConnectionOptions({
serverApi: String(options.requireApiVersion) as '1',
});
}

async addAuthIfNeeded(): Promise<void> {
if (!this.users?.length) return;
// Sleep to give time for a possible replset election to settle.
Expand Down
Loading
Loading