Skip to content

Commit

Permalink
GODRIVER-2333 Assert that Ping op succeeds initial DNS spec tests (#1124
Browse files Browse the repository at this point in the history
)
  • Loading branch information
prestonvasquez committed Nov 16, 2022
1 parent 48b0701 commit 2053ec7
Show file tree
Hide file tree
Showing 55 changed files with 196 additions and 24 deletions.
40 changes: 40 additions & 0 deletions mongo/integration/initial_dns_seedlist_discovery_test.go
Expand Up @@ -15,12 +15,15 @@ import (
"runtime"
"strings"
"testing"
"time"

"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/description"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
)
Expand All @@ -37,19 +40,26 @@ type seedlistTest struct {
NumHosts *int `bson:"numHosts"`
Error bool `bson:"error"`
Options bson.Raw `bson:"options"`
Ping *bool `bson:"ping"`
}

func TestInitialDNSSeedlistDiscoverySpec(t *testing.T) {
mt := mtest.New(t, noClientOpts)
defer mt.Close()

mt.RunOpts("replica set", mtest.NewOptions().Topologies(mtest.ReplicaSet).CreateClient(false), func(mt *mtest.T) {
mt.Parallel()

runSeedlistDiscoveryDirectory(mt, "replica-set")
})
mt.RunOpts("sharded", mtest.NewOptions().Topologies(mtest.Sharded).CreateClient(false), func(mt *mtest.T) {
mt.Parallel()

runSeedlistDiscoveryDirectory(mt, "sharded")
})
mt.RunOpts("load balanced", mtest.NewOptions().Topologies(mtest.LoadBalanced).CreateClient(false), func(mt *mtest.T) {
mt.Parallel()

runSeedlistDiscoveryDirectory(mt, "load-balanced")
})
}
Expand All @@ -63,6 +73,24 @@ func runSeedlistDiscoveryDirectory(mt *mtest.T, subdirectory string) {
}
}

// runSeedlistDiscoveryPingTest will create a new connection using the test URI and attempt to "ping" the server.
func runSeedlistDiscoveryPingTest(mt *mtest.T, clientOpts *options.ClientOptions) {
ctx := context.Background()

client, err := mongo.Connect(ctx, clientOpts)
assert.Nil(mt, err, "Connect error: %v", err)

defer func() { _ = client.Disconnect(ctx) }()

// Create a context with a timeout to prevent the ping operation from blocking indefinitely.
pingCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()

// Ping the server.
err = client.Ping(pingCtx, readpref.Nearest())
assert.Nil(mt, err, "Ping error: %v", err)
}

func runSeedlistDiscoveryTest(mt *mtest.T, file string) {
content, err := ioutil.ReadFile(file)
assert.Nil(mt, err, "ReadFile error for %v: %v", file, err)
Expand Down Expand Up @@ -131,6 +159,10 @@ func runSeedlistDiscoveryTest(mt *mtest.T, file string) {
_, err := getServerByAddress(host, topo)
assert.Nil(mt, err, "error finding host %q: %v", host, err)
}

if ping := test.Ping; ping == nil || *ping {
runSeedlistDiscoveryPingTest(mt, opts)
}
}

func buildSet(list []string) map[string]struct{} {
Expand Down Expand Up @@ -230,6 +262,14 @@ func getServerByAddress(address string, topo *topology.Topology) (description.Se
if err != nil {
return description.Server{}, err
}

// If the selected server is a topology.SelectedServer, then we can get the description without creating a
// connect pool.
topologySelectedServer, ok := selectedServer.(*topology.SelectedServer)
if ok {
return topologySelectedServer.Description().Server, nil
}

selectedServerConnection, err := selectedServer.Connection(context.Background())
if err != nil {
return description.Server{}, err
Expand Down
Expand Up @@ -10,5 +10,6 @@
"loadBalanced": true,
"ssl": true,
"directConnection": false
}
},
"ping": true
}
Expand Up @@ -11,3 +11,4 @@ options:
loadBalanced: true
ssl: true
directConnection: false
ping: true
Expand Up @@ -9,5 +9,6 @@
"options": {
"loadBalanced": true,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -8,3 +8,4 @@ hosts:
options:
loadBalanced: true
ssl: true
ping: true
Expand Up @@ -10,5 +10,6 @@
"loadBalanced": true,
"srvMaxHosts": 0,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -8,3 +8,4 @@ options:
loadBalanced: true
srvMaxHosts: 0
ssl: true
ping: true
Expand Up @@ -10,5 +10,6 @@
"loadBalanced": true,
"srvMaxHosts": 0,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -8,3 +8,4 @@ options:
loadBalanced: true
srvMaxHosts: 0
ssl: true
ping: true
Expand Up @@ -11,5 +11,6 @@
"options": {
"ssl": true,
"directConnection": false
}
},
"ping": true
}
Expand Up @@ -8,3 +8,4 @@ hosts:
options:
ssl: true
directConnection: false
ping: true
@@ -0,0 +1,22 @@
{
"uri": "mongodb+srv://b*b%40f3tt%3D:%244to%40L8%3DMC@test3.test.build.10gen.cc/mydb%3F?replicaSet=repl0",
"seeds": [
"localhost.test.build.10gen.cc:27017"
],
"hosts": [
"localhost:27017",
"localhost:27018",
"localhost:27019"
],
"options": {
"replicaSet": "repl0",
"ssl": true
},
"parsed_options": {
"user": "b*b@f3tt=",
"password": "$4to@L8=MC",
"db": "mydb?"
},
"ping": false,
"comment": "Encoded user, pass, and DB parse correctly"
}
@@ -0,0 +1,19 @@
uri: "mongodb+srv://b*b%40f3tt%3D:%244to%40L8%3DMC@test3.test.build.10gen.cc/mydb%3F?replicaSet=repl0"
seeds:
- localhost.test.build.10gen.cc:27017
hosts:
- localhost:27017
- localhost:27018
- localhost:27019
options:
replicaSet: repl0
ssl: true
parsed_options:
user: "b*b@f3tt="
password: "$4to@L8=MC"
db: "mydb?"
# Don't run a ping for URIs that include userinfo. Ping doesn't require authentication, so missing
# userinfo isn't a problem, but some drivers will fail handshake on a connection if userinfo is
# provided but incorrect.
ping: false
comment: Encoded user, pass, and DB parse correctly
Expand Up @@ -11,5 +11,6 @@
"options": {
"loadBalanced": false,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -8,3 +8,4 @@ hosts:
options:
loadBalanced: false
ssl: true
ping: true
Expand Up @@ -12,5 +12,6 @@
"replicaSet": "repl0",
"ssl": true
},
"ping": true,
"comment": "Is correct, as returned host name shared the URI root \"test.build.10gen.cc\"."
}
Expand Up @@ -8,4 +8,5 @@ hosts:
options:
replicaSet: repl0
ssl: true
ping: true
comment: Is correct, as returned host name shared the URI root "test.build.10gen.cc".
Expand Up @@ -11,5 +11,6 @@
"options": {
"replicaSet": "repl0",
"ssl": true
}
},
"ping": true
}
Expand Up @@ -8,3 +8,4 @@ hosts:
options:
replicaSet: repl0
ssl: true
ping: true
Expand Up @@ -11,5 +11,6 @@
"options": {
"replicaSet": "repl0",
"ssl": true
}
},
"ping": true
}
Expand Up @@ -8,3 +8,4 @@ hosts:
options:
replicaSet: repl0
ssl: true
ping: true
Expand Up @@ -12,5 +12,6 @@
"replicaSet": "repl0",
"authSource": "thisDB",
"ssl": true
}
},
"ping": true
}
Expand Up @@ -9,3 +9,4 @@ options:
replicaSet: repl0
authSource: thisDB
ssl: true
ping: true
Expand Up @@ -12,5 +12,6 @@
"options": {
"ssl": true,
"srvServiceName": "customname"
}
},
"ping": true
}
Expand Up @@ -9,3 +9,4 @@ hosts:
options:
ssl: true
srvServiceName: "customname"
ping: true
Expand Up @@ -13,5 +13,6 @@
"options": {
"srvMaxHosts": 2,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -14,3 +14,4 @@ hosts:
options:
srvMaxHosts: 2
ssl: true
ping: true
Expand Up @@ -12,5 +12,6 @@
"options": {
"srvMaxHosts": 3,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -13,3 +13,4 @@ hosts:
options:
srvMaxHosts: 3
ssl: true
ping: true
Expand Up @@ -9,5 +9,6 @@
"options": {
"srvMaxHosts": 1,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -13,3 +13,4 @@ hosts:
options:
srvMaxHosts: 1
ssl: true
ping: true
Expand Up @@ -13,5 +13,6 @@
"replicaSet": "repl0",
"srvMaxHosts": 0,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -13,3 +13,4 @@ options:
replicaSet: repl0
srvMaxHosts: 0
ssl: true
ping: true
Expand Up @@ -13,5 +13,6 @@
"replicaSet": "repl0",
"srvMaxHosts": 0,
"ssl": true
}
},
"ping": true
}
Expand Up @@ -13,3 +13,4 @@ options:
replicaSet: repl0
srvMaxHosts: 0
ssl: true
ping: true
Expand Up @@ -12,5 +12,6 @@
"options": {
"replicaSet": "repl0",
"ssl": true
}
},
"ping": true
}
Expand Up @@ -9,3 +9,4 @@ hosts:
options:
replicaSet: repl0
ssl: true
ping: true
Expand Up @@ -12,5 +12,6 @@
"options": {
"replicaSet": "repl0",
"ssl": true
}
},
"ping": true
}
Expand Up @@ -9,3 +9,4 @@ hosts:
options:
replicaSet: repl0
ssl: true
ping: true
Expand Up @@ -12,5 +12,6 @@
"replicaSet": "repl0",
"authSource": "thisDB",
"ssl": false
}
},
"ping": true
}
Expand Up @@ -9,3 +9,4 @@ options:
replicaSet: repl0
authSource: thisDB
ssl: false
ping: true
Expand Up @@ -12,5 +12,6 @@
"replicaSet": "repl0",
"authSource": "otherDB",
"ssl": true
}
},
"ping": true
}
Expand Up @@ -9,3 +9,4 @@ options:
replicaSet: repl0
authSource: otherDB
ssl: true
ping: true

0 comments on commit 2053ec7

Please sign in to comment.