Skip to content

Commit

Permalink
UT: Increase unit test coverage of pkg/bootstrap (alibaba#101)
Browse files Browse the repository at this point in the history
Signed-off-by: charlie <[email protected]>
  • Loading branch information
Charlie17Li authored Jan 14, 2023
1 parent 926f858 commit 616b37a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ replace github.com/docker/docker => github.com/moby/moby v17.12.0-ce-rc1.0.20200
replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.5

require (
github.com/agiledragon/gomonkey/v2 v2.2.0
github.com/dubbogo/go-zookeeper v1.0.4-0.20211212162352-f9d2183d89d5
github.com/dubbogo/gost v1.13.1
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrU
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/agiledragon/gomonkey/v2 v2.2.0 h1:QJWqpdEhGV/JJy70sZ/LDnhbSlMrqHAWHcNOjz1kyuI=
github.com/agiledragon/gomonkey/v2 v2.2.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/ahmetb/gen-crd-api-reference-docs v0.3.0/go.mod h1:TdjdkYhlOifCQWPs1UdTma97kQQMozf5h26hTuG70u8=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
13 changes: 6 additions & 7 deletions pkg/bootstrap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ func (s *Server) initConfigController() error {

// Defer starting the controller until after the service is created.
s.server.RunComponent(func(stop <-chan struct{}) error {
ingressConfig.InitializeCluster(ingressController, stop)
if err := ingressConfig.InitializeCluster(ingressController, stop); err != nil {
return err
}
go s.configController.Run(stop)
return nil
})
Expand Down Expand Up @@ -322,8 +324,7 @@ func (s *Server) initXdsServer() error {
s.xdsServer.Start(stop)
return nil
})
s.initGrpcServer()
return nil
return s.initGrpcServer()
}

func (s *Server) initGrpcServer() error {
Expand Down Expand Up @@ -381,6 +382,7 @@ func (s *Server) initHttpServer() error {
return nil
}

// readyHandler checks whether the http server is ready
func (s *Server) readyHandler(w http.ResponseWriter, _ *http.Request) {
for name, fn := range s.readinessProbes {
if ready, err := fn(); !ready {
Expand All @@ -394,10 +396,7 @@ func (s *Server) readyHandler(w http.ResponseWriter, _ *http.Request) {

// cachesSynced checks whether caches have been synced.
func (s *Server) cachesSynced() bool {
if !s.configController.HasSynced() {
return false
}
return true
return s.configController.HasSynced()
}

func (s *Server) waitForCacheSync(stop <-chan struct{}) bool {
Expand Down
66 changes: 66 additions & 0 deletions pkg/bootstrap/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bootstrap

import (
"context"
"testing"

"github.com/agiledragon/gomonkey/v2"
"istio.io/istio/pilot/pkg/features"
"istio.io/istio/pkg/keepalive"

higresskube "github.com/alibaba/higress/pkg/kube"
)

func TestStartWithNoError(t *testing.T) {
var (
s *Server
err error
)

mockFn := func(s *Server) error {
s.kubeClient = higresskube.NewFakeClient()
return nil
}

gomonkey.ApplyFunc((*Server).initKubeClient, mockFn)

if s, err = NewServer(newServerArgs()); err != nil {
t.Errorf("failed to create server: %v", err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err = s.Start(ctx.Done()); err != nil {
t.Errorf("failed to start the server: %v", err)
}
}

func newServerArgs() *ServerArgs {
return &ServerArgs{
Debug: true,
NativeIstio: true,
HttpAddress: ":8888",
GrpcAddress: ":15051",
GrpcKeepAliveOptions: keepalive.DefaultOption(),
XdsOptions: XdsOptions{
DebounceAfter: features.DebounceAfter,
DebounceMax: features.DebounceMax,
EnableEDSDebounce: features.EnableEDSDebounce,
},
}
}
1 change: 1 addition & 0 deletions pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func NewFakeClient(objects ...runtime.Object) Client {
}
c.higress = higressfake.NewSimpleClientset()
c.higressInformer = higressinformer.NewSharedInformerFactoryWithOptions(c.higress, resyncInterval)
c.informerWatchesPending = atomic.NewInt32(0)

// https://github.com/kubernetes/kubernetes/issues/95372
// There is a race condition in the client fakes, where events that happen between the List and Watch
Expand Down

0 comments on commit 616b37a

Please sign in to comment.