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
143 changes: 143 additions & 0 deletions packages/mongodb-runner/src/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
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 type { ExecOptions } from 'child_process';
import { MongoClient } from 'mongodb';

const execFileAsync = promisify(execFile);

async function runCli(
args: string[],
options: ExecOptions = {},
): Promise<string> {
const { stdout } = await execFileAsync(
process.execPath,
[path.resolve(__dirname, '..', 'bin', 'runner.js'), ...args],
options,
);
return stdout;
}

describe('cli', function () {
this.timeout(process.platform === 'win32' ? 400_000 : 100_000);
let tmpDir = '';

before(async function () {
tmpDir = path.join(os.tmpdir(), `runner-cli-tests-${Date.now()}`);
await fs.mkdir(tmpDir, { recursive: true });
});

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

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:\/\//);

// 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);

// Exercise the rest of the cli.
const lsStdout = await runCli(['ls']);
expect(lsStdout.includes(connectionString)).to.be.true;

await runCli(['stop', '--all']);

await runCli(['prune']);
});

it('can execute against a cluster', async function () {
const stdout = await runCli([
'exec',
'-t',
'standalone',
'--',
'sh',
'-c',
'echo $MONGODB_URI',
]);
const connectionString = stdout.trim();
expect(connectionString).to.match(/^mongodb:\/\//);
});

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;

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

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();

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

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;

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

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
108 changes: 108 additions & 0 deletions packages/mongodb-runner/src/mongocluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,65 @@ describe('MongoCluster', function () {
await cluster.close();
});

it('can serialize and deserialize sharded cluster with keyfile and auth (no enableTestCommands)', async function () {
const keyFile = path.join(tmpDir, 'keyFile2');
await fs.writeFile(keyFile, 'secret', { mode: 0o400 });
cluster = await MongoCluster.start({
version: '8.x',
topology: 'sharded',
tmpDir,
secondaries: 0,
users: [
{
username: 'testuser',
password: 'testpass',
roles: [{ role: 'readWriteAnyDatabase', db: 'admin' }],
},
],
args: ['--keyFile', keyFile],
});
cluster = await MongoCluster.deserialize(cluster.serialize());
await cluster.withClient(async (client) => {
expect(
(await client.db('admin').command({ connectionStatus: 1 })).authInfo
.authenticatedUsers,
).to.deep.equal([{ user: 'testuser', db: 'admin' }]);
});
await cluster.close();
});

it('can serialize and deserialize sharded cluster with keyfile and auth (enableTestCommands=true)', async function () {
const keyFile = path.join(tmpDir, 'keyFile3');
await fs.writeFile(keyFile, 'secret', { mode: 0o400 });
cluster = await MongoCluster.start({
version: '8.x',
topology: 'sharded',
tmpDir,
secondaries: 0,
users: [
{
username: 'testuser',
password: 'testpass',
roles: [{ role: 'readWriteAnyDatabase', db: 'admin' }],
},
],
args: ['--keyFile', keyFile, '--setParameter', 'enableTestCommands=true'],
});
cluster = await MongoCluster.deserialize(cluster.serialize());
const doc = await cluster.withClient(
async (client) => {
expect(
(await client.db('admin').command({ connectionStatus: 1 })).authInfo
.authenticatedUsers,
).to.deep.equal([{ user: '__system', db: 'local' }]);
return await client.db('config').collection('mongodbrunner').findOne();
},
{ auth: { username: '__system', password: 'secret' } },
);
expect(doc?._id).to.be.a('string');
await cluster.close();
});

it('can let callers listen for server log events', async function () {
cluster = await MongoCluster.start({
version: '8.x',
Expand Down Expand Up @@ -630,4 +689,53 @@ 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');
});
});
});
Loading
Loading