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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package integration
import (
"bytes"
"context"
"errors"
"testing"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
func TestWriteErrorsWithLabels(t *testing.T) {
clientOpts := options.Client().SetRetryWrites(false).SetWriteConcern(mtest.MajorityWc).
SetReadConcern(mtest.MajorityRc)
mtOpts := mtest.NewOptions().ClientOptions(clientOpts).MinServerVersion("4.0").Topologies(mtest.ReplicaSet).
CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()
label := "ExampleError"
mt.Run("InsertMany errors with label", func(mt *mtest.T) {
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"insert"},
WriteConcernError: &mtest.WriteConcernErrorData{
Code: 100,
ErrorLabels: &[]string{label},
},
},
})
_, err := mt.Coll.InsertMany(mtest.Background,
[]interface{}{
bson.D{
{"a", 1},
},
bson.D{
{"a", 2},
},
})
assert.NotNil(mt, err, "expected non-nil error, got nil")
we, ok := err.(mongo.BulkWriteException)
assert.True(mt, ok, "expected mongo.BulkWriteException, got %T", err)
assert.True(mt, we.HasErrorLabel(label), "expected error to have label: %v", label)
})
mt.Run("WriteException with label", func(mt *mtest.T) {
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"delete"},
WriteConcernError: &mtest.WriteConcernErrorData{
Code: 100,
ErrorLabels: &[]string{label},
},
},
})
_, err := mt.Coll.DeleteMany(mtest.Background, bson.D{{"a", 1}})
assert.NotNil(mt, err, "expected non-nil error, got nil")
we, ok := err.(mongo.WriteException)
assert.True(mt, ok, "expected mongo.WriteException, got %T", err)
assert.True(mt, we.HasErrorLabel(label), "expected error to have label: %v", label)
})
mt.Run("BulkWriteException with label", func(mt *mtest.T) {
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"delete"},
WriteConcernError: &mtest.WriteConcernErrorData{
Code: 100,
ErrorLabels: &[]string{label},
},
},
})
models := []mongo.WriteModel{
&mongo.InsertOneModel{bson.D{{"a", 2}}},
&mongo.DeleteOneModel{bson.D{{"a", 2}}, nil, nil},
}
_, err := mt.Coll.BulkWrite(mtest.Background, models)
assert.NotNil(mt, err, "expected non-nil error, got nil")
we, ok := err.(mongo.BulkWriteException)
assert.True(mt, ok, "expected mongo.BulkWriteException, got %T", err)
assert.True(mt, we.HasErrorLabel(label), "expected error to have label: %v", label)
})
}
func TestWriteErrorsDetails(t *testing.T) {
clientOpts := options.Client().
SetRetryWrites(false).
SetWriteConcern(mtest.MajorityWc).
SetReadConcern(mtest.MajorityRc)
mtOpts := mtest.NewOptions().
ClientOptions(clientOpts).
MinServerVersion("5.0").
Topologies(mtest.ReplicaSet, mtest.Single).
CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()
mt.Run("JSON Schema validation", func(mt *mtest.T) {
// Create a JSON Schema validator document that requires properties "a" and "b". Use it in
// the collection creation options so that collections created for subtests have the JSON
// Schema validator applied.
validator := bson.D{{
Key: "validator",
Value: bson.M{
"$jsonSchema": bson.M{
"bsonType": "object",
"required": []string{"a", "b"},
"properties": bson.M{
"a": bson.M{"bsonType": "string"},
"b": bson.M{"bsonType": "int"},
},
},
},
}}
validatorOpts := mtest.NewOptions().CollectionCreateOptions(validator)
cases := []struct {
desc string
operation func(*mongo.Collection) error
expectBulkError bool
expectedCommandName string
}{
{
desc: "InsertOne schema validation errors should include Details",
operation: func(coll *mongo.Collection) error {
// Try to insert a document that doesn't contain the required properties.
_, err := coll.InsertOne(context.Background(), bson.D{{"nope", 1}})
return err
},
expectBulkError: false,
expectedCommandName: "insert",
},
{
desc: "InsertMany schema validation errors should include Details",
operation: func(coll *mongo.Collection) error {
// Try to insert a document that doesn't contain the required properties.
_, err := coll.InsertMany(context.Background(), []interface{}{bson.D{{"nope", 1}}})
return err
},
expectBulkError: true,
expectedCommandName: "insert",
},
{
desc: "UpdateOne schema validation errors should include Details",
operation: func(coll *mongo.Collection) error {
// Try to set "a" to be an int, which violates the string type requirement.
_, err := coll.UpdateOne(
context.Background(),
bson.D{},
bson.D{{"$set", bson.D{{"a", 1}}}})
return err
},
expectBulkError: false,
expectedCommandName: "update",
},
{
desc: "UpdateMany schema validation errors should include Details",
operation: func(coll *mongo.Collection) error {
// Try to set "a" to be an int in all documents in the collection, which violates
// the string type requirement.
_, err := coll.UpdateMany(
context.Background(),
bson.D{},
bson.D{{"$set", bson.D{{"a", 1}}}})
return err
},
expectBulkError: false,
expectedCommandName: "update",
},
}
for _, tc := range cases {
mt.RunOpts(tc.desc, validatorOpts, func(mt *mtest.T) {
// Insert two valid documents so that the Update* tests can try to update them.
{
_, err := mt.Coll.InsertMany(
context.Background(),
[]interface{}{
bson.D{{"a", "str1"}, {"b", 1}},
bson.D{{"a", "str2"}, {"b", 2}},
})
assert.Nil(mt, err, "unexpected error inserting valid documents: %s", err)
}
err := tc.operation(mt.Coll)
assert.NotNil(mt, err, "expected an error from calling the operation")
sErr := err.(mongo.ServerError)
assert.True(
mt,
sErr.HasErrorCode(121),
"expected mongo.ServerError to have error code 121 (DocumentValidationFailure)")
var details bson.Raw
if tc.expectBulkError {
bwe, ok := err.(mongo.BulkWriteException)
assert.True(
mt,
ok,
"expected error to be type mongo.BulkWriteException, got type %T (error %q)",
err,
err)
// Assert that there is one WriteError and that the Details field is populated.
assert.Equal(
mt,
1,
len(bwe.WriteErrors),
"expected exactly 1 write error, but got %d write errors (error %q)",
len(bwe.WriteErrors),
err)
details = bwe.WriteErrors[0].Details
} else {
we, ok := err.(mongo.WriteException)
assert.True(
mt,
ok,
"expected error to be type mongo.WriteException, got type %T (error %q)",
err,
err)
// Assert that there is one WriteError and that the Details field is populated.
assert.Equal(
mt,
1,
len(we.WriteErrors),
"expected exactly 1 write error, but got %d write errors (error %q)",
len(we.WriteErrors),
err)
details = we.WriteErrors[0].Details
}
assert.True(
mt,
len(details) > 0,
"expected WriteError.Details to be populated, but is empty")
// Assert that the most recent CommandSucceededEvent was triggered by the expected
// operation and contains the resulting write errors and that
// "writeErrors[0].errInfo" is the same as "WriteException.WriteErrors[0].Details".
evts := mt.GetAllSucceededEvents()
assert.True(
mt,
len(evts) >= 2,
"expected there to be at least 2 CommandSucceededEvent recorded")
evt := evts[len(evts)-1]
assert.Equal(
mt,
tc.expectedCommandName,
evt.CommandName,
"expected the last CommandSucceededEvent to be for %q, was %q",
tc.expectedCommandName,
evt.CommandName)
errInfo, ok := evt.Reply.Lookup("writeErrors", "0", "errInfo").DocumentOK()
assert.True(
mt,
ok,
"expected evt.Reply to contain writeErrors[0].errInfo but doesn't (evt.Reply = %v)",
evt.Reply)
assert.Equal(mt, details, errInfo, "want %v, got %v", details, errInfo)
})
}
})
}
func TestHintErrors(t *testing.T) {
mtOpts := mtest.NewOptions().MaxServerVersion("3.2").CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()
expected := errors.New("the 'hint' command parameter requires a minimum server wire version of 5")
mt.Run("UpdateMany", func(mt *mtest.T) {
_, got := mt.Coll.UpdateMany(mtest.Background, bson.D{{"a", 1}}, bson.D{{"$inc", bson.D{{"a", 1}}}},
options.Update().SetHint("_id_"))
assert.NotNil(mt, got, "expected non-nil error, got nil")
assert.Equal(mt, got, expected, "expected: %v got: %v", expected, got)
})
mt.Run("ReplaceOne", func(mt *mtest.T) {
_, got := mt.Coll.ReplaceOne(mtest.Background, bson.D{{"a", 1}}, bson.D{{"a", 2}},
options.Replace().SetHint("_id_"))
assert.NotNil(mt, got, "expected non-nil error, got nil")
assert.Equal(mt, got, expected, "expected: %v got: %v", expected, got)
})
mt.Run("BulkWrite", func(mt *mtest.T) {
models := []mongo.WriteModel{
&mongo.InsertOneModel{bson.D{{"_id", 2}}},
&mongo.ReplaceOneModel{Filter: bson.D{{"_id", 2}}, Replacement: bson.D{{"a", 2}}, Hint: "_id_"},
}
_, got := mt.Coll.BulkWrite(mtest.Background, models)
assert.NotNil(mt, got, "expected non-nil error, got nil")
assert.Equal(mt, got, expected, "expected: %v got: %v", expected, got)
})
}
func TestWriteConcernError(t *testing.T) {
mt := mtest.New(t, noClientOpts)
defer mt.Close()
errInfoOpts := mtest.NewOptions().MinServerVersion("4.0").Topologies(mtest.ReplicaSet)
mt.RunOpts("errInfo is propagated", errInfoOpts, func(mt *mtest.T) {
wcDoc := bsoncore.BuildDocumentFromElements(nil,
bsoncore.AppendInt32Element(nil, "w", 2),
bsoncore.AppendInt32Element(nil, "wtimeout", 0),
bsoncore.AppendStringElement(nil, "provenance", "clientSupplied"),
)
errInfoDoc := bsoncore.BuildDocumentFromElements(nil,
bsoncore.AppendDocumentElement(nil, "writeConcern", wcDoc),
)
fp := mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"insert"},
WriteConcernError: &mtest.WriteConcernErrorData{
Code: 100,
Name: "UnsatisfiableWriteConcern",
Errmsg: "Not enough data-bearing nodes",
ErrInfo: errInfoDoc,
},
},
}
mt.SetFailPoint(fp)
_, err := mt.Coll.InsertOne(mtest.Background, bson.D{{"x", 1}})
assert.NotNil(mt, err, "expected InsertOne error, got nil")
writeException, ok := err.(mongo.WriteException)
assert.True(mt, ok, "expected WriteException, got error %v of type %T", err, err)
wcError := writeException.WriteConcernError
assert.NotNil(mt, wcError, "expected write-concern error, got %v", err)
assert.True(mt, bytes.Equal(wcError.Details, errInfoDoc), "expected errInfo document %v, got %v",
bson.Raw(errInfoDoc), wcError.Details)
})
}
func TestErrorsCodeNamePropagated(t *testing.T) {
// Ensure the codeName field is propagated for both command and write concern errors.
mtOpts := mtest.NewOptions().
Topologies(mtest.ReplicaSet).
CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()
mt.RunOpts("command error", mtest.NewOptions().MinServerVersion("3.4"), func(mt *mtest.T) {
// codeName is propagated in an ok:0 error.
cmd := bson.D{
{"insert", mt.Coll.Name()},
{"documents", []bson.D{}},
}
err := mt.DB.RunCommand(mtest.Background, cmd).Err()
assert.NotNil(mt, err, "expected RunCommand error, got nil")
ce, ok := err.(mongo.CommandError)
assert.True(mt, ok, "expected error of type %T, got %v of type %T", mongo.CommandError{}, err, err)
expectedCodeName := "InvalidLength"
assert.Equal(mt, expectedCodeName, ce.Name, "expected error code name %q, got %q", expectedCodeName, ce.Name)
})
wcCollOpts := options.Collection().
SetWriteConcern(impossibleWc)
wcMtOpts := mtest.NewOptions().
CollectionOptions(wcCollOpts)
mt.RunOpts("write concern error", wcMtOpts, func(mt *mtest.T) {
// codeName is propagated for write concern errors.
_, err := mt.Coll.InsertOne(mtest.Background, bson.D{})
assert.NotNil(mt, err, "expected InsertOne error, got nil")
we, ok := err.(mongo.WriteException)
assert.True(mt, ok, "expected error of type %T, got %v of type %T", mongo.WriteException{}, err, err)
wce := we.WriteConcernError
assert.NotNil(mt, wce, "expected write concern error, got %v", we)
var expectedCodeName string
if codeNameVal, err := mt.GetSucceededEvent().Reply.LookupErr("writeConcernError", "codeName"); err == nil {
expectedCodeName = codeNameVal.StringValue()
}
assert.Equal(mt, expectedCodeName, wce.Name, "expected code name %q, got %q", expectedCodeName, wce.Name)
})
}
|