Fix tests - Update MongoDB driver (#17662)

* Fix tests - Update MongoDB driver

* increase timeout and disconnect client after ping

* increase timeout

* disconnect client after the ping
This commit is contained in:
Milena Zlaticanin 2022-10-27 15:48:33 -05:00 committed by GitHub
parent 4f4a3b215a
commit a7e6ec47c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 109 deletions

View File

@ -2,19 +2,15 @@ package mongodb
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/hashicorp/vault/helper/testhelpers/docker"
"gopkg.in/mgo.v2"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// PrepareTestContainer calls PrepareTestContainerWithDatabase without a
@ -44,22 +40,16 @@ func PrepareTestContainerWithDatabase(t *testing.T, version, dbName string) (fun
if dbName != "" {
connURL = fmt.Sprintf("%s/%s", connURL, dbName)
}
dialInfo, err := ParseMongoURL(connURL)
ctx, _ = context.WithTimeout(context.Background(), 1*time.Minute)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(connURL))
if err != nil {
return nil, err
}
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
return nil, err
}
defer session.Close()
session.SetSyncTimeout(1 * time.Minute)
session.SetSocketTimeout(1 * time.Minute)
err = session.Ping()
if err != nil {
return nil, err
err = client.Ping(ctx, readpref.Primary())
if err = client.Disconnect(ctx); err != nil {
t.Fatal()
}
return docker.NewServiceURLParse(connURL)
@ -70,72 +60,3 @@ func PrepareTestContainerWithDatabase(t *testing.T, version, dbName string) (fun
return svc.Cleanup, svc.Config.URL().String()
}
// ParseMongoURL will parse a connection string and return a configured dialer
func ParseMongoURL(rawURL string) (*mgo.DialInfo, error) {
url, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
info := mgo.DialInfo{
Addrs: strings.Split(url.Host, ","),
Database: strings.TrimPrefix(url.Path, "/"),
Timeout: 10 * time.Second,
}
if url.User != nil {
info.Username = url.User.Username()
info.Password, _ = url.User.Password()
}
query := url.Query()
for key, values := range query {
var value string
if len(values) > 0 {
value = values[0]
}
switch key {
case "authSource":
info.Source = value
case "authMechanism":
info.Mechanism = value
case "gssapiServiceName":
info.Service = value
case "replicaSet":
info.ReplicaSetName = value
case "maxPoolSize":
poolLimit, err := strconv.Atoi(value)
if err != nil {
return nil, errors.New("bad value for maxPoolSize: " + value)
}
info.PoolLimit = poolLimit
case "ssl":
// Unfortunately, mgo doesn't support the ssl parameter in its MongoDB URI parsing logic, so we have to handle that
// ourselves. See https://github.com/go-mgo/mgo/issues/84
ssl, err := strconv.ParseBool(value)
if err != nil {
return nil, errors.New("bad value for ssl: " + value)
}
if ssl {
info.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), &tls.Config{})
}
}
case "connect":
if value == "direct" {
info.Direct = true
break
}
if value == "replicaSet" {
break
}
fallthrough
default:
return nil, errors.New("unsupported connection URL option: " + key + "=" + value)
}
}
return &info, nil
}

View File

@ -14,13 +14,11 @@ import (
"time"
"github.com/hashicorp/vault/helper/testhelpers/certhelpers"
"github.com/hashicorp/vault/helper/testhelpers/mongodb"
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
"github.com/ory/dockertest"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"gopkg.in/mgo.v2"
)
func TestInit_clientTLS(t *testing.T) {
@ -215,19 +213,12 @@ func startMongoWithTLS(t *testing.T, version string, confDir string) (retURL str
// exponential backoff-retry
err = pool.Retry(func() error {
var err error
dialInfo, err := mongodb.ParseMongoURL(retURL)
if err != nil {
return err
ctx, _ := context.WithTimeout(context.Background(), 1*time.Minute)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(retURL))
if err = client.Disconnect(ctx); err != nil {
t.Fatal()
}
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
return err
}
defer session.Close()
session.SetSyncTimeout(1 * time.Minute)
session.SetSocketTimeout(1 * time.Minute)
return session.Ping()
return client.Ping(ctx, readpref.Primary())
})
if err != nil {
cleanup()

View File

@ -13,7 +13,6 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/vault/helper/testhelpers/certhelpers"
"github.com/hashicorp/vault/helper/testhelpers/mongodb"
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
@ -27,7 +26,7 @@ import (
const mongoAdminRole = `{ "db": "admin", "roles": [ { "role": "readWrite" } ] }`
func TestMongoDB_Initialize(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "5.0.10")
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
defer cleanup()
db := new()
@ -120,7 +119,7 @@ func TestNewUser_usernameTemplate(t *testing.T) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "5.0.10")
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
defer cleanup()
db := new()
@ -146,7 +145,7 @@ func TestNewUser_usernameTemplate(t *testing.T) {
}
func TestMongoDB_CreateUser(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "5.0.10")
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
defer cleanup()
db := new()
@ -178,7 +177,7 @@ func TestMongoDB_CreateUser(t *testing.T) {
}
func TestMongoDB_CreateUser_writeConcern(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "5.0.10")
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
defer cleanup()
initReq := dbplugin.InitializeRequest{
@ -212,7 +211,7 @@ func TestMongoDB_CreateUser_writeConcern(t *testing.T) {
}
func TestMongoDB_DeleteUser(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "5.0.10")
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
defer cleanup()
db := new()
@ -252,7 +251,7 @@ func TestMongoDB_DeleteUser(t *testing.T) {
}
func TestMongoDB_UpdateUser_Password(t *testing.T) {
cleanup, connURL := mongodb.PrepareTestContainer(t, "5.0.10")
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
defer cleanup()
// The docker test method PrepareTestContainer defaults to a database "test"