summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/log_backtrace.js
blob: a5c7cd72fd90ade67a403e354e88cc21cca3eb90 (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
/**
 * Validate that backtraces are logged to a separate file
 *
 * @tags: [requires_persistence]
 */

(function() {
'use strict';

function waitFailedToStart(pid, exitCode) {
    assert.soon(function() {
        return !checkProgram(pid).alive;
    }, `Failed to wait for ${pid} to die`, 3 * 60 * 1000);

    assert.eq(exitCode,
              checkProgram(pid).exitCode,
              `Failed to wait for ${pid} to die with exit code ${exitCode}`);
}

function parseLogFile(file) {
    const result = cat(file);
    const json_str = result.split("\n")[0];
    try {
        return JSON.parse(json_str);
    } catch (e) {
        jsTestLog("Failed to parse: " + result + "\n" + json_str);
        throw e;
    }
}

// Run MongoD successfully so we populate the data directory
var m = MongoRunner.runMongod({});

assert.commandWorked(m.getDB("test").foo.insert({x: 1}));

MongoRunner.stopMongod(m);

// Make sure it is stopped before we manipulate the turtle file
waitFailedToStart(m.pid, 0);

// Sleep
sleep(5000);

const dbpath = m.dbpath.replace("\\", "/");
const backtraceLogFile = `backtrace.log`;
const backtraceLogFileFullPath = dbpath + "/" + backtraceLogFile;

print(dbpath);

// Corrupt the WiredTiger. turtle file to trigger a backtrace
const command = `echo xxxxxxxxxxxxxxxxxxxxx > ${dbpath}/WiredTiger.turtle`;

let ret;
if (_isWindows()) {
    ret = runProgram('cmd.exe', '/c', command);
} else {
    ret = runProgram('/bin/sh', '-c', command);
}

assert.eq(ret, 0);

// Restart MongoD with the corrupted turtle file
m = MongoRunner.runMongod({
    setParameter: "backtraceLogFile=" + backtraceLogFileFullPath,
    dbpath: dbpath,
    restart: true,
    cleanData: false,
    waitForConnect: false
});

if (_isWindows()) {
    waitFailedToStart(m.pid, 14);  // MongoRunner.EXIT_ABORT
} else {
    waitFailedToStart(m.pid, 6);  // MongoRunner.EXIT_ABORT
}

// Check we have one log line
let log = parseLogFile(backtraceLogFileFullPath);
assert.eq(log["id"], 31380);
assert.eq(log["msg"], "BACKTRACE");
})();