diff --git a/packages/shell-api/src/helpers.spec.ts b/packages/shell-api/src/helpers.spec.ts index d23939054..2661bea90 100644 --- a/packages/shell-api/src/helpers.spec.ts +++ b/packages/shell-api/src/helpers.spec.ts @@ -19,6 +19,7 @@ import sinon from 'ts-sinon'; import chai, { expect } from 'chai'; import { EventEmitter } from 'events'; import sinonChai from 'sinon-chai'; +import { stub } from 'sinon'; chai.use(sinonChai); const fakeConfigDb = makeFakeConfigDatabase( @@ -129,6 +130,7 @@ describe('getPrintableShardStatus', function () { const testServer = startSharedTestServer(); let mongo: Mongo; + let db: Database; let configDatabase: Database; let serviceProvider: ServiceProvider; let inBalancerRound = false; @@ -150,6 +152,11 @@ describe('getPrintableShardStatus', function () { configDatabase = new Database(mongo, 'config_test'); expect(configDatabase.getName()).to.equal('config_test'); + db = { + _maybeCachedHello: stub().returns({ msg: 'isdbgrid' }), + getSiblingDB: stub().withArgs('config').returns(configDatabase), + } as unknown as Database; + const origRunCommandWithCheck = serviceProvider.runCommandWithCheck; serviceProvider.runCommandWithCheck = async (db, cmd) => { if (cmd.hello) { @@ -186,7 +193,7 @@ describe('getPrintableShardStatus', function () { }); it('returns an object with sharding information', async function () { - const status = await getPrintableShardStatus(configDatabase, false); + const status = await getPrintableShardStatus(db, false); expect(status.shardingVersion.clusterId).to.be.instanceOf(bson.ObjectId); expect(status.shards.map(({ host }: { host: string }) => host)).to.include( 'shard01/localhost:27018,localhost:27019,localhost:27020' @@ -213,7 +220,7 @@ describe('getPrintableShardStatus', function () { 'upgradeState', ]) { it(`does not show ${hiddenField} in shardingVersion`, async function () { - const status = await getPrintableShardStatus(configDatabase, false); + const status = await getPrintableShardStatus(db, false); expect((status.shardingVersion as any)[hiddenField]).to.equal( undefined ); @@ -224,19 +231,19 @@ describe('getPrintableShardStatus', function () { it('returns whether the balancer is currently running', async function () { { inBalancerRound = true; - const status = await getPrintableShardStatus(configDatabase, true); + const status = await getPrintableShardStatus(db, true); expect(status.balancer['Currently running']).to.equal('yes'); } { inBalancerRound = false; - const status = await getPrintableShardStatus(configDatabase, true); + const status = await getPrintableShardStatus(db, true); expect(status.balancer['Currently running']).to.equal('no'); } }); it('returns an object with verbose sharding information if requested', async function () { - const status = await getPrintableShardStatus(configDatabase, true); + const status = await getPrintableShardStatus(db, true); expect((status['most recently active mongoses'][0] as any).up).to.be.a( 'number' ); @@ -250,7 +257,7 @@ describe('getPrintableShardStatus', function () { _id: 'balancer', activeWindow: { start: '00:00', stop: '23:59' }, }); - const status = await getPrintableShardStatus(configDatabase, false); + const status = await getPrintableShardStatus(db, false); expect(status.balancer['Balancer active window is set between']).to.equal( '00:00 and 23:59 server local time' ); @@ -266,7 +273,7 @@ describe('getPrintableShardStatus', function () { what: 'balancer.round', ns: '', }); - const status = await getPrintableShardStatus(configDatabase, false); + const status = await getPrintableShardStatus(db, false); expect( status.balancer['Failed balancer rounds in last 5 attempts'] ).to.equal(1); @@ -280,7 +287,7 @@ describe('getPrintableShardStatus', function () { ts: new bson.ObjectId('5fce116c579db766a198a176'), when: new Date('2020-12-07T11:26:36.803Z'), }); - const status = await getPrintableShardStatus(configDatabase, false); + const status = await getPrintableShardStatus(db, false); expect( status.balancer['Collections with active migrations'] ).to.have.lengthOf(1); @@ -295,7 +302,7 @@ describe('getPrintableShardStatus', function () { what: 'moveChunk.from', details: { from: 'shard0', to: 'shard1', note: 'success' }, }); - const status = await getPrintableShardStatus(configDatabase, false); + const status = await getPrintableShardStatus(db, false); expect( status.balancer['Migration Results for the last 24 hours'] ).to.deep.equal({ 1: 'Success' }); @@ -307,7 +314,7 @@ describe('getPrintableShardStatus', function () { what: 'moveChunk.from', details: { from: 'shard0', to: 'shard1', errmsg: 'oopsie' }, }); - const status = await getPrintableShardStatus(configDatabase, false); + const status = await getPrintableShardStatus(db, false); expect( status.balancer['Migration Results for the last 24 hours'] @@ -317,7 +324,7 @@ describe('getPrintableShardStatus', function () { it('fails when config.version is empty', async function () { await configDatabase.getCollection('version').drop(); try { - await getPrintableShardStatus(configDatabase, false); + await getPrintableShardStatus(db, false); } catch (err: any) { expect(err.name).to.equal('MongoshInvalidInputError'); return;