summaryrefslogtreecommitdiff
path: root/jstests/core/apitest_db_profile_level.js
blob: 733c4df87415b429f29481741a61b515cc7647e6 (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
/**
 *  Tests for setting of profile levels
 *  @tags: [does_not_support_stepdowns, requires_profiling, requires_fcv_47]
 */

(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.
var profileLevelDB = db.getSiblingDB("apitest_db_profile_level");

// Checks for the log that was expected to be created when profile level changed.
function profilerChangeWasLogged({from, to} = {}) {
    const globalLog = assert.commandWorked(profileLevelDB.adminCommand({getLog: 'global'}));

    const fieldMatcher = {msg: "Profiler settings changed"};
    if (from && to) {
        const lines = [...findMatchingLogLines(globalLog.log, fieldMatcher)];
        return lines.find(line => line.match(new RegExp(/"from":{/.source + from.source)) &&
                              line.match(new RegExp(/"to":{/.source + to.source)));
    } else {
        return findMatchingLogLine(globalLog.log, fieldMatcher);
    }
}

profileLevelDB.getProfilingLevel();
assert(!profilerChangeWasLogged({from: /"level":0/, to: /"level":-1/}),
       "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: /"level":0/, to: /"level":0/}),
       "Didn't find expected log line");

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

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

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

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