github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mongodb/config_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package mongodb
    21  
    22  import (
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestGetMongoDBMetadata(t *testing.T) {
    29  	t.Run("With defaults", func(t *testing.T) {
    30  		properties := map[string]string{
    31  			host: "127.0.0.1",
    32  		}
    33  
    34  		metadata, err := NewConfig(properties)
    35  		assert.Nil(t, err)
    36  		assert.Equal(t, properties[host], metadata.hosts[0])
    37  		assert.Equal(t, adminDatabase, metadata.databaseName)
    38  	})
    39  
    40  	t.Run("With custom values", func(t *testing.T) {
    41  		properties := map[string]string{
    42  			host:         "127.0.0.2",
    43  			databaseName: "TestDB",
    44  			username:     "username",
    45  			password:     "password",
    46  		}
    47  
    48  		metadata, err := NewConfig(properties)
    49  		assert.Nil(t, err)
    50  		assert.Equal(t, properties[host], metadata.hosts[0])
    51  		assert.Equal(t, properties[databaseName], metadata.databaseName)
    52  		assert.Equal(t, properties[username], metadata.username)
    53  		assert.Equal(t, properties[password], metadata.password)
    54  	})
    55  
    56  	t.Run("Missing hosts", func(t *testing.T) {
    57  		properties := map[string]string{
    58  			username: "username",
    59  			password: "password",
    60  		}
    61  
    62  		_, err := NewConfig(properties)
    63  		assert.NotNil(t, err)
    64  	})
    65  
    66  	t.Run("Invalid without host/server", func(t *testing.T) {
    67  		properties := map[string]string{
    68  			databaseName: "TestDB",
    69  		}
    70  
    71  		_, err := NewConfig(properties)
    72  		assert.NotNil(t, err)
    73  
    74  		expected := "must set 'host' in metadata or KB_SERVICE_PORT environment variable"
    75  		assert.Equal(t, expected, err.Error())
    76  	})
    77  }