summaryrefslogtreecommitdiff
path: root/src/mongo/rpc/metadata/server_selection_metadata_test.cpp
blob: ae8d4057b45d666594c86c486ea54e3532154c3f (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *    Copyright (C) 2015 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include <utility>

#include "mongo/base/status.h"
#include "mongo/client/dbclientinterface.h"
#include "mongo/client/read_preference.h"
#include "mongo/db/jsobj.h"
#include "mongo/rpc/metadata/server_selection_metadata.h"
#include "mongo/unittest/unittest.h"

namespace {
using namespace mongo;
using namespace mongo::rpc;
using mongo::unittest::assertGet;

ServerSelectionMetadata checkParse(const BSONObj& metadata) {
    return assertGet(ServerSelectionMetadata::readFromMetadata(metadata));
}

TEST(ServerSelectionMetadata, ReadFromMetadata) {
    {
        // Empty object - should work just fine.
        auto ss = checkParse(BSONObj());
        ASSERT_FALSE(ss.isSecondaryOk());
        ASSERT_FALSE(ss.getReadPreference().is_initialized());
    }
    {
        // Set secondaryOk but not readPreference.
        auto ss = checkParse(BSON("$secondaryOk" << 1));
        ASSERT_TRUE(ss.isSecondaryOk());
        ASSERT_FALSE(ss.getReadPreference().is_initialized());
    }
    {
        // Set readPreference but not secondaryOk.
        auto ss = checkParse(BSON("$readPreference" << BSON("mode"
                                                            << "primary")));
        ASSERT_FALSE(ss.isSecondaryOk());
        ASSERT_TRUE(ss.getReadPreference().is_initialized());
        ASSERT_TRUE(ss.getReadPreference()->pref == ReadPreference::PrimaryOnly);
    }
    {
        // Set both.
        auto ss = checkParse(
            BSON("$secondaryOk" << 1 << "$readPreference" << BSON("mode"
                                                                  << "secondaryPreferred")));
        ASSERT_TRUE(ss.isSecondaryOk());
        ASSERT_TRUE(ss.getReadPreference()->pref == ReadPreference::SecondaryPreferred);
    }
}

void checkUpconvert(const BSONObj& legacyCommand,
                    const int legacyQueryFlags,
                    const BSONObj& upconvertedCommand,
                    const BSONObj& upconvertedMetadata) {
    BSONObjBuilder upconvertedCommandBob;
    BSONObjBuilder upconvertedMetadataBob;
    auto convertStatus = ServerSelectionMetadata::upconvert(
        legacyCommand, legacyQueryFlags, &upconvertedCommandBob, &upconvertedMetadataBob);
    ASSERT_OK(convertStatus);
    // We don't care about the order of the fields in the metadata object
    const auto sorted = [](const BSONObj& obj) {
        BSONObjIteratorSorted iter(obj);
        BSONObjBuilder bob;
        while (iter.more()) {
            bob.append(iter.next());
        }
        return bob.obj();
    };

    ASSERT_EQ(upconvertedCommand, upconvertedCommandBob.done());
    ASSERT_EQ(sorted(upconvertedMetadata), sorted(upconvertedMetadataBob.done()));
}

TEST(ServerSelectionMetadata, UpconvertValidMetadata) {
    // Wrapped in $query, with readPref and slaveOk bit set.
    checkUpconvert(BSON("$query" << BSON("ping" << 1) << "$readPreference" << BSON("mode"
                                                                                   << "secondary")),
                   mongo::QueryOption_SlaveOk,
                   BSON("ping" << 1),
                   BSON("$secondaryOk" << 1 << "$readPreference" << BSON("mode"
                                                                         << "secondary")));

    // Wrapped in 'query', with readPref.
    checkUpconvert(BSON("query" << BSON("pong" << 1 << "foo"
                                               << "bar") << "$readPreference"
                                << BSON("mode"
                                        << "primary"
                                        << "tags" << BSON("dc"
                                                          << "ny"))),
                   0,
                   BSON("pong" << 1 << "foo"
                               << "bar"),
                   BSON("$readPreference" << BSON("mode"
                                                  << "primary"
                                                  << "tags" << BSON("dc"
                                                                    << "ny"))));
    // Unwrapped, no readPref, no slaveOk
    checkUpconvert(BSON("ping" << 1), 0, BSON("ping" << 1), BSONObj());

    // Readpref wrapped in $queryOptions
    checkUpconvert(BSON("pang"
                        << "pong"
                        << "$queryOptions"
                        << BSON("$readPreference" << BSON("mode"
                                                          << "nearest"
                                                          << "tags" << BSON("rack"
                                                                            << "city")))),
                   0,
                   BSON("pang"
                        << "pong"),
                   BSON("$readPreference" << BSON("mode"
                                                  << "nearest"
                                                  << "tags" << BSON("rack"
                                                                    << "city"))));
}

void checkUpconvertFails(const BSONObj& legacyCommand, ErrorCodes::Error error) {
    BSONObjBuilder upconvertedCommandBob;
    BSONObjBuilder upconvertedMetadataBob;
    auto upconvertStatus = ServerSelectionMetadata::upconvert(
        legacyCommand, 0, &upconvertedCommandBob, &upconvertedMetadataBob);
    ASSERT_NOT_OK(upconvertStatus);
    ASSERT_EQ(upconvertStatus.code(), error);
}

TEST(ServerSelectionMetadata, UpconvertInvalidMetadata) {
    // $readPreference not an object.
    checkUpconvertFails(BSON("$query" << BSON("pang"
                                              << "pong") << "$readPreference" << 2),
                        ErrorCodes::TypeMismatch);

    // has $maxTimeMS option
    checkUpconvertFails(BSON("query" << BSON("foo"
                                             << "bar") << "$maxTimeMS" << 200),
                        ErrorCodes::InvalidOptions);
    checkUpconvertFails(BSON("$query" << BSON("foo"
                                              << "bar") << "$maxTimeMS" << 200),
                        ErrorCodes::InvalidOptions);

    // has $queryOptions field, but invalid $readPreference
    checkUpconvertFails(BSON("ping"
                             << "pong"
                             << "$queryOptions" << BSON("$readPreference" << 1.2)),
                        ErrorCodes::TypeMismatch);

    // has $queryOptions field, but no $readPreference
    checkUpconvertFails(BSON("ping"
                             << "pong"
                             << "$queryOptions" << BSONObj()),
                        ErrorCodes::NoSuchKey);
}

}  // namespace