|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import { DEFAULT_MAX_DOCUMENT_LENGTH, type Document } from '../../mongodb'; |
| 4 | + |
| 5 | +describe('Command Logging and Monitoring Prose Tests', function () { |
| 6 | + const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger'); |
| 7 | + const ELLIPSES_LENGTH = 3; |
| 8 | + context('When no custom truncation limit is provided', function () { |
| 9 | + /* |
| 10 | + 1. Configure logging with a minimum severity level of "debug" for the "command" component. |
| 11 | + Do not explicitly configure the max document length. |
| 12 | +
|
| 13 | + 2. Construct an array docs containing the document {"x" : "y"} repeated 100 times. |
| 14 | +
|
| 15 | + 3. Insert docs to a collection via insertMany. |
| 16 | +
|
| 17 | + 4. Inspect the resulting "command started" log message and assert that |
| 18 | + the "command" value is a string of length 1000 + (length of trailing ellipsis). |
| 19 | +
|
| 20 | + 5. Inspect the resulting "command succeeded" log message and assert that |
| 21 | + the "reply" value is a string of length <= 1000 + (length of trailing ellipsis). |
| 22 | +
|
| 23 | + 6. Run find() on the collection where the document was inserted. |
| 24 | +
|
| 25 | + 7. Inspect the resulting "command succeeded" log message and assert that |
| 26 | + the reply is a string of length 1000 + (length of trailing ellipsis). |
| 27 | + */ |
| 28 | + |
| 29 | + it('should follow default truncation limit of 1000', { |
| 30 | + metadata: {}, |
| 31 | + test: async function () { |
| 32 | + const writable = { |
| 33 | + buffer: [], |
| 34 | + write(log) { |
| 35 | + this.buffer.push(log); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + // 1. |
| 40 | + const client = this.configuration.newClient( |
| 41 | + {}, |
| 42 | + { |
| 43 | + [loggerFeatureFlag]: true, |
| 44 | + mongodbLogPath: writable, |
| 45 | + mongodbLogComponentSeverities: { |
| 46 | + command: 'debug' |
| 47 | + } |
| 48 | + } |
| 49 | + ); |
| 50 | + |
| 51 | + // 2. |
| 52 | + const docs: Array<Document> = []; |
| 53 | + for (let i = 0; i < 100; i++) { |
| 54 | + docs.push({ x: 'y' }); |
| 55 | + } |
| 56 | + |
| 57 | + // 3. |
| 58 | + await client.db('admin').collection('test').insertMany(docs); |
| 59 | + |
| 60 | + // 4. |
| 61 | + const insertManyCommandStarted = writable.buffer[2]; |
| 62 | + expect(insertManyCommandStarted?.message).to.equal('Command started'); |
| 63 | + expect(insertManyCommandStarted?.command).to.be.a('string'); |
| 64 | + expect(insertManyCommandStarted?.command?.length).to.equal( |
| 65 | + DEFAULT_MAX_DOCUMENT_LENGTH + ELLIPSES_LENGTH |
| 66 | + ); |
| 67 | + |
| 68 | + // 5. |
| 69 | + const insertManyCommandSucceeded = writable.buffer[3]; |
| 70 | + expect(insertManyCommandSucceeded?.message).to.equal('Command succeeded'); |
| 71 | + expect(insertManyCommandSucceeded?.reply).to.be.a('string'); |
| 72 | + expect(insertManyCommandSucceeded?.reply?.length).to.be.at.most( |
| 73 | + DEFAULT_MAX_DOCUMENT_LENGTH + ELLIPSES_LENGTH |
| 74 | + ); |
| 75 | + |
| 76 | + // 6. |
| 77 | + await client.db('admin').collection('test').find()._initialize(); |
| 78 | + |
| 79 | + // 7. |
| 80 | + const findCommandSucceeded = writable.buffer[5]; |
| 81 | + expect(findCommandSucceeded?.message).to.equal('Command succeeded'); |
| 82 | + expect(findCommandSucceeded?.reply).to.be.a('string'); |
| 83 | + expect(findCommandSucceeded?.reply?.length).to.equal( |
| 84 | + DEFAULT_MAX_DOCUMENT_LENGTH + ELLIPSES_LENGTH |
| 85 | + ); |
| 86 | + |
| 87 | + await client.close(); |
| 88 | + } |
| 89 | + }); |
| 90 | + }); |
| 91 | + context('When custom truncation limit is provided', function () { |
| 92 | + /* |
| 93 | + 1. Configure logging with a minimum severity level of "debug" for the "command" component. |
| 94 | + Set the max document length to 5. |
| 95 | +
|
| 96 | + 2. Run the command {"hello": true}. |
| 97 | +
|
| 98 | + 3. Inspect the resulting "command started" log message and assert that |
| 99 | + the "command" value is a string of length 5 + (length of trailing ellipsis). |
| 100 | +
|
| 101 | + 4. Inspect the resulting "command succeeded" log message and assert that |
| 102 | + the "reply" value is a string of length 5 + (length of trailing ellipsis). |
| 103 | + |
| 104 | + 5. (Optional) |
| 105 | + If the driver attaches raw server responses to failures |
| 106 | + and can access these via log messages to assert on, |
| 107 | + run the command {"notARealCommand": true}. |
| 108 | +
|
| 109 | + Inspect the resulting "command failed" log message |
| 110 | + and confirm that the server error is a string of length 5 + (length of trailing ellipsis). |
| 111 | + */ |
| 112 | + it('should follow custom truncation limit', { |
| 113 | + metadata: {}, |
| 114 | + test: async function () { |
| 115 | + const writable = { |
| 116 | + buffer: [], |
| 117 | + write(log) { |
| 118 | + this.buffer.push(log); |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + // 1. |
| 123 | + const client = this.configuration.newClient( |
| 124 | + {}, |
| 125 | + { |
| 126 | + [loggerFeatureFlag]: true, |
| 127 | + mongodbLogPath: writable, |
| 128 | + mongodbLogComponentSeverities: { |
| 129 | + command: 'debug' |
| 130 | + }, |
| 131 | + mongodbLogMaxDocumentLength: 5 |
| 132 | + } |
| 133 | + ); |
| 134 | + |
| 135 | + // 2. |
| 136 | + await client.db('admin').command({ hello: 'true' }); |
| 137 | + |
| 138 | + // 3. |
| 139 | + const insertManyCommandStarted = writable.buffer[2]; |
| 140 | + expect(insertManyCommandStarted?.message).to.equal('Command started'); |
| 141 | + expect(insertManyCommandStarted?.command).to.be.a('string'); |
| 142 | + expect(insertManyCommandStarted?.command?.length).to.equal(5 + ELLIPSES_LENGTH); |
| 143 | + |
| 144 | + // 4. |
| 145 | + const insertManyCommandSucceeded = writable.buffer[3]; |
| 146 | + expect(insertManyCommandSucceeded?.message).to.equal('Command succeeded'); |
| 147 | + expect(insertManyCommandSucceeded?.reply).to.be.a('string'); |
| 148 | + expect(insertManyCommandSucceeded?.reply?.length).to.be.at.most(5 + ELLIPSES_LENGTH); |
| 149 | + |
| 150 | + await client.close(); |
| 151 | + } |
| 152 | + }); |
| 153 | + }); |
| 154 | + context('Truncation with multi-byte codepoints', function () { |
| 155 | + /* |
| 156 | + A specific test case is not provided here due to the allowed variations in truncation logic |
| 157 | + as well as varying extended JSON whitespace usage. |
| 158 | + Drivers MUST write language-specific tests that confirm truncation of commands, replies, |
| 159 | + and (if applicable) server responses included in error messages |
| 160 | + work as expected when the data being truncated includes multi-byte Unicode codepoints. |
| 161 | + |
| 162 | + If the driver uses anything other than Unicode codepoints as the unit for max document length, |
| 163 | + there also MUST be tests confirming that cases |
| 164 | + where the max length falls in the middle of a multi-byte codepoint are handled gracefully. |
| 165 | + */ |
| 166 | + it('should handle unicode codepoints in middle and end of truncation gracefully', { |
| 167 | + metadata: {}, |
| 168 | + test: async function () { |
| 169 | + const writable = { |
| 170 | + buffer: [], |
| 171 | + write(log) { |
| 172 | + this.buffer.push(log); |
| 173 | + } |
| 174 | + }; |
| 175 | + |
| 176 | + const client = this.configuration.newClient( |
| 177 | + {}, |
| 178 | + { |
| 179 | + [loggerFeatureFlag]: true, |
| 180 | + mongodbLogPath: writable, |
| 181 | + mongodbLogComponentSeverities: { |
| 182 | + command: 'debug' |
| 183 | + }, |
| 184 | + mongodbLogMaxDocumentLength: 50 |
| 185 | + } |
| 186 | + ); |
| 187 | + |
| 188 | + const docs: Array<Document> = [{ x: '&&&&&&&&&&&&&&&&' }]; |
| 189 | + |
| 190 | + await client.db('admin').collection('test').insertMany(docs); |
| 191 | + |
| 192 | + const insertManyCommandStarted = writable.buffer[2]; |
| 193 | + expect(insertManyCommandStarted?.message).to.equal('Command started'); |
| 194 | + expect(insertManyCommandStarted?.command).to.be.a('string'); |
| 195 | + expect(insertManyCommandStarted?.command?.length).to.equal(50 + ELLIPSES_LENGTH); |
| 196 | + |
| 197 | + await client.close(); |
| 198 | + } |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments