From 1f4d86b7c1feae349e6ac3c91d7aba7b528c82ad Mon Sep 17 00:00:00 2001 From: Joy Wang <49984055+joyjwang@users.noreply.github.com> Date: Wed, 11 Sep 2024 14:34:34 -0400 Subject: [PATCH] GODRIVER-2587 Implement modifyCollection for the unified test runner (#1796) (cherry picked from commit 4f21584a8b55e27f7067e2c5888fe9142baf115b) --- .../unified/database_operation_execution.go | 31 +++++ mongo/integration/unified/operation.go | 4 +- .../modifyCollection-errorResponse.json | 118 ++++++++++++++++++ .../modifyCollection-errorResponse.yml | 59 +++++++++ 4 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 testdata/collection-management/modifyCollection-errorResponse.json create mode 100644 testdata/collection-management/modifyCollection-errorResponse.yml diff --git a/mongo/integration/unified/database_operation_execution.go b/mongo/integration/unified/database_operation_execution.go index 675ab480b7..f42932bda0 100644 --- a/mongo/integration/unified/database_operation_execution.go +++ b/mongo/integration/unified/database_operation_execution.go @@ -224,6 +224,37 @@ func executeListCollectionNames(ctx context.Context, operation *operation) (*ope return newValueResult(bsontype.Array, data, nil), nil } +func executeModifyCollection(ctx context.Context, operation *operation) (*operationResult, error) { + db, err := entities(ctx).database(operation.Object) + if err != nil { + return nil, err + } + + collModCmd := bson.D{} + + elems, _ := operation.Arguments.Elements() + for _, elem := range elems { + key := elem.Key() + val := elem.Value() + + switch key { + case "collection": + collModCmd = append(collModCmd, bson.E{"collMod", val.StringValue()}) + case "changeStreamPreAndPostImages": + collModCmd = append(collModCmd, bson.E{"changeStreamPreAndPostImages", val.Document()}) + case "validator": + collModCmd = append(collModCmd, bson.E{"validator", val.Document()}) + case "index": + collModCmd = append(collModCmd, bson.E{"index", val.Document()}) + default: + return nil, fmt.Errorf("unrecognized modifyCollection option %q", key) + } + } + + res, err := db.RunCommand(ctx, collModCmd).Raw() + return newDocumentResult(res, err), nil +} + func executeRunCommand(ctx context.Context, operation *operation) (*operationResult, error) { db, err := entities(ctx).database(operation.Object) if err != nil { diff --git a/mongo/integration/unified/operation.go b/mongo/integration/unified/operation.go index 59aa36ae8c..8e0f108b16 100644 --- a/mongo/integration/unified/operation.go +++ b/mongo/integration/unified/operation.go @@ -136,6 +136,8 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat return executeListCollections(ctx, op) case "listCollectionNames": return executeListCollectionNames(ctx, op) + case "modifyCollection": + return executeModifyCollection(ctx, op) case "runCommand": return executeRunCommand(ctx, op) case "runCursorCommand": @@ -259,7 +261,7 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat return executeAddKeyAltName(ctx, op) // Unsupported operations - case "count", "listIndexNames", "modifyCollection": + case "count", "listIndexNames": return nil, newSkipTestError(fmt.Sprintf("the %q operation is not supported", op.Name)) default: return nil, fmt.Errorf("unrecognized entity operation %q", op.Name) diff --git a/testdata/collection-management/modifyCollection-errorResponse.json b/testdata/collection-management/modifyCollection-errorResponse.json new file mode 100644 index 0000000000..adbd3167d7 --- /dev/null +++ b/testdata/collection-management/modifyCollection-errorResponse.json @@ -0,0 +1,118 @@ +{ + "description": "modifyCollection-errorResponse", + "schemaVersion": "1.12", + "createEntities": [ + { + "client": { + "id": "client0", + "observeEvents": [ + "commandStartedEvent" + ] + } + }, + { + "database": { + "id": "database0", + "client": "client0", + "databaseName": "collMod-tests" + } + }, + { + "collection": { + "id": "collection0", + "database": "database0", + "collectionName": "test" + } + } + ], + "initialData": [ + { + "collectionName": "test", + "databaseName": "collMod-tests", + "documents": [ + { + "_id": 1, + "x": 1 + }, + { + "_id": 2, + "x": 1 + } + ] + } + ], + "tests": [ + { + "description": "modifyCollection prepareUnique violations are accessible", + "runOnRequirements": [ + { + "minServerVersion": "5.2" + } + ], + "operations": [ + { + "name": "createIndex", + "object": "collection0", + "arguments": { + "keys": { + "x": 1 + } + } + }, + { + "name": "modifyCollection", + "object": "database0", + "arguments": { + "collection": "test", + "index": { + "keyPattern": { + "x": 1 + }, + "prepareUnique": true + } + } + }, + { + "name": "insertOne", + "object": "collection0", + "arguments": { + "document": { + "_id": 3, + "x": 1 + } + }, + "expectError": { + "errorCode": 11000 + } + }, + { + "name": "modifyCollection", + "object": "database0", + "arguments": { + "collection": "test", + "index": { + "keyPattern": { + "x": 1 + }, + "unique": true + } + }, + "expectError": { + "isClientError": false, + "errorCode": 359, + "errorResponse": { + "violations": [ + { + "ids": [ + 1, + 2 + ] + } + ] + } + } + } + ] + } + ] + } \ No newline at end of file diff --git a/testdata/collection-management/modifyCollection-errorResponse.yml b/testdata/collection-management/modifyCollection-errorResponse.yml new file mode 100644 index 0000000000..edff573295 --- /dev/null +++ b/testdata/collection-management/modifyCollection-errorResponse.yml @@ -0,0 +1,59 @@ +description: "modifyCollection-errorResponse" + +schemaVersion: "1.12" + +createEntities: + - client: + id: &client0 client0 + observeEvents: [ commandStartedEvent ] + - database: + id: &database0 database0 + client: *client0 + databaseName: &database0Name collMod-tests + - collection: + id: &collection0 collection0 + database: *database0 + collectionName: &collection0Name test + +initialData: &initialData + - collectionName: *collection0Name + databaseName: *database0Name + documents: + - { _id: 1, x: 1 } + - { _id: 2, x: 1 } + +tests: + - description: "modifyCollection prepareUnique violations are accessible" + runOnRequirements: + - minServerVersion: "5.2" # SERVER-61158 + operations: + - name: createIndex + object: *collection0 + arguments: + keys: { x: 1 } + - name: modifyCollection + object: *database0 + arguments: + collection: *collection0Name + index: + keyPattern: { x: 1 } + prepareUnique: true + - name: insertOne + object: *collection0 + arguments: + document: { _id: 3, x: 1 } + expectError: + errorCode: 11000 # DuplicateKey + - name: modifyCollection + object: *database0 + arguments: + collection: *collection0Name + index: + keyPattern: { x: 1 } + unique: true + expectError: + isClientError: false + errorCode: 359 # CannotConvertIndexToUnique + errorResponse: + violations: + - { ids: [ 1, 2 ] } \ No newline at end of file