add ut for bookkeeper
[oom/registrator.git] / src / kube2msb / kube2msb_test.go
1 /*
2 Copyright 2018 ZTE, Inc. and others.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16 package main
17
18 import (
19         "os"
20         "testing"
21
22         kapi "k8s.io/kubernetes/pkg/api"
23 )
24
25 func urlFormateValidate(t *testing.T, method string) {
26         cases := []struct{ in, want string }{
27                 {"", ""},                                                                             // nil
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"},
41         }
42
43         // set os env
44         os.Setenv("var1", "value1")
45         os.Setenv("var2", "value2")
46         os.Setenv("var3", "value3")
47
48         for _, c := range cases {
49                 var got string
50                 if method == "getMSBUrl" {
51                         argMSBUrl = &c.in
52                         got, _ = getMSBUrl()
53                 } else if method == "getKubeMasterUrl" {
54                         argKubeMasterUrl = &c.in
55                         got, _ = getKubeMasterUrl()
56                 }
57
58                 if got != c.want {
59                         t.Errorf("getMSBUrl() arg %s, want %s, got %s", c.in, c.want, got)
60                 }
61         }
62 }
63
64 func TestGetMSBUrl(t *testing.T) {
65         urlFormateValidate(t, "getMSBUrl")
66 }
67
68 func TestGetKubeMasterUrl(t *testing.T) {
69         urlFormateValidate(t, "getKubeMasterUrl")
70 }
71
72 func TestSendServiceWork(t *testing.T) {
73
74         kubeWorkQueue := make(chan KubeWork, 1)
75         serviceObj := kapi.Service{}
76
77         cases := []KubeWorkAction{
78                 KubeWorkAddService,
79                 KubeWorkRemoveService,
80                 KubeWorkUpdateService,
81         }
82
83         for _, c := range cases {
84                 sendServiceWork(c, kubeWorkQueue, &serviceObj)
85                 got := <-kubeWorkQueue
86
87                 if got.Action != c {
88                         t.Errorf("sendServiceWork(%action, queue, service) got %gotAction", c, got.Action)
89                 }
90         }
91 }
92
93 func TestSendPodWork(t *testing.T) {
94
95         kubeWorkQueue := make(chan KubeWork, 1)
96         podObj := kapi.Pod{}
97
98         cases := []KubeWorkAction{
99                 KubeWorkAddPod,
100                 KubeWorkRemovePod,
101                 KubeWorkUpdatePod,
102         }
103
104         for _, c := range cases {
105                 sendPodWork(c, kubeWorkQueue, &podObj)
106                 got := <-kubeWorkQueue
107
108                 if got.Action != c {
109                         t.Errorf("sendPodWork(%action, queue, service) got %gotAction", c, got.Action)
110                 }
111         }
112 }
113
114 func TestRunBookKeeper(t *testing.T) {
115         kubeWorkQueue := make(chan KubeWork)
116         msbWorkQueue := make(chan MSBWork)
117
118         go runBookKeeper(kubeWorkQueue, msbWorkQueue)
119
120         serviceCases := []struct {
121                 work      KubeWork
122                 ip        string
123                 msbAction MSBWorkAction
124         }{
125                 {
126                         KubeWork{
127                                 Action:  KubeWorkAddService,
128                                 Service: createMockService("RunBookKeeper", "127.0.0.1", kapi.ServiceTypeClusterIP),
129                         },
130                         "127.0.0.1",
131                         MSBWorkAddService,
132                 },
133                 {
134                         KubeWork{
135                                 Action:  KubeWorkUpdateService,
136                                 Service: createMockService("RunBookKeeper", "127.0.0.2", kapi.ServiceTypeNodePort),
137                         },
138                         "127.0.0.2",
139                         MSBWorkAddService,
140                 },
141                 {
142                         KubeWork{
143                                 Action:  KubeWorkRemoveService,
144                                 Service: createMockService("RunBookKeeper", "127.0.0.3", kapi.ServiceTypeLoadBalancer),
145                         },
146                         "127.0.0.3",
147                         MSBWorkRemoveService,
148                 },
149         }
150
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"
155                 //              }
156                 kubeWorkQueue <- c.work
157
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)
161                 } else {
162                         msbWorkValidate(t, msbWorkQueue, c.work.Service, c.msbAction, c.ip)
163                 }
164         }
165
166         podCases := []struct {
167                 work      KubeWork
168                 msbAction MSBWorkAction
169                 ip        string
170         }{
171                 {
172                         KubeWork{
173                                 Action: KubeWorkAddPod,
174                                 Pod:    createMockPod("RunBookKeeper", "192.168.1.2"),
175                         },
176                         MSBWorkAddPod,
177                         "192.168.1.2",
178                 },
179                 {
180                         KubeWork{
181                                 Action: KubeWorkUpdatePod,
182                                 Pod:    createMockPod("RunBookKeeper", "192.168.1.3"),
183                         },
184                         MSBWorkAddPod,
185                         "",
186                 },
187                 {
188                         KubeWork{
189                                 Action: KubeWorkRemovePod,
190                                 Pod:    createMockPod("RunBookKeeper", "192.168.1.4"),
191                         },
192                         MSBWorkRemovePod,
193                         "192.168.1.3",
194                 },
195         }
196
197         for _, c := range podCases {
198                 kubeWorkQueue <- c.work
199
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")
203                 } else {
204                         msbWorkPodValidate(t, msbWorkQueue, c.work.Pod, c.msbAction, c.ip)
205                 }
206         }
207 }