Skip to content

Commit f71faa0

Browse files
committed
Add integration test
1 parent 4842194 commit f71faa0

File tree

3 files changed

+97
-12
lines changed

3 files changed

+97
-12
lines changed

packages/shell-api/src/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ export async function getPrintableShardStatus(
671671
let shardedDataDistribution: ShardedDataDistribution | undefined;
672672

673673
try {
674-
// Available since 6.0.3
674+
// Available since >= 6.0.3
675675
const adminDb = db.getSiblingDB('admin');
676676
shardedDataDistribution = (await (
677677
await adminDb.aggregate([{ $shardedDataDistribution: {} }])
@@ -686,7 +686,7 @@ export async function getPrintableShardStatus(
686686
return result;
687687
}
688688

689-
type ShardingStatusResult = {
689+
export type ShardingStatusResult = {
690690
shardingVersion: {
691691
_id: number;
692692
clusterId: ObjectId;
@@ -697,6 +697,7 @@ type ShardingStatusResult = {
697697
_id: string;
698698
host: string;
699699
state: number;
700+
tags: string[];
700701
topologyTime: Timestamp;
701702
replSetConfigVersion: Long;
702703
}[];

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

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import Database from './database';
3333
import { inspect } from 'util';
3434
import { dummyOptions } from './helpers.spec';
35+
import type { ShardedDataDistribution } from './helpers';
3536

3637
describe('Shard', function () {
3738
skipIfApiStrict();
@@ -2121,7 +2122,7 @@ describe('Shard', function () {
21212122
expect(
21222123
(await sh.status()).value.databases.find(
21232124
(d: Document) => d.database._id === 'test'
2124-
).collections[ns].shardKey
2125+
)?.collections[ns].shardKey
21252126
).to.deep.equal({ key: 1 });
21262127

21272128
const db = instanceState.currentDb.getSiblingDB(dbName);
@@ -2166,13 +2167,13 @@ describe('Shard', function () {
21662167
describe('tags', function () {
21672168
it('creates a zone', async function () {
21682169
expect((await sh.addShardTag(`${shardId}-1`, 'zone1')).ok).to.equal(1);
2169-
expect((await sh.status()).value.shards[1].tags).to.deep.equal([
2170+
expect((await sh.status()).value.shards[1]?.tags).to.deep.equal([
21702171
'zone1',
21712172
]);
21722173
expect((await sh.addShardToZone(`${shardId}-0`, 'zone0')).ok).to.equal(
21732174
1
21742175
);
2175-
expect((await sh.status()).value.shards[0].tags).to.deep.equal([
2176+
expect((await sh.status()).value.shards[0]?.tags).to.deep.equal([
21762177
'zone0',
21772178
]);
21782179
});
@@ -2241,7 +2242,7 @@ describe('Shard', function () {
22412242

22422243
const tags = (await sh.status()).value.databases.find(
22432244
(d: Document) => d.database._id === 'test'
2244-
).collections[ns].tags;
2245+
)?.collections[ns].tags;
22452246
expect(tags.length).to.equal(19);
22462247
});
22472248
it('cuts a tag list when there are more than 20 tags', async function () {
@@ -2251,7 +2252,7 @@ describe('Shard', function () {
22512252

22522253
const tags = (await sh.status()).value.databases.find(
22532254
(d: Document) => d.database._id === 'test'
2254-
).collections[ns].tags;
2255+
)?.collections[ns].tags;
22552256
expect(tags.length).to.equal(21);
22562257
expect(
22572258
tags.indexOf(
@@ -2885,6 +2886,87 @@ describe('Shard', function () {
28852886
});
28862887
});
28872888
});
2889+
2890+
describe('collection.status()', function () {
2891+
let db: Database;
2892+
2893+
const dbName = 'shard-stats-test';
2894+
const ns = `${dbName}.test`;
2895+
2896+
beforeEach(async function () {
2897+
db = sh._database.getSiblingDB(dbName);
2898+
await db.getCollection('test').insertOne({ key: 1 });
2899+
await db.getCollection('test').createIndex({ key: 1 });
2900+
});
2901+
afterEach(async function () {
2902+
await db.dropDatabase();
2903+
});
2904+
describe('unsharded collections', function () {
2905+
describe('with >= 6.0.3', function () {
2906+
skipIfServerVersion(mongos, '< 6.0.3');
2907+
2908+
it('returns shardedDataDistribution as an empty array', async function () {
2909+
const status = await sh.status();
2910+
expect(status.value.shardedDataDistribution).deep.equals([]);
2911+
});
2912+
});
2913+
2914+
describe('with < 6.0.3', function () {
2915+
skipIfServerVersion(mongos, '>= 6.0.3');
2916+
2917+
it('returns shardedDataDistribution as undefined', async function () {
2918+
const status = await sh.status();
2919+
expect(status.value.shardedDataDistribution).equals(undefined);
2920+
});
2921+
});
2922+
});
2923+
2924+
describe('sharded collections', function () {
2925+
beforeEach(async function () {
2926+
expect((await sh.enableSharding(dbName)).ok).to.equal(1);
2927+
expect(
2928+
(await sh.shardCollection(ns, { key: 1 })).collectionsharded
2929+
).to.equal(ns);
2930+
});
2931+
2932+
describe('with >= 6.0.3', function () {
2933+
skipIfServerVersion(mongos, '< 6.0.3');
2934+
2935+
it('returns correct shardedDataDistribution', async function () {
2936+
const expectedShardedDataDistribution: ShardedDataDistribution = [
2937+
{
2938+
ns: 'shard-stats-test.test',
2939+
shards: [
2940+
{
2941+
shardName: 'rs-shard0-0',
2942+
numOrphanedDocs: 0,
2943+
numOwnedDocuments: 1,
2944+
ownedSizeBytes: 31,
2945+
orphanedSizeBytes: 0,
2946+
},
2947+
],
2948+
},
2949+
];
2950+
2951+
const status = await sh.status();
2952+
2953+
expect(status.value.shardedDataDistribution).deep.equals(
2954+
expectedShardedDataDistribution
2955+
);
2956+
});
2957+
});
2958+
2959+
describe('with < 6.0.3', function () {
2960+
skipIfServerVersion(mongos, '>= 6.0.3');
2961+
2962+
it('returns shardedDataDistribution as undefined', async function () {
2963+
const status = await sh.status();
2964+
expect(status.value.shardedDataDistribution).equals(undefined);
2965+
});
2966+
});
2967+
});
2968+
});
2969+
28882970
describe('collection.isCapped', function () {
28892971
it('returns true for config.changelog', async function () {
28902972
const ret = await sh._database
@@ -2929,15 +3011,15 @@ describe('Shard', function () {
29293011
(item: Document) => item.database._id === 'db'
29303012
);
29313013
// Cannot get strict guarantees about the value of this field since SERVER-63983
2932-
expect(databasesDbItem.database.partitioned).to.be.oneOf([
3014+
expect(databasesDbItem?.database.partitioned).to.be.oneOf([
29333015
false,
29343016
undefined,
29353017
]);
29363018
const databasesDbShItem = result.value.databases.find(
29373019
(item: Document) => item.database._id === 'dbSh'
29383020
);
29393021
// Cannot get strict guarantees about the value of this field since SERVER-60926 and SERVER-63983
2940-
expect(databasesDbShItem.database.partitioned).to.be.oneOf([
3022+
expect(databasesDbShItem?.database.partitioned).to.be.oneOf([
29413023
true,
29423024
false,
29433025
undefined,
@@ -3051,15 +3133,15 @@ describe('Shard', function () {
30513133
}
30523134
const chunks = (await sh.status()).value.databases.find(
30533135
(d: Document) => d.database._id === 'test'
3054-
).collections[ns].chunks;
3136+
)?.collections[ns].chunks;
30553137
expect(chunks.length).to.equal(20);
30563138
});
30573139

30583140
it('cuts a chunk list when there are more than 20 chunks', async function () {
30593141
await sh.splitAt(ns, { key: 20 });
30603142
const chunks = (await sh.status()).value.databases.find(
30613143
(d: Document) => d.database._id === 'test'
3062-
).collections[ns].chunks;
3144+
)?.collections[ns].chunks;
30633145
expect(chunks.length).to.equal(21);
30643146
expect(
30653147
chunks.indexOf(

packages/shell-api/src/shard.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import type {
1212
Document,
1313
CheckMetadataConsistencyOptions,
1414
} from '@mongosh/service-provider-core';
15+
import type { ShardingStatusResult } from './helpers';
1516
import {
1617
assertArgsDefinedType,
1718
getConfigDB,
1819
getPrintableShardStatus,
20+
ShardedDataDistribution,
1921
} from './helpers';
2022
import { ServerVersions, asPrintable } from './enums';
2123
import type { UpdateResult } from './result';
@@ -205,7 +207,7 @@ export default class Shard extends ShellApiWithMongoClass {
205207
async status(
206208
verbose = false,
207209
configDB?: Database
208-
): Promise<CommandResult<Document>> {
210+
): Promise<CommandResult<ShardingStatusResult>> {
209211
const result = await getPrintableShardStatus(
210212
configDB ?? (await getConfigDB(this._database)),
211213
verbose

0 commit comments

Comments
 (0)