mirror of
https://github.com/alibaba/higress.git
synced 2026-06-08 12:17:28 +08:00
Improve the UT coverage of cmd/higress. (#142)
This commit is contained in:
@@ -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
78
cmd/higress/main_test.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user