github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/postgres/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 postgres 21 22 import ( 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 27 "github.com/1aal/kubeblocks/pkg/lorry/dcs" 28 viper "github.com/1aal/kubeblocks/pkg/viperx" 29 ) 30 31 func TestGetPostgresqlMetadata(t *testing.T) { 32 t.Run("With defaults", func(t *testing.T) { 33 properties := map[string]string{ 34 ConnectionURLKey: "user=postgres password=docker host=localhost port=5432 dbname=postgres pool_min_conns=1 pool_max_conns=10", 35 } 36 37 metadata, err := NewConfig(properties) 38 assert.Nil(t, err) 39 assert.Equal(t, "postgres", metadata.Username) 40 assert.Equal(t, "docker", metadata.Password) 41 assert.Equal(t, "localhost", metadata.Host) 42 assert.Equal(t, 5432, metadata.Port) 43 assert.Equal(t, "postgres", metadata.Database) 44 assert.Equal(t, int32(1), metadata.MinConnections) 45 assert.Equal(t, int32(10), metadata.MaxConnections) 46 }) 47 48 t.Run("url not set", func(t *testing.T) { 49 properties := map[string]string{} 50 51 _, err := NewConfig(properties) 52 assert.NotNil(t, err) 53 }) 54 55 t.Run("pool max connection too small", func(t *testing.T) { 56 properties := map[string]string{ 57 ConnectionURLKey: "user=postgres password=docker host=localhost port=5432 dbname=postgres pool_min_conns=1 pool_max_conns=0", 58 } 59 60 _, err := NewConfig(properties) 61 assert.NotNil(t, err) 62 }) 63 64 t.Run("set env", func(t *testing.T) { 65 viper.Set("KB_SERVICE_USER", "test") 66 viper.Set("KB_SERVICE_PASSWORD", "test_pwd") 67 properties := map[string]string{ 68 ConnectionURLKey: "user=postgres password=docker host=localhost port=5432 dbname=postgres pool_min_conns=1 pool_max_conns=10", 69 } 70 metadata, err := NewConfig(properties) 71 assert.Nil(t, err) 72 73 assert.Equal(t, metadata.Username, "test") 74 assert.Equal(t, metadata.Password, "test_pwd") 75 }) 76 } 77 78 func TestConfigFunc(t *testing.T) { 79 properties := map[string]string{ 80 ConnectionURLKey: "user=postgres password=docker host=localhost port=5432 dbname=postgres pool_min_conns=1 pool_max_conns=10", 81 } 82 metadata, err := NewConfig(properties) 83 assert.NotNil(t, metadata) 84 assert.Nil(t, err) 85 86 t.Run("get db port", func(t *testing.T) { 87 port := metadata.GetDBPort() 88 assert.Equal(t, port, 5432) 89 90 metadata.Port = 0 91 port = metadata.GetDBPort() 92 assert.Equal(t, port, 5432) 93 }) 94 95 t.Run("get consensus IP port", func(t *testing.T) { 96 cluster := &dcs.Cluster{ 97 ClusterCompName: "test", 98 Namespace: "default", 99 } 100 101 consensusIPPort := metadata.GetConsensusIPPort(cluster, "test") 102 assert.Equal(t, consensusIPPort, "test.test-headless.default.svc:15432") 103 }) 104 }