Skip to content

Commit

Permalink
basic testing added, need default testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Nov 2, 2023
1 parent e3801c6 commit 1105d8b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 53 deletions.
14 changes: 12 additions & 2 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ export function parseOptions(
};
loggerClientOptions = {
mongodbLogPath: mongoOptions.mongodbLogPath,
mongodbLogComponentSeverities: mongoOptions.LogComponentSeverities,
mongodbLogComponentSeverities: mongoOptions.mongodbLogComponentSeverities,
mongodblogMaxDocumentLength: mongoOptions.mongodbLogMaxDocumentLength
};
}
Expand Down Expand Up @@ -1231,7 +1231,17 @@ export const OPTIONS = {
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogPath: { type: 'any' }
mongodbLogPath: { type: 'any' },
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogComponentSeverities: { type: 'any' },
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogMaxDocumentLength: { type: 'string' }
} as Record<keyof MongoClientOptions, OptionDescriptor>;

export const DEFAULT_OPTIONS = new CaseInsensitiveMap(
Expand Down
10 changes: 10 additions & 0 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable;
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions;
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogMaxDocumentLength?: string;

/** @internal */
[featureFlag: symbol]: any;
Expand Down
163 changes: 112 additions & 51 deletions test/unit/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const { ReadPreference } = require('../mongodb');
const { MongoCredentials } = require('../mongodb');
const { MongoClient, MongoParseError, ServerApiVersion } = require('../mongodb');
const { MongoLogger } = require('../mongodb');
const {
SeverityLevel,
MongoLoggableComponent,
MongoLoggerEnvOptions
// eslint-disable-next-line no-restricted-modules
} = require('../../src/mongo_logger');
const sinon = require('sinon');
const { Writable } = require('stream');

Expand Down Expand Up @@ -817,71 +823,126 @@ describe('MongoOptions', function () {
});
});

context('when mongodbLogPath is in options', function () {
describe('logging client options', function () {
const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
context('when mongodbLogPath is in options', function () {
let stderrStub;
let stdoutStub;

let stderrStub;
let stdoutStub;

beforeEach(() => {
stdoutStub = sinon.stub(process.stdout);
stderrStub = sinon.stub(process.stderr);
});
beforeEach(() => {
stdoutStub = sinon.stub(process.stdout);
stderrStub = sinon.stub(process.stderr);
});

afterEach(() => {
sinon.restore();
});
afterEach(() => {
sinon.restore();
});

context('when option is `stderr`', function () {
it('it is accessible through mongoLogger.logDestination', function () {
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: 'stderr'
context('when option is `stderr`', function () {
it('it is accessible through mongoLogger.logDestination', function () {
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: 'stderr'
});
const log = { t: new Date(), c: 'constructorStdErr', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
});
const log = { t: new Date(), c: 'constructorStdErr', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
});
});

context('when option is a MongoDBLogWritable stream', function () {
it('it is accessible through mongoLogger.logDestination', function () {
const writable = {
buffer: [],
write(log) {
this.buffer.push(log);
}
};
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: writable
context('when option is a MongoDBLogWritable stream', function () {
it('it is accessible through mongoLogger.logDestination', function () {
const writable = {
buffer: [],
write(log) {
this.buffer.push(log);
}
};
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: writable
});
expect(client.options.mongoLoggerOptions.logDestination).to.deep.equal(writable);
});
expect(client.options.mongoLoggerOptions.logDestination).to.deep.equal(writable);
});
});

context('when option is `stdout`', function () {
it('it is accessible through mongoLogger.logDestination', function () {
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: 'stdout'
context('when option is `stdout`', function () {
it('it is accessible through mongoLogger.logDestination', function () {
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: 'stdout'
});
const log = { t: new Date(), c: 'constructorStdOut', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
});
const log = { t: new Date(), c: 'constructorStdOut', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
});
});

context('when option is invalid', function () {
it('it defaults to stderr', function () {
const invalidOption = 'stdnothing';
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: invalidOption
context('when option is invalid', function () {
it('it defaults to stderr', function () {
const invalidOption = 'stdnothing';
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: invalidOption
});
const log = { t: new Date(), c: 'constructorInvalidOption', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stderrStub.write).calledWith(
inspect(log, { breakLength: Infinity, compact: true })
);
});
const log = { t: new Date(), c: 'constructorInvalidOption', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
});
});
describe.only('component severities', function () {
const components = Object.values(MongoLoggableComponent);
const env_component_names = [
'MONGODB_LOG_COMMAND',
'MONGODB_LOG_TOPOLOGY',
'MONGODB_LOG_SERVER_SELECTION',
'MONGODB_LOG_CONNECTION',
'MONGODB_LOG_CLIENT'
];
context('when only client option is provided for', function () {
for (let i = 0; i < components.length; i++) {
it(`it stores severity levels for ${components[i]} component correctly`, function () {
for (const severityLevel of Object.values(SeverityLevel)) {
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogComponentSeverities: {
[components[i]]: severityLevel
}
});
expect(client.options.mongoLoggerOptions.componentSeverities[components[i]]).to.equal(
severityLevel
);
}
});
}
});
context('when both client and environment option is provided', function () {
for (let i = 0; i < components.length; i++) {
beforeEach(function () {
process.env[env_component_names[i]] = 'emergency';
});

afterEach(function () {
process.env[env_component_names[i]] = undefined;
});

it(`it stores severity levels for ${components[i]} component correctly (client options have precedence)`, function () {
for (const severityLevel of Object.values(SeverityLevel)) {
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogComponentSeverities: {
[components[i]]: severityLevel
}
});
expect(client.options.mongoLoggerOptions.componentSeverities[components[i]]).to.equal(
severityLevel
);
}
});
};
});
});
});
Expand Down

0 comments on commit 1105d8b

Please sign in to comment.