diff --git a/tests/mongo-wrapper-unit.test.js b/tests/mongo-wrapper-unit.test.js index 8591ce6..b9e7fc3 100644 --- a/tests/mongo-wrapper-unit.test.js +++ b/tests/mongo-wrapper-unit.test.js @@ -23,17 +23,21 @@ describe("MTWrapper unit tests", function () { const mtOptions = new MTOptions({ db: testDbName, port: testFixedPort, - path: testBackupDirectory + path: testBackupDirectory, + username:"myUser", + password:"myPass" }); const command = wrapper.commandConnectFromOptions(mtOptions, '--beginning'); - command.should.be.eql("--beginning --host 127.0.0.1 --port 17017 --username root --password mypass --authenticationDatabase admin --db myDbForTest"); + command.should.be.eql("--beginning --host 127.0.0.1 --port 17017 --username myUser --password myPass --authenticationDatabase admin --db myDbForTest"); }); it("should wrap dump commandConnectFromOptions uri ssl", async function () { const mtOptions = new MTOptions({ uri: testDbUri, + username:"myUser", + password:"myPass", ssl: "1", sslCAFile: "/tmp/myCAfile", sslPEMKeyFile: "/tmp/pem/sslPEMKeyFile", @@ -45,7 +49,7 @@ describe("MTWrapper unit tests", function () { const command = wrapper.commandConnectFromOptions(mtOptions, '--beginning'); - command.should.be.eql("--beginning --uri mongodb://root:mypass@127.0.0.1:17017/myDbForTest?authSource=admin " + command.should.be.eql(`--beginning --uri ${testDbUri} ` + "--ssl --sslCAFile /tmp/myCAfile --sslPEMKeyFile /tmp/pem/sslPEMKeyFile --sslPEMKeyPassword strongPassHere " + "--sslCRLFile /tmp/pem/sslCRLFile --sslFIPSMode --tlsInsecure"); }); @@ -54,12 +58,14 @@ describe("MTWrapper unit tests", function () { const mtOptions = new MTOptions({ db: testDbName, port: testFixedPort, - path: testBackupDirectory + path: testBackupDirectory, + username:"myUser", + password:"myPass", }); const command = wrapper.commandConnectFromOptions(mtOptions, '--beginning', true); - command.should.be.eql("--beginning --host 127.0.0.1 --port 17017 --username root --password mypass --authenticationDatabase admin --nsInclude myDbForTest.*"); + command.should.be.eql("--beginning --host 127.0.0.1 --port 17017 --username myUser --password myPass --authenticationDatabase admin --nsInclude myDbForTest.*"); }); it("should wrap restore commandConnectFromOptions db user basic source and target dbs", async function () { @@ -68,12 +74,14 @@ describe("MTWrapper unit tests", function () { dbFrom: testSourceDbName, dbTo: testTargetDbName, port: testFixedPort, - path: testBackupDirectory + path: testBackupDirectory, + username:"myUser", + password:"myPass", }); const command = wrapper.commandConnectFromOptions(mtOptions, '--beginning', true); - command.should.be.eql("--beginning --host 127.0.0.1 --port 17017 --username root --password mypass --authenticationDatabase admin --nsFrom mySourceDbForTest.* --nsTo myTargetDbForTest.*"); + command.should.be.eql("--beginning --host 127.0.0.1 --port 17017 --username myUser --password myPass --authenticationDatabase admin --nsFrom mySourceDbForTest.* --nsTo myTargetDbForTest.*"); }); it("should wrap restore commandConnectFromOptions uri ssl", async function () { @@ -91,7 +99,7 @@ describe("MTWrapper unit tests", function () { const command = wrapper.commandConnectFromOptions(mtOptions, '--beginning', true); command.should.be.eql( - "--beginning --uri mongodb://root:mypass@127.0.0.1:17017/myDbForTest?authSource=admin " + `--beginning --uri ${testDbUri} ` + "--ssl --sslCAFile /tmp/myCAFile --sslPEMKeyFile /tmp/pem/sslPEMKeyFile --sslPEMKeyPassword strongPassHere " + "--sslCRLFile /tmp/pem/sslCRLFile --sslFIPSMode --tlsInsecure"); }); diff --git a/tests/tests-utils.js b/tests/tests-utils.js index 177590f..9f58403 100644 --- a/tests/tests-utils.js +++ b/tests/tests-utils.js @@ -1,5 +1,11 @@ import mongoose from 'mongoose'; +// set 3sec timeout to avoid waiting 30 seconds on misconfiguration: +// warn: this may produce flaky issues on some host with network latency +const serverSelectionTimeoutMS = 3000;// https://mongoosejs.com/docs/connections.html#serverselectiontimeoutms +const socketTimeoutMS = 3000;// https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.connect() +const MONGOOSE_CONNECT_OPTIONS = {serverSelectionTimeoutMS, socketTimeoutMS}; + const mtMochaTests = mongoose.model('MT-mocha-tests', new mongoose.Schema({ _id: String, string: String @@ -12,7 +18,7 @@ const givenDocumentData = (i) => { export const createDatabaseWithNDocs = async (uri, info = "", nbDocs = 2) => { console.log(` ▶ create test database ${info} with ${nbDocs} documents`); try { - await mongoose.connect(uri, {}); + await mongoose.connect(uri, MONGOOSE_CONNECT_OPTIONS); for (let i = 0; i < nbDocs; i++) { await mtMochaTests.create(givenDocumentData(i)); } @@ -23,7 +29,7 @@ export const createDatabaseWithNDocs = async (uri, info = "", nbDocs = 2) => { export const countDocumentsInCollection = async uri => { try { - await mongoose.connect(uri, {}); + await mongoose.connect(uri, MONGOOSE_CONNECT_OPTIONS); return await mtMochaTests.countDocuments(); } finally { await mongoose.disconnect(); @@ -33,7 +39,13 @@ export const countDocumentsInCollection = async uri => { export const dropDatabase = async (uri, info = "") => { console.log(` ▶ drop test database ${info}`); try { - await mongoose.connect(uri, {}); + try { + await mongoose.connect(uri, MONGOOSE_CONNECT_OPTIONS); + } catch (veryFirstConnectError) { + console.warn("⚠️ integration test require ready-to-use mongo connection,\nℹ️ please review you test environment variables"); + console.error(`❌ Unable to connect to test database (${uri}) - error : ${veryFirstConnectError.message}`); + process.exit(1) + } await mongoose.connection.dropDatabase(); } finally { await mongoose.disconnect();