summaryrefslogtreecommitdiff
path: root/jstests/aggregation/api_version_stage_allowance_checks.js
blob: 50b40ce4a3a3095efe9c94ae08d9ba943f3659a8 (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
/**
 * Tests the aggregation stages marked with 'LiteParsedDocumentSource::AllowedWithApiStrict'
 * flags.
 *
 * @tags: [
 *   assumes_against_mongod_not_mongos,
 *   assumes_read_concern_unchanged,
 *   assumes_read_preference_unchanged,
 *   assumes_unsharded_collection,
 *   do_not_wrap_aggregations_in_facets,
 *   uses_api_parameters,
 * ]
 */
(function() {
"use strict";

const dbName = jsTestName();
const testDB = db.getSiblingDB(dbName);
testDB.dropDatabase();

const collName = "testColl";

const testInternalClient = (function createInternalClient() {
    const connInternal = new Mongo(testDB.getMongo().host);
    const curDB = connInternal.getDB(dbName);
    assert.commandWorked(curDB.runCommand({
        ["hello"]: 1,
        internalClient: {minWireVersion: NumberInt(0), maxWireVersion: NumberInt(7)}
    }));
    return connInternal;
})();

const curDB = testInternalClient.getDB(dbName);

// Tests that the internal stage '$mergeCursors' does not throw 'ApiStrictError' with an internal
// client and 'apiStrict' set to true.
let result = curDB.runCommand({
    aggregate: collName,
    pipeline: [{
        $mergeCursors: {
            sort: {y: 1, z: 1},
            compareWholeSortKey: false,
            remotes: [],
            nss: "test.mergeCursors",
            allowPartialResults: false,
        }
    }],
    cursor: {},
    writeConcern: {w: "majority"},
    apiVersion: "1",
    apiStrict: true
});
assert.commandWorked(result);

// Tests that the internal stage '$mergeCursors' throws 'ApiStrictError' with default external
// client when 'apiStrict' is set to true.
result = testDB.runCommand({
    aggregate: collName,
    pipeline: [{
        $mergeCursors: {
            sort: {y: 1, z: 1},
            compareWholeSortKey: false,
            remotes: [],
            nss: "test.mergeCursors",
            allowPartialResults: false,
        }
    }],
    cursor: {},
    writeConcern: {w: "majority"},
    apiVersion: "1",
    apiStrict: true
});
assert.commandFailedWithCode(result, ErrorCodes.APIStrictError);

// Tests that the internal stage '$mergeCursors' should not fail with 'ApiStrictError' with default
// external client without specifying 'apiStrict' flag.
result = testDB.runCommand({
    aggregate: collName,
    pipeline: [{
        $mergeCursors: {
            sort: {y: 1, z: 1},
            compareWholeSortKey: false,
            remotes: [],
            nss: "test.mergeCursors",
            allowPartialResults: false,
        }
    }],
    cursor: {},
    writeConcern: {w: "majority"},
    apiVersion: "1"
});
assert.commandWorked(result);

// Tests that the 'exchange' option cannot be specified by external client with 'apiStrict' set to
// true.
result = testDB.runCommand({
    aggregate: collName,
    pipeline: [{$project: {_id: 0}}],
    cursor: {},
    writeConcern: {w: "majority"},
    apiVersion: "1",
    apiStrict: true,
    exchange: {policy: "broadcast", consumers: NumberInt(10)}
});
assert.commandFailedWithCode(result, ErrorCodes.APIStrictError);

// Tests that the 'fromMongos' option cannot be specified by external client with 'apiStrict' set to
// true.
result = testDB.runCommand({
    aggregate: collName,
    pipeline: [{$project: {_id: 0}}],
    cursor: {},
    writeConcern: {w: "majority"},
    apiVersion: "1",
    apiStrict: true,
    fromMongos: true
});
assert.commandFailedWithCode(result, ErrorCodes.APIStrictError);

// Tests that the 'fromMongos' option should not fail by internal client with 'apiStrict' set to
// true.
result = curDB.runCommand({
    aggregate: collName,
    pipeline: [{$project: {_id: 0}}],
    cursor: {},
    writeConcern: {w: "majority"},
    apiVersion: "1",
    apiStrict: true
});
assert.commandWorked(result);

// Tests that the 'fromMongos' option should not fail by external client without 'apiStrict'.
result = testDB.runCommand({
    aggregate: collName,
    pipeline: [{$project: {_id: 0}}],
    cursor: {},
    writeConcern: {w: "majority"},
    apiVersion: "1",
    apiStrict: true
});
assert.commandWorked(result);

// Tests that time-series collection can be queried (invoking $_internalUnpackBucket stage)
// from an external client with 'apiStrict'.
(function testInternalUnpackBucketAllowance() {
    const collName = 'timeseriesColl';
    const timeField = 'tm';
    assert.commandWorked(testDB.createCollection(collName, {timeseries: {timeField: timeField}}));
    const coll = testDB[collName];
    assert.commandWorked(coll.insert({[timeField]: ISODate('2021-01-01')}));
    assert.commandWorked(testDB.runCommand({
        find: collName,
        apiVersion: "1",
        apiStrict: true,
    }));
    assert.commandWorked(testDB.runCommand({
        aggregate: collName,
        pipeline: [{$match: {}}],
        cursor: {},
        apiVersion: "1",
        apiStrict: true,
    }));
    const plans = [coll.find().explain(), coll.explain().aggregate([{$match: {}}])];
    assert(plans.every(
        plan => plan.stages.map(x => Object.keys(x)[0]).includes("$_internalUnpackBucket")));
})();
})();