@@ -32,6 +32,7 @@ import {
3232import Database from './database' ;
3333import { inspect } from 'util' ;
3434import { dummyOptions } from './helpers.spec' ;
35+ import type { ShardedDataDistribution } from './helpers' ;
3536
3637describe ( '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 (
0 commit comments