summaryrefslogtreecommitdiff
path: root/jstests/replsets/not_primary_errors_returned_if_client_sends_helloOk.js
blob: e8cca15064aba42fe505c4127132e4aaec605ae6 (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
/*
 * Tests that if a client sends "helloOk: true" as a part of their isMaster request,
 * mongod will replace "not master" error messages with "not primary".
 *
 * In practice, drivers will send "helloOk: true" in the initial handshake when
 * opening a connection to the database.
 */

(function() {
"use strict";

const dbName = "test";
const collName = "not_primary_errors_returned_if_client_sends_helloOk";
const rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();
const primary = rst.getPrimary();
const secondary = rst.getSecondary();

assert.commandWorked(primary.getDB(dbName)[collName].insert({x: 1}));
rst.awaitReplication();

// Attempts to write to a secondary should fail with NotWritablePrimary.
let res = assert.commandFailedWithCode(secondary.getDB(dbName)[collName].insert({y: 2}),
                                       ErrorCodes.NotWritablePrimary,
                                       "insert did not fail with NotWritablePrimary");
// Since the shell opens connections without using "helloOk: true", the error message
// should include "not master".
assert(res.errmsg.includes("not master"), res);

// Set secondaryOk to false, disallowing reads on secondaries.
secondary.getDB(dbName).getMongo().setSecondaryOk(false);
assert.eq(secondary.getDB(dbName).getMongo().getSecondaryOk(), false);
res = assert.commandFailedWithCode(secondary.getDB(dbName).runCommand({find: collName}),
                                   ErrorCodes.NotPrimaryNoSecondaryOk,
                                   "find did not fail with NotPrimaryNoSecondaryOk");
// Since we did not send "helloOk: true", the error message should include "not master".
assert(res.errmsg.includes("not master"), res);

// An isMaster response will not contain "helloOk: true" if the client does not send
// helloOk in the request.
res = assert.commandWorked(secondary.getDB(dbName).adminCommand({isMaster: 1}));
assert.eq(res.helloOk, undefined);

try {
    // "helloOk" field type must be a boolean.
    secondary.getDB(dbName).adminCommand({isMaster: 1, helloOk: 3});
} catch (e) {
    assert.eq(e.code, ErrorCodes.TypeMismatch, "invalid helloOk field did not fail to parse");
}

// Run the isMaster command with "helloOk: true" on the secondary.
res = assert.commandWorked(secondary.getDB(dbName).adminCommand({isMaster: 1, helloOk: true}));
// The response should contain "helloOk: true", which indicates to the client that the
// server supports the hello command.
assert.eq(res.helloOk, true);

// Attempts to write to a secondary should fail with NotWritablePrimary.
res = assert.commandFailedWithCode(secondary.getDB(dbName)[collName].insert({z: 3}),
                                   ErrorCodes.NotWritablePrimary,
                                   "insert did not fail with NotWritablePrimary");
// Since we sent "helloOk: true", the error message should include "not primary".
assert(res.errmsg.includes("not primary"), res);
assert(!res.errmsg.includes("not master"), res);

// secondaryOk was already set to false, so the following read should still fail with
// NotPrimaryNoSecondaryOk.
assert.eq(secondary.getDB(dbName).getMongo().getSecondaryOk(), false);
res = assert.commandFailedWithCode(secondary.getDB(dbName).runCommand({find: collName}),
                                   ErrorCodes.NotPrimaryNoSecondaryOk,
                                   "find did not fail with NotPrimaryNoSecondaryOk");
// Since we sent "helloOk: true", the error message should include "not primary".
assert(res.errmsg.includes("not primary"), res);
assert(!res.errmsg.includes("not master"), res);

rst.stopSet();
})();