Skip to content

Commit

Permalink
Fix helper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Oct 14, 2024
1 parent d082248 commit fc93cbd
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/shell-api/src/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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'
Expand All @@ -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
);
Expand All @@ -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'
);
Expand All @@ -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'
);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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' });
Expand All @@ -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']
Expand All @@ -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;
Expand Down

0 comments on commit fc93cbd

Please sign in to comment.