diff options
| author | zhengxinwei <zhengxinwei@huawei.com> | 2023-11-18 15:10:54 +0800 |
|---|---|---|
| committer | zhengxinwei <zhengxinwei@huawei.com> | 2023-11-20 10:38:00 +0800 |
| commit | 353db5eda6051a37a40a3364c261fbb6d784e4b2 (patch) | |
| tree | 46aff09cb787e38029a8bc7556bfc29a7cde155e /cloud | |
| parent | Merge pull request #5146 from Shelley-BaoYue/bump-go1.20 (diff) | |
| download | kubeedge-353db5eda6051a37a40a3364c261fbb6d784e4b2.tar.gz | |
add go test for NegotiateTunnelPort function
Signed-off-by: zhengxinwei <zhengxinwei@huawei.com>
Diffstat (limited to 'cloud')
| -rw-r--r-- | cloud/cmd/cloudcore/app/server_test.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/cloud/cmd/cloudcore/app/server_test.go b/cloud/cmd/cloudcore/app/server_test.go new file mode 100644 index 000000000..fbdd39572 --- /dev/null +++ b/cloud/cmd/cloudcore/app/server_test.go @@ -0,0 +1,88 @@ +package app + +import ( + "reflect" + "testing" + + "github.com/agiledragon/gomonkey" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + fakekube "k8s.io/client-go/kubernetes/fake" + + "github.com/kubeedge/kubeedge/cloud/pkg/common/client" + "github.com/kubeedge/kubeedge/cloud/pkg/common/modules" + "github.com/kubeedge/kubeedge/common/constants" + "github.com/kubeedge/kubeedge/pkg/util" +) + +func TestNegotiateTunnelPort(t *testing.T) { + type testCase struct { + isConfigExits bool + isPortExist bool + isPortUsed bool + } + cases := testCase{} + var cm = v1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: modules.TunnelPort, + Namespace: constants.SystemNamespace, + Annotations: map[string]string{}, + }, + } + hostnameOverride := util.GetHostname() + localIP, _ := util.GetLocalIP(hostnameOverride) + patch := gomonkey.NewPatches() + defer patch.Reset() + patch.ApplyFunc(client.GetKubeClient, func() kubernetes.Interface { + if cases.isConfigExits { + record := "{}" + if cases.isPortExist { + record = "{\"ipTunnelPort\":{\"" + localIP + "\":10351},\"port\":{\"10351\":true}}" + } else if cases.isPortUsed { + record = "{\"ipTunnelPort\":{\"127.0.0.1\":10351},\"port\":{\"10351\":true}}" + } + cm.ObjectMeta.Annotations[modules.TunnelPortRecordAnnotationKey] = record + return fakekube.NewSimpleClientset(&cm) + } + return fakekube.NewSimpleClientset() + }) + + tests := []struct { + name string + cases testCase + want int + wantErr bool + }{ + { + name: "config not exits", + want: 10351, + wantErr: false, + }, + { + name: "port record exits", + cases: testCase{isConfigExits: true, isPortExist: true}, + want: 10351, + wantErr: false, + }, + { + name: "port used", + cases: testCase{isConfigExits: true, isPortUsed: true}, + want: 10352, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cases = tt.cases + got, err := NegotiateTunnelPort() + if (err != nil) != tt.wantErr { + t.Errorf("NegotiateTunnelPort() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, &tt.want) { + t.Errorf("NegotiateTunnelPort() got = %v, want %v", *got, tt.want) + } + }) + } +} |
