From 7184dd30da82f55aa9318823be518570657c3430 Mon Sep 17 00:00:00 2001 From: Erin McNulty Date: Thu, 22 Aug 2024 09:25:58 -0400 Subject: [PATCH] SERVER-93699: Remove shell-only testing code from opcounters_active.js (#26319) GitOrigin-RevId: 694acd84471d3000fb911cf5a993cbc96ed2191e --- .eslintrc.yml | 1 - jstests/core/opcounters_active.js | 35 ------------------------------- jstests/core/opcounters_basic.js | 12 +++++++++++ src/mongo/shell/db.js | 35 ++----------------------------- 4 files changed, 14 insertions(+), 69 deletions(-) delete mode 100644 jstests/core/opcounters_active.js create mode 100644 jstests/core/opcounters_basic.js diff --git a/.eslintrc.yml b/.eslintrc.yml index 01356b03277..de5260d22f4 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -184,7 +184,6 @@ globals: shellPrintHelper: true shellAutocomplete: true __autocomplete__: true - getActiveCommands: true defaultPrompt: true ___it___: true __promptWrapper__: true diff --git a/jstests/core/opcounters_active.js b/jstests/core/opcounters_active.js deleted file mode 100644 index 559af70196d..00000000000 --- a/jstests/core/opcounters_active.js +++ /dev/null @@ -1,35 +0,0 @@ -// checks that db.serverStatus will not throw errors when metrics tree is not present. -// -// @tags: [ -// # The test runs commands that are not allowed with security token: mapreduce. -// not_allowed_with_signed_security_token -// ] - -// Test the getActiveCommands function -// Should remove the listCollections section but keep the rest -var testInput = { - "isMaster": {"failed": NumberLong(0), "total": NumberLong(3)}, - "mapreduce": {"failed": NumberLong(0), "total": NumberLong(1)}, - "listCollections": {"failed": NumberLong(0), "total": NumberLong(0)} -}; -var testExpected = { - "isMaster": {"failed": NumberLong(0), "total": NumberLong(3)}, - "mapreduce": {"failed": NumberLong(0), "total": NumberLong(1)} -}; -var testResult = getActiveCommands(testInput); - -assert.eq(testResult, testExpected, "getActiveCommands did not return the expected result"); - -// Test that the serverstatus helper works -var result = db.serverStatus(); -assert.neq(undefined, result, tojson(result)); -// Test that the metrics tree returns -assert.neq(undefined, result.metrics, tojson(result)); -// Test that the metrics.commands tree returns -assert.neq(undefined, result.metrics.commands, tojson(result)); -// Test that the metrics.commands.serverStatus value is non-zero -assert.neq(0, result.metrics.commands.serverStatus.total, tojson(result)); - -// Test that the command returns successfully when no metrics tree is present -var result = db.serverStatus({"metrics": 0}); -assert.eq(undefined, result.metrics, tojson(result)); diff --git a/jstests/core/opcounters_basic.js b/jstests/core/opcounters_basic.js new file mode 100644 index 00000000000..916eabdc0f8 --- /dev/null +++ b/jstests/core/opcounters_basic.js @@ -0,0 +1,12 @@ +// Checks that db.serverStatus will not throw errors when metrics tree is not present. + +{ + const result = db.serverStatus().metrics.commands; + // Test that the metrics.commands.serverStatus value is non-zero + assert.neq(0, db.serverStatus().metrics.commands.serverStatus.total, tojson(result)); +} +{ + // Test that the command returns successfully when no metrics tree is present + const result = db.serverStatus({"metrics": 0}); + assert.eq(undefined, result.metrics, tojson(result)); +} diff --git a/src/mongo/shell/db.js b/src/mongo/shell/db.js index 460fbd0343c..6f841a53d21 100644 --- a/src/mongo/shell/db.js +++ b/src/mongo/shell/db.js @@ -1046,44 +1046,13 @@ DB.prototype.serverBuildInfo = function() { this.getSiblingDB("admin")._runCommandWithoutApiStrict({buildinfo: 1})); }; -// Used to trim entries from the metrics.commands that have never been executed -getActiveCommands = function(tree) { - var result = {}; - for (var i in tree) { - if (!tree.hasOwnProperty(i)) - continue; - if (tree[i].hasOwnProperty("total")) { - if (tree[i].total > 0) { - result[i] = tree[i]; - } - continue; - } - if (i == "") { - if (tree[i] > 0) { - result[i] = tree[i]; - } - continue; - } - // Handles nested commands - var subStatus = getActiveCommands(tree[i]); - if (Object.keys(subStatus).length > 0) { - result[i] = tree[i]; - } - } - return result; -}; - DB.prototype.serverStatus = function(options) { var cmd = {serverStatus: 1}; if (options) { Object.extend(cmd, options); } - var res = this._adminCommand(cmd); - // Only prune if we have a metrics tree with commands. - if (res.metrics && res.metrics.commands) { - res.metrics.commands = getActiveCommands(res.metrics.commands); - } - return res; + + return this._adminCommand(cmd); }; DB.prototype.hostInfo = function() {