Skip to content

Commit 95a8aa1

Browse files
authored
MONGOSH-384 - e2e sh tests (#379)
* MONGOSH-384 - e2e sh tests * skip tests
1 parent 31c4c42 commit 95a8aa1

File tree

3 files changed

+142
-20
lines changed

3 files changed

+142
-20
lines changed

packages/shell-api/src/helpers.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -406,16 +406,14 @@ export async function getPrintableShardStatus(mongo: Mongo, verbose: boolean): P
406406
(await chunksColl.find({ 'ns': coll._id })
407407
.sort({ min: 1 }).toArray())
408408
.forEach((chunk) => {
409-
const c = [
410-
JSON.stringify(chunk.min),
411-
'-->>',
412-
JSON.stringify(chunk.max),
413-
'on',
414-
JSON.stringify(chunk.shard),
415-
JSON.stringify(chunk.lastmod)
416-
];
417-
if (chunk.jumbo) c.push('jumbo');
418-
chunksRes.push(c.join(' '));
409+
const c = {
410+
min: chunk.min,
411+
max: chunk.max,
412+
'on shard': chunk.shard,
413+
'last modified': chunk.lastmod
414+
} as any;
415+
if (chunk.jumbo) c.jumbo = 'yes';
416+
chunksRes.push(c);
419417
});
420418
} else {
421419
chunksRes.push('too many chunks to print, use verbose if you want to force print');
@@ -427,13 +425,11 @@ export async function getPrintableShardStatus(mongo: Mongo, verbose: boolean): P
427425
.sort({ min: 1 })
428426
.toArray())
429427
.forEach((tag) => {
430-
const t = [
431-
tag.tag,
432-
tag.min,
433-
' -->> ',
434-
tag.max
435-
];
436-
tagsRes.push(t.join(' '));
428+
tagsRes.push({
429+
tag: tag.tag,
430+
min: tag.min,
431+
max: tag.max
432+
});
437433
});
438434
collRes.chunks = chunksRes;
439435
collRes.tags = tagsRes;

packages/shell-api/src/shard.spec.ts

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { bson, ServiceProvider, Cursor as ServiceProviderCursor } from '@mongosh
88
import { EventEmitter } from 'events';
99
import ShellInternalState from './shell-internal-state';
1010
import { UpdateResult } from './result';
11+
import { CliServiceProvider } from '../../service-provider-server';
1112

1213
describe('Shard', () => {
1314
describe('help', () => {
@@ -51,7 +52,7 @@ describe('Shard', () => {
5152
});
5253
});
5354
});
54-
describe('commands', () => {
55+
describe('unit', () => {
5556
let mongo: Mongo;
5657
let serviceProvider: StubbedInstance<ServiceProvider>;
5758
let shard: Shard;
@@ -1082,4 +1083,131 @@ describe('Shard', () => {
10821083
});
10831084
});
10841085
});
1086+
1087+
xdescribe('integration', () => {
1088+
let serviceProvider: CliServiceProvider;
1089+
let internalState;
1090+
let mongo;
1091+
let sh;
1092+
const dbName = 'test';
1093+
const ns = `${dbName}.coll`;
1094+
const mongosPort = 27017;
1095+
const host = 'localhost';
1096+
const shardId = 'rs-shard0';
1097+
const shardPorts = ['47017', '47020'];
1098+
const connectionString = `mongodb://${host}:${mongosPort}`; // startTestServer();
1099+
1100+
before(async() => {
1101+
serviceProvider = await CliServiceProvider.connect(connectionString);
1102+
internalState = new ShellInternalState(serviceProvider);
1103+
mongo = internalState.currentDb.getMongo();
1104+
sh = new Shard(mongo);
1105+
1106+
// check replset uninitialized
1107+
let members = await sh._mongo.getDB('config').getCollection('shards').find().sort({ _id: 1 }).toArray();
1108+
if (members.length === 0) {
1109+
// add new shards
1110+
expect((await sh.addShard(`${shardId}-0/${host}:${shardPorts[0]}`)).shardAdded).to.equal(`${shardId}-0`);
1111+
expect((await sh.addShard(`${shardId}-1/${host}:${shardPorts[1]}`)).shardAdded).to.equal(`${shardId}-1`);
1112+
members = await sh._mongo.getDB('config').getCollection('shards').find().sort({ _id: 1 }).toArray();
1113+
} else {
1114+
console.log('WARN: shards already added');
1115+
}
1116+
expect(members.length).to.equal(2);
1117+
await sh._mongo.getDB(dbName).dropDatabase();
1118+
});
1119+
1120+
after(() => {
1121+
return serviceProvider.close(true);
1122+
});
1123+
1124+
describe('sharding info', () => {
1125+
it('returns the status', async() => {
1126+
const result = await sh.status();
1127+
expect(result.type).to.equal('StatsResult');
1128+
expect(Object.keys(result.value)).to.include.members([
1129+
'shardingVersion', 'shards', 'active mongoses', 'autosplit', 'balancer', 'databases'
1130+
]);
1131+
});
1132+
});
1133+
describe('turn on sharding', () => {
1134+
it('enableSharding for a db', async() => {
1135+
expect((await sh.status()).value.databases.length).to.equal(1);
1136+
expect((await sh.enableSharding(dbName)).ok).to.equal(1);
1137+
expect((await sh.status()).value.databases.length).to.equal(2);
1138+
});
1139+
it('enableSharding for a collection', async() => {
1140+
expect(Object.keys((await sh.status()).value.databases[0].collections).length).to.equal(0);
1141+
expect((await sh.shardCollection(ns, { key: 1 })).collectionsharded).to.equal(ns);
1142+
expect((await sh.status()).value.databases[0].collections[ns].shardKey).to.deep.equal({ key: 1 });
1143+
});
1144+
});
1145+
describe('autosplit', () => {
1146+
it('disables correctly', async() => {
1147+
expect((await sh.disableAutoSplit()).acknowledged).to.equal(1);
1148+
expect((await sh.status()).value.autosplit['Currently enabled']).to.equal('no');
1149+
});
1150+
it('enables correctly', async() => {
1151+
expect((await sh.enableAutoSplit()).acknowledged).to.equal(1);
1152+
expect((await sh.status()).value.autosplit['Currently enabled']).to.equal('yes');
1153+
});
1154+
});
1155+
describe('tags', () => {
1156+
it('creates a zone', async() => {
1157+
expect((await sh.addShardTag(`${shardId}-1`, 'zone1')).ok).to.equal(1);
1158+
expect((await sh.status()).value.shards[1].tags).to.deep.equal(['zone1']);
1159+
expect((await sh.addShardToZone(`${shardId}-0`, 'zone0')).ok).to.equal(1);
1160+
expect((await sh.status()).value.shards[0].tags).to.deep.equal(['zone0']);
1161+
});
1162+
it('sets a zone key range', async() => {
1163+
expect((await sh.updateZoneKeyRange(ns, { key: 0 }, { key: 20 }, 'zone1')).ok).to.equal(1);
1164+
expect((await sh.status()).value.databases[0].collections[ns].tags[0]).to.deep.equal({
1165+
tag: 'zone1', min: { key: 0 }, max: { key: 20 }
1166+
});
1167+
expect((await sh.addTagRange(ns, { key: 21 }, { key: 40 }, 'zone0')).ok).to.equal(1);
1168+
expect((await sh.status()).value.databases[0].collections[ns].tags[1]).to.deep.equal({
1169+
tag: 'zone0', min: { key: 21 }, max: { key: 40 }
1170+
});
1171+
});
1172+
it('removes a key range', async() => {
1173+
expect((await sh.status()).value.databases[0].collections[ns].tags.length).to.equal(2);
1174+
expect((await sh.removeRangeFromZone(ns, { key: 0 }, { key: 20 })).ok).to.equal(1);
1175+
expect((await sh.status()).value.databases[0].collections[ns].tags.length).to.equal(1);
1176+
expect((await sh.removeTagRange(ns, { key: 21 }, { key: 40 })).ok).to.equal(1);
1177+
expect((await sh.status()).value.databases[0].collections[ns].tags.length).to.equal(0);
1178+
});
1179+
it('removes zones', async() => {
1180+
expect((await sh.removeShardFromZone(`${shardId}-1`, 'zone1')).ok).to.equal(1);
1181+
expect((await sh.status()).value.shards[1].tags).to.deep.equal([]);
1182+
expect((await sh.removeShardTag(`${shardId}-0`, 'zone0')).ok).to.equal(1);
1183+
expect((await sh.status()).value.shards[0].tags).to.deep.equal([]);
1184+
});
1185+
});
1186+
describe('balancer', () => {
1187+
it('reports balancer state', async() => {
1188+
expect(Object.keys(await sh.isBalancerRunning())).to.include.members([
1189+
'mode', 'inBalancerRound', 'numBalancerRounds'
1190+
]);
1191+
});
1192+
it('stops balancer', async() => {
1193+
expect((await sh.stopBalancer()).ok).to.equal(1);
1194+
expect((await sh.isBalancerRunning()).mode).to.equal('off');
1195+
});
1196+
it('starts balancer', async() => {
1197+
expect((await sh.startBalancer()).ok).to.equal(1);
1198+
expect((await sh.isBalancerRunning()).mode).to.equal('full');
1199+
});
1200+
it('reports state for collection', async() => {
1201+
expect(Object.keys(await sh.balancerCollectionStatus(ns))).to.include('balancerCompliant');
1202+
});
1203+
it('disables balancing', async() => {
1204+
expect((await sh.disableBalancing(ns)).acknowledged).to.equal(1);
1205+
expect((await sh._mongo.getDB('config').getCollection('collections').findOne({ _id: ns })).noBalance).to.equal(true);
1206+
});
1207+
it('enables balancing', async() => {
1208+
expect((await sh.enableBalancing(ns)).acknowledged).to.equal(1);
1209+
expect((await sh._mongo.getDB('config').getCollection('collections').findOne({ _id: ns })).noBalance).to.equal(false);
1210+
});
1211+
});
1212+
});
10851213
});

packages/shell-api/src/shard.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ export default class Shard extends ShellApiClass {
223223
@returnsPromise
224224
@serverVersions(['3.4.0', ServerVersions.latest])
225225
async enableAutoSplit(): Promise<any> {
226-
assertArgsDefined();
227226
this._emitShardApiCall('enableAutoSplit', {});
228227
const config = await getConfigDB(this._mongo);
229228
return await config.getCollection('settings').updateOne(
@@ -236,7 +235,6 @@ export default class Shard extends ShellApiClass {
236235
@returnsPromise
237236
@serverVersions(['3.4.0', ServerVersions.latest])
238237
async disableAutoSplit(): Promise<any> {
239-
assertArgsDefined();
240238
this._emitShardApiCall('disableAutoSplit', {});
241239
const config = await getConfigDB(this._mongo);
242240
return await config.getCollection('settings').updateOne(

0 commit comments

Comments
 (0)