Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions internal/assert/assertbson/assertbson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) MongoDB, Inc. 2025-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 assertbson

import (
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/internal/assert"
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
)

type tHelper interface {
Helper()
}

// EqualDocument asserts that the expected and actual BSON documents are equal.
// If the documents are not equal, it prints both the binary diff and Extended
// JSON representation of the BSON documents.
func EqualDocument(t assert.TestingT, expected, actual []byte) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

return assert.Equal(t,
expected,
actual,
`expected and actual BSON documents do not match
As Extended JSON:
Expected: %s
Actual : %s`,
bson.Raw(expected),
bson.Raw(actual))
}

// EqualValue asserts that the expected and actual BSON values are equal. If the
// values are not equal, it prints both the binary diff and Extended JSON
// representation of the BSON values.
func EqualValue[T bson.RawValue | bsoncore.Value](t assert.TestingT, expected, actual T) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

return assert.Equal(t,
expected,
actual,
`expected and actual BSON values do not match
As Extended JSON:
Expected: %s
Actual : %s`,
expected,
actual)
}
62 changes: 62 additions & 0 deletions internal/assert/assertbson/assertbson_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (C) MongoDB, Inc. 2025-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 assertbson

import (
"testing"

"go.mongodb.org/mongo-driver/v2/bson"
)

func TestEqualDocument(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
expected []byte
actual []byte
want bool
}{
{
name: "equal bson.Raw",
expected: bson.Raw{5, 0, 0, 0, 0},
actual: bson.Raw{5, 0, 0, 0, 0},
want: true,
},
{
name: "different bson.Raw",
expected: bson.Raw{8, 0, 0, 0, 10, 120, 0, 0},
actual: bson.Raw{5, 0, 0, 0, 0},
want: false,
},
{
name: "invalid bson.Raw",
expected: bson.Raw{99, 99, 99, 99},
actual: bson.Raw{5, 0, 0, 0, 0},
want: false,
},
{
name: "nil bson.Raw",
expected: bson.Raw(nil),
actual: bson.Raw(nil),
want: true,
},
}

for _, tc := range testCases {
tc := tc // Capture range variable.

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

got := EqualDocument(new(testing.T), tc.expected, tc.actual)
if got != tc.want {
t.Errorf("EqualBSON(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
}
})
}
}
21 changes: 0 additions & 21 deletions internal/assert/assertion_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package assert

import (
"context"
"fmt"
"reflect"
"time"
"unsafe"
Expand Down Expand Up @@ -71,26 +70,6 @@ func DifferentAddressRanges(t TestingT, a, b []byte) (ok bool) {
return false
}

// EqualBSON asserts that the expected and actual BSON binary values are equal.
// If the values are not equal, it prints both the binary and Extended JSON diff
// of the BSON values. The provided BSON value types must implement the
// fmt.Stringer interface.
func EqualBSON(t TestingT, expected, actual interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

return Equal(t,
expected,
actual,
`expected and actual BSON values do not match
As Extended JSON:
Expected: %s
Actual : %s`,
expected.(fmt.Stringer).String(),
actual.(fmt.Stringer).String())
}

// Soon runs the provided callback and fails the passed-in test if the callback
// does not complete within timeout. The provided callback should respect the
// passed-in context and cease execution when it has expired.
Expand Down
51 changes: 0 additions & 51 deletions internal/assert/assertion_mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package assert

import (
"testing"

"go.mongodb.org/mongo-driver/v2/bson"
)

func TestDifferentAddressRanges(t *testing.T) {
Expand Down Expand Up @@ -74,52 +72,3 @@ func TestDifferentAddressRanges(t *testing.T) {
})
}
}

func TestEqualBSON(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
expected interface{}
actual interface{}
want bool
}{
{
name: "equal bson.Raw",
expected: bson.Raw{5, 0, 0, 0, 0},
actual: bson.Raw{5, 0, 0, 0, 0},
want: true,
},
{
name: "different bson.Raw",
expected: bson.Raw{8, 0, 0, 0, 10, 120, 0, 0},
actual: bson.Raw{5, 0, 0, 0, 0},
want: false,
},
{
name: "invalid bson.Raw",
expected: bson.Raw{99, 99, 99, 99},
actual: bson.Raw{5, 0, 0, 0, 0},
want: false,
},
{
name: "nil bson.Raw",
expected: bson.Raw(nil),
actual: bson.Raw(nil),
want: true,
},
}

for _, tc := range testCases {
tc := tc // Capture range variable.

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

got := EqualBSON(new(testing.T), tc.expected, tc.actual)
if got != tc.want {
t.Errorf("EqualBSON(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
}
})
}
}
97 changes: 54 additions & 43 deletions internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ import (
"net"
"os"
"reflect"
"runtime"
"strings"
"testing"
"time"

"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/event"
"go.mongodb.org/mongo-driver/v2/internal/assert"
"go.mongodb.org/mongo-driver/v2/internal/assert/assertbson"
"go.mongodb.org/mongo-driver/v2/internal/eventtest"
"go.mongodb.org/mongo-driver/v2/internal/failpoint"
"go.mongodb.org/mongo-driver/v2/internal/handshake"
"go.mongodb.org/mongo-driver/v2/internal/integration/mtest"
"go.mongodb.org/mongo-driver/v2/internal/integtest"
"go.mongodb.org/mongo-driver/v2/internal/require"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
"go.mongodb.org/mongo-driver/v2/version"
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/wiremessage"
Expand Down Expand Up @@ -456,26 +458,27 @@ func TestClient(t *testing.T) {
err := mt.Client.Ping(context.Background(), mtest.PrimaryRp)
assert.Nil(mt, err, "Ping error: %v", err)

msgPairs := mt.GetProxiedMessages()
assert.True(mt, len(msgPairs) >= 2, "expected at least 2 events sent, got %v", len(msgPairs))
want := mustMarshalBSON(bson.D{
{Key: "driver", Value: bson.D{
{Key: "name", Value: "mongo-go-driver"},
{Key: "version", Value: version.Driver},
}},
{Key: "os", Value: bson.D{
{Key: "type", Value: runtime.GOOS},
{Key: "architecture", Value: runtime.GOARCH},
}},
{Key: "platform", Value: runtime.Version()},
{Key: "application", Value: bson.D{
bson.E{Key: "name", Value: "foo"},
}},
})

// First two messages should be connection handshakes: one for the heartbeat connection and the other for the
// application connection.
for idx, pair := range msgPairs[:2] {
helloCommand := handshake.LegacyHello
// Expect "hello" command name with API version.
if os.Getenv("REQUIRE_API_VERSION") == "true" {
helloCommand = "hello"
}
assert.Equal(mt, pair.CommandName, helloCommand, "expected command name %s at index %d, got %s", helloCommand, idx,
pair.CommandName)

sent := pair.Sent
appNameVal, err := sent.Command.LookupErr("client", "application", "name")
assert.Nil(mt, err, "expected command %s at index %d to contain app name", sent.Command, idx)
appName := appNameVal.StringValue()
assert.Equal(mt, testAppName, appName, "expected app name %v at index %d, got %v", testAppName, idx,
appName)
for i := 0; i < 2; i++ {
message := mt.GetProxyCapture().TryNext()
require.NotNil(mt, message, "expected handshake message, got nil")

clientMetadata := clientMetadataFromHandshake(mt, message.Sent.Command)
assertbson.EqualDocument(mt, want, clientMetadata)
}
})

Expand Down Expand Up @@ -604,24 +607,32 @@ func TestClient(t *testing.T) {
err := mt.Client.Ping(context.Background(), mtest.PrimaryRp)
assert.Nil(mt, err, "Ping error: %v", err)

msgPairs := mt.GetProxiedMessages()
assert.True(mt, len(msgPairs) >= 3, "expected at least 3 events, got %v", len(msgPairs))
proxyCapture := mt.GetProxyCapture()

// The first message should be a connection handshake.
pair := msgPairs[0]
assert.Equal(mt, handshake.LegacyHello, pair.CommandName, "expected command name %s at index 0, got %s",
handshake.LegacyHello, pair.CommandName)
assert.Equal(mt, wiremessage.OpQuery, pair.Sent.OpCode,
"expected 'OP_QUERY' OpCode in wire message, got %q", pair.Sent.OpCode.String())

// Look for a saslContinue in the remaining proxied messages and assert that it uses the OP_MSG OpCode, as wire
// version is now known to be >= 6.
firstMessage := proxyCapture.TryNext()
require.NotNil(mt, firstMessage, "expected handshake message, got nil")

assert.True(t, firstMessage.IsHandshake())

opCode := firstMessage.Sent.OpCode
assert.Equal(mt, wiremessage.OpQuery, opCode,
"expected 'OP_MSG' OpCode in wire message, got %q", opCode.String())

// Look for a saslContinue in the remaining proxied messages and assert that
// it uses the OP_MSG OpCode, as wire version is now known to be >= 6.
var saslContinueFound bool
for _, pair := range msgPairs[1:] {
if pair.CommandName == "saslContinue" {
for {
message := proxyCapture.TryNext()
if message == nil {
break
}

if message.CommandName == "saslContinue" {
saslContinueFound = true
assert.Equal(mt, wiremessage.OpMsg, pair.Sent.OpCode,
"expected 'OP_MSG' OpCode in wire message, got %s", pair.Sent.OpCode.String())
opCode := message.Sent.OpCode
assert.Equal(mt, wiremessage.OpMsg, opCode,
"expected 'OP_MSG' OpCode in wire message, got %q", opCode.String())
break
}
}
Expand All @@ -634,18 +645,18 @@ func TestClient(t *testing.T) {
err := mt.Client.Ping(context.Background(), mtest.PrimaryRp)
assert.Nil(mt, err, "Ping error: %v", err)

msgPairs := mt.GetProxiedMessages()
assert.True(mt, len(msgPairs) >= 3, "expected at least 3 events, got %v", len(msgPairs))

// First three messages should be connection handshakes: one for the heartbeat connection, another for the
// application connection, and a final one for the RTT monitor connection.
for idx, pair := range msgPairs[:3] {
assert.Equal(mt, "hello", pair.CommandName, "expected command name 'hello' at index %d, got %s", idx,
pair.CommandName)
for idx := 0; idx < 3; idx++ {
message := mt.GetProxyCapture().TryNext()
require.NotNil(mt, message, "expected handshake message, got nil")

assert.True(t, message.IsHandshake())

// Assert that appended OpCode is OP_MSG when API version is set.
assert.Equal(mt, wiremessage.OpMsg, pair.Sent.OpCode,
"expected 'OP_MSG' OpCode in wire message, got %q", pair.Sent.OpCode.String())
opCode := message.Sent.OpCode
assert.Equal(mt, wiremessage.OpMsg, opCode,
"expected 'OP_MSG' OpCode in wire message, got %q", opCode.String())
}
})

Expand Down Expand Up @@ -1086,7 +1097,7 @@ func TestClient_BSONOptions(t *testing.T) {
got, err := sr.Raw()
require.NoError(mt, err, "Raw error")

assert.EqualBSON(mt, tc.wantRaw, got)
assertbson.EqualDocument(mt, tc.wantRaw, got)
}
})
}
Expand Down
Loading
Loading