(function() {
'use strict';
var st = new ShardingTest({mongos: 1, shards: 2, other: {enableBalancer: false}});
const zoneName = 'zone';
const dbName = 'db';
const collName = 'coll';
const shard0 = st.shard0.shardName;
const shard1 = st.shard1.shardName;
const sKey = 'x';
assert.commandWorked(st.s.adminCommand({enableSharding: dbName, primaryShard: shard0}));
const coll = st.s.getDB(dbName)[collName];
assert.commandWorked(st.s.adminCommand({addShardToZone: shard0, zone: zoneName}));
assert.commandWorked(st.configRS.getPrimary().adminCommand({_flushDatabaseCacheUpdates: dbName}));
assert.commandWorked(st.s.adminCommand({shardCollection: coll.getFullName(), key: {sKey: 1}}));
assert(st.shard0.getCollection(coll.getFullName()).exists(),
"Collection does not exist on shard0 despite the shard has chunks and is database primary");
assert(
!st.shard1.getCollection(coll.getFullName()).exists(),
"Collection exists on shard1 even but shard1 is not db primary nor has chunks for the collection");
assert(coll.exists());
assert.commandWorked(st.s.adminCommand({movePrimary: dbName, to: shard1}))
assert.commandWorked(st.s.adminCommand({
reshardCollection: coll.getFullName(),
key: {sKey: 1, "newField": 1},
zones: [{
min: {sKey: MinKey(), newField: MinKey()},
max: {sKey: MaxKey(), newField: MaxKey()},
zone: zoneName,
}]
}));
assert(st.shard0.getCollection(coll.getFullName()).exists(),
"Collection does not exist on shard0 despite the shard has chunks");
let res = assert.commandWorked(st.s.adminCommand({checkMetadataConsistency: 1}));
assert.neq(res.cursor.firstBatch.length, 0);
jsTest.log("Logging checkMetadataConsistency");
jsTest.log(res);
assert(st.shard1.getCollection(coll.getFullName()).exists(),
"Collection does not exist on shard1 despite the shard is database primary");
assert(coll.exists());
st.stop();
})();