summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/exit_logging.js
blob: 468c58a928c1eb0195707aa89def4734000c31ba (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
 * Tests that various forms of normal and abnormal shutdown write to the log files as expected.
 * @tags: [
 *   requires_sharding,
 * ]
 */

(function() {

function makeShutdownByCrashFn(crashHow) {
    return function(conn) {
        var admin = conn.getDB("admin");
        assert.commandWorked(admin.runCommand(
            {configureFailPoint: "crashOnShutdown", mode: "alwaysOn", data: {how: crashHow}}));
        admin.shutdownServer();
    };
}

function makeRegExMatchFn(pattern) {
    return function(text) {
        return pattern.test(text);
    };
}

function testShutdownLogging(launcher, crashFn, matchFn, expectedExitCode) {
    clearRawMongoProgramOutput();
    var conn = launcher.start({});

    function checkOutput() {
        var logContents = rawMongoProgramOutput();
        function printLog() {
            // We can't just return a string because it will be well over the max
            // line length.
            // So we just print manually.
            print("================ BEGIN LOG CONTENTS ==================");
            logContents.split(/\n/).forEach((line) => {
                print(line);
            });
            print("================ END LOG CONTENTS =====================");
            return "";
        }

        assert(matchFn(logContents), printLog);
    }

    crashFn(conn);
    launcher.stop(conn, undefined, {allowedExitCode: expectedExitCode});
    checkOutput();
}

function runAllTests(launcher) {
    const SIGSEGV = 11;
    const SIGABRT = 6;
    testShutdownLogging(launcher, function(conn) {
        conn.getDB('admin').shutdownServer();
    }, makeRegExMatchFn(/Terminating via shutdown command/), MongoRunner.EXIT_CLEAN);

    testShutdownLogging(launcher,
                        makeShutdownByCrashFn('fault'),
                        makeRegExMatchFn(/Invalid access at address[\s\S]*printStackTrace/),
                        SIGSEGV);

    testShutdownLogging(launcher,
                        makeShutdownByCrashFn('abort'),
                        makeRegExMatchFn(/Got signal[\s\S]*printStackTrace/),
                        SIGABRT);
}

if (_isWindows()) {
    print("SKIPPING TEST ON WINDOWS");
    return;
}

if (_isAddressSanitizerActive()) {
    print("SKIPPING TEST ON ADDRESS SANITIZER BUILD");
    return;
}

(function testMongod() {
    print("********************\nTesting exit logging in mongod\n********************");

    runAllTests({
        start: function(opts) {
            return MongoRunner.runMongod(opts);
        },

        stop: MongoRunner.stopMongod
    });
}());

(function testMongos() {
    print("********************\nTesting exit logging in mongos\n********************");

    var st = new ShardingTest({shards: 1});
    var mongosLauncher = {
        start: function(opts) {
            var actualOpts = {configdb: st._configDB};
            Object.extend(actualOpts, opts);
            return MongoRunner.runMongos(actualOpts);
        },

        stop: MongoRunner.stopMongos
    };

    runAllTests(mongosLauncher);
    st.stop();
}());
}());