feat: sync mode modify api server & add ut for mode run and mode sync (#704)

* feat: mode sync modify kubeconfig apiserver to in cluster apiserver
* feat: add ut for sync mode and run mode
* fix: bugs
This commit is contained in:
naison
2025-08-21 22:45:47 +08:00
committed by GitHub
parent 4df63d1642
commit 98c4a61ca1
29 changed files with 1787 additions and 393 deletions

View File

@@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"reflect"
"runtime"
"sync"
"testing"
"time"
@@ -30,101 +31,150 @@ import (
"github.com/wencaiwulue/kubevpn/v2/pkg/util"
)
var (
type ut struct {
namespace string
clientset *kubernetes.Clientset
restconfig *rest.Config
)
}
const (
local = `{"status": "Reviews is healthy on local pc"}`
local8080 = `{"status": "Reviews is healthy on local pc 8080"}`
remote = `{"status": "Reviews is healthy"}`
local = `{"status": "Reviews is healthy on local pc"}`
local8080 = `{"status": "Reviews is healthy on local pc 8080"}`
remote = `{"status": "Reviews is healthy"}`
remoteSyncOrigin = `{"status":"Authors is healthy"}`
remoteSyncPod = `{"status":"Authors is healthy in pod"}`
)
func TestFunctions(t *testing.T) {
u := &ut{}
// 1) test connect
t.Run("init", Init)
t.Run("kubevpnConnect", kubevpnConnect)
t.Run("commonTest", commonTest)
t.Run("checkConnectStatus", checkConnectStatus)
t.Run("init", u.init)
t.Run("kubevpnConnect", u.kubevpnConnect)
t.Run("commonTest", u.commonTest)
t.Run("checkConnectStatus", u.checkConnectStatus)
// 2) test proxy mode
t.Run("kubevpnProxy", kubevpnProxy)
t.Run("commonTest", commonTest)
t.Run("testUDP", testUDP)
t.Run("proxyServiceReviewsServiceIP", proxyServiceReviewsServiceIP)
t.Run("proxyServiceReviewsPodIP", proxyServiceReviewsPodIP)
t.Run("checkProxyStatus", checkProxyStatus)
t.Run("kubevpnProxy", u.kubevpnProxy)
t.Run("commonTest", u.commonTest)
t.Run("testUDP", u.testUDP)
t.Run("proxyServiceReviewsServiceIP", u.proxyServiceReviewsServiceIP)
t.Run("proxyServiceReviewsPodIP", u.proxyServiceReviewsPodIP)
t.Run("checkProxyStatus", u.checkProxyStatus)
// 3) test proxy mode with service mesh
t.Run("kubevpnLeave", kubevpnLeave)
t.Run("kubevpnProxyWithServiceMesh", kubevpnProxyWithServiceMesh)
t.Run("commonTest", commonTest)
t.Run("serviceMeshReviewsServiceIP", serviceMeshReviewsServiceIP)
t.Run("serviceMeshReviewsPodIP", serviceMeshReviewsPodIP)
t.Run("checkProxyWithServiceMeshStatus", checkProxyWithServiceMeshStatus)
t.Run("kubevpnLeave", u.kubevpnLeave)
t.Run("kubevpnProxyWithServiceMesh", u.kubevpnProxyWithServiceMesh)
t.Run("commonTest", u.commonTest)
t.Run("serviceMeshReviewsServiceIP", u.serviceMeshReviewsServiceIP)
t.Run("serviceMeshReviewsPodIP", u.serviceMeshReviewsPodIP)
t.Run("checkProxyWithServiceMeshStatus", u.checkProxyWithServiceMeshStatus)
// 4) test proxy mode with service mesh and gvisor
t.Run("kubevpnLeave", kubevpnLeave)
t.Run("kubevpnUninstall", kubevpnUninstall)
t.Run("kubevpnProxyWithServiceMeshAndGvisorMode", kubevpnProxyWithServiceMeshAndGvisorMode)
t.Run("commonTest", commonTest)
t.Run("serviceMeshReviewsServiceIP", serviceMeshReviewsServiceIP)
t.Run("checkProxyWithServiceMeshAndGvisorStatus", checkProxyWithServiceMeshAndGvisorStatus)
t.Run("kubevpnLeaveService", kubevpnLeaveService)
t.Run("kubevpnQuit", kubevpnQuit)
t.Run("kubevpnLeave", u.kubevpnLeave)
t.Run("kubevpnUninstall", u.kubevpnUninstall)
t.Run("kubevpnProxyWithServiceMeshAndFargateMode", u.kubevpnProxyWithServiceMeshAndFargateMode)
t.Run("commonTest", u.commonTest)
t.Run("serviceMeshReviewsServiceIP", u.serviceMeshReviewsServiceIP)
t.Run("checkProxyWithServiceMeshAndGvisorStatus", u.checkProxyWithServiceMeshAndGvisorStatus)
t.Run("kubevpnLeaveService", u.kubevpnLeaveService)
t.Run("kubevpnQuit", u.kubevpnQuit)
// 5) install centrally in ns test -- connect mode
t.Run("centerKubevpnUninstall", kubevpnUninstall)
t.Run("centerKubevpnInstallInNsKubevpn", kubevpnConnectToNsKubevpn)
t.Run("centerKubevpnConnect", kubevpnConnect)
t.Run("checkServiceShouldNotInNsDefault", checkServiceShouldNotInNsDefault)
t.Run("centerCheckConnectStatus", centerCheckConnectStatus)
t.Run("centerCommonTest", commonTest)
// 5) test mode sync
t.Run("deleteDeployForSaveResource", u.deleteDeployForSaveResource)
t.Run("kubevpnSyncWithFullProxy", u.kubevpnSyncWithFullProxy)
t.Run("kubevpnSyncWithFullProxyStatus", u.checkSyncWithFullProxyStatus)
t.Run("commonTest", u.commonTest)
t.Run("kubevpnUnSync", u.kubevpnUnSync)
t.Run("kubevpnSyncWithServiceMesh", u.kubevpnSyncWithServiceMesh)
t.Run("kubevpnSyncWithServiceMeshStatus", u.checkSyncWithServiceMeshStatus)
t.Run("commonTest", u.commonTest)
t.Run("kubevpnUnSync", u.kubevpnUnSync)
// 6) install centrally in ns test -- proxy mode
t.Run("centerKubevpnProxy", kubevpnProxy)
t.Run("checkServiceShouldNotInNsDefault", checkServiceShouldNotInNsDefault)
t.Run("centerCommonTest", commonTest)
t.Run("centerTestUDP", testUDP)
t.Run("centerProxyServiceReviewsServiceIP", proxyServiceReviewsServiceIP)
t.Run("centerProxyServiceReviewsPodIP", proxyServiceReviewsPodIP)
t.Run("centerCheckProxyStatus", centerCheckProxyStatus)
// 6) test mode run
// because of:
// Run container with cmd: [docker run --env-file /tmp/623917040.env --domainname --workdir --cap-add SYS_PTRACE --cap-add SYS_ADMIN --cap-add SYS_PTRACE --cap-add SYS_ADMIN --security-opt apparmor=unconfined --security-opt seccomp=unconfined --pull missing --name default_authors_716db --user root --env LC_ALL=C.UTF-8 --label app=authors --volume /tmp/329021857635767916:/var/run/secrets/kubernetes.io/serviceaccount --network container:default_nginx_45ee1 --pid container:default_nginx_45ee1 --pull missing --attach STDIN --attach STDOUT --attach STDERR --interactive --privileged --volume /tmp/TestFunctionskubevpnRunWithFullProxy2095435677/001:/app/test --rm --entrypoint go ghcr.io/kubenetworks/authors:latest run /app/test/main.go]
// Error: stat /app/test/main.go: no such file or directory
if runtime.GOOS != "darwin" {
t.Run("resetDeployAuthors", u.resetDeployAuthors)
t.Run("kubevpnRunWithFullProxy", u.kubevpnRunWithFullProxy)
t.Run("kubevpnRunWithServiceMesh", u.kubevpnRunWithServiceMesh)
t.Run("kubevpnQuit", u.kubevpnQuit)
}
// 7) install centrally in ns test -- proxy mode with service mesh
t.Run("kubevpnLeave", kubevpnLeave)
t.Run("kubevpnProxyWithServiceMesh", kubevpnProxyWithServiceMesh)
t.Run("checkServiceShouldNotInNsDefault", checkServiceShouldNotInNsDefault)
t.Run("commonTest", commonTest)
t.Run("serviceMeshReviewsServiceIP", serviceMeshReviewsServiceIP)
t.Run("serviceMeshReviewsPodIP", serviceMeshReviewsPodIP)
t.Run("centerCheckProxyWithServiceMeshStatus", centerCheckProxyWithServiceMeshStatus)
// 7) install centrally in ns test -- connect mode
t.Run("centerKubevpnUninstall", u.kubevpnUninstall)
t.Run("centerKubevpnInstallInNsKubevpn", u.kubevpnConnectToNsKubevpn)
t.Run("centerKubevpnConnect", u.kubevpnConnect)
t.Run("checkServiceShouldNotInNsDefault", u.checkServiceShouldNotInNsDefault)
t.Run("centerCheckConnectStatus", u.centerCheckConnectStatus)
t.Run("centerCommonTest", u.commonTest)
// 8) install centrally in ns test -- proxy mode with service mesh and gvisor
t.Run("kubevpnQuit", kubevpnQuit)
t.Run("kubevpnProxyWithServiceMeshAndK8sServicePortMap", kubevpnProxyWithServiceMeshAndK8sServicePortMap)
t.Run("checkServiceShouldNotInNsDefault", checkServiceShouldNotInNsDefault)
t.Run("commonTest", commonTest)
t.Run("serviceMeshReviewsServiceIPPortMap", serviceMeshReviewsServiceIPPortMap)
t.Run("kubevpnLeave", kubevpnLeave)
t.Run("centerCheckProxyWithServiceMeshAndGvisorStatus", centerCheckProxyWithServiceMeshAndGvisorStatus)
t.Run("kubevpnLeaveService", kubevpnLeaveService)
t.Run("kubevpnQuit", kubevpnQuit)
// 8) install centrally in ns test -- proxy mode
t.Run("centerKubevpnProxy", u.kubevpnProxy)
t.Run("checkServiceShouldNotInNsDefault", u.checkServiceShouldNotInNsDefault)
t.Run("centerCommonTest", u.commonTest)
t.Run("centerTestUDP", u.testUDP)
t.Run("centerProxyServiceReviewsServiceIP", u.proxyServiceReviewsServiceIP)
t.Run("centerProxyServiceReviewsPodIP", u.proxyServiceReviewsPodIP)
t.Run("centerCheckProxyStatus", u.centerCheckProxyStatus)
// 9) install centrally in ns test -- proxy mode with service mesh
t.Run("kubevpnLeave", u.kubevpnLeave)
t.Run("kubevpnProxyWithServiceMesh", u.kubevpnProxyWithServiceMesh)
t.Run("checkServiceShouldNotInNsDefault", u.checkServiceShouldNotInNsDefault)
t.Run("commonTest", u.commonTest)
t.Run("serviceMeshReviewsServiceIP", u.serviceMeshReviewsServiceIP)
t.Run("serviceMeshReviewsPodIP", u.serviceMeshReviewsPodIP)
t.Run("centerCheckProxyWithServiceMeshStatus", u.centerCheckProxyWithServiceMeshStatus)
// 10) install centrally in ns test -- proxy mode with service mesh and gvisor
t.Run("kubevpnQuit", u.kubevpnQuit)
t.Run("kubevpnProxyWithServiceMeshAndK8sServicePortMap", u.kubevpnProxyWithServiceMeshAndK8sServicePortMap)
t.Run("checkServiceShouldNotInNsDefault", u.checkServiceShouldNotInNsDefault)
t.Run("commonTest", u.commonTest)
t.Run("serviceMeshReviewsServiceIPPortMap", u.serviceMeshReviewsServiceIPPortMap)
t.Run("kubevpnLeave", u.kubevpnLeave)
t.Run("centerCheckProxyWithServiceMeshAndGvisorStatus", u.centerCheckProxyWithServiceMeshAndGvisorStatus)
t.Run("kubevpnLeaveService", u.kubevpnLeaveService)
t.Run("kubevpnQuit", u.kubevpnQuit)
// 11) test mode sync
t.Run("kubevpnSyncWithFullProxy", u.kubevpnSyncWithFullProxy)
t.Run("checkServiceShouldNotInNsDefault", u.checkServiceShouldNotInNsDefault)
t.Run("kubevpnSyncWithFullProxyStatus", u.checkSyncWithFullProxyStatus)
t.Run("commonTest", u.commonTest)
t.Run("kubevpnUnSync", u.kubevpnUnSync)
t.Run("kubevpnSyncWithServiceMesh", u.kubevpnSyncWithServiceMesh)
t.Run("checkServiceShouldNotInNsDefault", u.checkServiceShouldNotInNsDefault)
t.Run("kubevpnSyncWithServiceMeshStatus", u.checkSyncWithServiceMeshStatus)
t.Run("commonTest", u.commonTest)
t.Run("kubevpnUnSync", u.kubevpnUnSync)
t.Run("kubevpnQuit", u.kubevpnQuit)
// 12) test mode run
// because of:
// Run container with cmd: [docker run --env-file /tmp/623917040.env --domainname --workdir --cap-add SYS_PTRACE --cap-add SYS_ADMIN --cap-add SYS_PTRACE --cap-add SYS_ADMIN --security-opt apparmor=unconfined --security-opt seccomp=unconfined --pull missing --name default_authors_716db --user root --env LC_ALL=C.UTF-8 --label app=authors --volume /tmp/329021857635767916:/var/run/secrets/kubernetes.io/serviceaccount --network container:default_nginx_45ee1 --pid container:default_nginx_45ee1 --pull missing --attach STDIN --attach STDOUT --attach STDERR --interactive --privileged --volume /tmp/TestFunctionskubevpnRunWithFullProxy2095435677/001:/app/test --rm --entrypoint go ghcr.io/kubenetworks/authors:latest run /app/test/main.go]
// Error: stat /app/test/main.go: no such file or directory
if runtime.GOOS != "darwin" {
t.Run("resetDeployAuthors", u.resetDeployAuthors)
t.Run("kubevpnRunWithFullProxy", u.kubevpnRunWithFullProxy)
t.Run("kubevpnRunWithServiceMesh", u.kubevpnRunWithServiceMesh)
t.Run("kubevpnQuit", u.kubevpnQuit)
}
}
func commonTest(t *testing.T) {
func (u *ut) commonTest(t *testing.T) {
// 1) test domain access
t.Run("kubevpnStatus", kubevpnStatus)
t.Run("pingPodIP", pingPodIP)
t.Run("healthCheckPodDetails", healthCheckPodDetails)
t.Run("healthCheckServiceDetails", healthCheckServiceDetails)
t.Run("shortDomainDetails", shortDomainDetails)
t.Run("fullDomainDetails", fullDomainDetails)
t.Run("kubevpnStatus", u.kubevpnStatus)
t.Run("pingPodIP", u.pingPodIP)
t.Run("healthCheckPodDetails", u.healthCheckPodDetails)
t.Run("healthCheckServiceDetails", u.healthCheckServiceDetails)
t.Run("shortDomainDetails", u.shortDomainDetails)
t.Run("fullDomainDetails", u.fullDomainDetails)
}
func pingPodIP(t *testing.T) {
list, err := clientset.CoreV1().Pods(namespace).List(context.Background(), v1.ListOptions{})
func (u *ut) pingPodIP(t *testing.T) {
list, err := u.clientset.CoreV1().Pods(u.namespace).List(context.Background(), v1.ListOptions{})
if err != nil {
t.Fatal(err)
}
@@ -149,23 +199,23 @@ func pingPodIP(t *testing.T) {
}
}
t.Errorf("Failed to ping IP: %s of pod: %s", item.Status.PodIP, item.Name)
kubectl(t)
u.kubectl(t)
}()
}
wg.Wait()
}
func healthCheckPodDetails(t *testing.T) {
func (u *ut) healthCheckPodDetails(t *testing.T) {
var app = "details"
ip, err := getPodIP(app)
ip, err := u.getPodIP(app)
if err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("http://%s:%v/health", ip, 9080)
healthChecker(t, endpoint, nil, "")
u.healthChecker(t, endpoint, nil, "")
}
func healthChecker(t *testing.T, endpoint string, header map[string]string, keyword string) {
func (u *ut) healthChecker(t *testing.T, endpoint string, header map[string]string, keyword string) {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
t.Fatal(err)
@@ -176,7 +226,7 @@ func healthChecker(t *testing.T, endpoint string, header map[string]string, keyw
client := &http.Client{Timeout: time.Second * 1}
err = retry.OnError(
wait.Backoff{Duration: time.Second, Factor: 1, Jitter: 0, Steps: 120},
wait.Backoff{Duration: time.Second, Factor: 1, Jitter: 0, Steps: 240},
func(err error) bool { return err != nil },
func() error {
var resp *http.Response
@@ -208,76 +258,76 @@ func healthChecker(t *testing.T, endpoint string, header map[string]string, keyw
},
)
if err != nil {
kubectl(t)
u.kubectl(t)
t.Fatal(err)
}
}
func healthCheckServiceDetails(t *testing.T) {
func (u *ut) healthCheckServiceDetails(t *testing.T) {
var app = "details"
ip, err := getServiceIP(app)
ip, err := u.getServiceIP(app)
if err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("http://%s:%v/health", ip, 9080)
healthChecker(t, endpoint, nil, "")
u.healthChecker(t, endpoint, nil, "")
}
func shortDomainDetails(t *testing.T) {
func (u *ut) shortDomainDetails(t *testing.T) {
var app = "details"
endpoint := fmt.Sprintf("http://%s:%v/health", app, 9080)
healthChecker(t, endpoint, nil, "")
u.healthChecker(t, endpoint, nil, "")
}
func fullDomainDetails(t *testing.T) {
func (u *ut) fullDomainDetails(t *testing.T) {
var app = "details"
domains := []string{
fmt.Sprintf("%s.%s.svc.cluster.local", app, namespace),
fmt.Sprintf("%s.%s.svc", app, namespace),
fmt.Sprintf("%s.%s", app, namespace),
fmt.Sprintf("%s.%s.svc.cluster.local", app, u.namespace),
fmt.Sprintf("%s.%s.svc", app, u.namespace),
fmt.Sprintf("%s.%s", app, u.namespace),
}
for _, domain := range domains {
endpoint := fmt.Sprintf("http://%s:%v/health", domain, 9080)
healthChecker(t, endpoint, nil, "")
u.healthChecker(t, endpoint, nil, "")
}
}
func serviceMeshReviewsPodIP(t *testing.T) {
func (u *ut) serviceMeshReviewsPodIP(t *testing.T) {
app := "reviews"
ip, err := getPodIP(app)
ip, err := u.getPodIP(app)
if err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("http://%s:%v/health", ip, 9080)
healthChecker(t, endpoint, nil, remote)
healthChecker(t, endpoint, map[string]string{"env": "test"}, local)
u.healthChecker(t, endpoint, nil, remote)
u.healthChecker(t, endpoint, map[string]string{"env": "test"}, local)
}
func serviceMeshReviewsServiceIP(t *testing.T) {
func (u *ut) serviceMeshReviewsServiceIP(t *testing.T) {
app := "reviews"
ip, err := getServiceIP(app)
ip, err := u.getServiceIP(app)
if err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("http://%s:%v/health", ip, 9080)
healthChecker(t, endpoint, nil, remote)
healthChecker(t, endpoint, map[string]string{"env": "test"}, local)
u.healthChecker(t, endpoint, nil, remote)
u.healthChecker(t, endpoint, map[string]string{"env": "test"}, local)
}
func serviceMeshReviewsServiceIPPortMap(t *testing.T) {
func (u *ut) serviceMeshReviewsServiceIPPortMap(t *testing.T) {
app := "reviews"
ip, err := getServiceIP(app)
ip, err := u.getServiceIP(app)
if err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("http://%s:%v/health", ip, 9080)
healthChecker(t, endpoint, nil, remote)
healthChecker(t, endpoint, map[string]string{"env": "test"}, local8080)
u.healthChecker(t, endpoint, nil, remote)
u.healthChecker(t, endpoint, map[string]string{"env": "test"}, local8080)
}
func getServiceIP(app string) (string, error) {
serviceList, err := clientset.CoreV1().Services(namespace).List(context.Background(), v1.ListOptions{
func (u *ut) getServiceIP(app string) (string, error) {
serviceList, err := u.clientset.CoreV1().Services(u.namespace).List(context.Background(), v1.ListOptions{
LabelSelector: fields.OneTermEqualSelector("app", app).String(),
})
if err != nil {
@@ -293,19 +343,19 @@ func getServiceIP(app string) (string, error) {
return "", fmt.Errorf("failed to found service ip for service %s", app)
}
func proxyServiceReviewsPodIP(t *testing.T) {
func (u *ut) proxyServiceReviewsPodIP(t *testing.T) {
app := "reviews"
ip, err := getPodIP(app)
ip, err := u.getPodIP(app)
if err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("http://%s:%v/health", ip, 9080)
healthChecker(t, endpoint, nil, local)
healthChecker(t, endpoint, map[string]string{"env": "test"}, local)
u.healthChecker(t, endpoint, nil, local)
u.healthChecker(t, endpoint, map[string]string{"env": "test"}, local)
}
func getPodIP(app string) (string, error) {
list, err := clientset.CoreV1().Pods(namespace).List(context.Background(), v1.ListOptions{
func (u *ut) getPodIP(app string) (string, error) {
list, err := u.clientset.CoreV1().Pods(u.namespace).List(context.Background(), v1.ListOptions{
LabelSelector: fields.OneTermEqualSelector("app", app).String(),
})
if err != nil {
@@ -320,24 +370,24 @@ func getPodIP(app string) (string, error) {
return "", fmt.Errorf("failed to found pod ip for service %s", app)
}
func proxyServiceReviewsServiceIP(t *testing.T) {
func (u *ut) proxyServiceReviewsServiceIP(t *testing.T) {
app := "reviews"
ip, err := getServiceIP(app)
ip, err := u.getServiceIP(app)
if err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("http://%s:%v/health", ip, 9080)
healthChecker(t, endpoint, nil, local)
healthChecker(t, endpoint, map[string]string{"env": "test"}, local)
u.healthChecker(t, endpoint, nil, local)
u.healthChecker(t, endpoint, map[string]string{"env": "test"}, local)
}
func testUDP(t *testing.T) {
func (u *ut) testUDP(t *testing.T) {
app := "reviews"
port, err := util.GetAvailableUDPPortOrDie()
if err != nil {
t.Fatal(err)
}
go udpServer(t, port)
go u.udpServer(t, port)
var ip string
err = retry.OnError(
@@ -346,19 +396,19 @@ func testUDP(t *testing.T) {
return err != nil
},
func() error {
ip, err = getPodIP(app)
ip, err = u.getPodIP(app)
if err != nil {
t.Fatal(err)
}
t.Logf("Dail udp to IP: %s", ip)
return udpClient(t, ip, port)
return u.udpClient(t, ip, port)
})
if err != nil {
t.Fatalf("Failed to access pod IP: %s, port: %v", ip, port)
}
}
func udpClient(t *testing.T, ip string, port int) error {
func (u *ut) udpClient(t *testing.T, ip string, port int) error {
udpConn, err := net.DialUDP("udp4", nil, &net.UDPAddr{
IP: net.ParseIP(ip),
Port: port,
@@ -390,7 +440,7 @@ func udpClient(t *testing.T, ip string, port int) error {
return nil
}
func udpServer(t *testing.T, port int) {
func (u *ut) udpServer(t *testing.T, port int) {
// 创建监听
udpConn, err := net.ListenUDP("udp4", &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
@@ -420,7 +470,7 @@ func udpServer(t *testing.T, port int) {
}
}
func kubevpnConnect(t *testing.T) {
func (u *ut) kubevpnConnect(t *testing.T) {
cmd := exec.Command("kubevpn", "connect", "--debug")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -430,8 +480,8 @@ func kubevpnConnect(t *testing.T) {
}
}
func kubevpnConnectToNsKubevpn(t *testing.T) {
_, err := clientset.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
func (u *ut) kubevpnConnectToNsKubevpn(t *testing.T) {
_, err := u.clientset.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: "kubevpn",
},
@@ -451,7 +501,7 @@ func kubevpnConnectToNsKubevpn(t *testing.T) {
}
}
func kubevpnProxy(t *testing.T) {
func (u *ut) kubevpnProxy(t *testing.T) {
cmd := exec.Command("kubevpn", "proxy", "deployments/reviews", "--debug")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -461,7 +511,7 @@ func kubevpnProxy(t *testing.T) {
}
}
func kubevpnProxyWithServiceMesh(t *testing.T) {
func (u *ut) kubevpnProxyWithServiceMesh(t *testing.T) {
cmd := exec.Command("kubevpn", "proxy", "deployments/reviews", "--headers", "env=test", "--debug")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -471,7 +521,7 @@ func kubevpnProxyWithServiceMesh(t *testing.T) {
}
}
func kubevpnProxyWithServiceMeshAndGvisorMode(t *testing.T) {
func (u *ut) kubevpnProxyWithServiceMeshAndFargateMode(t *testing.T) {
cmd := exec.Command("kubevpn", "proxy", "svc/reviews", "--headers", "env=test", "--debug")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -481,7 +531,7 @@ func kubevpnProxyWithServiceMeshAndGvisorMode(t *testing.T) {
}
}
func kubevpnProxyWithServiceMeshAndK8sServicePortMap(t *testing.T) {
func (u *ut) kubevpnProxyWithServiceMeshAndK8sServicePortMap(t *testing.T) {
cmd := exec.Command("kubevpn", "proxy", "svc/reviews", "--headers", "env=test", "--debug", "--portmap", "9080:8080")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -491,7 +541,7 @@ func kubevpnProxyWithServiceMeshAndK8sServicePortMap(t *testing.T) {
}
}
func kubevpnLeave(t *testing.T) {
func (u *ut) kubevpnLeave(t *testing.T) {
cmd := exec.Command("kubevpn", "leave", "deployments/reviews")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -501,7 +551,7 @@ func kubevpnLeave(t *testing.T) {
}
}
func kubevpnLeaveService(t *testing.T) {
func (u *ut) kubevpnLeaveService(t *testing.T) {
cmd := exec.Command("kubevpn", "leave", "services/reviews")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -511,7 +561,7 @@ func kubevpnLeaveService(t *testing.T) {
}
}
func checkConnectStatus(t *testing.T) {
func (u *ut) checkConnectStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status", "-o", "json")
output, err := cmd.Output()
if err != nil {
@@ -519,7 +569,7 @@ func checkConnectStatus(t *testing.T) {
}
expect := status{List: []*connection{{
Namespace: namespace,
Namespace: u.namespace,
Status: "connected",
ProxyList: nil,
}}}
@@ -533,7 +583,7 @@ func checkConnectStatus(t *testing.T) {
t.Fatalf("expect: %s, but was: %s", string(marshal), string(output))
}
}
func centerCheckConnectStatus(t *testing.T) {
func (u *ut) centerCheckConnectStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status", "-o", "json")
output, err := cmd.Output()
if err != nil {
@@ -563,20 +613,29 @@ type status struct {
type connection struct {
Namespace string
Status string
ProxyList []*proxy
ProxyList []*proxyItem
SyncList []*syncItem
}
type proxy struct {
type proxyItem struct {
Namespace string
Workload string
RuleList []*rule
RuleList []*proxyRule
}
type rule struct {
type proxyRule struct {
Headers map[string]string
CurrentDevice bool
PortMap map[int32]int32
}
type syncItem struct {
Namespace string
Workload string
RuleList []*syncRule
}
type syncRule struct {
DstWorkload string
}
func checkProxyStatus(t *testing.T) {
func (u *ut) checkProxyStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status", "-o", "json")
output, err := cmd.Output()
if err != nil {
@@ -584,12 +643,12 @@ func checkProxyStatus(t *testing.T) {
}
expect := status{List: []*connection{{
Namespace: namespace,
Namespace: u.namespace,
Status: "connected",
ProxyList: []*proxy{{
Namespace: namespace,
ProxyList: []*proxyItem{{
Namespace: u.namespace,
Workload: "deployments.apps/reviews",
RuleList: []*rule{{
RuleList: []*proxyRule{{
Headers: nil,
CurrentDevice: true,
PortMap: map[int32]int32{9080: 9080},
@@ -607,7 +666,7 @@ func checkProxyStatus(t *testing.T) {
}
}
func centerCheckProxyStatus(t *testing.T) {
func (u *ut) centerCheckProxyStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status", "-o", "json")
output, err := cmd.Output()
if err != nil {
@@ -617,10 +676,10 @@ func centerCheckProxyStatus(t *testing.T) {
expect := status{List: []*connection{{
Namespace: "default",
Status: "connected",
ProxyList: []*proxy{{
ProxyList: []*proxyItem{{
Namespace: "default",
Workload: "deployments.apps/reviews",
RuleList: []*rule{{
RuleList: []*proxyRule{{
Headers: nil,
CurrentDevice: true,
PortMap: map[int32]int32{9080: 9080},
@@ -638,7 +697,7 @@ func centerCheckProxyStatus(t *testing.T) {
}
}
func checkProxyWithServiceMeshStatus(t *testing.T) {
func (u *ut) checkProxyWithServiceMeshStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status", "-o", "json")
output, err := cmd.Output()
if err != nil {
@@ -646,12 +705,12 @@ func checkProxyWithServiceMeshStatus(t *testing.T) {
}
expect := status{List: []*connection{{
Namespace: namespace,
Namespace: u.namespace,
Status: "connected",
ProxyList: []*proxy{{
Namespace: namespace,
ProxyList: []*proxyItem{{
Namespace: u.namespace,
Workload: "deployments.apps/reviews",
RuleList: []*rule{{
RuleList: []*proxyRule{{
Headers: map[string]string{"env": "test"},
CurrentDevice: true,
PortMap: map[int32]int32{9080: 9080},
@@ -669,7 +728,7 @@ func checkProxyWithServiceMeshStatus(t *testing.T) {
}
}
func centerCheckProxyWithServiceMeshStatus(t *testing.T) {
func (u *ut) centerCheckProxyWithServiceMeshStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status", "-o", "json")
output, err := cmd.Output()
if err != nil {
@@ -679,10 +738,10 @@ func centerCheckProxyWithServiceMeshStatus(t *testing.T) {
expect := status{List: []*connection{{
Namespace: "default",
Status: "connected",
ProxyList: []*proxy{{
ProxyList: []*proxyItem{{
Namespace: "default",
Workload: "deployments.apps/reviews",
RuleList: []*rule{{
RuleList: []*proxyRule{{
Headers: map[string]string{"env": "test"},
CurrentDevice: true,
PortMap: map[int32]int32{9080: 9080},
@@ -700,7 +759,7 @@ func centerCheckProxyWithServiceMeshStatus(t *testing.T) {
}
}
func checkProxyWithServiceMeshAndGvisorStatus(t *testing.T) {
func (u *ut) checkProxyWithServiceMeshAndGvisorStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status", "-o", "json")
output, err := cmd.Output()
if err != nil {
@@ -708,12 +767,12 @@ func checkProxyWithServiceMeshAndGvisorStatus(t *testing.T) {
}
expect := status{List: []*connection{{
Namespace: namespace,
Namespace: u.namespace,
Status: "connected",
ProxyList: []*proxy{{
Namespace: namespace,
ProxyList: []*proxyItem{{
Namespace: u.namespace,
Workload: "services/reviews",
RuleList: []*rule{{
RuleList: []*proxyRule{{
Headers: map[string]string{"env": "test"},
CurrentDevice: true,
PortMap: map[int32]int32{9080: 9080},
@@ -738,7 +797,7 @@ func checkProxyWithServiceMeshAndGvisorStatus(t *testing.T) {
}
}
func centerCheckProxyWithServiceMeshAndGvisorStatus(t *testing.T) {
func (u *ut) centerCheckProxyWithServiceMeshAndGvisorStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status", "-o", "json")
output, err := cmd.Output()
if err != nil {
@@ -748,10 +807,10 @@ func centerCheckProxyWithServiceMeshAndGvisorStatus(t *testing.T) {
expect := status{List: []*connection{{
Namespace: "default",
Status: "connected",
ProxyList: []*proxy{{
ProxyList: []*proxyItem{{
Namespace: "default",
Workload: "services/reviews",
RuleList: []*rule{{
RuleList: []*proxyRule{{
Headers: map[string]string{"env": "test"},
CurrentDevice: true,
PortMap: map[int32]int32{9080: 8080},
@@ -776,7 +835,7 @@ func centerCheckProxyWithServiceMeshAndGvisorStatus(t *testing.T) {
}
}
func kubevpnUninstall(t *testing.T) {
func (u *ut) kubevpnUninstall(t *testing.T) {
cmd := exec.Command("kubevpn", "uninstall", "kubevpn")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -786,7 +845,7 @@ func kubevpnUninstall(t *testing.T) {
}
}
func kubevpnStatus(t *testing.T) {
func (u *ut) kubevpnStatus(t *testing.T) {
cmd := exec.Command("kubevpn", "status")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -796,7 +855,7 @@ func kubevpnStatus(t *testing.T) {
}
}
func kubevpnQuit(t *testing.T) {
func (u *ut) kubevpnQuit(t *testing.T) {
cmd := exec.Command("kubevpn", "quit")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -806,14 +865,14 @@ func kubevpnQuit(t *testing.T) {
}
}
func checkServiceShouldNotInNsDefault(t *testing.T) {
_, err := clientset.CoreV1().Services(namespace).Get(context.Background(), pkgconfig.ConfigMapPodTrafficManager, v1.GetOptions{})
func (u *ut) checkServiceShouldNotInNsDefault(t *testing.T) {
_, err := u.clientset.CoreV1().Services(u.namespace).Get(context.Background(), pkgconfig.ConfigMapPodTrafficManager, v1.GetOptions{})
if !k8serrors.IsNotFound(err) {
t.Fatal(err)
}
}
func kubectl(t *testing.T) {
func (u *ut) kubectl(t *testing.T) {
cmdGetPod := exec.Command("kubectl", "get", "pods", "-o", "wide")
cmdDescribePod := exec.Command("kubectl", "describe", "pods")
cmdGetSvc := exec.Command("kubectl", "get", "services", "-o", "wide")
@@ -829,27 +888,27 @@ func kubectl(t *testing.T) {
}
}
func Init(t *testing.T) {
func (u *ut) init(t *testing.T) {
var err error
configFlags := genericclioptions.NewConfigFlags(true)
f := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(configFlags))
if restconfig, err = f.ToRESTConfig(); err != nil {
if u.restconfig, err = f.ToRESTConfig(); err != nil {
t.Fatal(err)
}
if clientset, err = kubernetes.NewForConfig(restconfig); err != nil {
if u.clientset, err = kubernetes.NewForConfig(u.restconfig); err != nil {
t.Fatal(err)
}
if namespace, _, err = f.ToRawKubeConfigLoader().Namespace(); err != nil {
if u.namespace, _, err = f.ToRawKubeConfigLoader().Namespace(); err != nil {
t.Fatal(err)
}
go startupHttpServer(t, "localhost:9080", local)
go startupHttpServer(t, "localhost:8080", local8080)
go u.startupHttpServer(t, "localhost:9080", local)
go u.startupHttpServer(t, "localhost:8080", local8080)
}
func startupHttpServer(t *testing.T, addr, str string) {
func (u *ut) startupHttpServer(t *testing.T, addr, str string) {
var health = func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(str))
}