summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/views_legacy.js
blob: c52aca93bc343cfc8b1aa9521d431647c7e80b40 (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
/**
 * Tests that views properly reject queries in legacy read mode, and reject writes performed in
 * legacy write mode. Also confirms that legacy killCursors execution is successful.
 */
(function() {
    "use strict";

    let conn = MongoRunner.runMongod({});

    let viewsDB = conn.getDB("views_legacy");
    assert.commandWorked(viewsDB.dropDatabase());
    assert.commandWorked(viewsDB.createView("view", "collection", []));
    let coll = viewsDB.getCollection("collection");

    for (let i = 0; i < 10; ++i) {
        assert.writeOK(coll.insert({a: i}));
    }

    conn.forceReadMode("legacy");
    conn.forceWriteMode("legacy");

    //
    // Legacy getMore is explicitly prohibited on views; you must use the getMore command.
    //
    let cmdRes =
        viewsDB.runCommand({find: "view", filter: {a: {$gt: 0}}, sort: {a: 1}, batchSize: 0});
    assert.commandWorked(cmdRes);
    let cursor = new DBCommandCursor(viewsDB, cmdRes, 2);

    let err = assert.throws(function() {
        cursor.itcount();
    }, [], "Legacy getMore expected to fail on a view cursor");
    assert.eq(ErrorCodes.CommandNotSupportedOnView, err.code, tojson(err));

    //
    // Legacy killcursors is expected to work on views.
    //
    cmdRes = viewsDB.runCommand({find: "view", filter: {a: {$gt: 0}}, sort: {a: 1}, batchSize: 0});
    assert.commandWorked(cmdRes);
    cursor = new DBCommandCursor(viewsDB, cmdRes, 2);

    // When DBCommandCursor is constructed under legacy readMode, cursor.close() will execute a
    // legacy killcursors operation.
    cursor.close();
    assert.gleSuccess(viewsDB, "legacy killcursors expected to work on view cursor");

    //
    // A view should reject all write CRUD operations performed in legacy write mode.
    //
    viewsDB.view.insert({x: 1});
    assert.gleErrorCode(viewsDB, ErrorCodes.CommandNotSupportedOnView);

    viewsDB.view.remove({x: 1});
    assert.gleErrorCode(viewsDB, ErrorCodes.CommandNotSupportedOnView);

    viewsDB.view.update({x: 1}, {x: 2});
    assert.gleErrorCode(viewsDB, ErrorCodes.CommandNotSupportedOnView);

    //
    // Legacy find is explicitly prohibited on views; you must use the find command.
    //
    let res = assert.throws(function() {
        viewsDB.view.find({x: 1}).toArray();
    });
    assert.eq(res.code, ErrorCodes.CommandNotSupportedOnView, tojson(res));

    // Ensure that legacy getMore succeeds even when a cursor is established on a namespace whose
    // database does not exist. Legacy getMore must check that the cursor is not over a view, and
    // this must handle the case where the namespace is not a view by virtue of the database not
    // existing.
    assert.commandWorked(viewsDB.dropDatabase());

    cmdRes = viewsDB.runCommand({find: "view", filter: {a: {$gt: 0}}, sort: {a: 1}, batchSize: 0});
    assert.commandWorked(cmdRes);
    cursor = new DBCommandCursor(viewsDB, cmdRes, 2);
    assert.eq(0, cursor.itcount());

    cmdRes = viewsDB.runCommand({aggregate: "view", pipeline: [], cursor: {batchSize: 0}});
    assert.commandWorked(cmdRes);
    cursor = new DBCommandCursor(viewsDB, cmdRes, 2);
    assert.eq(0, cursor.itcount());

    MongoRunner.stopMongod(conn);
}());