summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLvbo163 <lv.bo163@zte.com.cn>2018-03-05 16:15:04 +0800
committerLvbo163 <lv.bo163@zte.com.cn>2018-03-05 16:15:52 +0800
commit73f4df506751f186208c03a5223a61bb438f3130 (patch)
tree7db4ab0558a6c3632eb8eb190eb6c3f2f2fd45a9
parentf482dbecbe879489753d16798919a014df9ac001 (diff)
add ut for addService method of kubework
Issue-ID: MSB-157 Change-Id: I61502f15df28f90b3dc6f115fe94b8a93fd3a92b Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
-rw-r--r--src/kube2msb/kube_work_test.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/kube2msb/kube_work_test.go b/src/kube2msb/kube_work_test.go
new file mode 100644
index 0000000..b3495ce
--- /dev/null
+++ b/src/kube2msb/kube_work_test.go
@@ -0,0 +1,87 @@
+/*
+Copyright 2018 ZTE, Inc. and others.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+package main
+
+import (
+ "testing"
+
+ kapi "k8s.io/kubernetes/pkg/api"
+)
+
+func TestAddServiceKube(t *testing.T) {
+ client := newClientBookKeeper()
+ msbWorkQueue := make(chan MSBWork, 10)
+ client.msbQueue = msbWorkQueue
+
+ // add ServiceTypeClusterIP
+ serviceTypeClusterIP := createMockService("serviceTypeClusterIP", "192.168.10.10", kapi.ServiceTypeClusterIP)
+ client.AddService(serviceTypeClusterIP)
+ msbWorkValidate(t, msbWorkQueue, serviceTypeClusterIP, MSBWorkAddService, "192.168.10.10")
+
+ // add ServiceTypeNodePort
+ serviceTypeNodePort := createMockService("ServiceTypeNodePort", "192.168.10.11", kapi.ServiceTypeNodePort)
+ client.AddService(serviceTypeNodePort)
+ msbWorkValidate(t, msbWorkQueue, serviceTypeNodePort, MSBWorkAddService, "192.168.10.11")
+
+ // add ServiceTypeLoadBalancer
+ serviceTypeLoadBalancer := createMockService("ServiceTypeLoadBalancer", "192.168.10.12", kapi.ServiceTypeLoadBalancer)
+ client.AddService(serviceTypeLoadBalancer)
+ msbWorkValidate(t, msbWorkQueue, serviceTypeLoadBalancer, MSBWorkAddService, "192.168.10.12")
+
+ // exception process
+ // TODO ClusterIP is not set , cannot check result for there would be no return
+ serviceClusterIPNotSet := &kapi.Service{
+ ObjectMeta: kapi.ObjectMeta{
+ Annotations: map[string]string{serviceKey: "clusterIp not set"},
+ },
+ }
+ client.AddService(serviceClusterIPNotSet)
+
+ // TODO servicekey is not set , cannot check result for there would be no return
+ serviceWithoutServiceKey := &kapi.Service{
+ ObjectMeta: kapi.ObjectMeta{
+ Annotations: map[string]string{},
+ },
+ }
+ client.AddService(serviceWithoutServiceKey)
+
+ // TODO service already exist , cannot check result for there would be no return
+ client.AddService(serviceTypeClusterIP)
+}
+
+func createMockService(name string, ip string, serviceType kapi.ServiceType) *kapi.Service {
+ service := kapi.Service{
+ ObjectMeta: kapi.ObjectMeta{
+ Annotations: map[string]string{serviceKey: name},
+ },
+ Spec: kapi.ServiceSpec{
+ ClusterIP: ip,
+ Type: serviceType,
+ },
+ }
+ service.Name = name
+
+ return &service
+}
+
+func msbWorkValidate(t *testing.T, queue <-chan MSBWork, service *kapi.Service, action MSBWorkAction, ip string) {
+ work := <-queue
+
+ if work.Action != action || work.IPAddress != ip || work.ServiceInfo != service.Name {
+ t.Errorf("msbWork validate error, expect %s,%s,%s to be %s %s,%s",
+ work.Action, work.IPAddress, work.ServiceInfo, action, ip, service.Name)
+ }
+}