summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/set_read_preference_secondary_test.js
blob: 475f7a5908bae48674f1ebdd4e235f09a96af448 (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
/**
 * @tags: [requires_sharding]
 *
 * Tests that the runCommandWithReadPreferenceSecondary override refuses to duplicate a
 * readPreference that already exists inside the top-level command object.
 */

(function() {
'use strict';

load('jstests/libs/override_methods/set_read_preference_secondary.js');

const st = new ShardingTest({shards: 1});

// We should never override any read preference that was explicitly set.
let err = assert.throws(() => {
    assert.commandWorked(st.s.getDB('db').runCommand({
        find: 'foo',
        $readPreference: {mode: 'nearest'},
    }));
});

assert(err.message.startsWith('Cowardly refusing to override read preference'));

// Setting secondary read preference ourselves should work without error.
assert.commandWorked(st.s.getDB('db').runCommand({
    find: 'foo',
    $readPreference: {mode: 'secondary'},
}));

// An unset read preference should be overriden to secondary.
assert.commandWorked(st.s.getDB('db').runCommand({find: 'foo'}));

// Necessary to turn this off so that ShardingTest post-test hooks don't fail by erroneously
// performing reads against secondary nodes.
TestData.doNotOverrideReadPreference = true;

st.stop();
})();