mongo/jstests/extensions/logging.js
Enrico Golfieri be4a3e3e27 SERVER-117147 Deploy 3 mongos for the extensions_* suites (#46672)
GitOrigin-RevId: 134783b2350f98658129603e8240b4d36a280cb2
2026-01-28 18:43:33 +00:00

118 lines
3.7 KiB
JavaScript

/**
* Tests that the $log extension stage works end-to-end, populating Info, Warning, and Error log lines.
*
* @tags: [featureFlagExtensionsAPI]
*/
// The test attempts to run some aggregation stages and expect to see specific log lines on the mongos (via getLog).
// In case of multiple mongos, the aggregation and the getLog may hit different nodes, causing test failures.
// pinToSingleMongos due to getLog command.
TestData.pinToSingleMongos = true;
const coll = db[jsTestName()];
coll.drop();
const testData = [
{_id: 0, x: 1},
{_id: 1, x: 2},
{_id: 2, x: 3},
];
assert.commandWorked(coll.insertMany(testData));
function testLogStage({
pipeline,
expectedInfoLogCount,
expectedAttrs,
expectedOtherLog: {expectedLogCode, expectedLogMessage, severity} = {},
}) {
// Clear the logs before running the aggregation.
assert.commandWorked(db.adminCommand({clearLog: "global"}));
// Run the aggregation with the $log stage.
assert.commandWorked(db.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}}));
// First check the correct number of info log lines were printed.
let infoLogs = checkLog.getFilteredLogMessages(db, 11134004, {}, "I");
assert.eq(infoLogs.length, expectedInfoLogCount, tojson(infoLogs));
for (let log of infoLogs) {
assert.eq(log.msg, "Logging info line for $log");
assert.eq(log.c, "EXTENSION-MONGOT");
if (expectedAttrs) {
assert.eq(log.attr, expectedAttrs, log);
}
}
// Then check any expected non-info log message appear as expected.
if (expectedLogCode) {
let otherLogs = checkLog.getFilteredLogMessages(db, expectedLogCode, {}, severity);
for (let log of otherLogs) {
assert.eq(log.msg, expectedLogMessage);
assert.eq(log.c, "EXTENSION-MONGOT");
}
}
}
// Test that the $log stage logs info log lines as expected.
testLogStage({
pipeline: [{$log: {numInfoLogLines: 1}}],
expectedInfoLogCount: 1,
});
testLogStage({
pipeline: [{$log: {numInfoLogLines: 3}}],
expectedInfoLogCount: 3,
});
testLogStage({
pipeline: [{$log: {numInfoLogLines: 5}}],
expectedInfoLogCount: 5,
});
testLogStage({
pipeline: [{$log: {numInfoLogLines: 1, attrs: {a: "hi", b: "12345"}}}],
expectedInfoLogCount: 1,
expectedAttrs: {attr: {a: "hi", b: "12345"}},
});
// Test that the $log stage with an empty spec or non-object spec logs an error but no info log.
testLogStage({
pipeline: [{$log: {}}],
expectedInfoLogCount: 0,
expectedOtherLog: {
expectedLogCode: 11134000,
expectedLogMessage: "$log stage spec is empty or not an object.",
expectedLogCount: 1,
severity: "E",
},
});
// Test that the $log stage with missing numInfoLogLines logs a warning.
testLogStage({
pipeline: [{$log: {otherField: 1}}],
expectedInfoLogCount: 0,
expectedOtherLog: {
expectedLogCode: 11134001,
expectedLogMessage: "$log stage missing or invalid numInfoLogLines field.",
severity: "W",
},
});
// Test that the $log stage with an numInfoLogLines < 0 logs a warning.
testLogStage({
pipeline: [{$log: {numInfoLogLines: -1}}],
expectedInfoLogCount: 0,
expectedOtherLog: {
expectedLogCode: 11134002,
expectedLogMessage: "$log stage must have non-negative value for numInfoLogLines.",
severity: "W",
},
});
// Test that the $log stage with an numInfoLogLines > 5 logs a warning and 5 info log lines.
testLogStage({
pipeline: [{$log: {numInfoLogLines: 99}}],
expectedInfoLogCount: 5,
expectedOtherLog: {
expectedLogCode: 11134003,
expectedLogMessage: "$log stage will not print more than 5 log lines.",
severity: "W",
},
});