2017-10-06 11:31:06 -04:00
|
|
|
// Test mongo shell connect strings.
|
2023-08-23 16:07:27 +00:00
|
|
|
//
|
2019-03-11 18:17:29 -04:00
|
|
|
// @tags: [
|
2023-08-23 16:07:27 +00:00
|
|
|
// # The test runs commands that are not allowed with security token: eval.
|
2023-12-11 22:44:46 +00:00
|
|
|
// not_allowed_with_signed_security_token,
|
2019-03-11 18:17:29 -04:00
|
|
|
// uses_multiple_connections,
|
2022-08-09 23:50:00 +00:00
|
|
|
// docker_incompatible,
|
2019-03-11 18:17:29 -04:00
|
|
|
// ]
|
2023-08-23 16:07:27 +00:00
|
|
|
|
2026-02-11 11:18:21 +01:00
|
|
|
assert(db.getMongo().uri, "Mongo object should have 'uri' property");
|
|
|
|
|
|
|
|
|
|
const mongod = new MongoURI(db.getMongo().uri).servers[0];
|
2017-10-06 11:31:06 -04:00
|
|
|
const host = mongod.host;
|
|
|
|
|
const port = mongod.port;
|
|
|
|
|
|
|
|
|
|
function testConnect(ok, ...args) {
|
2025-08-21 10:17:44 -07:00
|
|
|
const exitCode = runMongoProgram("mongo", "--eval", ";", ...args);
|
2017-10-06 11:31:06 -04:00
|
|
|
if (ok) {
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.eq(exitCode, 0, "failed to connect with `" + args.join(" ") + "`");
|
2017-10-06 11:31:06 -04:00
|
|
|
} else {
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.neq(exitCode, 0, "unexpectedly succeeded connecting with `" + args.join(" ") + "`");
|
2017-10-06 11:31:06 -04:00
|
|
|
}
|
2019-07-26 18:20:35 -04:00
|
|
|
}
|
2017-10-06 11:31:06 -04:00
|
|
|
|
|
|
|
|
testConnect(true, `${host}:${port}`);
|
|
|
|
|
testConnect(true, `${host}:${port}/test`);
|
|
|
|
|
testConnect(true, `${host}:${port}/admin`);
|
2025-08-21 10:17:44 -07:00
|
|
|
testConnect(true, host, "--port", port);
|
|
|
|
|
testConnect(true, "--host", host, "--port", port, "test");
|
|
|
|
|
testConnect(true, "--host", host, "--port", port, "admin");
|
2017-10-06 11:31:06 -04:00
|
|
|
testConnect(true, `mongodb://${host}:${port}/test`);
|
|
|
|
|
testConnect(true, `mongodb://${host}:${port}/test?connectTimeoutMS=10000`);
|
|
|
|
|
|
|
|
|
|
// if a full URI is provided, you cannot also specify host or port
|
2025-08-21 10:17:44 -07:00
|
|
|
testConnect(false, `${host}/test`, "--port", port);
|
|
|
|
|
testConnect(false, `mongodb://${host}:${port}/test`, "--port", port);
|
|
|
|
|
testConnect(false, `mongodb://${host}:${port}/test`, "--host", host);
|
|
|
|
|
testConnect(false, `mongodb://${host}:${port}/test`, "--host", host, "--port", port);
|
2026-02-11 11:18:21 +01:00
|
|
|
|
|
|
|
|
// Test that the 'uri' property returns a valid mongodb:// URI that can be used for new connections
|
|
|
|
|
{
|
|
|
|
|
const mongo = db.getMongo();
|
|
|
|
|
|
|
|
|
|
// The 'uri' should be usable to create a new connection
|
|
|
|
|
const newMongo = new Mongo(mongo.uri);
|
|
|
|
|
assert(newMongo, "Should be able to create new Mongo connection using uri property");
|
|
|
|
|
|
|
|
|
|
const newMongo2 = connect(mongo.uri).getMongo();
|
|
|
|
|
assert(newMongo2, "Should be able to create new Mongo connection using uri property");
|
|
|
|
|
}
|