summaryrefslogtreecommitdiff
path: root/jstests/core/apitest_db_profile_level.js
blob: f32b996f8ce180f1baa3bf6160423025e584e92b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 *  Tests for setting of profile levels
 * @tags: [
 *   does_not_support_stepdowns,
 *   requires_profiling,
 *   requires_fcv_62,
 * ]
 */

(function() {
'use strict';

load("jstests/libs/log.js");  // For findMatchingLogLine, findMatchingLogLines.

/*
 *  be sure the public collection API is complete
 */
assert(db.getProfilingLevel, "getProfilingLevel");
assert(db.setProfilingLevel, "setProfilingLevel");

// A test-specific database is used for profiler testing so as not to interfere with
// other tests that modify profiler level, when run in parallel.
const profileLevelDB = db.getSiblingDB("apitest_db_profile_level");

// Checks the log for the expected change in profile level and applicable database.
function profilerChangeWasLogged({from, to, db}) {
    const globalLog = assert.commandWorked(profileLevelDB.adminCommand({getLog: 'global'}));

    const fieldMatcher = {msg: "Profiler settings changed"};
    const lines = [...findMatchingLogLines(globalLog.log, fieldMatcher)];
    const matches = lines.filter((line) => {
        const attr = JSON.parse(line).attr;
        return attr.from.level == from && attr.to.level == to && attr.db == db;
    });
    return matches.length ? matches : false;
}

profileLevelDB.getProfilingLevel();
assert(!profilerChangeWasLogged({from: 0, to: -1, db: profileLevelDB}),
       "Didn't expect anything to be logged");

assert.throws(() => {
    profileLevelDB.setProfilingLevel(-1);
});

profileLevelDB.setProfilingLevel(0);
assert(profileLevelDB.getProfilingLevel() == 0, "prof level 0");
assert(profilerChangeWasLogged({from: 0, to: 0, db: profileLevelDB}),
       "Didn't find expected log line");

profileLevelDB.setProfilingLevel(1);
assert(profileLevelDB.getProfilingLevel() == 1, "p1");
assert(profilerChangeWasLogged({from: 0, to: 1, db: profileLevelDB}),
       "Didn't find expected log line");

profileLevelDB.setProfilingLevel(2);
assert(profileLevelDB.getProfilingLevel() == 2, "p2");
assert(profilerChangeWasLogged({from: 1, to: 2, db: profileLevelDB}),
       "Didn't find expected log line");

profileLevelDB.setProfilingLevel(0);
assert(profileLevelDB.getProfilingLevel() == 0, "prof level 0");
assert(profilerChangeWasLogged({from: 2, to: 0, db: profileLevelDB}),
       "Didn't find expected log line");

assert.throws(() => {
    profileLevelDB.setProfilingLevel(10);
});
// Check that didn't log an invalid profile level change.
assert(!profilerChangeWasLogged({from: 0, to: 10, db: profileLevelDB}), "Didn't expect log line");
})();