Skip to content

Commit 27adc78

Browse files
authored
Merge pull request #144 from chentao1596/add-unit-test-for-k8s
add unit test cases for core.pkg.k8s
2 parents f1520a1 + 5b31373 commit 27adc78

File tree

1 file changed

+178
-10
lines changed

1 file changed

+178
-10
lines changed

core/pkg/k8s/main_test.go

+178-10
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121

2222
"k8s.io/kubernetes/pkg/api"
2323
testclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
24+
"os"
2425
)
2526

2627
func TestParseNameNS(t *testing.T) {
27-
2828
tests := []struct {
2929
title string
3030
input string
@@ -42,15 +42,15 @@ func TestParseNameNS(t *testing.T) {
4242
ns, name, err := ParseNameNS(test.input)
4343
if test.expErr {
4444
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)
4646
}
4747
continue
4848
}
4949
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)
5151
}
5252
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)
5454
}
5555
}
5656
}
@@ -65,20 +65,20 @@ func TestIsValidService(t *testing.T) {
6565

6666
_, err := IsValidService(fk, "")
6767
if err == nil {
68-
t.Errorf("expected error but retuned nil")
68+
t.Errorf("expected error but returned nil")
6969
}
7070
s, err := IsValidService(fk, "default/demo")
7171
if err != nil {
7272
t.Errorf("unexpected error: %v", err)
7373
}
7474
if s == nil {
75-
t.Errorf("expected a Service but retuned nil")
75+
t.Errorf("expected a Service but returned nil")
7676
}
7777

7878
fk = testclient.NewSimpleClientset()
7979
s, err = IsValidService(fk, "default/demo")
8080
if err == nil {
81-
t.Errorf("expected an error but retuned nil")
81+
t.Errorf("expected an error but returned nil")
8282
}
8383
if s != nil {
8484
t.Errorf("unexpected Service returned: %v", s)
@@ -95,22 +95,190 @@ func TestIsValidSecret(t *testing.T) {
9595

9696
_, err := IsValidSecret(fk, "")
9797
if err == nil {
98-
t.Errorf("expected error but retuned nil")
98+
t.Errorf("expected error but returned nil")
9999
}
100100
s, err := IsValidSecret(fk, "default/demo")
101101
if err != nil {
102102
t.Errorf("unexpected error: %v", err)
103103
}
104104
if s == nil {
105-
t.Errorf("expected a Secret but retuned nil")
105+
t.Errorf("expected a Secret but returned nil")
106106
}
107107

108108
fk = testclient.NewSimpleClientset()
109109
s, err = IsValidSecret(fk, "default/demo")
110110
if err == nil {
111-
t.Errorf("expected an error but retuned nil")
111+
t.Errorf("expected an error but returned nil")
112112
}
113113
if s != nil {
114114
t.Errorf("unexpected Secret returned: %v", s)
115115
}
116116
}
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

Comments
 (0)