1
2 /**
3 * Copyright (C) 2018-present MongoDB, Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the Server Side Public License, version 1,
7 * as published by MongoDB, Inc.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * Server Side Public License for more details.
13 *
14 * You should have received a copy of the Server Side Public License
15 * along with this program. If not, see
16 * <http://www.mongodb.com/licensing/server-side-public-license>.
17 *
18 * As a special exception, the copyright holders give permission to link the
19 * code of portions of this program with the OpenSSL library under certain
20 * conditions as described in each individual source file and distribute
21 * linked combinations including the program with the OpenSSL library. You
22 * must comply with the Server Side Public License in all respects for
23 * all of the code used other than as permitted herein. If you modify file(s)
24 * with this exception, you may extend this exception to your version of the
25 * file(s), but you are not obligated to do so. If you do not wish to do so,
26 * delete this exception statement from your version. If you delete this
27 * exception statement from all source files in the program, then also delete
28 * it in the license file.
29 */
30
31 #include "mongo/platform/basic.h"
32
33 #include "mongo/s/request_types/add_shard_request_type.h"
34
35 #include "mongo/unittest/unittest.h"
36
37 namespace mongo {
38
39 namespace {
40
41 const char kConnString[] = "setname/localhost:27017,localhost:27018,localhost:27019";
42 const char kConnStringNonLocalHost[] = "setname/host1:27017,host2:27017,host3:27017";
43 const char kShardName[] = "shardName";
44 const long long kMaxSizeMB = 10;
45
46 // Test parsing the internal fields from a command BSONObj. The internal fields (besides the
47 // top-level command name) are identical between the external mongos version and internal config
48 // version.
49
TEST(AddShardRequest,ParseInternalFieldsInvalidConnectionString)50 TEST(AddShardRequest, ParseInternalFieldsInvalidConnectionString) {
51 {
52 BSONObj obj = BSON(AddShardRequest::mongosAddShard << ",,,");
53
54 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(obj);
55 ASSERT_NOT_OK(swAddShardRequest.getStatus());
56 ASSERT_EQUALS(ErrorCodes::FailedToParse, swAddShardRequest.getStatus());
57 }
58
59 {
60 BSONObj obj = BSON(AddShardRequest::configsvrAddShard << ",,,");
61
62 auto swAddShardRequest = AddShardRequest::parseFromConfigCommand(obj);
63 ASSERT_NOT_OK(swAddShardRequest.getStatus());
64 ASSERT_EQUALS(ErrorCodes::FailedToParse, swAddShardRequest.getStatus());
65 }
66 }
67
TEST(AddShardRequest,ParseInternalFieldsMissingMaxSize)68 TEST(AddShardRequest, ParseInternalFieldsMissingMaxSize) {
69 {
70 BSONObj obj =
71 BSON(AddShardRequest::mongosAddShard << kConnString << AddShardRequest::shardName
72 << kShardName);
73
74 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(obj);
75 ASSERT_OK(swAddShardRequest.getStatus());
76
77 auto req = swAddShardRequest.getValue();
78 ASSERT_EQ(req.getConnString().toString(), kConnString);
79 ASSERT_TRUE(req.hasName());
80 ASSERT_EQ(req.getName(), kShardName);
81 ASSERT_FALSE(req.hasMaxSize());
82 }
83
84 {
85 BSONObj obj =
86 BSON(AddShardRequest::configsvrAddShard << kConnString << AddShardRequest::shardName
87 << kShardName);
88
89
90 auto swAddShardRequest = AddShardRequest::parseFromConfigCommand(obj);
91 ASSERT_OK(swAddShardRequest.getStatus());
92
93 auto req = swAddShardRequest.getValue();
94 ASSERT_EQ(req.getConnString().toString(), kConnString);
95 ASSERT_TRUE(req.hasName());
96 ASSERT_EQ(req.getName(), kShardName);
97 ASSERT_FALSE(req.hasMaxSize());
98 }
99 }
100
TEST(AddShardRequest,ParseInternalFieldsMissingName)101 TEST(AddShardRequest, ParseInternalFieldsMissingName) {
102 {
103 BSONObj obj =
104 BSON(AddShardRequest::mongosAddShard << kConnString << AddShardRequest::maxSizeMB
105 << kMaxSizeMB);
106
107 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(obj);
108 ASSERT_OK(swAddShardRequest.getStatus());
109
110 auto req = swAddShardRequest.getValue();
111 ASSERT_EQ(req.getConnString().toString(), kConnString);
112 ASSERT_TRUE(req.hasMaxSize());
113 ASSERT_EQ(req.getMaxSize(), kMaxSizeMB);
114 ASSERT_FALSE(req.hasName());
115 }
116
117 {
118 BSONObj obj =
119 BSON(AddShardRequest::configsvrAddShard << kConnString << AddShardRequest::maxSizeMB
120 << kMaxSizeMB);
121
122 auto swAddShardRequest = AddShardRequest::parseFromConfigCommand(obj);
123 ASSERT_OK(swAddShardRequest.getStatus());
124
125 auto req = swAddShardRequest.getValue();
126 ASSERT_EQ(req.getConnString().toString(), kConnString);
127 ASSERT_TRUE(req.hasMaxSize());
128 ASSERT_EQ(req.getMaxSize(), kMaxSizeMB);
129 ASSERT_FALSE(req.hasName());
130 }
131 }
132
TEST(AddShardRequest,ParseInternalFieldsAllFieldsPresent)133 TEST(AddShardRequest, ParseInternalFieldsAllFieldsPresent) {
134 {
135 BSONObj obj =
136 BSON(AddShardRequest::mongosAddShard << kConnString << AddShardRequest::shardName
137 << kShardName
138 << AddShardRequest::maxSizeMB
139 << kMaxSizeMB);
140
141 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(obj);
142 ASSERT_OK(swAddShardRequest.getStatus());
143
144 auto req = swAddShardRequest.getValue();
145 ASSERT_EQ(req.getConnString().toString(), kConnString);
146 ASSERT_TRUE(req.hasMaxSize());
147 ASSERT_EQ(req.getMaxSize(), kMaxSizeMB);
148 ASSERT_TRUE(req.hasName());
149 ASSERT_EQ(req.getName(), kShardName);
150 }
151
152 {
153 BSONObj obj =
154 BSON(AddShardRequest::configsvrAddShard << kConnString << AddShardRequest::shardName
155 << kShardName
156 << AddShardRequest::maxSizeMB
157 << kMaxSizeMB);
158
159 auto swAddShardRequest = AddShardRequest::parseFromConfigCommand(obj);
160 ASSERT_OK(swAddShardRequest.getStatus());
161
162 auto req = swAddShardRequest.getValue();
163 ASSERT_EQ(req.getConnString().toString(), kConnString);
164 ASSERT_TRUE(req.hasMaxSize());
165 ASSERT_EQ(req.getMaxSize(), kMaxSizeMB);
166 ASSERT_TRUE(req.hasName());
167 ASSERT_EQ(req.getName(), kShardName);
168 }
169 }
170
171 // Test converting a valid AddShardRequest to the internal config version of the command.
172
TEST(AddShardRequest,ToCommandForConfig)173 TEST(AddShardRequest, ToCommandForConfig) {
174 BSONObj mongosCmdObj = BSON(
175 AddShardRequest::mongosAddShard << kConnString << AddShardRequest::shardName << kShardName
176 << AddShardRequest::maxSizeMB
177 << kMaxSizeMB);
178
179 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(mongosCmdObj);
180 ASSERT_OK(swAddShardRequest.getStatus());
181 auto req = swAddShardRequest.getValue();
182
183 auto configCmdObj = req.toCommandForConfig();
184 ASSERT_EQ(configCmdObj[AddShardRequest::configsvrAddShard.name()].String(), kConnString);
185 ASSERT_EQ(configCmdObj[AddShardRequest::shardName.name()].String(), kShardName);
186 ASSERT_EQ(configCmdObj[AddShardRequest::maxSizeMB.name()].Long(), kMaxSizeMB);
187 }
188
TEST(AddShardRequest,ToCommandForConfigMissingName)189 TEST(AddShardRequest, ToCommandForConfigMissingName) {
190 BSONObj mongosCmdObj = BSON(
191 AddShardRequest::mongosAddShard << kConnString << AddShardRequest::maxSizeMB << kMaxSizeMB);
192
193 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(mongosCmdObj);
194 ASSERT_OK(swAddShardRequest.getStatus());
195 auto req = swAddShardRequest.getValue();
196
197 auto configCmdObj = req.toCommandForConfig();
198 ASSERT_EQ(configCmdObj[AddShardRequest::configsvrAddShard.name()].String(), kConnString);
199 ASSERT_EQ(configCmdObj[AddShardRequest::maxSizeMB.name()].Long(), kMaxSizeMB);
200 ASSERT_FALSE(configCmdObj.hasField(AddShardRequest::shardName.name()));
201 }
202
TEST(AddShardRequest,ToCommandForConfigMissingMaxSize)203 TEST(AddShardRequest, ToCommandForConfigMissingMaxSize) {
204 BSONObj mongosCmdObj = BSON(
205 AddShardRequest::mongosAddShard << kConnString << AddShardRequest::shardName << kShardName);
206
207 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(mongosCmdObj);
208 ASSERT_OK(swAddShardRequest.getStatus());
209 auto req = swAddShardRequest.getValue();
210
211 auto configCmdObj = req.toCommandForConfig();
212 ASSERT_EQ(configCmdObj[AddShardRequest::configsvrAddShard.name()].String(), kConnString);
213 ASSERT_EQ(configCmdObj[AddShardRequest::shardName.name()].String(), kShardName);
214 ASSERT_FALSE(configCmdObj.hasField(AddShardRequest::maxSizeMB.name()));
215 }
216
217 // Test validating an AddShardRequest that was successfully parsed.
218
TEST(AddShardRequest,ValidateLocalHostAllowed)219 TEST(AddShardRequest, ValidateLocalHostAllowed) {
220 // Using a connection string with localhost should succeed.
221 {
222 BSONObj mongosCmdObj = BSON(AddShardRequest::mongosAddShard << kConnString);
223
224 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(mongosCmdObj);
225 ASSERT_OK(swAddShardRequest.getStatus());
226 auto req = swAddShardRequest.getValue();
227
228 auto validateStatus = req.validate(true);
229 ASSERT_OK(validateStatus);
230 }
231
232 // Using a connection string with non-localhost hostnames should fail.
233 {
234 BSONObj mongosCmdObj = BSON(AddShardRequest::mongosAddShard << kConnStringNonLocalHost);
235
236 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(mongosCmdObj);
237 ASSERT_OK(swAddShardRequest.getStatus());
238 auto req = swAddShardRequest.getValue();
239
240 auto validateStatus = req.validate(true);
241 ASSERT_NOT_OK(validateStatus);
242 ASSERT_EQUALS(ErrorCodes::InvalidOptions, validateStatus);
243 }
244 }
245
TEST(AddShardRequest,ValidateLocalHostNotAllowed)246 TEST(AddShardRequest, ValidateLocalHostNotAllowed) {
247 // Using a connection string with localhost should fail.
248 {
249 BSONObj mongosCmdObj = BSON(AddShardRequest::mongosAddShard << kConnString);
250
251 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(mongosCmdObj);
252 ASSERT_OK(swAddShardRequest.getStatus());
253 auto req = swAddShardRequest.getValue();
254
255 auto validateStatus = req.validate(false);
256 ASSERT_NOT_OK(validateStatus);
257 ASSERT_EQUALS(ErrorCodes::InvalidOptions, validateStatus);
258 }
259
260 // Using a connection string with non-localhost hostnames should succeed.
261 {
262 BSONObj mongosCmdObj = BSON(AddShardRequest::mongosAddShard << kConnStringNonLocalHost);
263
264 auto swAddShardRequest = AddShardRequest::parseFromMongosCommand(mongosCmdObj);
265 ASSERT_OK(swAddShardRequest.getStatus());
266 auto req = swAddShardRequest.getValue();
267
268 auto validateStatus = req.validate(false);
269 ASSERT_OK(validateStatus);
270 }
271 }
272
273 } // namespace
274 } // namespace mongo
275