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
 | 
						Writeconcern     string
 | 
				
			||||||
	Readconcern      string
 | 
						Readconcern      string
 | 
				
			||||||
	Params           string
 | 
						Params           string
 | 
				
			||||||
 | 
						ConnectionString string
 | 
				
			||||||
	OperationTimeout time.Duration
 | 
						OperationTimeout time.Duration
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -415,7 +416,11 @@ func (m *MongoDB) Query(ctx context.Context, req *state.QueryRequest) (*state.Qu
 | 
				
			||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getMongoURI(metadata *mongoDBMetadata) string {
 | 
					func getMongoConnectionString(metadata *mongoDBMetadata) string {
 | 
				
			||||||
 | 
						if metadata.ConnectionString != "" {
 | 
				
			||||||
 | 
							return metadata.ConnectionString
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(metadata.Server) != 0 {
 | 
						if len(metadata.Server) != 0 {
 | 
				
			||||||
		if metadata.Username != "" && metadata.Password != "" {
 | 
							if metadata.Username != "" && metadata.Password != "" {
 | 
				
			||||||
			return fmt.Sprintf(connectionURIFormatWithSrvAndCredentials, metadata.Username, metadata.Password, metadata.Server, metadata.DatabaseName, metadata.Params)
 | 
								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) {
 | 
					func getMongoDBClient(ctx context.Context, metadata *mongoDBMetadata) (*mongo.Client, error) {
 | 
				
			||||||
	uri := getMongoURI(metadata)
 | 
						uri := getMongoConnectionString(metadata)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Set client options
 | 
						// Set client options
 | 
				
			||||||
	clientOptions := options.Client().ApplyURI(uri)
 | 
						clientOptions := options.Client().ApplyURI(uri)
 | 
				
			||||||
| 
						 | 
					@ -468,12 +473,14 @@ func getMongoDBMetaData(meta state.Metadata) (*mongoDBMetadata, error) {
 | 
				
			||||||
		return nil, decodeErr
 | 
							return nil, decodeErr
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(m.Host) == 0 && len(m.Server) == 0 {
 | 
						if m.ConnectionString == "" {
 | 
				
			||||||
		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("must set 'host' or 'server' fields in metadata")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(m.Host) != 0 && len(m.Server) != 0 {
 | 
							if len(m.Host) != 0 && len(m.Server) != 0 {
 | 
				
			||||||
		return nil, errors.New("'host' or 'server' fields are mutually exclusive")
 | 
								return nil, errors.New("'host' or 'server' fields are mutually exclusive")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -72,7 +72,7 @@ func TestGetMongoDBMetadata(t *testing.T) {
 | 
				
			||||||
		assert.NotNil(t, err)
 | 
							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{
 | 
							properties := map[string]string{
 | 
				
			||||||
			host:           "127.0.0.2",
 | 
								host:           "127.0.0.2",
 | 
				
			||||||
			databaseName:   "TestDB",
 | 
								databaseName:   "TestDB",
 | 
				
			||||||
| 
						 | 
					@ -87,13 +87,13 @@ func TestGetMongoDBMetadata(t *testing.T) {
 | 
				
			||||||
		metadata, err := getMongoDBMetaData(m)
 | 
							metadata, err := getMongoDBMetaData(m)
 | 
				
			||||||
		assert.Nil(t, err)
 | 
							assert.Nil(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		uri := getMongoURI(metadata)
 | 
							uri := getMongoConnectionString(metadata)
 | 
				
			||||||
		expected := "mongodb://username:password@127.0.0.2/TestDB"
 | 
							expected := "mongodb://username:password@127.0.0.2/TestDB"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert.Equal(t, expected, uri)
 | 
							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{
 | 
							properties := map[string]string{
 | 
				
			||||||
			host:           "localhost:27017",
 | 
								host:           "localhost:27017",
 | 
				
			||||||
			databaseName:   "TestDB",
 | 
								databaseName:   "TestDB",
 | 
				
			||||||
| 
						 | 
					@ -106,13 +106,13 @@ func TestGetMongoDBMetadata(t *testing.T) {
 | 
				
			||||||
		metadata, err := getMongoDBMetaData(m)
 | 
							metadata, err := getMongoDBMetaData(m)
 | 
				
			||||||
		assert.Nil(t, err)
 | 
							assert.Nil(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		uri := getMongoURI(metadata)
 | 
							uri := getMongoConnectionString(metadata)
 | 
				
			||||||
		expected := "mongodb://localhost:27017/TestDB"
 | 
							expected := "mongodb://localhost:27017/TestDB"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert.Equal(t, expected, uri)
 | 
							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{
 | 
							properties := map[string]string{
 | 
				
			||||||
			host:           "127.0.0.2",
 | 
								host:           "127.0.0.2",
 | 
				
			||||||
			databaseName:   "TestDB",
 | 
								databaseName:   "TestDB",
 | 
				
			||||||
| 
						 | 
					@ -128,13 +128,13 @@ func TestGetMongoDBMetadata(t *testing.T) {
 | 
				
			||||||
		metadata, err := getMongoDBMetaData(m)
 | 
							metadata, err := getMongoDBMetaData(m)
 | 
				
			||||||
		assert.Nil(t, err)
 | 
							assert.Nil(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		uri := getMongoURI(metadata)
 | 
							uri := getMongoConnectionString(metadata)
 | 
				
			||||||
		expected := "mongodb://username:password@127.0.0.2/TestDB?ssl=true"
 | 
							expected := "mongodb://username:password@127.0.0.2/TestDB?ssl=true"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert.Equal(t, expected, uri)
 | 
							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{
 | 
							properties := map[string]string{
 | 
				
			||||||
			server:         "server.example.com",
 | 
								server:         "server.example.com",
 | 
				
			||||||
			databaseName:   "TestDB",
 | 
								databaseName:   "TestDB",
 | 
				
			||||||
| 
						 | 
					@ -148,7 +148,7 @@ func TestGetMongoDBMetadata(t *testing.T) {
 | 
				
			||||||
		metadata, err := getMongoDBMetaData(m)
 | 
							metadata, err := getMongoDBMetaData(m)
 | 
				
			||||||
		assert.Nil(t, err)
 | 
							assert.Nil(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		uri := getMongoURI(metadata)
 | 
							uri := getMongoConnectionString(metadata)
 | 
				
			||||||
		expected := "mongodb+srv://server.example.com/?ssl=true"
 | 
							expected := "mongodb+srv://server.example.com/?ssl=true"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert.Equal(t, expected, uri)
 | 
							assert.Equal(t, expected, uri)
 | 
				
			||||||
| 
						 | 
					@ -187,4 +187,24 @@ func TestGetMongoDBMetadata(t *testing.T) {
 | 
				
			||||||
		expected := "'host' or 'server' fields are mutually exclusive"
 | 
							expected := "'host' or 'server' fields are mutually exclusive"
 | 
				
			||||||
		assert.Equal(t, expected, err.Error())
 | 
							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
 | 
					  version: v1
 | 
				
			||||||
  initTimeout: 5m
 | 
					  initTimeout: 5m
 | 
				
			||||||
  metadata:
 | 
					  metadata:
 | 
				
			||||||
    - name: host
 | 
					    - name: connectionString
 | 
				
			||||||
      value: "localhost:27017"
 | 
					      value: "mongodb://localhost:27017/admin"
 | 
				
			||||||
    - name: databaseName
 | 
					    - name: databaseName
 | 
				
			||||||
      value: "admin"
 | 
					      value: "admin"
 | 
				
			||||||
    - name: writeConcern
 | 
					    - name: writeConcern
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -189,7 +189,7 @@ func TestMongoDB(t *testing.T) {
 | 
				
			||||||
		Step("Get Values Saved Earlier And Not Expired, after MongoDB restart", testGetAfterMongoDBRestart).
 | 
							Step("Get Values Saved Earlier And Not Expired, after MongoDB restart", testGetAfterMongoDBRestart).
 | 
				
			||||||
		Run()
 | 
							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").
 | 
							"write concerns and operation timeout").
 | 
				
			||||||
		Step(dockercompose.Run("mongodb", dockerComposeSingleYAML)).
 | 
							Step(dockercompose.Run("mongodb", dockerComposeSingleYAML)).
 | 
				
			||||||
		Step("Waiting for component to start...", flow.Sleep(20*time.Second)).
 | 
							Step("Waiting for component to start...", flow.Sleep(20*time.Second)).
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue