diff options
author | Yuhong Zhang <yuhong.zhang@mongodb.com> | 2022-11-03 18:29:39 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-11-08 19:29:02 +0000 |
commit | d63bf9dfa20c1bafaa87a320cc5f4d4c2c401742 (patch) | |
tree | fefbb6c137167c8a3b9e6a27911bcb0218f48977 /jstests | |
parent | fead3bc9fea5b363e4e351e2ef675c9e1190e057 (diff) | |
download | mongo-d63bf9dfa20c1bafaa87a320cc5f4d4c2c401742.tar.gz |
SERVER-65078 Report schema violations as errors when the validation level is 'error' in testing
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/core/bypass_doc_validation.js | 6 | ||||
-rw-r--r-- | jstests/core/json_schema/encrypt.js | 23 | ||||
-rw-r--r-- | jstests/core/views/views_aggregation.js | 4 | ||||
-rw-r--r-- | jstests/libs/assert_schema_match.js | 10 | ||||
-rw-r--r-- | jstests/noPassthrough/invalid_collection_validator_at_startup.js | 4 | ||||
-rw-r--r-- | jstests/noPassthrough/operator_counters_expressions.js | 3 | ||||
-rw-r--r-- | jstests/noPassthrough/validate_doc_schema.js | 5 | ||||
-rw-r--r-- | jstests/replsets/initial_sync_document_validation.js | 5 | ||||
-rw-r--r-- | jstests/replsets/rollback_collmods.js | 6 | ||||
-rw-r--r-- | jstests/sharding/chunk_migration_with_schema_validation.js | 3 |
10 files changed, 48 insertions, 21 deletions
diff --git a/jstests/core/bypass_doc_validation.js b/jstests/core/bypass_doc_validation.js index 4af35a52d2e..d499e1560fa 100644 --- a/jstests/core/bypass_doc_validation.js +++ b/jstests/core/bypass_doc_validation.js @@ -32,6 +32,7 @@ load("jstests/libs/doc_validation_utils.js"); const dbName = 'bypass_document_validation'; const collName = 'bypass_document_validation'; +const outputCollName = 'bypass_output_coll'; const myDb = db.getSiblingDB(dbName); const coll = myDb[collName]; @@ -61,7 +62,6 @@ function runBypassDocumentValidationTest(validator) { } // Test the aggregation command with a $out stage. - const outputCollName = 'bypass_output_coll'; const outputColl = myDb[outputCollName]; outputColl.drop(); assert.commandWorked(myDb.createCollection(outputCollName, {validator: validator})); @@ -196,4 +196,8 @@ runBypassDocumentValidationTest({a: {$exists: true}}); // Run the test again with an equivalent JSON Schema validator. runBypassDocumentValidationTest({$jsonSchema: {required: ['a']}}); + +// Set the validationAction to "warn" to avoid failing collection validation. +assert.commandWorked(myDb.runCommand({collMod: collName, validationAction: "warn"})); +assert.commandWorked(myDb.runCommand({collMod: outputCollName, validationAction: "warn"})); })(); diff --git a/jstests/core/json_schema/encrypt.js b/jstests/core/json_schema/encrypt.js index 32d93f43da4..2df773878af 100644 --- a/jstests/core/json_schema/encrypt.js +++ b/jstests/core/json_schema/encrypt.js @@ -15,41 +15,48 @@ const encryptedBinDataElement = BinData(6, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA"); const nonEncryptedBinDataElement = BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA"); // Only elements of type BinData with subtype '6' should match. -assertSchemaMatch(coll, {properties: {bin: {encrypt: {}}}}, {bin: encryptedBinDataElement}, true); -assertSchemaMatch(coll, {properties: {bin: {encrypt: {}}}}, {bin: {}}, false); assertSchemaMatch( - coll, {properties: {bin: {encrypt: {}}}}, {bin: nonEncryptedBinDataElement}, false); + coll, {properties: {bin: {encrypt: {}}}}, {bin: encryptedBinDataElement}, true, true); +assertSchemaMatch(coll, {properties: {bin: {encrypt: {}}}}, {bin: {}}, false, true); +assertSchemaMatch( + coll, {properties: {bin: {encrypt: {}}}}, {bin: nonEncryptedBinDataElement}, false, true); // Nested in object. assertSchemaMatch(coll, {properties: {obj: {type: 'object', properties: {a: {encrypt: {}}}}}}, {obj: {a: encryptedBinDataElement}}, + true, true); assertSchemaMatch(coll, {properties: {obj: {type: 'object', properties: {a: {encrypt: {}}}}}}, {obj: {a: {}}}, - false); + false, + true); assertSchemaMatch(coll, {properties: {obj: {type: 'object', properties: {a: {encrypt: {}}}}}}, {obj: {a: nonEncryptedBinDataElement}}, - false); + false, + true); // Nested in array. assertSchemaMatch(coll, {properties: {arr: {type: 'array', items: {encrypt: {}}}}}, {arr: [encryptedBinDataElement, encryptedBinDataElement]}, + true, true); assertSchemaMatch( - coll, {properties: {arr: {type: 'array', items: {encrypt: {}}}}}, {arr: [{}, {}]}, false); + coll, {properties: {arr: {type: 'array', items: {encrypt: {}}}}}, {arr: [{}, {}]}, false, true); assertSchemaMatch(coll, {properties: {arr: {type: 'array', items: {encrypt: {}}}}}, {arr: [encryptedBinDataElement, nonEncryptedBinDataElement]}, - false); + false, + true); // If array is not specified, should not traverse array of encrypted BinData's. assertSchemaMatch(coll, {properties: {bin: {encrypt: {}}}}, {bin: [encryptedBinDataElement, encryptedBinDataElement]}, - false); + false, + true); // Encrypt alongside type/bsontype should fail to parse. assert.commandFailedWithCode( diff --git a/jstests/core/views/views_aggregation.js b/jstests/core/views/views_aggregation.js index 50b2edfd4a7..f8e99fe1ffd 100644 --- a/jstests/core/views/views_aggregation.js +++ b/jstests/core/views/views_aggregation.js @@ -151,6 +151,10 @@ assert.commandWorked(viewsDB.runCommand({ assert.commandWorked( viewsDB.runCommand({aggregate: "largeView", pipeline: [{$sort: {x: -1}}], cursor: {}}), "Expected aggregate to succeed since 'allowDiskUse' is true by default"); + + // Set the validationAction to "warn" to avoid failing collection validation. + assert.commandWorked( + viewsDB.runCommand({collMod: validatedCollName, validationAction: "warn"})); })(); // Test explain modes on a view. diff --git a/jstests/libs/assert_schema_match.js b/jstests/libs/assert_schema_match.js index f59e7a1c2c4..8523d4f09bc 100644 --- a/jstests/libs/assert_schema_match.js +++ b/jstests/libs/assert_schema_match.js @@ -10,7 +10,7 @@ * Asserts that 'doc' matches 'schema' if and only if 'valid' is true. Drops 'coll' in the process, * so do not pass a collection whose contents you wish to preserve. */ -function assertSchemaMatch(coll, schema, doc, valid) { +function assertSchemaMatch(coll, schema, doc, valid, removeValidator = false) { const errmsg = "Document " + tojson(doc) + (valid ? " should have matched the schema " : " unexpectedly matched the schema ") + tojson(schema); @@ -60,4 +60,12 @@ function assertSchemaMatch(coll, schema, doc, valid) { ErrorCodes.DocumentValidationFailure, errmsg + " during update document validation in strict mode"); } + + if (removeValidator) { + // Remove the validator to avoid failing collection validation. + assert.commandWorked(coll.runCommand("collMod", {validator: {}})); + } else { + // Set the validationAction to "warn" to avoid failing collection validation. + assert.commandWorked(coll.runCommand("collMod", {validationAction: "warn"})); + } } diff --git a/jstests/noPassthrough/invalid_collection_validator_at_startup.js b/jstests/noPassthrough/invalid_collection_validator_at_startup.js index 2d453345a00..a4abcceac62 100644 --- a/jstests/noPassthrough/invalid_collection_validator_at_startup.js +++ b/jstests/noPassthrough/invalid_collection_validator_at_startup.js @@ -29,7 +29,7 @@ const collName = "collectionWithMalformedValidator"; assert.commandWorked( testDB.runCommand({collMod: collName, validator: {email: {$regex: invalidRegex}}})); - MongoRunner.stopMongod(conn); + MongoRunner.stopMongod(conn, null, {skipValidation: true}); })(); (function startUpWithMalformedValidator() { @@ -48,6 +48,6 @@ const collName = "collectionWithMalformedValidator"; assert.commandWorked(testDB.someOtherCollection.insert({a: 1})); assert.eq(testDB.someOtherCollection.find().itcount(), 1); - MongoRunner.stopMongod(conn); + MongoRunner.stopMongod(conn, null, {skipValidation: true}); })(); })(); diff --git a/jstests/noPassthrough/operator_counters_expressions.js b/jstests/noPassthrough/operator_counters_expressions.js index 87082bac534..df569f18946 100644 --- a/jstests/noPassthrough/operator_counters_expressions.js +++ b/jstests/noPassthrough/operator_counters_expressions.js @@ -253,5 +253,8 @@ bulkOp.find({$expr: {$eq: ["$x", 2]}}).update({$set: {x: 10}}); bulkOp.find({$expr: {$lt: ["$x", 1]}}).remove(); checkCounters(() => assert.commandWorked(bulkOp.execute()), {"$eq": 1, "$lt": 1}); +// Set the validationAction to "warn" to avoid failing collection validation. +assert.commandWorked(testColl.runCommand("collMod", {validationAction: "warn"})); + MongoRunner.stopMongod(mongod); })(); diff --git a/jstests/noPassthrough/validate_doc_schema.js b/jstests/noPassthrough/validate_doc_schema.js index 8834ed5073f..0af638a0722 100644 --- a/jstests/noPassthrough/validate_doc_schema.js +++ b/jstests/noPassthrough/validate_doc_schema.js @@ -4,11 +4,6 @@ (function() { "use strict"; -// TODO SERVER-65078: remove this block. -// Disable the testing proctor. When the testing proctor is enabled, 'validate' will only warn about -// non-compliant documents, even when the validation action is 'error'. -TestData.testingDiagnosticsEnabled = false; - const conn = MongoRunner.runMongod(); const dbName = "test"; diff --git a/jstests/replsets/initial_sync_document_validation.js b/jstests/replsets/initial_sync_document_validation.js index fad4601e797..28ac2d1dd8e 100644 --- a/jstests/replsets/initial_sync_document_validation.js +++ b/jstests/replsets/initial_sync_document_validation.js @@ -18,12 +18,15 @@ var coll = primary.getDB('test').getCollection(name); assert.commandWorked(coll.insert({_id: 0, x: 1})); assert.commandWorked(coll.runCommand("collMod", {"validator": {a: {$exists: true}}})); -secondary = replSet.restart(secondary, {startClean: true}); +secondary = replSet.restart(secondary, {startClean: true, skipValidation: true}); replSet.awaitReplication(); replSet.awaitSecondaryNodes(); assert.eq(1, secondary.getDB("test")[name].count()); assert.docEq({_id: 0, x: 1}, secondary.getDB("test")[name].findOne()); +// Set the validationAction to "warn" to avoid failing collection validation. +assert.commandWorked(coll.runCommand("collMod", {validationAction: "warn"})); + replSet.stopSet(); })(); diff --git a/jstests/replsets/rollback_collmods.js b/jstests/replsets/rollback_collmods.js index 75c9af51d96..79e62107fc7 100644 --- a/jstests/replsets/rollback_collmods.js +++ b/jstests/replsets/rollback_collmods.js @@ -32,9 +32,9 @@ function printCollectionOptions(rollbackTest, time) { let CommonOps = (node) => { let testDb = node.getDB(dbName); assert.commandWorked(testDb[coll1Name].insert({a: 1, b: 1})); - assert.commandWorked(testDb[coll2Name].insert({a: 2, b: 2})); - assert.commandWorked(testDb[coll3Name].insert({a: 3, b: 3})); - assert.commandWorked(testDb[coll4Name].insert({a: 4, b: 4})); + assert.commandWorked(testDb[coll2Name].insert({a: 1, b: 1})); + assert.commandWorked(testDb[coll3Name].insert({a: 1, b: 1})); + assert.commandWorked(testDb[coll4Name].insert({a: 1, b: 1})); // Start with no validation action. assert.commandWorked(testDb.runCommand({ diff --git a/jstests/sharding/chunk_migration_with_schema_validation.js b/jstests/sharding/chunk_migration_with_schema_validation.js index 28b8427d45a..4ebff7a0a8c 100644 --- a/jstests/sharding/chunk_migration_with_schema_validation.js +++ b/jstests/sharding/chunk_migration_with_schema_validation.js @@ -63,5 +63,8 @@ assert.eq(125, "Number of documents on the recipient shard after moveChunk is incorrect."); assert.eq(175, testColl.find().itcount(), "Number of total documents is incorrect"); +// Set the validationAction to "warn" to avoid failing collection validation. +assert.commandWorked(testColl.runCommand("collMod", {validationAction: "warn"})); + st.stop(); })(); |