summaryrefslogtreecommitdiff
path: root/jstests/core/views/views_all_commands.js
blob: fbdf3942785ef158336f2706cc4aa664b62f2d56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Uses features that require featureCompatibilityVersion 3.6.
// @tags: [
//   requires_fcv36,
//   does_not_support_stepdowns,
//   requires_getmore,
//   requires_non_retryable_commands,
//   requires_non_retryable_writes,
//   requires_fastcount,
// ]

/*
 * Declaratively-defined tests for views for all database commands. This file contains a map of test
 * definitions as well as code to run them.
 *
 * The example test
 *
 *      {
 *          command: {insert: "view", documents: [{x: 1}]},
 *          expectFailure: true
 *      }
 *
 * invokes runCommand with `command` and expects it to fail with an error code specific to views.
 * A test can be an array of subtests as well.
 *
 * Each test or subtest takes the following options:
 *
 *  command
 *      The command object to pass to db.runCommand(). Each command can assume that there exists a
 *      view named "view", built on top of a collection named "collection". A command can also be a
 *      function that takes a db handle as argument and handles pass/failure checking internally.
 *
 *  isAdminCommand
 *      If true, will execute 'command' against the admin db.
 *
 *  skip
 *      A string that, if present, causes the test runner to skip running this command altogether.
 *      The value should be the reason why the test is being skipped. (There is a predefined
 *      selection of commonly-used reasons below.)
 *
 *  expectFailure
 *      If true, assert that the command fails. Otherwise, all commands are expected to succeed.
 *
 *  expectedErrorCode
 *      When 'expectFailure' is true, specifies the error code expected. Defaults to
 *      'CommandNotSupportedOnView' when not specified. Set to 'null' when expecting an error
 *      without an error code field.
 *
 *  setup
 *      A function that will be run before the command is executed. It takes a handle to the 'test'
 *      database as its single argument.
 *
 *  teardown
 *      A function that will be run after the command is executed. It takes a handle to the 'test'
 *      database as its single argument.
 *
 *  skipSharded
 *      If true, do not run this command on a mongos.
 *
 *  skipStandalone
 *      If true, do not run this command on a standalone mongod.
 */

(function() {
    "use strict";

    // Pre-written reasons for skipping a test.
    const isAnInternalCommand = "internal command";
    const isUnrelated = "is unrelated";

    let viewsCommandTests = {
        _configsvrAddShard: {skip: isAnInternalCommand},
        _configsvrAddShardToZone: {skip: isAnInternalCommand},
        _configsvrBalancerStart: {skip: isAnInternalCommand},
        _configsvrBalancerStatus: {skip: isAnInternalCommand},
        _configsvrBalancerStop: {skip: isAnInternalCommand},
        _configsvrCommitChunkMerge: {skip: isAnInternalCommand},
        _configsvrCommitChunkMigration: {skip: isAnInternalCommand},
        _configsvrCommitChunkSplit: {skip: isAnInternalCommand},
        _configsvrCreateDatabase: {skip: isAnInternalCommand},
        _configsvrEnableSharding: {skip: isAnInternalCommand},
        _configsvrMoveChunk: {skip: isAnInternalCommand},
        _configsvrMovePrimary: {skip: isAnInternalCommand},
        _configsvrRemoveShard: {skip: isAnInternalCommand},
        _configsvrRemoveShardFromZone: {skip: isAnInternalCommand},
        _configsvrShardCollection: {skip: isAnInternalCommand},
        _configsvrSetFeatureCompatibilityVersion: {skip: isAnInternalCommand},
        _configsvrUpdateZoneKeyRange: {skip: isAnInternalCommand},
        _flushRoutingTableCacheUpdates: {skip: isUnrelated},
        _getNextSessionMods: {skip: isAnInternalCommand},
        _getUserCacheGeneration: {skip: isAnInternalCommand},
        _hashBSONElement: {skip: isAnInternalCommand},
        _isSelf: {skip: isAnInternalCommand},
        _mergeAuthzCollections: {skip: isAnInternalCommand},
        _migrateClone: {skip: isAnInternalCommand},
        _recvChunkAbort: {skip: isAnInternalCommand},
        _recvChunkCommit: {skip: isAnInternalCommand},
        _recvChunkStart: {skip: isAnInternalCommand},
        _recvChunkStatus: {skip: isAnInternalCommand},
        _transferMods: {skip: isAnInternalCommand},
        addShard: {skip: isUnrelated},
        addShardToZone: {skip: isUnrelated},
        aggregate: {command: {aggregate: "view", pipeline: [{$match: {}}], cursor: {}}},
        appendOplogNote: {skip: isUnrelated},
        applyOps: {
            command: {applyOps: [{op: "i", o: {_id: 1}, ns: "test.view"}]},
            expectFailure: true,
            skipSharded: true,
        },
        authSchemaUpgrade: {skip: isUnrelated},
        authenticate: {skip: isUnrelated},
        availableQueryOptions: {skip: isAnInternalCommand},
        balancerStart: {skip: isUnrelated},
        balancerStatus: {skip: isUnrelated},
        balancerStop: {skip: isUnrelated},
        buildInfo: {skip: isUnrelated},
        captrunc: {
            command: {captrunc: "view", n: 2, inc: false},
            expectFailure: true,
        },
        checkShardingIndex: {skip: isUnrelated},
        cleanupOrphaned: {
            skip: "Tested in views/views_sharded.js",
        },
        clearLog: {skip: isUnrelated},
        clone: {skip: "Tested in replsets/cloneDb.js"},
        cloneCollection: {skip: "Tested in noPassthroughWithMongod/clonecollection.js"},
        cloneCollectionAsCapped: {
            command: {cloneCollectionAsCapped: "view", toCollection: "testcapped", size: 10240},
            expectFailure: true,
        },
        collMod: {command: {collMod: "view", viewOn: "other", pipeline: []}},
        collStats: {skip: "Tested in views/views_coll_stats.js"},
        compact: {command: {compact: "view", force: true}, expectFailure: true, skipSharded: true},
        configureFailPoint: {skip: isUnrelated},
        connPoolStats: {skip: isUnrelated},
        connPoolSync: {skip: isUnrelated},
        connectionStatus: {skip: isUnrelated},
        convertToCapped: {command: {convertToCapped: "view", size: 12345}, expectFailure: true},
        copydb: {skip: "Tested in replsets/copydb.js"},
        copydbgetnonce: {skip: isUnrelated},
        copydbsaslstart: {skip: isUnrelated},
        count: {command: {count: "view"}},
        cpuload: {skip: isAnInternalCommand},
        create: {skip: "tested in views/views_creation.js"},
        createIndexes: {
            command: {createIndexes: "view", indexes: [{key: {x: 1}, name: "x_1"}]},
            expectFailure: true,
        },
        createRole: {
            command: {createRole: "testrole", privileges: [], roles: []},
            setup: function(conn) {
                assert.commandWorked(conn.runCommand({dropAllRolesFromDatabase: 1}));
            },
            teardown: function(conn) {
                assert.commandWorked(conn.runCommand({dropAllRolesFromDatabase: 1}));
            }
        },
        createUser: {
            command: {createUser: "testuser", pwd: "testpass", roles: []},
            setup: function(conn) {
                assert.commandWorked(conn.runCommand({dropAllUsersFromDatabase: 1}));
            },
            teardown: function(conn) {
                assert.commandWorked(conn.runCommand({dropAllUsersFromDatabase: 1}));
            }
        },
        currentOp: {skip: isUnrelated},
        dataSize: {
            command: {dataSize: "test.view"},
            expectFailure: true,
        },
        dbCheck: {command: {dbCheck: "view"}, expectFailure: true},
        dbHash: {
            command: function(conn) {
                let getHash = function() {
                    let cmd = {dbHash: 1};
                    let res = conn.runCommand(cmd);
                    assert.commandWorked(res, tojson(cmd));
                    return res.collections["system.views"];
                };
                // The checksum below should change if we change the views, but not otherwise.
                let hash1 = getHash();
                assert.commandWorked(conn.runCommand({create: "view2", viewOn: "view"}),
                                     "could not create view 'view2' on 'view'");
                let hash2 = getHash();
                assert.neq(hash1, hash2, "expected hash to change after creating new view");
                assert.commandWorked(conn.runCommand({drop: "view2"}), "problem dropping view2");
                let hash3 = getHash();
                assert.eq(hash1, hash3, "hash should be the same again after removing 'view2'");
            }
        },
        dbStats: {skip: "TODO(SERVER-25948)"},
        delete: {command: {delete: "view", deletes: [{q: {x: 1}, limit: 1}]}, expectFailure: true},
        distinct: {command: {distinct: "view", key: "_id"}},
        driverOIDTest: {skip: isUnrelated},
        drop: {command: {drop: "view"}},
        dropAllRolesFromDatabase: {skip: isUnrelated},
        dropAllUsersFromDatabase: {skip: isUnrelated},
        dropDatabase: {command: {dropDatabase: 1}},
        dropIndexes: {command: {dropIndexes: "view"}, expectFailure: true},
        dropRole: {
            command: {dropRole: "testrole"},
            setup: function(conn) {
                assert.commandWorked(
                    conn.runCommand({createRole: "testrole", privileges: [], roles: []}));
            },
            teardown: function(conn) {
                assert.commandWorked(conn.runCommand({dropAllRolesFromDatabase: 1}));
            }
        },
        dropUser: {skip: isUnrelated},
        emptycapped: {
            command: {emptycapped: "view"},
            expectFailure: true,
        },
        enableSharding: {skip: "Tested as part of shardCollection"},
        endSessions: {skip: isUnrelated},
        eval: {skip: isUnrelated},
        explain: {command: {explain: {count: "view"}}},
        features: {skip: isUnrelated},
        filemd5: {skip: isUnrelated},
        find: {skip: "tested in views/views_find.js & views/views_sharded.js"},
        findAndModify: {
            command: {findAndModify: "view", query: {a: 1}, update: {$set: {a: 2}}},
            expectFailure: true
        },
        flushRouterConfig: {skip: isUnrelated},
        forceerror: {skip: isUnrelated},
        fsync: {skip: isUnrelated},
        fsyncUnlock: {skip: isUnrelated},
        geoNear: {
            command:
                {geoNear: "view", near: {type: "Point", coordinates: [-50, 37]}, spherical: true},
            expectFailure: true
        },
        geoSearch: {
            command: {
                geoSearch: "view",
                search: {},
                near: [-50, 37],
            },
            expectFailure: true
        },
        getCmdLineOpts: {skip: isUnrelated},
        getDiagnosticData: {skip: isUnrelated},
        getLastError: {skip: isUnrelated},
        getLog: {skip: isUnrelated},
        getMore: {
            setup: function(conn) {
                assert.writeOK(conn.collection.remove({}));
                assert.writeOK(conn.collection.insert([{_id: 1}, {_id: 2}, {_id: 3}]));
            },
            command: function(conn) {
                function testGetMoreForCommand(cmd) {
                    let res = conn.runCommand(cmd);
                    assert.commandWorked(res, cmd);
                    let cursor = res.cursor;
                    assert.eq(cursor.ns,
                              "test.view",
                              "expected view namespace in cursor: " + tojson(cursor));
                    let expectedFirstBatch = [{_id: 1}, {_id: 2}];
                    assert.eq(cursor.firstBatch, expectedFirstBatch, "returned wrong firstBatch");
                    let getmoreCmd = {getMore: cursor.id, collection: "view"};
                    res = conn.runCommand(getmoreCmd);

                    assert.commandWorked(res, getmoreCmd);
                    assert.eq("test.view",
                              res.cursor.ns,
                              "expected view namespace in cursor: " + tojson(res));
                }
                // find command.
                let findCmd = {find: "view", filter: {_id: {$gt: 0}}, batchSize: 2};
                testGetMoreForCommand(findCmd);

                // aggregate command.
                let aggCmd = {
                    aggregate: "view",
                    pipeline: [{$match: {_id: {$gt: 0}}}],
                    cursor: {batchSize: 2}
                };
                testGetMoreForCommand(aggCmd);
            }
        },
        getParameter: {skip: isUnrelated},
        getPrevError: {skip: isUnrelated},
        getShardMap: {skip: isUnrelated},
        getShardVersion: {
            command: {getShardVersion: "test.view"},
            isAdminCommand: true,
            expectFailure: true,
            skipSharded: true,  // mongos is tested in views/views_sharded.js
        },
        getnonce: {skip: isUnrelated},
        godinsert: {skip: isAnInternalCommand},
        grantPrivilegesToRole: {skip: "tested in auth/commands_user_defined_roles.js"},
        grantRolesToRole: {skip: isUnrelated},
        grantRolesToUser: {skip: isUnrelated},
        group: {
            command: {group: {ns: "test.view", key: "x", $reduce: function() {}, initial: {}}},
        },
        handshake: {skip: isUnrelated},
        hostInfo: {skip: isUnrelated},
        insert: {command: {insert: "view", documents: [{x: 1}]}, expectFailure: true},
        invalidateUserCache: {skip: isUnrelated},
        isdbgrid: {skip: isUnrelated},
        isMaster: {skip: isUnrelated},
        journalLatencyTest: {skip: isUnrelated},
        killCursors: {
            setup: function(conn) {
                assert.writeOK(conn.collection.remove({}));
                assert.writeOK(conn.collection.insert([{_id: 1}, {_id: 2}, {_id: 3}]));
            },
            command: function(conn) {
                // First get and check a partial result for an aggregate command.
                let aggCmd = {
                    aggregate: "view",
                    pipeline: [{$sort: {_id: 1}}],
                    cursor: {batchSize: 2}
                };
                let res = conn.runCommand(aggCmd);
                assert.commandWorked(res, aggCmd);
                let cursor = res.cursor;
                assert.eq(
                    cursor.ns, "test.view", "expected view namespace in cursor: " + tojson(cursor));
                let expectedFirstBatch = [{_id: 1}, {_id: 2}];
                assert.eq(
                    cursor.firstBatch, expectedFirstBatch, "aggregate returned wrong firstBatch");

                // Then check correct execution of the killCursors command.
                let killCursorsCmd = {killCursors: "view", cursors: [cursor.id]};
                res = conn.runCommand(killCursorsCmd);
                assert.commandWorked(res, killCursorsCmd);
                let expectedRes = {
                    cursorsKilled: [cursor.id],
                    cursorsNotFound: [],
                    cursorsAlive: [],
                    cursorsUnknown: [],
                    ok: 1
                };
                delete res.operationTime;
                delete res.$clusterTime;
                assert.eq(expectedRes, res, "unexpected result for: " + tojson(killCursorsCmd));
            }
        },
        killOp: {skip: isUnrelated},
        killSessions: {skip: isUnrelated},
        killAllSessions: {skip: isUnrelated},
        killAllSessionsByPattern: {skip: isUnrelated},
        listCollections: {skip: "tested in views/views_creation.js"},
        listCommands: {skip: isUnrelated},
        listDatabases: {skip: isUnrelated},
        listIndexes: {command: {listIndexes: "view"}, expectFailure: true},
        listShards: {skip: isUnrelated},
        lockInfo: {skip: isUnrelated},
        logApplicationMessage: {skip: isUnrelated},
        logRotate: {skip: isUnrelated},
        logout: {skip: isUnrelated},
        makeSnapshot: {skip: isAnInternalCommand},
        mapReduce: {
            command:
                {mapReduce: "view", map: function() {}, reduce: function(key, vals) {}, out: "out"},
            expectFailure: true
        },
        "mapreduce.shardedfinish": {skip: isAnInternalCommand},
        mergeChunks: {
            command: {mergeChunks: "test.view", bounds: [{x: 0}, {x: 10}]},
            skipStandalone: true,
            isAdminCommand: true,
            expectFailure: true,
            expectedErrorCode: ErrorCodes.NamespaceNotSharded,
        },
        moveChunk: {
            command: {moveChunk: "test.view"},
            skipStandalone: true,
            isAdminCommand: true,
            expectFailure: true,
            expectedErrorCode: ErrorCodes.NamespaceNotSharded,
        },
        movePrimary: {skip: "Tested in sharding/movePrimary1.js"},
        multicast: {skip: isUnrelated},
        netstat: {skip: isAnInternalCommand},
        parallelCollectionScan: {command: {parallelCollectionScan: "view"}, expectFailure: true},
        ping: {command: {ping: 1}},
        planCacheClear: {command: {planCacheClear: "view"}, expectFailure: true},
        planCacheClearFilters: {command: {planCacheClearFilters: "view"}, expectFailure: true},
        planCacheListFilters: {command: {planCacheListFilters: "view"}, expectFailure: true},
        planCacheListPlans: {command: {planCacheListPlans: "view"}, expectFailure: true},
        planCacheListQueryShapes:
            {command: {planCacheListQueryShapes: "view"}, expectFailure: true},
        planCacheSetFilter: {command: {planCacheSetFilter: "view"}, expectFailure: true},
        profile: {skip: isUnrelated},
        refreshLogicalSessionCacheNow: {skip: isAnInternalCommand},
        reapLogicalSessionCacheNow: {skip: isAnInternalCommand},
        refreshSessions: {skip: isUnrelated},
        refreshSessionsInternal: {skip: isAnInternalCommand},
        reIndex: {command: {reIndex: "view"}, expectFailure: true},
        removeShard: {skip: isUnrelated},
        removeShardFromZone: {skip: isUnrelated},
        renameCollection: [
            {
              isAdminCommand: true,
              command: {renameCollection: "test.view", to: "test.otherview"},
              expectFailure: true,
              skipSharded: true,
            },
            {
              isAdminCommand: true,
              command: {renameCollection: "test.collection", to: "test.view"},
              expectFailure: true,
              expectedErrorCode: ErrorCodes.NamespaceExists,
              skipSharded: true,
            }
        ],
        repairCursor: {command: {repairCursor: "view"}, expectFailure: true},
        repairDatabase: {command: {repairDatabase: 1}},
        replSetAbortPrimaryCatchUp: {skip: isUnrelated},
        replSetElect: {skip: isUnrelated},
        replSetFreeze: {skip: isUnrelated},
        replSetFresh: {skip: isUnrelated},
        replSetGetConfig: {skip: isUnrelated},
        replSetGetRBID: {skip: isUnrelated},
        replSetGetStatus: {skip: isUnrelated},
        replSetHeartbeat: {skip: isUnrelated},
        replSetInitiate: {skip: isUnrelated},
        replSetMaintenance: {skip: isUnrelated},
        replSetReconfig: {skip: isUnrelated},
        replSetRequestVotes: {skip: isUnrelated},
        replSetStepDown: {skip: isUnrelated},
        replSetStepUp: {skip: isUnrelated},
        replSetSyncFrom: {skip: isUnrelated},
        replSetTest: {skip: isUnrelated},
        replSetUpdatePosition: {skip: isUnrelated},
        replSetResizeOplog: {skip: isUnrelated},
        resetError: {skip: isUnrelated},
        resync: {skip: isUnrelated},
        revokePrivilegesFromRole: {
            command: {
                revokePrivilegesFromRole: "testrole",
                privileges: [{resource: {db: "test", collection: "view"}, actions: ["find"]}]
            },
            setup: function(conn) {
                assert.commandWorked(
                    conn.runCommand({createRole: "testrole", privileges: [], roles: []}));
            },
            teardown: function(conn) {
                assert.commandWorked(conn.runCommand({dropAllRolesFromDatabase: 1}));
            }
        },
        revokeRolesFromRole: {skip: isUnrelated},
        revokeRolesFromUser: {skip: isUnrelated},
        rolesInfo: {skip: isUnrelated},
        saslContinue: {skip: isUnrelated},
        saslStart: {skip: isUnrelated},
        serverStatus: {command: {serverStatus: 1}, skip: isUnrelated},
        setCommittedSnapshot: {skip: isAnInternalCommand},
        setFeatureCompatibilityVersion: {skip: isUnrelated},
        setParameter: {skip: isUnrelated},
        setShardVersion: {skip: isUnrelated},
        shardCollection: {
            command: {shardCollection: "test.view", key: {_id: 1}},
            setup: function(conn) {
                assert.commandWorked(conn.adminCommand({enableSharding: "test"}));
            },
            skipStandalone: true,
            expectFailure: true,
            isAdminCommand: true,
        },
        shardConnPoolStats: {skip: isUnrelated},
        shardingState: {skip: isUnrelated},
        shutdown: {skip: isUnrelated},
        sleep: {skip: isUnrelated},
        split: {
            command: {split: "test.view", find: {_id: 1}},
            skipStandalone: true,
            expectFailure: true,
            expectedErrorCode: ErrorCodes.NamespaceNotSharded,
            isAdminCommand: true,
        },
        splitChunk: {
            command: {
                splitChunk: "test.view",
                from: "shard0000",
                min: {x: MinKey},
                max: {x: 0},
                keyPattern: {x: 1},
                splitKeys: [{x: -2}, {x: -1}],
                shardVersion: [ObjectId(), 2]
            },
            skipSharded: true,
            expectFailure: true,
            expectedErrorCode: 193,
            isAdminCommand: true,
        },
        splitVector: {
            command: {
                splitVector: "test.view",
                keyPattern: {x: 1},
                maxChunkSize: 1,
            },
            expectFailure: true,
        },
        stageDebug: {skip: isAnInternalCommand},
        startSession: {skip: isAnInternalCommand},
        top: {skip: "tested in views/views_stats.js"},
        touch: {
            command: {touch: "view", data: true},
            expectFailure: true,
        },
        unsetSharding: {skip: isAnInternalCommand},
        update: {command: {update: "view", updates: [{q: {x: 1}, u: {x: 2}}]}, expectFailure: true},
        updateRole: {
            command: {
                updateRole: "testrole",
                privileges: [{resource: {db: "test", collection: "view"}, actions: ["find"]}]
            },
            setup: function(conn) {
                assert.commandWorked(
                    conn.runCommand({createRole: "testrole", privileges: [], roles: []}));
            },
            teardown: function(conn) {
                assert.commandWorked(conn.runCommand({dropAllRolesFromDatabase: 1}));
            }
        },
        updateUser: {skip: isUnrelated},
        updateZoneKeyRange: {skip: isUnrelated},
        usersInfo: {skip: isUnrelated},
        validate: {command: {validate: "view"}, expectFailure: true},
        whatsmyuri: {skip: isUnrelated}
    };

    /**
     * Helper function for failing commands or writes that checks the result 'res' of either.
     * If 'code' is null we only check for failure, otherwise we confirm error code matches as
     * well. On assert 'msg' is printed.
     */
    let assertCommandOrWriteFailed = function(res, code, msg) {
        if (res.writeErrors !== undefined)
            assert.neq(0, res.writeErrors.length, msg);
        else if (res.code !== null)
            assert.commandFailedWithCode(res, code, msg);
        else
            assert.commandFailed(res, msg);
    };

    // Are we on a mongos?
    var isMaster = db.runCommand("ismaster");
    assert.commandWorked(isMaster);
    var isMongos = (isMaster.msg === "isdbgrid");

    // Obtain a list of all commands.
    let res = db.runCommand({listCommands: 1});
    assert.commandWorked(res);

    let commands = Object.keys(res.commands);
    for (let command of commands) {
        let test = viewsCommandTests[command];
        assert(test !== undefined,
               "Coverage failure: must explicitly define a views test for " + command);

        if (!(test instanceof Array))
            test = [test];
        let subtest_nr = 0;
        for (let subtest of test) {
            // Tests can be explicitly skipped. Print the name of the skipped test, as well as
            // the reason why.
            if (subtest.skip !== undefined) {
                print("Skipping " + command + ": " + subtest.skip);
                continue;
            }

            let dbHandle = db.getSiblingDB("test");
            let commandHandle = dbHandle;

            // Skip tests depending on sharding configuration.
            if (subtest.skipSharded && isMongos) {
                print("Skipping " + command + ": not applicable to mongoS");
                continue;
            }

            if (subtest.skipStandalone && !isMongos) {
                print("Skipping " + command + ": not applicable to mongoD");
                continue;
            }

            // Perform test setup, and call any additional setup callbacks provided by the test.
            // All tests assume that there exists a view named 'view' that is backed by
            // 'collection'.
            assert.commandWorked(dbHandle.dropDatabase());
            assert.commandWorked(dbHandle.runCommand({create: "view", viewOn: "collection"}));
            assert.writeOK(dbHandle.collection.insert({x: 1}));
            if (subtest.setup !== undefined)
                subtest.setup(dbHandle);

            // Execute the command. Print the command name for the first subtest, as otherwise
            // it may be hard to figure out what command caused a failure.
            if (!subtest_nr++)
                print("Testing " + command);

            if (subtest.isAdminCommand)
                commandHandle = db.getSiblingDB("admin");

            if (subtest.expectFailure) {
                let expectedErrorCode = subtest.expectedErrorCode;
                if (expectedErrorCode === undefined)
                    expectedErrorCode = ErrorCodes.CommandNotSupportedOnView;

                assertCommandOrWriteFailed(commandHandle.runCommand(subtest.command),
                                           expectedErrorCode,
                                           tojson(subtest.command));
            } else if (subtest.command instanceof Function)
                subtest.command(commandHandle);
            else
                assert.commandWorked(commandHandle.runCommand(subtest.command),
                                     tojson(subtest.command));

            if (subtest.teardown !== undefined)
                subtest.teardown(dbHandle);
        }
    }
}());