From 79488acab14b9ab402bcb0079989a76164f813f6 Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Mon, 5 Mar 2018 17:15:32 +0800 Subject: add ut for addPod of kube Issue-ID: MSB-160 Change-Id: Iac5e9310933cbcff9edc8f829840c6219e5658b1 Signed-off-by: Lvbo163 --- src/kube2msb/kube_work_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/kube2msb/kube_work_test.go b/src/kube2msb/kube_work_test.go index cc79439..a96f5c9 100644 --- a/src/kube2msb/kube_work_test.go +++ b/src/kube2msb/kube_work_test.go @@ -160,3 +160,61 @@ func TestUpdateServiceKube(t *testing.T) { client.UpdateService(notExistService) msbWorkValidate(t, msbWorkQueue, notExistService, MSBWorkAddService, "192.168.10.12") } + +func createMockPod(name string, ip string) *kapi.Pod { + pod := kapi.Pod{ + Status: kapi.PodStatus{ + PodIP: ip, + }, + } + + pod.Name = name + pod.Annotations = map[string]string{serviceKey: name} + + return &pod +} + +func msbWorkPodValidate(t *testing.T, queue <-chan MSBWork, pod *kapi.Pod, action MSBWorkAction) { + work := <-queue + + if work.Action != action || work.IPAddress != pod.Status.PodIP || work.ServiceInfo != pod.Name { + t.Errorf("expect %s,%s,%s to be %s %s,%s", + work.Action, work.IPAddress, work.ServiceInfo, action, pod.Status.PodIP, pod.Name) + } +} + +func TestAddPodKube(t *testing.T) { + client := newClientBookKeeper() + msbWorkQueue := make(chan MSBWork, 10) + client.msbQueue = msbWorkQueue + + // add ServiceTypeClusterIP + pod := createMockPod("addPodTest", "192.168.10.10") + client.AddPod(pod) + msbWorkPodValidate(t, msbWorkQueue, pod, MSBWorkAddPod) + if _, ok := client.pods[pod.Name]; !ok { + t.Errorf("add pod error, pod not exists in client.pods") + } + + // exception process + // TODO servicekey is not set , cannot check result for there would be no return + podWithoutServiceKey := &kapi.Pod{} + client.AddPod(podWithoutServiceKey) + + // TODO pod already exist , cannot check result for there would be no return + client.AddPod(pod) + + // pod.Name == "" || pod.Status.PodIP == "" + podWithoutName := createMockPod("", "192.168.10.10") + client.AddPod(podWithoutName) + if addedPod, ok := addMap[podWithoutName.Name]; !ok || addedPod.Status.PodIP != podWithoutName.Status.PodIP { + t.Errorf("podWithoutName didnot add to addMap") + } + + podWithoutIp := createMockPod("podWithoutIp", "") + client.AddPod(podWithoutIp) + if addedPod, ok := addMap[podWithoutIp.Name]; !ok || addedPod.Status.PodIP != podWithoutIp.Status.PodIP { + t.Errorf("podWithoutIp didnot add to addMap") + } + +} -- cgit 1.2.3-korg