summaryrefslogtreecommitdiff
path: root/jstests/libs/override_methods/inject_tenant_prefix.js
blob: 658c8affb046436e6d9f422f62074c5fdad75736 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
 * Overrides the database name of each accessed database ("config", "admin", "local" excluded) to
 * have the prefix TestData.dbPrefix so that the accessed data will be migrated by the background
 * tenant migrations run by the ContinuousTenantMigration hook.
 */
(function() {
'use strict';

load("jstests/libs/override_methods/override_helpers.js");  // For 'OverrideHelpers'.
load("jstests/libs/transactions_util.js");

// Save references to the original methods in the IIFE's scope.
// This scoping allows the original methods to be called by the overrides below.
// Override this method to make the accessed database have the prefix TestData.dbPrefix.
let originalRunCommand = Mongo.prototype.runCommand;

const blacklistedDbNames = ["config", "admin", "local"];

function isBlacklistedDb(dbName) {
    return blacklistedDbNames.includes(dbName);
}

/**
 * If the database with the given name can be migrated, prepends TestData.dbPrefix to the name if
 * it does not already start with the prefix.
 */
function prependDbPrefixToDbNameIfApplicable(dbName) {
    if (dbName.length === 0) {
        // There are input validation tests that use invalid database names, those should be
        // ignored.
        return dbName;
    }
    return isBlacklistedDb(dbName) ? dbName : TestData.dbPrefix + "_" + dbName;
}

/**
 * If the database for the given namespace can be migrated, prepends TestData.dbPrefix to the
 * namespace if it does not already start with the prefix.
 */
function prependDbPrefixToNsIfApplicable(ns) {
    if (ns.length === 0 || !ns.includes(".")) {
        // There are input validation tests that use invalid namespaces, those should be ignored.
        return ns;
    }
    let splitNs = ns.split(".");
    splitNs[0] = prependDbPrefixToDbNameIfApplicable(splitNs[0]);
    return splitNs.join(".");
}

/**
 * If the given database name starts TestData.dbPrefix, removes the prefix.
 */
function extractOriginalDbName(dbName) {
    return dbName.replace(TestData.dbPrefix + "_", "");
}

/**
 * If the database name for the given namespace starts TestData.dbPrefix, removes the prefix.
 */
function extractOriginalNs(ns) {
    let splitNs = ns.split(".");
    splitNs[0] = extractOriginalDbName(splitNs[0]);
    return splitNs.join(".");
}

/**
 * Removes all occurrences of TestDatabase.dbPrefix in the string.
 */
function removeDbPrefixFromString(string) {
    return string.replace(new RegExp(TestData.dbPrefix + "_", "g"), "");
}

/**
 * Prepends TestDatabase.dbPrefix to all the database name and namespace fields inside the given
 * object.
 */
function prependDbPrefix(obj) {
    for (let k of Object.keys(obj)) {
        let v = obj[k];
        if (typeof v === "string") {
            if (k === "dbName" || k == "db") {
                obj[k] = prependDbPrefixToDbNameIfApplicable(v);
            } else if (k === "namespace" || k === "ns") {
                obj[k] = prependDbPrefixToNsIfApplicable(v);
            }
        } else if (Array.isArray(v)) {
            obj[k] = v.map((item) => {
                return (typeof item === "object" && item !== null) ? prependDbPrefix(item) : item;
            });
        } else if (typeof v === "object" && v !== null && Object.keys(v).length > 0) {
            obj[k] = prependDbPrefix(v);
        }
    }
    return obj;
}

/**
 * Removes TestDatabase.dbPrefix from all the database name and namespace fields inside the given
 * object.
 */
function removeDbPrefix(obj) {
    for (let k of Object.keys(obj)) {
        let v = obj[k];
        let originalK = removeDbPrefixFromString(k);
        if (typeof v === "string") {
            if (k === "dbName" || k == "db" || k == "dropped") {
                obj[originalK] = extractOriginalDbName(v);
            } else if (k === "namespace" || k === "ns") {
                obj[originalK] = extractOriginalNs(v);
            } else if (k === "errmsg" || k == "name") {
                obj[originalK] = removeDbPrefixFromString(v);
            }
        } else if (Array.isArray(v)) {
            obj[originalK] = v.map((item) => {
                return (typeof item === "object" && item !== null) ? removeDbPrefix(item) : item;
            });
        } else if (typeof v === "object" && v !== null && Object.keys(v).length > 0) {
            obj[originalK] = removeDbPrefix(v);
        }
    }
    return obj;
}

const kCmdsWithNsAsFirstField =
    new Set(["renameCollection", "checkShardingIndex", "dataSize", "datasize", "splitVector"]);

/**
 * Returns a new cmdObj with TestData.dbPrefix prepended to all database name and namespace fields.
 */
function createCmdObjWithDbPrefix(cmdObj) {
    const cmdName = Object.keys(cmdObj)[0];
    let cmdObjWithDbPrefix = TransactionsUtil.deepCopyObject({}, cmdObj);

    // Handle commands with special database and namespace field names.
    if (kCmdsWithNsAsFirstField.has(cmdName)) {
        cmdObjWithDbPrefix[cmdName] = prependDbPrefixToNsIfApplicable(cmdObjWithDbPrefix[cmdName]);
    }

    switch (cmdName) {
        case "renameCollection":
            cmdObjWithDbPrefix.to = prependDbPrefixToNsIfApplicable(cmdObjWithDbPrefix.to);
            break;
        case "internalRenameIfOptionsAndIndexesMatch":
            cmdObjWithDbPrefix.from = prependDbPrefixToNsIfApplicable(cmdObjWithDbPrefix.from);
            cmdObjWithDbPrefix.to = prependDbPrefixToNsIfApplicable(cmdObjWithDbPrefix.to);
            break;
        case "configureFailPoint":
            if (cmdObjWithDbPrefix.data) {
                if (cmdObjWithDbPrefix.data.namespace) {
                    cmdObjWithDbPrefix.data.namespace =
                        prependDbPrefixToNsIfApplicable(cmdObjWithDbPrefix.data.namespace);
                } else if (cmdObjWithDbPrefix.data.ns) {
                    cmdObjWithDbPrefix.data.ns =
                        prependDbPrefixToNsIfApplicable(cmdObjWithDbPrefix.data.ns);
                }
            }
            break;
        case "applyOps":
            for (let op of cmdObjWithDbPrefix.applyOps) {
                if (typeof op.ns === "string" && op.ns.endsWith("system.views") && op.o._id &&
                    typeof op.o._id === "string") {
                    // For views, op.ns and op.o._id must be equal.
                    op.o._id = prependDbPrefixToNsIfApplicable(op.o._id);
                }
            }
            break;
        default:
            break;
    }

    // Recursively override the database name and namespace fields. Exclude 'configureFailPoint'
    // since data.errorExtraInfo.namespace or data.errorExtraInfo.ns can sometimes refer to
    // collection name instead of namespace.
    if (cmdName != "configureFailPoint") {
        prependDbPrefix(cmdObjWithDbPrefix);
    }

    return cmdObjWithDbPrefix;
}

Mongo.prototype.runCommand = function(dbName, cmdObj, options) {
    // Create another cmdObj from this command with TestData.dbPrefix prepended to all the
    // applicable database names and namespaces.
    const cmdObjWithDbPrefix = createCmdObjWithDbPrefix(cmdObj);

    let numAttempts = 0;

    while (true) {
        numAttempts++;
        let resObj = originalRunCommand.apply(
            this, [prependDbPrefixToDbNameIfApplicable(dbName), cmdObjWithDbPrefix, options]);

        // Remove TestData.dbPrefix from all database names and namespaces in the resObj since tests
        // assume the command was run against the original database.
        removeDbPrefix(resObj);

        if (resObj.code != ErrorCodes.TenantMigrationAborted) {
            return resObj;
        }
        jsTest.log("Got TenantMigrationAborted after trying " + numAttempts +
                   " times, retrying command " + tojson(cmdObj));
    }
};

Mongo.prototype.runCommandWithMetadata = function(dbName, metadata, commandArgs) {
    // Create another cmdObj from this command with TestData.dbPrefix prepended to all the
    // applicable database names and namespaces.
    const commandArgsWithDbPrefix = createCmdObjWithDbPrefix(commandArgs);

    let numAttempts = 0;

    while (true) {
        numAttempts++;
        let resObj = originalRunCommand.apply(
            this, [prependDbPrefixToDbNameIfApplicable(dbName), metadata, commandArgsWithDbPrefix]);

        // Remove TestData.dbPrefix from all database names and namespaces in the resObj since tests
        // assume the command was run against the original database.
        removeDbPrefix(resObj);

        if (resObj.code != ErrorCodes.TenantMigrationAborted) {
            return resObj;
        }
        jsTest.log("Got TenantMigrationAborted after trying " + numAttempts +
                   " times, retrying command " + tojson(commandArgs));
    }
};

OverrideHelpers.prependOverrideInParallelShell(
    "jstests/libs/override_methods/inject_tenant_prefix.js");
}());