@@ -19,11 +19,28 @@ import (
19
19
"testing"
20
20
21
21
"k8s.io/kubernetes/pkg/api"
22
+ k8sClient "k8s.io/kubernetes/pkg/client/unversioned"
22
23
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
23
24
25
+ "github.com/kubernetes/dashboard/client"
24
26
"github.com/kubernetes/dashboard/resource/common"
27
+ "github.com/kubernetes/dashboard/resource/pod"
25
28
)
26
29
30
+ type FakeHeapsterClient struct {
31
+ client k8sClient.Interface
32
+ }
33
+
34
+ type FakeRequest struct {}
35
+
36
+ func (FakeRequest ) DoRaw () ([]byte , error ) {
37
+ return nil , nil
38
+ }
39
+
40
+ func (c FakeHeapsterClient ) Get (path string ) client.RequestInterface {
41
+ return FakeRequest {}
42
+ }
43
+
27
44
func TestGetServiceDetail (t * testing.T ) {
28
45
cases := []struct {
29
46
service * api.Service
@@ -33,32 +50,35 @@ func TestGetServiceDetail(t *testing.T) {
33
50
}{
34
51
{
35
52
service : & api.Service {},
36
- namespace : "test-namespace" , name : "test-name" ,
37
- expectedActions : []string {"get" },
53
+ namespace : "test-namespace-1 " , name : "test-name" ,
54
+ expectedActions : []string {"get" , "list" },
38
55
expected : & ServiceDetail {
39
56
TypeMeta : common.TypeMeta {Kind : common .ResourceKindService },
57
+ PodList : pod.PodList {Pods : []pod.Pod {}},
40
58
},
41
59
}, {
42
60
service : & api.Service {ObjectMeta : api.ObjectMeta {
43
61
Name : "test-service" , Namespace : "test-namespace" ,
44
62
}},
45
- namespace : "test-namespace" , name : "test-name" ,
46
- expectedActions : []string {"get" },
63
+ namespace : "test-namespace-2 " , name : "test-name" ,
64
+ expectedActions : []string {"get" , "list" },
47
65
expected : & ServiceDetail {
48
66
ObjectMeta : common.ObjectMeta {
49
67
Name : "test-service" ,
50
68
Namespace : "test-namespace" ,
51
69
},
52
70
TypeMeta : common.TypeMeta {Kind : common .ResourceKindService },
53
71
InternalEndpoint : common.Endpoint {Host : "test-service.test-namespace" },
72
+ PodList : pod.PodList {Pods : []pod.Pod {}},
54
73
},
55
74
},
56
75
}
57
76
58
77
for _ , c := range cases {
59
78
fakeClient := testclient .NewSimpleFake (c .service )
79
+ fakeHeapsterClient := FakeHeapsterClient {client : testclient .NewSimpleFake ()}
60
80
61
- actual , _ := GetServiceDetail (fakeClient , c .namespace , c .name )
81
+ actual , _ := GetServiceDetail (fakeClient , fakeHeapsterClient , c .namespace , c .name )
62
82
63
83
actions := fakeClient .Actions ()
64
84
if len (actions ) != len (c .expectedActions ) {
@@ -80,3 +100,72 @@ func TestGetServiceDetail(t *testing.T) {
80
100
}
81
101
}
82
102
}
103
+
104
+ func TestGetServicePods (t * testing.T ) {
105
+ firstSelector := map [string ]string {"app" : "selector-1" }
106
+ secondSelector := map [string ]string {"app" : "selector-2" }
107
+ cases := []struct {
108
+ namespace string
109
+ serviceSelector map [string ]string
110
+ podList * api.PodList
111
+ expectedActions []string
112
+ expected * pod.PodList
113
+ }{
114
+ {
115
+ "test-namespace-1" , firstSelector ,
116
+ & api.PodList {Items : []api.Pod {}}, []string {"list" }, & pod.PodList {Pods : []pod.Pod {}},
117
+ }, {
118
+ "test-namespace-2" ,
119
+ firstSelector ,
120
+ & api.PodList {Items : []api.Pod {{ObjectMeta : api.ObjectMeta {
121
+ Name : "test-pod" ,
122
+ Labels : secondSelector ,
123
+ }}}},
124
+ []string {"list" },
125
+ & pod.PodList {Pods : []pod.Pod {}},
126
+ }, {
127
+ "test-namespace-3" ,
128
+ firstSelector ,
129
+ & api.PodList {Items : []api.Pod {{ObjectMeta : api.ObjectMeta {
130
+ Name : "test-pod" ,
131
+ Labels : firstSelector ,
132
+ Namespace : "test-namespace-3" ,
133
+ }}}},
134
+ []string {"list" },
135
+ & pod.PodList {Pods : []pod.Pod {{
136
+ ObjectMeta : common.ObjectMeta {
137
+ Name : "test-pod" ,
138
+ Labels : firstSelector ,
139
+ Namespace : "test-namespace-3" ,
140
+ },
141
+ TypeMeta : common.TypeMeta {Kind : common .ResourceKindPod },
142
+ }}},
143
+ },
144
+ }
145
+
146
+ for _ , c := range cases {
147
+ fakeClient := testclient .NewSimpleFake (c .podList )
148
+ fakeHeapsterClient := FakeHeapsterClient {client : testclient .NewSimpleFake ()}
149
+
150
+ actual , _ := GetServicePods (fakeClient , fakeHeapsterClient , c .namespace , c .serviceSelector )
151
+
152
+ actions := fakeClient .Actions ()
153
+ if len (actions ) != len (c .expectedActions ) {
154
+ t .Errorf ("Unexpected actions: %v, expected %d actions got %d" , actions ,
155
+ len (c .expectedActions ), len (actions ))
156
+ continue
157
+ }
158
+
159
+ for i , verb := range c .expectedActions {
160
+ if actions [i ].GetVerb () != verb {
161
+ t .Errorf ("Unexpected action: %+v, expected %s" ,
162
+ actions [i ], verb )
163
+ }
164
+ }
165
+
166
+ if ! reflect .DeepEqual (actual , c .expected ) {
167
+ t .Errorf ("GetServicePods(client, heapsterClient, %#v, %#v) == \n got %#v, \n expected %#v" ,
168
+ c .namespace , c .serviceSelector , actual , c .expected )
169
+ }
170
+ }
171
+ }
0 commit comments