Skip to content

Commit

Permalink
GODRIVER-3092 Fix UNIX socket in URL parsing. (#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Feb 12, 2024
1 parent fcaeddd commit 452780c
Show file tree
Hide file tree
Showing 11 changed files with 674 additions and 673 deletions.
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
2 changes: 1 addition & 1 deletion mongo/integration/initial_dns_seedlist_discovery_test.go
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 mongo/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 mongo/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
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

// AuthenticateToAnything skips server type checks when deciding if authentication is possible.
Expand Down Expand Up @@ -338,7 +337,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 @@ -360,13 +362,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 @@ -1134,9 +1135,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

0 comments on commit 452780c

Please sign in to comment.