Improve the UT coverage of cmd/higress. (#142)

This commit is contained in:
Kent Dong
2023-01-23 16:35:41 +08:00
committed by GitHub
parent 9593cb7340
commit 4fb9efe3bf
6 changed files with 101 additions and 4 deletions

View File

@@ -34,6 +34,14 @@ var (
serverArgs *bootstrap.ServerArgs
loggingOptions = log.DefaultOptions()
serverProvider = func(args *bootstrap.ServerArgs) (bootstrap.ServerInterface, error) {
return bootstrap.NewServer(serverArgs)
}
waitForMonitorSignal = func(stop chan struct{}) {
cmd.WaitSignal(stop)
}
rootCmd = &cobra.Command{
Use: "higress",
}
@@ -52,7 +60,7 @@ var (
stop := make(chan struct{})
server, err := bootstrap.NewServer(serverArgs)
server, err := serverProvider(serverArgs)
if err != nil {
return fmt.Errorf("fail to create higress server: %v", err)
}
@@ -61,7 +69,7 @@ var (
return fmt.Errorf("fail to start higress server: %v", err)
}
cmd.WaitSignal(stop)
waitForMonitorSignal(stop)
server.WaitUntilCompletion()
return nil

78
cmd/higress/main_test.go Normal file
View File

@@ -0,0 +1,78 @@
// 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 main
import (
"github.com/alibaba/higress/pkg/bootstrap"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
)
func TestServe(t *testing.T) {
runEBackup := serveCmd.RunE
argsBackup := os.Args
serverProviderBackup := serverProvider
executed := false
serverProvider = func(args *bootstrap.ServerArgs) (bootstrap.ServerInterface, error) {
return &delayedServer{Args: args, Delay: time.Second * 5}, nil
}
serveCmd.RunE = func(cmd *cobra.Command, args []string) error {
executed = true
return runEBackup(cmd, args)
}
defer func() {
serverProvider = serverProviderBackup
os.Args = argsBackup
serveCmd.RunE = runEBackup
}()
a := assert.New(t)
delay := time.Second * 5
start := time.Now()
os.Args = []string{"/app/higress", "serve"}
waitForMonitorSignal = func(stop chan struct{}) {
time.Sleep(delay)
close(stop)
}
main()
end := time.Now()
cost := end.Sub(start)
a.GreaterOrEqual(cost, delay)
a.True(executed)
}
type delayedServer struct {
Args *bootstrap.ServerArgs
Delay time.Duration
stop <-chan struct{}
}
func (d *delayedServer) Start(stop <-chan struct{}) error {
d.stop = stop
return nil
}
func (d *delayedServer) WaitUntilCompletion() {
<-d.stop
}