test: add test case for create shadow

This commit is contained in:
yunlzheng
2020-03-13 12:42:52 +08:00
parent 33ef1a03c9
commit 01ab876774
3 changed files with 93 additions and 8 deletions
+8 -3
View File
@@ -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,
},
}
}
+84
View File
@@ -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)
}
})
}
}
+1 -5
View File
@@ -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
}