Skip to content

GODRIVER-3092 Fix UNIX socket in URL parsing. #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func buildSet(list []string) map[string]struct{} {
return set
}

func verifyConnstringOptions(mt *mtest.T, expected bson.Raw, cs connstring.ConnString) {
func verifyConnstringOptions(mt *mtest.T, expected bson.Raw, cs *connstring.ConnString) {
mt.Helper()

elems, _ := expected.Elements()
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/mtest/global_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func MultiMongosLoadBalancerURI() string {
}

// ClusterConnString returns the parsed ConnString for the cluster.
func ClusterConnString() connstring.ConnString {
func ClusterConnString() *connstring.ConnString {
return testContext.connString
}

Expand Down
2 changes: 1 addition & 1 deletion internal/integration/mtest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
// once during the global setup in TestMain. These variables should only be accessed indirectly through MongoTest
// instances.
var testContext struct {
connString connstring.ConnString
connString *connstring.ConnString
topo *topology.Topology
topoKind TopologyKind
// shardedReplicaSet will be true if we're connected to a sharded cluster and each shard is backed by a replica set.
Expand Down
10 changes: 5 additions & 5 deletions internal/integtest/integtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
)

var connectionString connstring.ConnString
var connectionString *connstring.ConnString
var connectionStringOnce sync.Once
var connectionStringErr error
var liveTopology *topology.Topology
Expand Down Expand Up @@ -211,7 +211,7 @@ func AddServerlessAuthCredentials(uri string) (string, error) {
}

// ConnString gets the globally configured connection string.
func ConnString(t *testing.T) connstring.ConnString {
func ConnString(t *testing.T) *connstring.ConnString {
connectionStringOnce.Do(func() {
uri, err := MongoDBURI()
require.NoError(t, err, "error constructing mongodb URI: %v", err)
Expand All @@ -228,7 +228,7 @@ func ConnString(t *testing.T) connstring.ConnString {
return connectionString
}

func GetConnString() (connstring.ConnString, error) {
func GetConnString() (*connstring.ConnString, error) {
mongodbURI := os.Getenv("MONGODB_URI")
if mongodbURI == "" {
mongodbURI = "mongodb://localhost:27017"
Expand All @@ -238,7 +238,7 @@ func GetConnString() (connstring.ConnString, error) {

cs, err := connstring.ParseAndValidate(mongodbURI)
if err != nil {
return connstring.ConnString{}, err
return nil, err
}

return cs, nil
Expand All @@ -249,7 +249,7 @@ func DBName(t *testing.T) string {
return GetDBName(ConnString(t))
}

func GetDBName(cs connstring.ConnString) string {
func GetDBName(cs *connstring.ConnString) string {
if cs.Database != "" {
return cs.Database
}
Expand Down
12 changes: 5 additions & 7 deletions mongo/options/clientoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ type ClientOptions struct {
ZstdLevel *int

err error
uri string
cs *connstring.ConnString
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the uri so the ConnString.Original is the single point of truth.


// Crypt specifies a custom driver.Crypt to be used to encrypt and decrypt documents. The default is no
Expand Down Expand Up @@ -332,7 +331,10 @@ func (c *ClientOptions) validate() error {
// GetURI returns the original URI used to configure the ClientOptions instance. If ApplyURI was not called during
// construction, this returns "".
func (c *ClientOptions) GetURI() string {
return c.uri
if c.cs == nil {
return ""
}
return c.cs.Original
}

// ApplyURI parses the given URI and sets options accordingly. The URI can contain host names, IPv4/IPv6 literals, or
Expand All @@ -354,13 +356,12 @@ func (c *ClientOptions) ApplyURI(uri string) *ClientOptions {
return c
}

c.uri = uri
cs, err := connstring.ParseAndValidate(uri)
if err != nil {
c.err = err
return c
}
c.cs = &cs
c.cs = cs

if cs.AppName != "" {
c.AppName = &cs.AppName
Expand Down Expand Up @@ -1123,9 +1124,6 @@ func MergeClientOptions(opts ...*ClientOptions) *ClientOptions {
if opt.err != nil {
c.err = opt.err
}
if opt.uri != "" {
c.uri = opt.uri
}
if opt.cs != nil {
c.cs = opt.cs
}
Expand Down
13 changes: 1 addition & 12 deletions mongo/options/clientoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,6 @@ func TestClientOptions(t *testing.T) {
t.Errorf("Merged client options do not match. got %v; want %v", got.err.Error(), opt1.err.Error())
}
})

t.Run("MergeClientOptions/uri", func(t *testing.T) {
opt1, opt2 := Client(), Client()
opt1.uri = "Test URI"

got := MergeClientOptions(nil, opt1, opt2)
if got.uri != "Test URI" {
t.Errorf("Merged client options do not match. got %v; want %v", got.uri, opt1.uri)
}
})
})
t.Run("ApplyURI", func(t *testing.T) {
baseClient := func() *ClientOptions {
Expand Down Expand Up @@ -586,10 +576,9 @@ func TestClientOptions(t *testing.T) {

// Manually add the URI and ConnString to the test expectations to avoid adding them in each test
// definition. The ConnString should only be recorded if there was no error while parsing.
tc.result.uri = tc.uri
cs, err := connstring.ParseAndValidate(tc.uri)
if err == nil {
tc.result.cs = &cs
tc.result.cs = cs
}

// We have to sort string slices in comparison, as Hosts resolved from SRV URIs do not have a set order.
Expand Down
Loading