|
| 1 | +/** |
| 2 | + * Tests the FCV for long collection names. |
| 3 | + * |
| 4 | + * On 4.2 and below, the maximum fully qualified collection name is 120 characters or less |
| 5 | + * (which includes the database name). |
| 6 | + * |
| 7 | + * In this multi version test, we ensure that we can create long collection names on the 4.4 binary |
| 8 | + * while the FCV document is set to 4.4. Restarting with long collection names present on a 4.2 |
| 9 | + * binary should not crash the server. Users would need to manually remove or rename the long |
| 10 | + * collection names prior to downgrading. Additionally, we should be prevented from creating long |
| 11 | + * collection names when using FCV 4.2 on a 4.4 binary. |
| 12 | + */ |
| 13 | +(function() { |
| 14 | +'use strict'; |
| 15 | +load('jstests/libs/feature_compatibility_version.js'); |
| 16 | + |
| 17 | +const dbName = 'test'; |
| 18 | +const renameDbName = 'rename_test'; |
| 19 | +const shortCollName = 'short_collection'; |
| 20 | +const longCollName = 'long_collection' + |
| 21 | + 'a'.repeat(8192); |
| 22 | +const longCollNameRename = 'long_collection' + |
| 23 | + 'b'.repeat(8192); |
| 24 | + |
| 25 | +const dbpath = MongoRunner.dataPath + 'long_collection_names'; |
| 26 | +resetDbpath(dbpath); |
| 27 | + |
| 28 | +const mongodOptions42 = |
| 29 | + Object.extend({binVersion: 'last-stable'}, {dbpath: dbpath, cleanData: false}); |
| 30 | +const mongodOptions44 = Object.extend({binVersion: 'latest'}, {dbpath: dbpath, cleanData: false}); |
| 31 | + |
| 32 | +/** |
| 33 | + * Start up with the latest binary and ensure that long collection names can be created while |
| 34 | + * using FCV 4.4. |
| 35 | + */ |
| 36 | +let conn = MongoRunner.runMongod(mongodOptions44); |
| 37 | +assert.neq(null, conn, 'mongod was unable to start with version ' + tojson(mongodOptions44)); |
| 38 | + |
| 39 | +let testDb = conn.getDB(dbName); |
| 40 | + |
| 41 | +assert.commandWorked(testDb.adminCommand({setFeatureCompatibilityVersion: latestFCV})); |
| 42 | + |
| 43 | +// Create two collections, one with a short name and the other with a long name. |
| 44 | +assert.commandWorked(testDb.createCollection(shortCollName)); |
| 45 | +assert.commandWorked(testDb.createCollection(longCollName)); |
| 46 | + |
| 47 | +// Rename a short collection name to a long collection name within the same database. |
| 48 | +assert.commandWorked(testDb.adminCommand( |
| 49 | + {renameCollection: dbName + '.' + shortCollName, to: dbName + '.' + longCollNameRename})); |
| 50 | + |
| 51 | +assert.eq(true, testDb.getCollection(longCollNameRename).drop()); |
| 52 | +assert.commandWorked(testDb.createCollection(shortCollName)); |
| 53 | + |
| 54 | +// Rename a short collection name to a long collection name in a different database. |
| 55 | +assert.commandWorked(testDb.adminCommand( |
| 56 | + {renameCollection: dbName + '.' + shortCollName, to: renameDbName + '.' + longCollNameRename})); |
| 57 | + |
| 58 | +assert.eq(true, testDb.getSiblingDB(renameDbName).getCollection(longCollNameRename).drop()); |
| 59 | +assert.commandWorked(testDb.createCollection(shortCollName)); |
| 60 | + |
| 61 | +MongoRunner.stopMongod(conn); |
| 62 | + |
| 63 | +/** |
| 64 | + * Restarting with a 4.2 binary with FCV 4.4 shouldn't startup nor crash. |
| 65 | + */ |
| 66 | +let restartOpts42 = Object.extend(mongodOptions42, {restart: true}); |
| 67 | +conn = MongoRunner.runMongod(restartOpts42); |
| 68 | +assert.eq(null, conn, 'mongod was able to start with version ' + tojson(restartOpts42)); |
| 69 | + |
| 70 | +/** |
| 71 | + * Restart with the 4.4 binary to set the FCV to 4.2. |
| 72 | + */ |
| 73 | +let restartOpts44 = Object.extend(mongodOptions44, {restart: true}); |
| 74 | +conn = MongoRunner.runMongod(restartOpts44); |
| 75 | +assert.neq(null, conn, 'mongod was unable to start with version ' + tojson(restartOpts44)); |
| 76 | + |
| 77 | +testDb = conn.getDB(dbName); |
| 78 | +assert.commandWorked(testDb.adminCommand({setFeatureCompatibilityVersion: lastStableFCV})); |
| 79 | +MongoRunner.stopMongod(conn); |
| 80 | + |
| 81 | +/** |
| 82 | + * Restart with the 4.2 binary while in FCV 4.2 with long collection names present. This shouldn't |
| 83 | + * crash the server. |
| 84 | + */ |
| 85 | +conn = MongoRunner.runMongod(restartOpts42); |
| 86 | +assert.neq(null, conn, 'mongod was unable to start with version ' + tojson(restartOpts42)); |
| 87 | + |
| 88 | +testDb = conn.getDB(dbName); |
| 89 | + |
| 90 | +// Ensure we have the proper collections. |
| 91 | +let collNames = testDb.getCollectionNames(); |
| 92 | + |
| 93 | +assert.eq(true, collNames.includes(shortCollName)); |
| 94 | +assert.eq(true, collNames.includes(longCollName)); |
| 95 | + |
| 96 | +MongoRunner.stopMongod(conn); |
| 97 | + |
| 98 | +/** |
| 99 | + * Restart with the 4.4 binary while in FCV 4.2. We shouldn't be able to create any collections with |
| 100 | + * long names. |
| 101 | + */ |
| 102 | +conn = MongoRunner.runMongod(restartOpts44); |
| 103 | +assert.neq(null, conn, 'mongod was unable to start with version ' + tojson(restartOpts44)); |
| 104 | + |
| 105 | +testDb = conn.getDB(dbName); |
| 106 | + |
| 107 | +// Creating a long collection name on a 4.4 binary with FCV 4.2 should fail. |
| 108 | +assert.commandFailedWithCode(testDb.createCollection('c'.repeat(8192)), |
| 109 | + ErrorCodes.IncompatibleServerVersion); |
| 110 | + |
| 111 | +// Running rename within the same database or across two databases should fail for long collection |
| 112 | +// names. |
| 113 | +assert.commandFailedWithCode( |
| 114 | + testDb.adminCommand( |
| 115 | + {renameCollection: dbName + '.' + shortCollName, to: dbName + '.' + longCollNameRename}), |
| 116 | + ErrorCodes.IllegalOperation); |
| 117 | +assert.commandFailedWithCode(testDb.adminCommand({ |
| 118 | + renameCollection: dbName + '.' + shortCollName, |
| 119 | + to: renameDbName + '.' + longCollNameRename |
| 120 | +}), |
| 121 | + ErrorCodes.IllegalOperation); |
| 122 | + |
| 123 | +MongoRunner.stopMongod(conn); |
| 124 | +})(); |
0 commit comments