forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_uuid_drop.js
91 lines (83 loc) · 4.01 KB
/
collection_uuid_drop.js
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
/**
* Tests the collectionUUID parameter of the drop command.
*
* @tags: [
* requires_fcv_60,
* requires_non_retryable_commands,
* # Not prepared to handle changes to collections UUID
* # due to background activity (e.g. *Resharding)
* assumes_stable_collection_uuid,
* ]
*/
const testDB = db.getSiblingDB(jsTestName());
assert.commandWorked(testDB.dropDatabase());
const coll = testDB["coll"];
const createCollection = function(coll) {
assert.commandWorked(coll.insert({_id: 0}));
const uuid = assert.commandWorked(testDB.runCommand({listCollections: 1}))
.cursor.firstBatch.find(c => c.name === coll.getName())
.info.uuid;
return uuid;
};
// The command fails when the provided UUID does not correspond to an existing collection.
let uuid = createCollection(coll);
const nonexistentUUID = UUID();
let res = assert.commandFailedWithCode(
testDB.runCommand({drop: coll.getName(), collectionUUID: nonexistentUUID}),
ErrorCodes.CollectionUUIDMismatch);
assert.eq(res.db, testDB.getName());
assert.eq(res.collectionUUID, nonexistentUUID);
assert.eq(res.expectedCollection, coll.getName());
assert.eq(res.actualCollection, null);
// The command fails when the provided UUID corresponds to a different collection.
const coll2 = testDB['coll_2'];
assert.commandWorked(coll2.insert({_id: 1}));
res = assert.commandFailedWithCode(testDB.runCommand({drop: coll2.getName(), collectionUUID: uuid}),
ErrorCodes.CollectionUUIDMismatch);
assert.eq(res.db, testDB.getName());
assert.eq(res.collectionUUID, uuid);
assert.eq(res.expectedCollection, coll2.getName());
assert.eq(res.actualCollection, coll.getName());
// Only collections in the same database are specified by actualCollection.
const otherDB = testDB.getSiblingDB(testDB.getName() + '_2');
assert.commandWorked(otherDB.dropDatabase());
const coll3 = otherDB['coll_3'];
assert.commandWorked(coll3.insert({_id: 2}));
res =
assert.commandFailedWithCode(otherDB.runCommand({drop: coll3.getName(), collectionUUID: uuid}),
ErrorCodes.CollectionUUIDMismatch);
assert.eq(res.db, otherDB.getName());
assert.eq(res.collectionUUID, uuid);
assert.eq(res.expectedCollection, coll3.getName());
assert.eq(res.actualCollection, null);
// The command fails when the provided UUID corresponds to a different collection, even if the
// provided namespace does not exist.
assert.commandWorked(testDB.runCommand({drop: coll2.getName()}));
res = assert.commandFailedWithCode(testDB.runCommand({drop: coll2.getName(), collectionUUID: uuid}),
ErrorCodes.CollectionUUIDMismatch);
assert.eq(res.db, testDB.getName());
assert.eq(res.collectionUUID, uuid);
assert.eq(res.expectedCollection, coll2.getName());
assert.eq(res.actualCollection, coll.getName());
assert(!testDB.getCollectionNames().includes(coll2.getName()));
// The command fails with CollectionUUIDMismatch even if the database does not exist.
const nonexistentDB = testDB.getSiblingDB(testDB.getName() + '_nonexistent');
res = assert.commandFailedWithCode(
nonexistentDB.runCommand({drop: 'nonexistent', collectionUUID: uuid}),
ErrorCodes.CollectionUUIDMismatch);
assert.eq(res.db, nonexistentDB.getName());
assert.eq(res.collectionUUID, uuid);
assert.eq(res.expectedCollection, 'nonexistent');
assert.eq(res.actualCollection, null);
// The command fails when the provided UUID corresponds to a different collection, even if the
// provided namespace is a view.
const view = 'view';
assert.commandWorked(testDB.createView(view, coll.getName(), []));
res = assert.commandFailedWithCode(testDB.runCommand({drop: view, collectionUUID: uuid}),
ErrorCodes.CollectionUUIDMismatch);
assert.eq(res.db, testDB.getName());
assert.eq(res.collectionUUID, uuid);
assert.eq(res.expectedCollection, view);
assert.eq(res.actualCollection, coll.getName());
// The command succeeds when the correct UUID is provided.
assert.commandWorked(testDB.runCommand({drop: coll.getName(), collectionUUID: uuid}));