Skip to content

Commit

Permalink
Fix #95 autonomous UnitTest, and improve IT misconfig feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
boly38 committed May 4, 2024
1 parent 4f4da41 commit e4a770d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
24 changes: 16 additions & 8 deletions tests/mongo-wrapper-unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -45,7 +49,7 @@ describe("MTWrapper unit tests", function () {

const command = wrapper.commandConnectFromOptions(mtOptions, '--beginning');

command.should.be.eql("--beginning --uri mongodb://root:[email protected]: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");
});
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -91,7 +99,7 @@ describe("MTWrapper unit tests", function () {
const command = wrapper.commandConnectFromOptions(mtOptions, '--beginning', true);

command.should.be.eql(
"--beginning --uri mongodb://root:[email protected]:17017/myDbForTest?authSource=admin "
`--beginning --uri ${testDbUri} `
+ "--ssl --sslCAFile /tmp/myCAFile --sslPEMKeyFile /tmp/pem/sslPEMKeyFile --sslPEMKeyPassword strongPassHere "
+ "--sslCRLFile /tmp/pem/sslCRLFile --sslFIPSMode --tlsInsecure");
});
Expand Down
18 changes: 15 additions & 3 deletions tests/tests-utils.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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));
}
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit e4a770d

Please sign in to comment.