Add `connectionString` metadata option for MongoDB (#2659)
Signed-off-by: Bernd Verst <github@bernd.dev>
This commit is contained in:
parent
cfb4d7ecc9
commit
575567e30f
|
|
@ -94,6 +94,7 @@ type mongoDBMetadata struct {
|
|||
Writeconcern string
|
||||
Readconcern string
|
||||
Params string
|
||||
ConnectionString string
|
||||
OperationTimeout time.Duration
|
||||
}
|
||||
|
||||
|
|
@ -415,7 +416,11 @@ func (m *MongoDB) Query(ctx context.Context, req *state.QueryRequest) (*state.Qu
|
|||
}, nil
|
||||
}
|
||||
|
||||
func getMongoURI(metadata *mongoDBMetadata) string {
|
||||
func getMongoConnectionString(metadata *mongoDBMetadata) string {
|
||||
if metadata.ConnectionString != "" {
|
||||
return metadata.ConnectionString
|
||||
}
|
||||
|
||||
if len(metadata.Server) != 0 {
|
||||
if metadata.Username != "" && metadata.Password != "" {
|
||||
return fmt.Sprintf(connectionURIFormatWithSrvAndCredentials, metadata.Username, metadata.Password, metadata.Server, metadata.DatabaseName, metadata.Params)
|
||||
|
|
@ -432,7 +437,7 @@ func getMongoURI(metadata *mongoDBMetadata) string {
|
|||
}
|
||||
|
||||
func getMongoDBClient(ctx context.Context, metadata *mongoDBMetadata) (*mongo.Client, error) {
|
||||
uri := getMongoURI(metadata)
|
||||
uri := getMongoConnectionString(metadata)
|
||||
|
||||
// Set client options
|
||||
clientOptions := options.Client().ApplyURI(uri)
|
||||
|
|
@ -468,12 +473,14 @@ func getMongoDBMetaData(meta state.Metadata) (*mongoDBMetadata, error) {
|
|||
return nil, decodeErr
|
||||
}
|
||||
|
||||
if len(m.Host) == 0 && len(m.Server) == 0 {
|
||||
return nil, errors.New("must set 'host' or 'server' fields in metadata")
|
||||
}
|
||||
if m.ConnectionString == "" {
|
||||
if len(m.Host) == 0 && len(m.Server) == 0 {
|
||||
return nil, errors.New("must set 'host' or 'server' fields in metadata")
|
||||
}
|
||||
|
||||
if len(m.Host) != 0 && len(m.Server) != 0 {
|
||||
return nil, errors.New("'host' or 'server' fields are mutually exclusive")
|
||||
if len(m.Host) != 0 && len(m.Server) != 0 {
|
||||
return nil, errors.New("'host' or 'server' fields are mutually exclusive")
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ func TestGetMongoDBMetadata(t *testing.T) {
|
|||
assert.NotNil(t, err)
|
||||
})
|
||||
|
||||
t.Run("Valid connectionstring without params", func(t *testing.T) {
|
||||
t.Run("Valid connection details without params", func(t *testing.T) {
|
||||
properties := map[string]string{
|
||||
host: "127.0.0.2",
|
||||
databaseName: "TestDB",
|
||||
|
|
@ -87,13 +87,13 @@ func TestGetMongoDBMetadata(t *testing.T) {
|
|||
metadata, err := getMongoDBMetaData(m)
|
||||
assert.Nil(t, err)
|
||||
|
||||
uri := getMongoURI(metadata)
|
||||
uri := getMongoConnectionString(metadata)
|
||||
expected := "mongodb://username:password@127.0.0.2/TestDB"
|
||||
|
||||
assert.Equal(t, expected, uri)
|
||||
})
|
||||
|
||||
t.Run("Valid connectionstring without username", func(t *testing.T) {
|
||||
t.Run("Valid connection details without username", func(t *testing.T) {
|
||||
properties := map[string]string{
|
||||
host: "localhost:27017",
|
||||
databaseName: "TestDB",
|
||||
|
|
@ -106,13 +106,13 @@ func TestGetMongoDBMetadata(t *testing.T) {
|
|||
metadata, err := getMongoDBMetaData(m)
|
||||
assert.Nil(t, err)
|
||||
|
||||
uri := getMongoURI(metadata)
|
||||
uri := getMongoConnectionString(metadata)
|
||||
expected := "mongodb://localhost:27017/TestDB"
|
||||
|
||||
assert.Equal(t, expected, uri)
|
||||
})
|
||||
|
||||
t.Run("Valid connectionstring with params", func(t *testing.T) {
|
||||
t.Run("Valid connection details with params", func(t *testing.T) {
|
||||
properties := map[string]string{
|
||||
host: "127.0.0.2",
|
||||
databaseName: "TestDB",
|
||||
|
|
@ -128,13 +128,13 @@ func TestGetMongoDBMetadata(t *testing.T) {
|
|||
metadata, err := getMongoDBMetaData(m)
|
||||
assert.Nil(t, err)
|
||||
|
||||
uri := getMongoURI(metadata)
|
||||
uri := getMongoConnectionString(metadata)
|
||||
expected := "mongodb://username:password@127.0.0.2/TestDB?ssl=true"
|
||||
|
||||
assert.Equal(t, expected, uri)
|
||||
})
|
||||
|
||||
t.Run("Valid connectionstring with DNS SRV", func(t *testing.T) {
|
||||
t.Run("Valid connection details with DNS SRV", func(t *testing.T) {
|
||||
properties := map[string]string{
|
||||
server: "server.example.com",
|
||||
databaseName: "TestDB",
|
||||
|
|
@ -148,7 +148,7 @@ func TestGetMongoDBMetadata(t *testing.T) {
|
|||
metadata, err := getMongoDBMetaData(m)
|
||||
assert.Nil(t, err)
|
||||
|
||||
uri := getMongoURI(metadata)
|
||||
uri := getMongoConnectionString(metadata)
|
||||
expected := "mongodb+srv://server.example.com/?ssl=true"
|
||||
|
||||
assert.Equal(t, expected, uri)
|
||||
|
|
@ -187,4 +187,24 @@ func TestGetMongoDBMetadata(t *testing.T) {
|
|||
expected := "'host' or 'server' fields are mutually exclusive"
|
||||
assert.Equal(t, expected, err.Error())
|
||||
})
|
||||
|
||||
t.Run("Connectionstring ignores all other connection details", func(t *testing.T) {
|
||||
properties := map[string]string{
|
||||
host: "localhost:27017",
|
||||
databaseName: "TestDB",
|
||||
collectionName: "TestCollection",
|
||||
"connectionString": "mongodb://localhost:99999/UnchanedDB",
|
||||
}
|
||||
m := state.Metadata{
|
||||
Base: metadata.Base{Properties: properties},
|
||||
}
|
||||
|
||||
metadata, err := getMongoDBMetaData(m)
|
||||
assert.Nil(t, err)
|
||||
|
||||
uri := getMongoConnectionString(metadata)
|
||||
expected := "mongodb://localhost:99999/UnchanedDB"
|
||||
|
||||
assert.Equal(t, expected, uri)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ spec:
|
|||
version: v1
|
||||
initTimeout: 5m
|
||||
metadata:
|
||||
- name: host
|
||||
value: "localhost:27017"
|
||||
- name: connectionString
|
||||
value: "mongodb://localhost:27017/admin"
|
||||
- name: databaseName
|
||||
value: "admin"
|
||||
- name: writeConcern
|
||||
|
|
|
|||
|
|
@ -189,7 +189,7 @@ func TestMongoDB(t *testing.T) {
|
|||
Step("Get Values Saved Earlier And Not Expired, after MongoDB restart", testGetAfterMongoDBRestart).
|
||||
Run()
|
||||
|
||||
flow.New(t, "Connecting MongoDB And Verifying majority of the tests here for a single node with valid read, "+
|
||||
flow.New(t, "Connecting MongoDB using connectionString And Verifying majority of the tests here for a single node with valid read, "+
|
||||
"write concerns and operation timeout").
|
||||
Step(dockercompose.Run("mongodb", dockerComposeSingleYAML)).
|
||||
Step("Waiting for component to start...", flow.Sleep(20*time.Second)).
|
||||
|
|
|
|||
Loading…
Reference in New Issue