summaryrefslogtreecommitdiff
path: root/jstests/replsets/apply_ops_create_with_uuid.js
blob: d3f69fc2a7181907f862f1b33d9ed5688553c8c3 (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
(function() {
    // Test applyOps behavior for collection creation with explicit UUIDs.
    "use strict";

    const replTest = new ReplSetTest({nodes: 1});
    replTest.startSet();
    replTest.initiate();

    const db = replTest.getPrimary().getDB('test');

    const uuid = UUID();
    // Two applyOps to create a foo collection with given uuid, one each for 'test' and 'test2' dbs.
    var ops = (uuid => ["test", "test2"].map(db => {
        return {op: "c", ns: db + ".$cmd", ui: uuid, o: {create: "foo"}};
    }))(uuid);

    function checkUUID(coll, uuid) {
        const cmd = {listCollections: 1, filter: {name: coll}};
        const res = assert.commandWorked(db.runCommand(cmd), tojson(cmd));
        assert.eq(res.cursor.firstBatch[0].info.uuid,
                  uuid,
                  tojson(cmd) + " did not return expected uuid: " + tojson(res));
    }

    jsTestLog("Create a test.foo collection with uuid " + uuid + " through applyOps.");
    let cmd = {applyOps: [ops[0]]};
    let res = assert.commandWorked(db.runCommand(cmd), tojson(cmd));

    // Check that test.foo has the expected UUID.
    jsTestLog("Check that test.foo has UUID " + uuid);
    checkUUID("foo", uuid);

    // Change the ops to refer to bar, instead of foo. Command should still work, renaming the
    // collection.  Second command should fail as it tries to associate the "test2.foo" name with
    // an existing collection in the "test" database. This must fail.
    jsTestLog("Create test.bar and try to create test2.foo collections with the same UUID.");
    ops[0].o.create = "bar";
    res = assert.commandFailed(db.runCommand({applyOps: ops}));
    assert.eq(res.results,
              [true, false],
              "expected first operation " + tojson(ops[0]) + " to succeed, and second operation " +
                  tojson(ops[1]) + " to fail, got " + tojson(res));

    jsTestLog("Check that test.bar has UUID " + uuid);
    checkUUID("bar", uuid);
    jsTestLog("Check that test.foo no longer exists");
    assert.eq(db.getCollectionInfos({name: "foo"}).length,
              0,
              "expected foo collection to no longer exist");
    replTest.stopSet();
}());