2019-04-22 16:26:10 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2019-07-05 18:57:01 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2019-04-22 16:26:10 +00:00
|
|
|
"fmt"
|
2019-07-05 18:57:01 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2019-04-22 16:26:10 +00:00
|
|
|
"os"
|
2019-07-05 18:57:01 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-04-22 16:26:10 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ory/dockertest"
|
|
|
|
"gopkg.in/mgo.v2"
|
|
|
|
)
|
|
|
|
|
2019-07-05 18:57:01 +00:00
|
|
|
// PrepareTestContainer calls PrepareTestContainerWithDatabase without a
|
|
|
|
// database name value, which results in configuring a database named "test"
|
2019-04-22 16:26:10 +00:00
|
|
|
func PrepareTestContainer(t *testing.T, version string) (cleanup func(), retURL string) {
|
2019-07-05 18:57:01 +00:00
|
|
|
return PrepareTestContainerWithDatabase(t, version, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareTestContainerWithDatabase configures a test container with a given
|
|
|
|
// database name, to test non-test/admin database configurations
|
|
|
|
func PrepareTestContainerWithDatabase(t *testing.T, version, dbName string) (cleanup func(), retURL string) {
|
2019-04-22 16:26:10 +00:00
|
|
|
if os.Getenv("MONGODB_URL") != "" {
|
|
|
|
return func() {}, os.Getenv("MONGODB_URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-07-05 18:57:01 +00:00
|
|
|
resource, err := pool.Run("mongo", version, []string{})
|
2019-04-22 16:26:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local mongo docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() {
|
2019-07-05 18:57:01 +00:00
|
|
|
err := pool.Purge(resource)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to cleanup local container: %s", err)
|
|
|
|
}
|
2019-04-22 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 18:57:01 +00:00
|
|
|
retURL = fmt.Sprintf("mongodb://localhost:%s", resource.GetPort("27017/tcp"))
|
|
|
|
if dbName != "" {
|
|
|
|
retURL = fmt.Sprintf("%s/%s", retURL, dbName)
|
|
|
|
}
|
2019-04-22 16:26:10 +00:00
|
|
|
|
|
|
|
// exponential backoff-retry
|
|
|
|
if err = pool.Retry(func() error {
|
2019-07-05 18:57:01 +00:00
|
|
|
var err error
|
|
|
|
dialInfo, err := parseMongoURL(retURL)
|
2019-04-22 16:26:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-05 18:57:01 +00:00
|
|
|
session, err := mgo.DialWithInfo(dialInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer session.Close()
|
2019-04-22 16:26:10 +00:00
|
|
|
session.SetSyncTimeout(1 * time.Minute)
|
|
|
|
session.SetSocketTimeout(1 * time.Minute)
|
|
|
|
return session.Ping()
|
|
|
|
}); err != nil {
|
|
|
|
cleanup()
|
|
|
|
t.Fatalf("Could not connect to mongo docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-07-05 18:57:01 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|