From 1105d8b377467c6692f694ff0772101fd406bf36 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Thu, 2 Nov 2023 16:28:23 -0400 Subject: [PATCH] basic testing added, need default testing --- src/connection_string.ts | 14 ++- src/mongo_client.ts | 10 ++ test/unit/mongo_client.test.js | 163 ++++++++++++++++++++++----------- 3 files changed, 134 insertions(+), 53 deletions(-) diff --git a/src/connection_string.ts b/src/connection_string.ts index 4b0a30beb37..07db16383ba 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -531,7 +531,7 @@ export function parseOptions( }; loggerClientOptions = { mongodbLogPath: mongoOptions.mongodbLogPath, - mongodbLogComponentSeverities: mongoOptions.LogComponentSeverities, + mongodbLogComponentSeverities: mongoOptions.mongodbLogComponentSeverities, mongodblogMaxDocumentLength: mongoOptions.mongodbLogMaxDocumentLength }; } @@ -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; export const DEFAULT_OPTIONS = new CaseInsensitiveMap( diff --git a/src/mongo_client.ts b/src/mongo_client.ts index ff17c4f1f87..af13c4f618d 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -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; diff --git a/test/unit/mongo_client.test.js b/test/unit/mongo_client.test.js index 8757146baf3..b27c6668bfc 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -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'); @@ -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 + ); + } + }); + }; }); }); });