mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
github action add testcase
This commit is contained in:
@@ -1,12 +1,44 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
namespace string
|
||||
clientset *kubernetes.Clientset
|
||||
restclient *rest.RESTClient
|
||||
config *rest.Config
|
||||
)
|
||||
|
||||
func TestPingPodIP(t *testing.T) {
|
||||
list, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), v1.ListOptions{})
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
for _, item := range list.Items {
|
||||
command := exec.Command("ping", "-c", "4", item.Status.PodIP)
|
||||
command.Run()
|
||||
if !command.ProcessState.Success() {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUDP(t *testing.T) {
|
||||
go func() {
|
||||
server()
|
||||
@@ -77,3 +109,39 @@ func server() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
initClient()
|
||||
|
||||
command := exec.Command("nhctl", "connect", "--workloads=ratings")
|
||||
c := make(chan struct{})
|
||||
_, _, _ = util.RunWithRollingOutWithChecker(command, func(log string) bool {
|
||||
ok := strings.Contains(log, "dns service ok")
|
||||
if ok {
|
||||
c <- struct{}{}
|
||||
}
|
||||
return ok
|
||||
})
|
||||
<-c
|
||||
}
|
||||
|
||||
func initClient() {
|
||||
var err error
|
||||
|
||||
configFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
|
||||
configFlags.KubeConfig = &clientcmd.RecommendedHomeFile
|
||||
f := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(configFlags))
|
||||
|
||||
if config, err = f.ToRESTConfig(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if restclient, err = rest.RESTClientFor(config); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if clientset, err = kubernetes.NewForConfig(config); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if namespace, _, err = f.ToRawKubeConfigLoader().Namespace(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
40
util/util.go
40
util/util.go
@@ -39,6 +39,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
osexec "os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -414,3 +415,42 @@ func RolloutStatus(factory cmdutil.Factory, namespace, workloads string, timeout
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func RunWithRollingOutWithChecker(cmd *osexec.Cmd, checker func(log string) bool) (string, string, error) {
|
||||
stdoutBuf := bytes.NewBuffer(make([]byte, 0))
|
||||
stderrBuf := bytes.NewBuffer(make([]byte, 0))
|
||||
|
||||
stdoutPipe, _ := cmd.StdoutPipe()
|
||||
stderrPipe, _ := cmd.StderrPipe()
|
||||
stdout := io.MultiWriter(os.Stdout, stdoutBuf)
|
||||
stderr := io.MultiWriter(os.Stderr, stderrBuf)
|
||||
go func() {
|
||||
_, _ = io.Copy(stdout, stdoutPipe)
|
||||
}()
|
||||
go func() {
|
||||
_, _ = io.Copy(stderr, stderrPipe)
|
||||
}()
|
||||
go func() {
|
||||
if checker != nil {
|
||||
for {
|
||||
if checker(stdoutBuf.String()) || checker(stderrBuf.String()) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
if err := cmd.Start(); err != nil {
|
||||
_ = cmd.Process.Kill()
|
||||
return stdoutBuf.String(), stderrBuf.String(), err
|
||||
}
|
||||
_ = cmd.Wait()
|
||||
var err error
|
||||
if !cmd.ProcessState.Success() {
|
||||
err = errors.New("exit code is not 0")
|
||||
}
|
||||
|
||||
stdoutStr := strings.TrimSpace(stdoutBuf.String())
|
||||
stderrStr := strings.TrimSpace(stderrBuf.String())
|
||||
|
||||
return stdoutStr, stderrStr, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user