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
|
// This tests the emptycapped command in a replica set.
(function() {
"use strict";
var rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();
var primaryTestDB = rst.getPrimary().getDB('test');
var primaryLocalDB = rst.getPrimary().getDB('local');
var primaryAdminDB = rst.getPrimary().getDB('admin');
var secondaryTestDB = rst.getSecondary().getDB('test');
// Truncate a non-capped collection.
assert.writeOK(primaryTestDB.noncapped.insert({x: 1}));
assert.commandWorked(primaryTestDB.runCommand({emptycapped: 'noncapped'}));
assert.eq(primaryTestDB.noncapped.find().itcount(),
0,
"Expected 0 documents to exist after emptying the collection");
// Truncate a non-existent collection on a non-existent database.
assert.commandWorked(rst.getPrimary().getDB('nonexistent').dropDatabase());
assert.commandFailedWithCode(
rst.getPrimary().getDB('nonexistent').runCommand({emptycapped: 'nonexistent'}), 13429);
// Truncate a non-existent collection.
primaryTestDB.nonexistent.drop();
assert.commandFailedWithCode(primaryTestDB.runCommand({emptycapped: 'nonexistent'}), 28584);
// Truncate a capped collection.
assert.commandWorked(primaryTestDB.createCollection("capped", {capped: true, size: 4096}));
assert.writeOK(primaryTestDB.capped.insert({}));
assert.eq(
primaryTestDB.capped.find().itcount(), 1, "Expected 1 document to exist after an insert");
assert.commandWorked(primaryTestDB.runCommand({emptycapped: 'capped'}));
assert.eq(primaryTestDB.capped.find().itcount(),
0,
"Expected 0 documents to exist after emptying the collection");
// Truncate a capped collection on a secondary.
assert.commandFailedWithCode(secondaryTestDB.runCommand({emptycapped: 'capped'}),
ErrorCodes.NotMaster);
// Truncate the oplog.
assert.commandFailedWithCode(primaryLocalDB.runCommand({emptycapped: "oplog.rs"}),
ErrorCodes.OplogOperationUnsupported);
// Test system collections, which cannot be truncated except system.profile.
// Truncate the local system.js collection.
assert.writeOK(primaryTestDB.system.js.insert({_id: "mystring", value: "var root = this;"}));
assert.commandFailedWithCode(primaryTestDB.runCommand({emptycapped: "system.js"}),
ErrorCodes.IllegalOperation);
// Truncate the system.profile collection.
assert.commandWorked(
primaryTestDB.createCollection("system.profile", {capped: true, size: 4096}));
assert.commandWorked(primaryTestDB.runCommand({profile: 2}));
assert.commandWorked(primaryTestDB.runCommand({emptycapped: "system.profile"}));
assert.commandWorked(primaryTestDB.runCommand({profile: 0}));
assert(primaryTestDB.system.profile.drop(), "Failed to drop the system.profile collection");
// Truncate the local system.replset collection.
assert.commandFailedWithCode(primaryLocalDB.runCommand({emptycapped: "system.replset"}),
ErrorCodes.IllegalOperation);
// Test user & role management system collections.
assert.commandWorked(primaryAdminDB.runCommand({
createRole: "all1",
privileges: [{resource: {db: "", collection: ""}, actions: ["anyAction"]}],
roles: []
}));
assert.commandWorked(primaryAdminDB.runCommand(
{createUser: "root2", pwd: "pwd", roles: [{role: "root", db: "admin"}]}));
// TODO: Test system.backup_users & system.new_users.
// Truncate the admin system.roles collection.
assert.commandFailedWithCode(primaryAdminDB.runCommand({emptycapped: "system.roles"}),
ErrorCodes.IllegalOperation);
// Truncate the admin system.users collection.
assert.commandFailedWithCode(primaryAdminDB.runCommand({emptycapped: "system.users"}),
ErrorCodes.IllegalOperation);
// Truncate the admin system.version collection.
assert.commandFailedWithCode(primaryAdminDB.runCommand({emptycapped: "system.version"}),
ErrorCodes.IllegalOperation);
// Truncate the local system.views collection.
assert.commandWorked(primaryTestDB.runCommand(
{create: "view1", viewOn: "collection", pipeline: [{$match: {}}]}));
assert.commandFailedWithCode(primaryTestDB.runCommand({emptycapped: "system.views"}),
ErrorCodes.IllegalOperation);
})();
|