summaryrefslogtreecommitdiff
path: root/jstests/replsets/test_command.js
blob: d8d5eb429847adcf07e8c4c5f68d928c88f4c48e (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
165
166
// Tests 'replSetTest' command:
//    waitForMemberState - waits for node's state to become 'expectedState'.
//    waitForDrainFinish - waits for primary to finish draining its applier queue.

(function () {
    'use strict';
    var name = 'test_command';
    var replSet = new ReplSetTest({name: name, nodes: 3});
    var nodes = replSet.nodeList();
    replSet.startSet();
    replSet.initiate({
        _id: name,
        members: [
            {_id: 0, host: nodes[0], priority: 3},
            {_id: 1, host: nodes[1]},
            {_id: 2, host: nodes[2], arbiterOnly: true},
        ],
    });

    // Stabilize replica set with node 0 as primary.

    assert.commandWorked(
        replSet.nodes[0].adminCommand({
            replSetTest: 1,
            waitForMemberState: ReplSetTest.State.PRIMARY,
            timeoutMillis: 60 * 1000,
        }),
        'node 0' + replSet.nodes[0].host + ' failed to become primary'
    );

    // We need the try/catch to handle that the node may have hung up the connection due
    // to a state change.
    try {
        assert.commandWorked(replSet.nodes[1].adminCommand({
            replSetTest: 1,
            waitForMemberState: ReplSetTest.State.SECONDARY,
            timeoutMillis: 60 * 1000,
        }));
    } catch (e) {
        jsTestLog(e);
        assert.commandWorked(replSet.nodes[1].adminCommand({
            replSetTest: 1,
            waitForMemberState: ReplSetTest.State.SECONDARY,
            timeoutMillis: 60 * 1000,
        }), 'node 1' + replSet.nodes[1].host + ' failed to become secondary');
    }

    var primary = replSet.getPrimary();
    var secondary = replSet.getSecondary();

    // Check replication mode.

    assert.commandFailedWithCode(
        primary.getDB(name).runCommand({
            replSetTest: 1,
        }),
        ErrorCodes.Unauthorized,
        'replSetTest should fail against non-admin database'
    );

    assert.commandWorked(
        primary.adminCommand({
            replSetTest: 1,
        }),
        'failed to check replication mode'
    );

    // waitForMemberState tests.

    assert.commandFailedWithCode(
        primary.adminCommand({
            replSetTest: 1,
            waitForMemberState: 'what state',
            timeoutMillis: 1000,
        }),
        ErrorCodes.TypeMismatch,
        'replSetTest waitForMemberState should fail on non-numerical state'
    );

    assert.commandFailedWithCode(
        primary.adminCommand({
            replSetTest: 1,
            waitForMemberState: ReplSetTest.State.PRIMARY,
            timeoutMillis: "what timeout",
        }),
        ErrorCodes.TypeMismatch,
        'replSetTest waitForMemberState should fail on non-numerical timeout'
    );

    assert.commandFailedWithCode(
        primary.adminCommand({
            replSetTest: 1,
            waitForMemberState: 9999,
            timeoutMillis: 1000,
        }),
        ErrorCodes.BadValue,
        'replSetTest waitForMemberState should fail on invalid state'
    );

    assert.commandFailedWithCode(
        primary.adminCommand({
            replSetTest: 1,
            waitForMemberState: ReplSetTest.State.PRIMARY,
            timeoutMillis: -1000,
        }),
        ErrorCodes.BadValue,
        'replSetTest waitForMemberState should fail on negative timeout'
    );

    assert.commandFailedWithCode(
        primary.adminCommand({
            replSetTest: 1,
            waitForMemberState: ReplSetTest.State.SECONDARY,
            timeoutMillis: 1000,
        }),
        ErrorCodes.ExceededTimeLimit,
        'replSetTest waitForMemberState(SECONDARY) should time out on node 0 ' +
        primary.host
    );

    assert.commandWorked(
        secondary.adminCommand({
            replSetTest: 1,
            waitForMemberState: ReplSetTest.State.SECONDARY,
            timeoutMillis: 1000,
        }),
        'replSetTest waitForMemberState(SECONDARY) failed on node 1 ' +
        secondary.host
    );

    // waitForDrainFinish tests.

    assert.commandFailedWithCode(
        primary.adminCommand({
            replSetTest: 1,
            waitForDrainFinish: 'what state',
        }),
        ErrorCodes.TypeMismatch,
        'replSetTest waitForDrainFinish should fail on non-numerical timeout'
    );

    assert.commandFailedWithCode(
        primary.adminCommand({
            replSetTest: 1,
            waitForDrainFinish: -1000,
        }),
        ErrorCodes.BadValue,
        'replSetTest waitForDrainFinish should fail on negative timeout'
    );

    assert.commandWorked(
        primary.adminCommand({
            replSetTest: 1,
            waitForDrainFinish: 1000,
        }),
        'node 0' + primary.host + ' failed to wait for drain to finish'
    );

    assert.commandWorked(
        secondary.adminCommand({
            replSetTest: 1,
            waitForDrainFinish: 0,
        }),
        'node 1' + primary.host + ' failed to wait for drain to finish'
    );
 })();