From 01ab8767744bed1c19bc157645088244ef17458a Mon Sep 17 00:00:00 2001 From: yunlzheng Date: Fri, 13 Mar 2020 12:42:52 +0800 Subject: [PATCH] test: add test case for create shadow --- pkg/kt/cluster/helper_test.go | 11 ++-- pkg/kt/cluster/kubernetes_test.go | 84 +++++++++++++++++++++++++++++++ pkg/kt/cluster/types.go | 6 +-- 3 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 pkg/kt/cluster/kubernetes_test.go diff --git a/pkg/kt/cluster/helper_test.go b/pkg/kt/cluster/helper_test.go index cf48a84..24454ea 100644 --- a/pkg/kt/cluster/helper_test.go +++ b/pkg/kt/cluster/helper_test.go @@ -20,7 +20,7 @@ func Test_getPodCirds(t *testing.T) { { name: "should_get_pod_cird_from_pods", objs: []runtime.Object{ - pod("default", "a", "172.168.1.2"), + pod("POD1", "default", "a", "172.168.1.2", map[string]string{}), }, wantCidrs: []string{ "172.168.0.0/16", @@ -108,14 +108,19 @@ func node(namespace, name, crid string) *v1.Node { } } -func pod(namespace, image string, ip string) *v1.Pod { +func pod(name, namespace, image string, ip string, labels map[string]string) *v1.Pod { return &v1.Pod{ - ObjectMeta: metav1.ObjectMeta{Namespace: namespace}, + ObjectMeta: metav1.ObjectMeta{ + Namespace: namespace, + Name: name, + Labels: labels, + }, Spec: v1.PodSpec{ Containers: []v1.Container{{Image: image}}, }, Status: v1.PodStatus{ PodIP: ip, + Phase: v1.PodRunning, }, } } diff --git a/pkg/kt/cluster/kubernetes_test.go b/pkg/kt/cluster/kubernetes_test.go new file mode 100644 index 0000000..c467f3c --- /dev/null +++ b/pkg/kt/cluster/kubernetes_test.go @@ -0,0 +1,84 @@ +package cluster + +import ( + "testing" + + . "k8s.io/apimachinery/pkg/runtime" + + "github.com/alibaba/kt-connect/pkg/kt/util" + testclient "k8s.io/client-go/kubernetes/fake" +) + +func TestKubernetes_CreateShadow(t *testing.T) { + + type fields struct { + } + type args struct { + name string + namespace string + image string + labels map[string]string + debug bool + } + tests := []struct { + name string + fields fields + args args + objs []Object + wantPodIP string + wantPodName string + wantSshcm string + wantCredential *util.SSHCredential + wantErr bool + }{ + { + name: "shouldCreateShadowSuccessful", + fields: fields{}, + args: args{ + name: "shadow", + namespace: "default", + image: "shadow/shadow", + labels: map[string]string{ + "kt-component": "shadow-component", + "version": "0.0.1", + }, + debug: true, + }, + objs: []Object{ + pod( + "shadow-pod", + "default", + "a", + "172.168.1.2", map[string]string{ + "kt": "shadow", + }), + }, + wantPodIP: "172.168.1.2", + wantPodName: "shadow-pod", + wantSshcm: "kt-shadow-component-public-key-0.0.1", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + k := &Kubernetes{ + Clientset: testclient.NewSimpleClientset(tt.objs...), + } + + gotPodIP, gotPodName, gotSshcm, _, err := k.CreateShadow(tt.args.name, tt.args.namespace, tt.args.image, tt.args.labels, tt.args.debug) + if (err != nil) != tt.wantErr { + t.Errorf("Kubernetes.CreateShadow() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotPodIP != tt.wantPodIP { + t.Errorf("Kubernetes.CreateShadow() gotPodIP = %v, want %v", gotPodIP, tt.wantPodIP) + } + if gotPodName != tt.wantPodName { + t.Errorf("Kubernetes.CreateShadow() gotPodName = %v, want %v", gotPodName, tt.wantPodName) + } + if gotSshcm != tt.wantSshcm { + t.Errorf("Kubernetes.CreateShadow() gotSshcm = %v, want %v", gotSshcm, tt.wantSshcm) + } + }) + } +} diff --git a/pkg/kt/cluster/types.go b/pkg/kt/cluster/types.go index 0221455..dbc0ac5 100644 --- a/pkg/kt/cluster/types.go +++ b/pkg/kt/cluster/types.go @@ -5,7 +5,6 @@ import ( appV1 "k8s.io/api/apps/v1" coreV1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" - v1 "k8s.io/client-go/listers/core/v1" ) // Create kubernetes instance @@ -37,8 +36,5 @@ type KubernetesInterface interface { // Kubernetes implements KubernetesInterface type Kubernetes struct { KubeConfig string - // TODO: should remove - Clientset kubernetes.Interface - // TODO: should remove - ServiceListener v1.ServiceLister + Clientset kubernetes.Interface }