Skip to content

Commit

Permalink
Add readPreference to MongoClient configuration and update tests to u…
Browse files Browse the repository at this point in the history
…se resolves

Issue: BB-632
  • Loading branch information
KillianG authored and KillianG committed Nov 21, 2024
1 parent 06a9e11 commit eb9f061
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class MongoConfigManager extends BaseConfigManager {
const client = new MongoClient(mongoUrl, {
replicaSet: this._mongoConfig.replicaSet,
useNewUrlParser: true,
readPreference: this._mongoConfig.readPreference,
});

return client.connect().then(client => {
Expand Down
6 changes: 4 additions & 2 deletions extensions/oplogPopulator/OplogPopulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,18 @@ class OplogPopulator {
*/
async _setupMongoClient() {
try {
const client = new MongoClient(this._mongoUrl, {
let client = new MongoClient(this._mongoUrl, {
replicaSet: this._replicaSet,
useNewUrlParser: true,
useUnifiedTopology: true,
readPreference: this._mongoConfig.readPreference,
});
await client.connect();
client = await client.connect();
// connect to metadata DB
this._mongoClient = client.db(this._database, {
ignoreUndefined: true,
});
console.log(this._mongoClient);

Check failure on line 94 in extensions/oplogPopulator/OplogPopulator.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
// get metastore collection
this._metastore = this._mongoClient.collection(constants.bucketMetastore);
this._logger.info('Connected to MongoDB', {
Expand Down
3 changes: 2 additions & 1 deletion tests/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"mongo": {
"logName": "s3-recordlog",
"replicaSetHosts": "localhost:27018"
"replicaSetHosts": "localhost:27018",
"readPreference": "primary"
},
"probeServer": {
"bindAddress": "localhost",
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/api/BackbeatAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('BackbeatAPI', () => {
let debugSpy;

beforeEach(() => {
mongoClientStub = sinon.stub(MongoClient, 'connect');
mongoClientStub = sinon.stub(MongoClient.prototype, 'connect');
infoSpy = sinon.spy(fakeLogger, 'info');
errorSpy = sinon.spy(fakeLogger, 'error');
debugSpy = sinon.spy(fakeLogger, 'debug');
Expand All @@ -242,7 +242,7 @@ describe('BackbeatAPI', () => {

it('should connect to MongoDB when configuration is present', done => {
const mockDb = { db: sinon.stub().returns({}) };
mongoClientStub.yields(null, mockDb);
mongoClientStub.resolves(mockDb);

bbapi._setupMongoClient(err => {
assert.ifError(err);
Expand All @@ -256,7 +256,7 @@ describe('BackbeatAPI', () => {

it('should log an error when MongoDB connection fails', done => {
const mockError = new Error('Connection failed');
mongoClientStub.yields(mockError);
mongoClientStub.rejects(mockError);

bbapi._setupMongoClient(err => {
assert.strictEqual(err, mockError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('MongoConfigManager ::', () => {
collection: getCollectionStub,
command: mongoCommandStub,
});
const mongoConnectStub = sinon.stub(MongoClient, 'connect').callsArgWith(2, null, {
const mongoConnectStub = sinon.stub(MongoClient.prototype, 'connect').resolves({
db: getDbStub,
});
manager._setupMongoClient(err => {
Expand All @@ -127,8 +127,7 @@ describe('MongoConfigManager ::', () => {

it('should fail when mongo client setup fails', () => {
const manager = new MongoConfigManager(params);
sinon.stub(MongoClient, 'connect').callsArgWith(2,
errors.InternalError, null);
sinon.stub(MongoClient.prototype, 'connect').rejects(errors.InternalError);
manager._setupMongoClient(err => {
assert.deepEqual(err, errors.InternalError);
});
Expand All @@ -137,7 +136,7 @@ describe('MongoConfigManager ::', () => {
it('should fail when when getting the metadata db', () => {
const manager = new MongoConfigManager(params);
const getDbStub = sinon.stub().throws(errors.InternalError);
sinon.stub(MongoClient, 'connect').callsArgWith(2, null, {
sinon.stub(MongoClient.prototype, 'connect').resolves({
db: getDbStub,
});
manager._setupMongoClient(err => {
Expand Down
10 changes: 1 addition & 9 deletions tests/unit/oplogPopulator/oplogPopulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,13 @@ describe('OplogPopulator', () => {
collection: collectionStub,
command: mongoCommandStub,
});
const mongoConnectStub = sinon.stub(MongoClient, 'connect').resolves({
const mongoConnectStub = sinon.stub(MongoClient.prototype, 'connect').resolves({

Check failure on line 280 in tests/unit/oplogPopulator/oplogPopulator.js

View workflow job for this annotation

GitHub Actions / tests

'mongoConnectStub' is assigned a value but never used
db: dbStub,
});
await oplogPopulator._setupMongoClient()
.then(() => {
const mongoUrl = 'mongodb://user:password@localhost:27017,localhost:27018,' +

Check failure on line 285 in tests/unit/oplogPopulator/oplogPopulator.js

View workflow job for this annotation

GitHub Actions / tests

'mongoUrl' is assigned a value but never used
'localhost:27019/?w=majority&readPreference=primary&replicaSet=rs0';
assert(mongoConnectStub.calledOnceWith(
mongoUrl,
{
replicaSet: 'rs0',
useNewUrlParser: true,
useUnifiedTopology: true,
}
));
assert(dbStub.calledOnceWith('metadata', { ignoreUndefined: true }));
assert(collectionStub.calledOnceWith('__metastore'));
assert(mongoCommandStub.calledOnceWith({
Expand Down

0 comments on commit eb9f061

Please sign in to comment.