github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/etcd/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 etcd
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"net"
    26  
    27  	. "github.com/onsi/ginkgo/v2"
    28  	. "github.com/onsi/gomega"
    29  	"github.com/spf13/viper"
    30  
    31  	"github.com/1aal/kubeblocks/pkg/lorry/engines"
    32  )
    33  
    34  const (
    35  	urlWithPort = "127.0.0.1:2379"
    36  )
    37  
    38  // Test case for Init() function
    39  var _ = Describe("ETCD DBManager", func() {
    40  	// Set up relevant viper config variables
    41  	viper.Set("KB_SERVICE_USER", "testuser")
    42  	viper.Set("KB_SERVICE_PASSWORD", "testpassword")
    43  	Context("new db manager", func() {
    44  		It("with rigth configurations", func() {
    45  			properties := engines.Properties{
    46  				"endpoint": urlWithPort,
    47  			}
    48  			dbManger, err := NewManager(properties)
    49  			Expect(err).Should(Succeed())
    50  			Expect(dbManger).ShouldNot(BeNil())
    51  		})
    52  
    53  		It("with wrong configurations", func() {
    54  			properties := engines.Properties{}
    55  			dbManger, err := NewManager(properties)
    56  			Expect(err).Should(HaveOccurred())
    57  			Expect(dbManger).Should(BeNil())
    58  		})
    59  	})
    60  
    61  	Context("is db startup ready", func() {
    62  		It("it is ready", func() {
    63  			etcdServer, err := StartEtcdServer()
    64  			Expect(err).Should(BeNil())
    65  			defer etcdServer.Stop()
    66  			testEndpoint := fmt.Sprintf("http://%s", etcdServer.ETCD.Clients[0].Addr().(*net.TCPAddr).String())
    67  			manager := &Manager{
    68  				etcd:     etcdServer.client,
    69  				endpoint: testEndpoint,
    70  			}
    71  			Expect(manager.IsDBStartupReady()).Should(BeTrue())
    72  		})
    73  
    74  		It("it is not ready", func() {
    75  			etcdServer, err := StartEtcdServer()
    76  			Expect(err).Should(BeNil())
    77  			etcdServer.Stop()
    78  			testEndpoint := fmt.Sprintf("http://%s", etcdServer.ETCD.Clients[0].Addr().(*net.TCPAddr).String())
    79  			properties := engines.Properties{
    80  				"endpoint": testEndpoint,
    81  			}
    82  			manager, err := NewManager(properties)
    83  			Expect(err).Should(BeNil())
    84  			Expect(manager).ShouldNot(BeNil())
    85  			Expect(manager.IsDBStartupReady()).Should(BeFalse())
    86  		})
    87  	})
    88  
    89  	Context("get replica role", func() {
    90  		It("get leader", func() {
    91  			etcdServer, err := StartEtcdServer()
    92  			Expect(err).Should(BeNil())
    93  			defer etcdServer.Stop()
    94  			testEndpoint := fmt.Sprintf("http://%s", etcdServer.ETCD.Clients[0].Addr().(*net.TCPAddr).String())
    95  			manager := &Manager{
    96  				etcd:     etcdServer.client,
    97  				endpoint: testEndpoint,
    98  			}
    99  			role, err := manager.GetReplicaRole(context.Background(), nil)
   100  			Expect(err).Should(BeNil())
   101  			Expect(role).Should(Equal("Leader"))
   102  		})
   103  	})
   104  })