2 Copyright 2018 ZTE, Inc. and others.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
22 kapi "k8s.io/kubernetes/pkg/api"
25 func urlFormateValidate(t *testing.T, method string) {
26 cases := []struct{ in, want string }{
28 {"urlFormatError", ""}, // error
29 {"exmaple.com/", ""}, // Scheme == ""
30 {"http://", ""}, // Host == ""
31 {"http://:/", ""}, // Host == ":"
32 {"http://:/msb", ""}, // Host == ":"
33 {"http://192.168.1.100:8080/$var1", "http://192.168.1.100:8080/value1"}, // os.env
34 {"http://192.168.1.100:8080/$var1/$var2", "http://192.168.1.100:8080/value1/value2"}, //
35 {"http://192.168.1.100:8080/$var1/$var1", "http://192.168.1.100:8080/value1/value1"}, //
36 {"http://192.168.1.100:8080/$var1", "http://192.168.1.100:8080/value1"}, //
37 {"http://192.168.1.100:8080/msb", "http://192.168.1.100:8080/msb"}, //
38 {"http://192.168.1.100/msb", "http://192.168.1.100/msb"}, //
39 {"http://192.168.1.100/", "http://192.168.1.100/"}, //
40 {"postgres://user:pass@host.com:5432/path?k=v#f", "postgres://user:pass@host.com:5432/path?k=v#f"},
44 os.Setenv("var1", "value1")
45 os.Setenv("var2", "value2")
46 os.Setenv("var3", "value3")
48 for _, c := range cases {
50 if method == "getMSBUrl" {
53 } else if method == "getKubeMasterUrl" {
54 argKubeMasterUrl = &c.in
55 got, _ = getKubeMasterUrl()
59 t.Errorf("getMSBUrl() arg %s, want %s, got %s", c.in, c.want, got)
64 func TestGetMSBUrl(t *testing.T) {
65 urlFormateValidate(t, "getMSBUrl")
68 func TestGetKubeMasterUrl(t *testing.T) {
69 urlFormateValidate(t, "getKubeMasterUrl")
72 func TestSendServiceWork(t *testing.T) {
74 kubeWorkQueue := make(chan KubeWork, 1)
75 serviceObj := kapi.Service{}
77 cases := []KubeWorkAction{
79 KubeWorkRemoveService,
80 KubeWorkUpdateService,
83 for _, c := range cases {
84 sendServiceWork(c, kubeWorkQueue, &serviceObj)
85 got := <-kubeWorkQueue
88 t.Errorf("sendServiceWork(%action, queue, service) got %gotAction", c, got.Action)
93 func TestSendPodWork(t *testing.T) {
95 kubeWorkQueue := make(chan KubeWork, 1)
98 cases := []KubeWorkAction{
104 for _, c := range cases {
105 sendPodWork(c, kubeWorkQueue, &podObj)
106 got := <-kubeWorkQueue
109 t.Errorf("sendPodWork(%action, queue, service) got %gotAction", c, got.Action)
114 func TestRunBookKeeper(t *testing.T) {
115 kubeWorkQueue := make(chan KubeWork)
116 msbWorkQueue := make(chan MSBWork)
118 go runBookKeeper(kubeWorkQueue, msbWorkQueue)
120 serviceCases := []struct {
123 msbAction MSBWorkAction
127 Action: KubeWorkAddService,
128 Service: createMockService("RunBookKeeper", "127.0.0.1", kapi.ServiceTypeClusterIP),
135 Action: KubeWorkUpdateService,
136 Service: createMockService("RunBookKeeper", "127.0.0.2", kapi.ServiceTypeNodePort),
143 Action: KubeWorkRemoveService,
144 Service: createMockService("RunBookKeeper", "127.0.0.3", kapi.ServiceTypeLoadBalancer),
147 MSBWorkRemoveService,
151 for _, c := range serviceCases {
152 // if c.work.Service.Spec.Type == kapi.ServiceTypeLoadBalancer {
153 // c.work.Service.Spec.LoadBalancerIP = "127.0.0.4"
154 // c.ip = "127.0.0.4"
156 kubeWorkQueue <- c.work
158 if c.work.Action == KubeWorkUpdateService {
159 msbWorkValidate(t, msbWorkQueue, c.work.Service, MSBWorkRemoveService, c.ip)
160 msbWorkValidate(t, msbWorkQueue, c.work.Service, MSBWorkAddService, c.ip)
162 msbWorkValidate(t, msbWorkQueue, c.work.Service, c.msbAction, c.ip)
166 podCases := []struct {
168 msbAction MSBWorkAction
173 Action: KubeWorkAddPod,
174 Pod: createMockPod("RunBookKeeper", "192.168.1.2"),
181 Action: KubeWorkUpdatePod,
182 Pod: createMockPod("RunBookKeeper", "192.168.1.3"),
189 Action: KubeWorkRemovePod,
190 Pod: createMockPod("RunBookKeeper", "192.168.1.4"),
197 for _, c := range podCases {
198 kubeWorkQueue <- c.work
200 if c.work.Action == KubeWorkUpdatePod {
201 msbWorkPodValidate(t, msbWorkQueue, c.work.Pod, MSBWorkRemovePod, "192.168.1.2")
202 msbWorkPodValidate(t, msbWorkQueue, c.work.Pod, MSBWorkAddPod, "192.168.1.3")
204 msbWorkPodValidate(t, msbWorkQueue, c.work.Pod, c.msbAction, c.ip)