summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/tassert_failpoint.js
blob: e2ea5479c3783ee957312a8c306c02d1656e59fe (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
 * Configures the failCommand failpoint to test the firing of a tripwire assertion (tassert).
 */
(function() {
'use strict';

let conn, testDB, adminDB;

const mongoRunnerSetupHelper = () => {
    conn = MongoRunner.runMongod({});
    testDB = conn.getDB("tassert_failpoint");
    adminDB = conn.getDB("admin");
};

/**
 * Helper for verifying the server exits with `exitCode`.
 */
const mongoRunnerExitHelper = (exitCode) => {
    assert.commandWorked(adminDB.runCommand({configureFailPoint: "failCommand", mode: "off"}));
    assert.eq(exitCode, MongoRunner.stopMongod(conn, null, {allowedExitCode: exitCode}));
};

// test with a tassert: true and closeConnection:true configuration. This should fire a tassert.
mongoRunnerSetupHelper();

assert.commandWorked(adminDB.runCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {
        failCommands: ["ping"],
        closeConnection: true,
        tassert: true,
    }
}));

assert.throws(() => testDB.runCommand({ping: 1}));
mongoRunnerExitHelper(MongoRunner.EXIT_ABRUPT);

// test with a tassert:true and extraInfo:true configuration. This should fire a tassert.
mongoRunnerSetupHelper();

assert.commandWorked(adminDB.runCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {
        errorCode: ErrorCodes.CannotImplicitlyCreateCollection,
        failCommands: ["create"],
        tassert: true,
        errorExtraInfo: {
            "ns": "namespace error",
        }
    }
}));

{
    let result = testDB.runCommand({create: "collection"});
    assert(result.ok == 0);
    assert(result.code == ErrorCodes.CannotImplicitlyCreateCollection);
    assert(result.ns == "namespace error");
}

mongoRunnerExitHelper(MongoRunner.EXIT_ABRUPT);

// test with a tassert:true and errorCode-only configuration. This should fire a tassert.
mongoRunnerSetupHelper();

assert.commandWorked(adminDB.runCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {
        errorCode: ErrorCodes.InvalidNamespace,
        failCommands: ["ping"],
        tassert: true,
    }
}));

{
    let result = testDB.runCommand({ping: 1});
    assert(result.code == ErrorCodes.InvalidNamespace);
}

mongoRunnerExitHelper(MongoRunner.EXIT_ABRUPT);

// test with a tassert: false and closeConnection:true configuration. This should NOT fire a
// tassert.
mongoRunnerSetupHelper();

assert.commandWorked(adminDB.runCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {
        failCommands: ["ping"],
        closeConnection: true,
        tassert: false,
    }
}));

assert.throws(() => testDB.runCommand({ping: 1}));
mongoRunnerExitHelper(MongoRunner.EXIT_CLEAN);

// test with a tassert:false and extraErrorInfo + errorCode configuration.
// This should NOT fire a tassert and should instead produce a uassert.
mongoRunnerSetupHelper();

assert.commandWorked(adminDB.runCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {
        errorCode: ErrorCodes.CannotImplicitlyCreateCollection,
        failCommands: ["create"],
        tassert: false,
        errorExtraInfo: {
            "ns": "namespace error",
        }
    }
}));

{
    let result = testDB.runCommand({create: "collection"});
    assert(result.ok == 0);
    assert(result.code == ErrorCodes.CannotImplicitlyCreateCollection);
    assert(result.ns == "namespace error");
}
mongoRunnerExitHelper(MongoRunner.EXIT_CLEAN);

// test with a tassert:false and errorCode-only configuration.
// This should NOT fire a tassert and should instead produce a uassert..
mongoRunnerSetupHelper();

assert.commandWorked(adminDB.runCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {
        errorCode: ErrorCodes.InvalidNamespace,
        failCommands: ["ping"],
        tassert: false,
    }
}));

{
    let result = testDB.runCommand({ping: 1});
    assert(result.code == ErrorCodes.InvalidNamespace);
}

mongoRunnerExitHelper(MongoRunner.EXIT_CLEAN);

// test with a tassert:true only configuration.
// This should NOT fire a tassert and should NOT produce an error.
// tassert should only be fired with one of the other settings.
mongoRunnerSetupHelper();

assert.commandWorked(adminDB.runCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {
        failCommands: ["ping"],
        tassert: true,
    }
}));

assert.commandWorked(testDB.runCommand({ping: 1}));

mongoRunnerExitHelper(MongoRunner.EXIT_CLEAN);
})();