Skip to content

Commit 8fd2688

Browse files
authored
MONGOSH-383 - e2e repl tests (#377)
* MONGOSH-383 - e2e repl tests * use x instead of skip; cr comments
1 parent 95a8aa1 commit 8fd2688

File tree

2 files changed

+153
-4
lines changed

2 files changed

+153
-4
lines changed

packages/shell-api/src/replica-set.spec.ts

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
ALL_SERVER_VERSIONS,
1313
ALL_TOPOLOGIES
1414
} from './enums';
15+
import { CliServiceProvider } from '../../service-provider-server';
16+
import util from 'util';
1517

1618
describe('ReplicaSet', () => {
1719
describe('help', () => {
@@ -49,14 +51,14 @@ describe('ReplicaSet', () => {
4951
});
5052
});
5153

52-
describe('commands', () => {
54+
describe('unit', () => {
5355
let mongo: Mongo;
5456
let serviceProvider: StubbedInstance<ServiceProvider>;
5557
let rs: ReplicaSet;
5658
let bus: StubbedInstance<EventEmitter>;
5759
let internalState: ShellInternalState;
5860

59-
const findResolvesWith = (expectedResult) => {
61+
const findResolvesWith = (expectedResult): void => {
6062
const findCursor = stubInterface<ServiceProviderCursor>();
6163
findCursor.next.resolves(expectedResult);
6264
serviceProvider.find.returns(findCursor);
@@ -133,7 +135,7 @@ describe('ReplicaSet', () => {
133135
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
134136
ADMIN_DB,
135137
{
136-
replSetReconfig: 1
138+
replSetGetConfig: 1
137139
}
138140
);
139141
});
@@ -585,4 +587,151 @@ describe('ReplicaSet', () => {
585587
});
586588
});
587589
});
590+
591+
xdescribe('integration', () => {
592+
const port0 = 27017;
593+
const host = '127.0.0.1';
594+
const replId = 'rs0';
595+
const connectionString = `mongodb://${host}:${port0}`; // startTestServer();
596+
const cfg = {
597+
_id: replId,
598+
members: [
599+
{ _id: 0, host: `${host}:${port0}`, priority: 1 },
600+
{ _id: 1, host: `${host}:${port0 + 1}`, priority: 0 },
601+
{ _id: 2, host: `${host}:${port0 + 2}`, priority: 0 }
602+
]
603+
};
604+
let serviceProvider: CliServiceProvider;
605+
let internalState;
606+
let mongo;
607+
let rs;
608+
609+
const delay = util.promisify(setTimeout);
610+
const ensureMaster = async(timeout): Promise<void> => {
611+
while (!(await rs.isMaster()).ismaster) {
612+
if (timeout > 8000) {
613+
return expect.fail(`Waited for ${host}:${port0} to become master, never happened`);
614+
}
615+
await delay(timeout);
616+
timeout *= 2; // try again but wait double
617+
}
618+
expect((await rs.conf()).members.length).to.equal(3);
619+
};
620+
621+
before(async() => {
622+
serviceProvider = await CliServiceProvider.connect(connectionString);
623+
internalState = new ShellInternalState(serviceProvider);
624+
mongo = internalState.currentDb.getMongo();
625+
rs = new ReplicaSet(mongo);
626+
627+
// check replset uninitialized
628+
try {
629+
await rs.status();
630+
} catch (error) {
631+
// eslint-disable-next-line no-console
632+
console.log('WARNING: Initializing new replset');
633+
expect(error.message).to.include('no replset config');
634+
const result = await rs.initiate(cfg);
635+
expect(result.ok).to.equal(1);
636+
return expect(result.$clusterTime).to.not.be.undefined;
637+
}
638+
// eslint-disable-next-line no-console
639+
console.log('WARNING: Using existing replset');
640+
});
641+
642+
beforeEach(async() => {
643+
await ensureMaster(1000);
644+
});
645+
646+
after(() => {
647+
return serviceProvider.close(true);
648+
});
649+
650+
describe('replica set info', () => {
651+
it('returns the status', async() => {
652+
const result = await rs.status();
653+
expect(result.set).to.equal(replId);
654+
});
655+
it('returns the config', async() => {
656+
const result = await rs.conf();
657+
expect(result._id).to.equal(replId);
658+
});
659+
it('is connected to master', async() => {
660+
const result = await rs.isMaster();
661+
expect(result.ismaster).to.be.true;
662+
});
663+
it('returns StatsResult for print secondary replication info', async() => {
664+
const result = await rs.printSecondaryReplicationInfo();
665+
expect(result.type).to.equal('StatsResult');
666+
});
667+
it('returns StatsResult for print replication info', async() => {
668+
const result = await rs.printSecondaryReplicationInfo();
669+
expect(result.type).to.equal('StatsResult');
670+
});
671+
it('returns data for db.getReplicationInfo', async() => {
672+
const result = await rs._mongo.getDB('any').getReplicationInfo();
673+
expect(Object.keys(result)).to.include('logSizeMB');
674+
});
675+
});
676+
describe('reconfig', () => {
677+
it('reconfig with one less secondary', async() => {
678+
const newcfg = {
679+
_id: replId,
680+
members: [ cfg.members[0], cfg.members[1] ]
681+
};
682+
const version = (await rs.conf()).version;
683+
const result = await rs.reconfig(newcfg);
684+
expect(result.ok).to.equal(1);
685+
const status = await rs.conf();
686+
expect(status.members.length).to.equal(2);
687+
expect(status.version).to.equal(version + 1);
688+
});
689+
afterEach(async() => {
690+
await rs.reconfig(cfg);
691+
const status = await rs.conf();
692+
expect(status.members.length).to.equal(3);
693+
});
694+
});
695+
696+
describe('add member', () => {
697+
it('adds a regular member to the config', async() => {
698+
const version = (await rs.conf()).version;
699+
const result = await rs.add(`${host}:${port0 + 3}`);
700+
expect(result.ok).to.equal(1);
701+
const conf = await rs.conf();
702+
expect(conf.members.length).to.equal(4);
703+
expect(conf.version).to.equal(version + 1);
704+
});
705+
it('adds a arbiter member to the config', async() => {
706+
const version = (await rs.conf()).version;
707+
const result = await rs.addArb(`${host}:${port0 + 3}`);
708+
expect(result.ok).to.equal(1);
709+
const conf = await rs.conf();
710+
expect(conf.members.length).to.equal(4);
711+
expect(conf.members[3].arbiterOnly).to.equal(true);
712+
expect(conf.version).to.equal(version + 1);
713+
});
714+
afterEach(async() => {
715+
await rs.reconfig(cfg);
716+
const status = await rs.conf();
717+
expect(status.members.length).to.equal(3);
718+
});
719+
});
720+
721+
describe('remove member', () => {
722+
it('removes a member of the config', async() => {
723+
const version = (await rs.conf()).version;
724+
const result = await rs.remove(`${host}:${port0 + 2}`);
725+
expect(result.ok).to.equal(1);
726+
const conf = await rs.conf();
727+
expect(conf.members.length).to.equal(2);
728+
expect(conf.version).to.equal(version + 1);
729+
});
730+
afterEach(async() => {
731+
await rs.reconfig(cfg);
732+
const status = await rs.conf();
733+
expect(status.members.length).to.equal(3);
734+
});
735+
});
736+
});
588737
});

packages/shell-api/src/replica-set.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default class ReplicaSet extends ShellApiClass {
4646
try {
4747
const result = await this._mongo._serviceProvider.runCommandWithCheck(
4848
ADMIN_DB,
49-
{ replSetReconfig: 1 }
49+
{ replSetGetConfig: 1 }
5050
);
5151
if (result.config === undefined) {
5252
throw new MongoshInternalError('Documented returned from command replSetReconfig does not contain \'config\'');

0 commit comments

Comments
 (0)