github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/custom/manager_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 custom 21 22 import ( 23 "context" 24 "encoding/json" 25 "fmt" 26 "net/http" 27 "net/http/httptest" 28 "strconv" 29 "strings" 30 31 . "github.com/onsi/ginkgo/v2" 32 . "github.com/onsi/gomega" 33 34 "github.com/1aal/kubeblocks/pkg/common" 35 "github.com/1aal/kubeblocks/pkg/lorry/engines" 36 viper "github.com/1aal/kubeblocks/pkg/viperx" 37 ) 38 39 var _ = Describe("ETCD DBManager", func() { 40 // Set up relevant viper config variables 41 Context("new db manager", func() { 42 It("with rigth configurations", func() { 43 viper.Set("KB_RSM_ACTION_SVC_LIST", "[3502]") 44 properties := engines.Properties{} 45 dbManger, err := NewManager(properties) 46 Expect(err).Should(Succeed()) 47 Expect(dbManger).ShouldNot(BeNil()) 48 }) 49 50 It("with wrong configurations", func() { 51 viper.Set("KB_RSM_ACTION_SVC_LIST", "wrong-setting") 52 properties := engines.Properties{} 53 dbManger, err := NewManager(properties) 54 Expect(err).Should(HaveOccurred()) 55 Expect(dbManger).Should(BeNil()) 56 }) 57 }) 58 59 Context("global role snapshot", func() { 60 It("success", func() { 61 _ = setUpHost() 62 manager, err := NewManager(nil) 63 Expect(err).Should(BeNil()) 64 globalRole, err := manager.GetReplicaRole(context.TODO(), nil) 65 Expect(err).Should(BeNil()) 66 snapshot := &common.GlobalRoleSnapshot{} 67 Expect(json.Unmarshal([]byte(globalRole), snapshot)).Should(Succeed()) 68 Expect(snapshot.PodRoleNamePairs).Should(HaveLen(3)) 69 Expect(snapshot.Version).Should(Equal("1")) 70 }) 71 }) 72 }) 73 74 func setUpHost() *httptest.Server { 75 var lines []string 76 for i := 0; i < 3; i++ { 77 podName := "pod-" + strconv.Itoa(i) 78 var role string 79 if i == 0 { 80 role = "leader" 81 } else { 82 role = "follower" 83 } 84 lines = append(lines, fmt.Sprintf("%d,%s,%s", 1, podName, role)) 85 } 86 respContent := strings.Join(lines, "\n") 87 88 s := httptest.NewServer( 89 http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 90 _, _ = w.Write([]byte(respContent)) 91 }), 92 ) 93 addr := s.Listener.Addr().String() 94 index := strings.LastIndex(addr, ":") 95 portStr := addr[index+1:] 96 viper.Set("KB_RSM_ACTION_SVC_LIST", "["+portStr+"]") 97 return s 98 }