@@ -21,10 +21,10 @@ import (
21
21
22
22
"k8s.io/kubernetes/pkg/api"
23
23
testclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
24
+ "os"
24
25
)
25
26
26
27
func TestParseNameNS (t * testing.T ) {
27
-
28
28
tests := []struct {
29
29
title string
30
30
input string
@@ -42,15 +42,15 @@ func TestParseNameNS(t *testing.T) {
42
42
ns , name , err := ParseNameNS (test .input )
43
43
if test .expErr {
44
44
if err == nil {
45
- t .Errorf ("%v: expected error but retuned nil" , test .title )
45
+ t .Errorf ("%v: expected error but returned nil" , test .title )
46
46
}
47
47
continue
48
48
}
49
49
if test .ns != ns {
50
- t .Errorf ("%v: expected %v but retuned %v" , test .title , test .ns , ns )
50
+ t .Errorf ("%v: expected %v but returned %v" , test .title , test .ns , ns )
51
51
}
52
52
if test .name != name {
53
- t .Errorf ("%v: expected %v but retuned %v" , test .title , test .name , name )
53
+ t .Errorf ("%v: expected %v but returned %v" , test .title , test .name , name )
54
54
}
55
55
}
56
56
}
@@ -65,20 +65,20 @@ func TestIsValidService(t *testing.T) {
65
65
66
66
_ , err := IsValidService (fk , "" )
67
67
if err == nil {
68
- t .Errorf ("expected error but retuned nil" )
68
+ t .Errorf ("expected error but returned nil" )
69
69
}
70
70
s , err := IsValidService (fk , "default/demo" )
71
71
if err != nil {
72
72
t .Errorf ("unexpected error: %v" , err )
73
73
}
74
74
if s == nil {
75
- t .Errorf ("expected a Service but retuned nil" )
75
+ t .Errorf ("expected a Service but returned nil" )
76
76
}
77
77
78
78
fk = testclient .NewSimpleClientset ()
79
79
s , err = IsValidService (fk , "default/demo" )
80
80
if err == nil {
81
- t .Errorf ("expected an error but retuned nil" )
81
+ t .Errorf ("expected an error but returned nil" )
82
82
}
83
83
if s != nil {
84
84
t .Errorf ("unexpected Service returned: %v" , s )
@@ -95,22 +95,190 @@ func TestIsValidSecret(t *testing.T) {
95
95
96
96
_ , err := IsValidSecret (fk , "" )
97
97
if err == nil {
98
- t .Errorf ("expected error but retuned nil" )
98
+ t .Errorf ("expected error but returned nil" )
99
99
}
100
100
s , err := IsValidSecret (fk , "default/demo" )
101
101
if err != nil {
102
102
t .Errorf ("unexpected error: %v" , err )
103
103
}
104
104
if s == nil {
105
- t .Errorf ("expected a Secret but retuned nil" )
105
+ t .Errorf ("expected a Secret but returned nil" )
106
106
}
107
107
108
108
fk = testclient .NewSimpleClientset ()
109
109
s , err = IsValidSecret (fk , "default/demo" )
110
110
if err == nil {
111
- t .Errorf ("expected an error but retuned nil" )
111
+ t .Errorf ("expected an error but returned nil" )
112
112
}
113
113
if s != nil {
114
114
t .Errorf ("unexpected Secret returned: %v" , s )
115
115
}
116
116
}
117
+
118
+ func TestGetNodeIP (t * testing.T ) {
119
+ fKNodes := []struct {
120
+ cs * testclient.Clientset
121
+ n string
122
+ ea string
123
+ }{
124
+ // empty node list
125
+ {testclient .NewSimpleClientset (), "demo" , "" },
126
+
127
+ // node not exist
128
+ {testclient .NewSimpleClientset (& api.NodeList {Items : []api.Node {{
129
+ ObjectMeta : api.ObjectMeta {
130
+ Name : "demo" ,
131
+ },
132
+ Status : api.NodeStatus {
133
+ Addresses : []api.NodeAddress {
134
+ {
135
+ Type : api .NodeLegacyHostIP ,
136
+ Address : "10.0.0.1" ,
137
+ },
138
+ },
139
+ },
140
+ }}}), "notexistnode" , "" },
141
+
142
+ // node exist
143
+ {testclient .NewSimpleClientset (& api.NodeList {Items : []api.Node {{
144
+ ObjectMeta : api.ObjectMeta {
145
+ Name : "demo" ,
146
+ },
147
+ Status : api.NodeStatus {
148
+ Addresses : []api.NodeAddress {
149
+ {
150
+ Type : api .NodeLegacyHostIP ,
151
+ Address : "10.0.0.1" ,
152
+ },
153
+ },
154
+ },
155
+ }}}), "demo" , "10.0.0.1" },
156
+
157
+ // search the correct node
158
+ {testclient .NewSimpleClientset (& api.NodeList {Items : []api.Node {
159
+ {
160
+ ObjectMeta : api.ObjectMeta {
161
+ Name : "demo1" ,
162
+ },
163
+ Status : api.NodeStatus {
164
+ Addresses : []api.NodeAddress {
165
+ {
166
+ Type : api .NodeLegacyHostIP ,
167
+ Address : "10.0.0.1" ,
168
+ },
169
+ },
170
+ },
171
+ },
172
+ {
173
+ ObjectMeta : api.ObjectMeta {
174
+ Name : "demo2" ,
175
+ },
176
+ Status : api.NodeStatus {
177
+ Addresses : []api.NodeAddress {
178
+ {
179
+ Type : api .NodeLegacyHostIP ,
180
+ Address : "10.0.0.2" ,
181
+ },
182
+ },
183
+ },
184
+ },
185
+ }}), "demo2" , "10.0.0.2" },
186
+
187
+ // get NodeExternalIP
188
+ {testclient .NewSimpleClientset (& api.NodeList {Items : []api.Node {{
189
+ ObjectMeta : api.ObjectMeta {
190
+ Name : "demo" ,
191
+ },
192
+ Status : api.NodeStatus {
193
+ Addresses : []api.NodeAddress {
194
+ {
195
+ Type : api .NodeLegacyHostIP ,
196
+ Address : "10.0.0.1" ,
197
+ }, {
198
+ Type : api .NodeExternalIP ,
199
+ Address : "10.0.0.2" ,
200
+ },
201
+ },
202
+ },
203
+ }}}), "demo" , "10.0.0.2" },
204
+
205
+ // get NodeLegacyHostIP
206
+ {testclient .NewSimpleClientset (& api.NodeList {Items : []api.Node {{
207
+ ObjectMeta : api.ObjectMeta {
208
+ Name : "demo" ,
209
+ },
210
+ Status : api.NodeStatus {
211
+ Addresses : []api.NodeAddress {
212
+ {
213
+ Type : api .NodeExternalIP ,
214
+ Address : "" ,
215
+ }, {
216
+ Type : api .NodeLegacyHostIP ,
217
+ Address : "10.0.0.2" ,
218
+ },
219
+ },
220
+ },
221
+ }}}), "demo" , "10.0.0.2" },
222
+ }
223
+
224
+ for _ , fk := range fKNodes {
225
+ address := GetNodeIP (fk .cs , fk .n )
226
+ if address != fk .ea {
227
+ t .Errorf ("expected %s, but returned %s" , fk .ea , address )
228
+ }
229
+ }
230
+ }
231
+
232
+ func TestGetPodDetails (t * testing.T ) {
233
+ // POD_NAME & POD_NAMESPACE not exist
234
+ os .Setenv ("POD_NAME" , "" )
235
+ os .Setenv ("POD_NAMESPACE" , "" )
236
+ _ , err1 := GetPodDetails (testclient .NewSimpleClientset ())
237
+ if err1 == nil {
238
+ t .Errorf ("expected an error but returned nil" )
239
+ }
240
+
241
+ // POD not exist
242
+ os .Setenv ("POD_NAME" , "testpod" )
243
+ os .Setenv ("POD_NAMESPACE" , api .NamespaceDefault )
244
+ _ , err2 := GetPodDetails (testclient .NewSimpleClientset ())
245
+ if err2 == nil {
246
+ t .Errorf ("expected an error but returned nil" )
247
+ }
248
+
249
+ // success to get PodInfo
250
+ fkClient := testclient .NewSimpleClientset (
251
+ & api.PodList {Items : []api.Pod {{
252
+ ObjectMeta : api.ObjectMeta {
253
+ Name : "testpod" ,
254
+ Namespace : api .NamespaceDefault ,
255
+ Labels : map [string ]string {
256
+ "first" : "first_label" ,
257
+ "second" : "second_label" ,
258
+ },
259
+ },
260
+ }}},
261
+ & api.NodeList {Items : []api.Node {{
262
+ ObjectMeta : api.ObjectMeta {
263
+ Name : "demo" ,
264
+ },
265
+ Status : api.NodeStatus {
266
+ Addresses : []api.NodeAddress {
267
+ {
268
+ Type : api .NodeLegacyHostIP ,
269
+ Address : "10.0.0.1" ,
270
+ },
271
+ },
272
+ },
273
+ }}})
274
+
275
+ epi , err3 := GetPodDetails (fkClient )
276
+ if err3 != nil {
277
+ t .Errorf ("expected a PodInfo but returned error" )
278
+ return
279
+ }
280
+
281
+ if epi == nil {
282
+ t .Errorf ("expected a PodInfo but returned nil" )
283
+ }
284
+ }
0 commit comments