github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/grpcserver/grpc_server_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 grpcserver
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"net/http"
    26  	"net/http/httptest"
    27  	"strings"
    28  
    29  	. "github.com/onsi/ginkgo/v2"
    30  	. "github.com/onsi/gomega"
    31  	health "google.golang.org/grpc/health/grpc_health_v1"
    32  
    33  	"github.com/1aal/kubeblocks/pkg/lorry/engines/custom"
    34  	"github.com/1aal/kubeblocks/pkg/lorry/engines/register"
    35  	"github.com/1aal/kubeblocks/pkg/lorry/operations"
    36  	"github.com/1aal/kubeblocks/pkg/lorry/operations/replica"
    37  	"github.com/1aal/kubeblocks/pkg/lorry/util"
    38  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    39  )
    40  
    41  var _ = Describe("GRPC Server", func() {
    42  	Context("new GRPC server", func() {
    43  		It("fail -- no check role operation", func() {
    44  			delete(operations.Operations(), strings.ToLower(string(util.CheckRoleOperation)))
    45  			_, err := NewGRPCServer()
    46  			Expect(err).Should(HaveOccurred())
    47  		})
    48  
    49  		It("success", func() {
    50  			err := operations.Register(strings.ToLower(string(util.CheckRoleOperation)), &replica.CheckRole{})
    51  			Expect(err).ShouldNot(HaveOccurred())
    52  			server, err := NewGRPCServer()
    53  			Expect(err).ShouldNot(HaveOccurred())
    54  			Expect(server).ShouldNot(BeNil())
    55  			Expect(server.Watch(nil, nil)).ShouldNot(Succeed())
    56  		})
    57  	})
    58  
    59  	Context("check role", func() {
    60  		It("role changed", func() {
    61  			// set up the host
    62  			s := httptest.NewServer(
    63  				http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    64  					_, _ = w.Write([]byte("leader"))
    65  				}),
    66  			)
    67  			addr := s.Listener.Addr().String()
    68  			index := strings.LastIndex(addr, ":")
    69  			portStr := addr[index+1:]
    70  			// set up the environment
    71  			viper.Set("KB_RSM_ACTION_SVC_LIST", "["+portStr+"]")
    72  			viper.Set("KB_RSM_ROLE_UPDATE_MECHANISM", "ReadinessProbeEventUpdate")
    73  
    74  			customManager, err := custom.NewManager(nil)
    75  			Expect(err).Should(BeNil())
    76  			register.SetDBManager(customManager)
    77  
    78  			server, _ := NewGRPCServer()
    79  			check, err := server.Check(context.Background(), nil)
    80  
    81  			Expect(err).Should(HaveOccurred())
    82  			Expect(check.Status).Should(Equal(health.HealthCheckResponse_NOT_SERVING))
    83  
    84  			// set up the expected answer
    85  			result := map[string]string{}
    86  			result["event"] = "Success"
    87  			result["operation"] = "checkRole"
    88  			result["originalRole"] = ""
    89  			result["role"] = "leader"
    90  			ans, _ := json.Marshal(result)
    91  
    92  			Expect(err.Error()).Should(Equal(string(ans)))
    93  		})
    94  	})
    95  })