|
15 | 15 | package watcher |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "bytes" |
18 | 19 | "context" |
19 | 20 | "crypto/rand" |
20 | 21 | "crypto/rsa" |
@@ -369,6 +370,55 @@ func TestConstructMongoClientOptions_NoTLS(t *testing.T) { |
369 | 370 | } |
370 | 371 | } |
371 | 372 |
|
| 373 | +// TestConstructMongoClientOptions_BSONOptions_PreserveV1DecodeShape guards the two |
| 374 | +// driver v1 -> v2 decode behaviour changes this package depends on: |
| 375 | +// |
| 376 | +// - DefaultDocumentM: v2 decodes nested documents into bson.D; callers here |
| 377 | +// type-assert bson.M. |
| 378 | +// - ObjectIDAsHexString: v2 refuses to decode an ObjectID into a Go string, which |
| 379 | +// breaks structs binding `bson:"_id"` to a string field (e.g. the latest-event |
| 380 | +// lookup in fault-quarantine's CancelLatestQuarantiningEvents). Losing this |
| 381 | +// silently turns manual-uncordon cancellation into a no-op. |
| 382 | +func TestConstructMongoClientOptions_BSONOptions_PreserveV1DecodeShape(t *testing.T) { |
| 383 | + mongoConfig := MongoDBConfig{ |
| 384 | + URI: "mongodb://localhost:27017", |
| 385 | + Database: "test", |
| 386 | + Collection: "test", |
| 387 | + TotalPingTimeoutSeconds: 5, |
| 388 | + TotalPingIntervalSeconds: 1, |
| 389 | + } |
| 390 | + |
| 391 | + opts, err := constructMongoClientOptions(mongoConfig) |
| 392 | + require.NoError(t, err) |
| 393 | + require.NotNil(t, opts.BSONOptions, "BSON options must be set") |
| 394 | + require.True(t, opts.BSONOptions.DefaultDocumentM, "DefaultDocumentM must stay enabled") |
| 395 | + require.True(t, opts.BSONOptions.ObjectIDAsHexString, "ObjectIDAsHexString must stay enabled") |
| 396 | +} |
| 397 | + |
| 398 | +// TestObjectIDDecodesIntoStringField documents the underlying driver behaviour the |
| 399 | +// option above compensates for: without it, decoding _id into a string fails. |
| 400 | +func TestObjectIDDecodesIntoStringField(t *testing.T) { |
| 401 | + oid := bson.NewObjectID() |
| 402 | + |
| 403 | + raw, err := bson.Marshal(bson.M{"_id": oid}) |
| 404 | + require.NoError(t, err) |
| 405 | + |
| 406 | + var target struct { |
| 407 | + ID string `bson:"_id"` |
| 408 | + } |
| 409 | + |
| 410 | + // Default v2 decoder: this is the failure seen in CI. |
| 411 | + err = bson.Unmarshal(raw, &target) |
| 412 | + require.Error(t, err) |
| 413 | + require.Contains(t, err.Error(), "decoding an object ID into a string is not supported by default") |
| 414 | + |
| 415 | + // With ObjectIDAsHexString the same document decodes to the hex string. |
| 416 | + dec := bson.NewDecoder(bson.NewDocumentReader(bytes.NewReader(raw))) |
| 417 | + dec.ObjectIDAsHexString() |
| 418 | + require.NoError(t, dec.Decode(&target)) |
| 419 | + require.Equal(t, oid.Hex(), target.ID) |
| 420 | +} |
| 421 | + |
372 | 422 | func TestConstructMongoClientOptions_DynamicClientCertificateUsesX509Auth(t *testing.T) { |
373 | 423 | caCertPEM, caKeyPEM, err := generateCA() |
374 | 424 | if err != nil { |
|
0 commit comments