@@ -18,11 +18,15 @@ package apiserver
18
18
19
19
import (
20
20
"fmt"
21
+ "net/http"
22
+ "net/http/httptest"
23
+ "net/url"
24
+ "strings"
21
25
"testing"
22
26
23
27
"github.com/davecgh/go-spew/spew"
24
28
25
- "k8s.io/api/core/v1"
29
+ v1 "k8s.io/api/core/v1"
26
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
31
v1listers "k8s.io/client-go/listers/core/v1"
28
32
clienttesting "k8s.io/client-go/testing"
@@ -99,10 +103,12 @@ func TestSync(t *testing.T) {
99
103
tests := []struct {
100
104
name string
101
105
102
- apiServiceName string
103
- apiServices []* apiregistration.APIService
104
- services []* v1.Service
105
- endpoints []* v1.Endpoints
106
+ apiServiceName string
107
+ apiServices []* apiregistration.APIService
108
+ services []* v1.Service
109
+ endpoints []* v1.Endpoints
110
+ forceDiscoveryFail bool
111
+
106
112
expectedAvailability apiregistration.APIServiceCondition
107
113
}{
108
114
{
@@ -200,66 +206,97 @@ func TestSync(t *testing.T) {
200
206
Message : `all checks passed` ,
201
207
},
202
208
},
209
+ {
210
+ name : "remote-bad-return" ,
211
+ apiServiceName : "remote.group" ,
212
+ apiServices : []* apiregistration.APIService {newRemoteAPIService ("remote.group" )},
213
+ services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
214
+ endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , testServicePortName )},
215
+ forceDiscoveryFail : true ,
216
+ expectedAvailability : apiregistration.APIServiceCondition {
217
+ Type : apiregistration .Available ,
218
+ Status : apiregistration .ConditionFalse ,
219
+ Reason : "FailedDiscoveryCheck" ,
220
+ Message : `failing or missing response from` ,
221
+ },
222
+ },
203
223
}
204
224
205
225
for _ , tc := range tests {
206
- fakeClient := fake .NewSimpleClientset ()
207
- apiServiceIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
208
- serviceIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
209
- endpointsIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
210
- for _ , obj := range tc .apiServices {
211
- apiServiceIndexer .Add (obj )
212
- }
213
- for _ , obj := range tc .services {
214
- serviceIndexer .Add (obj )
215
- }
216
- for _ , obj := range tc .endpoints {
217
- endpointsIndexer .Add (obj )
218
- }
226
+ t .Run (tc .name , func (t * testing.T ) {
227
+ fakeClient := fake .NewSimpleClientset ()
228
+ apiServiceIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
229
+ serviceIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
230
+ endpointsIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
231
+ for _ , obj := range tc .apiServices {
232
+ apiServiceIndexer .Add (obj )
233
+ }
234
+ for _ , obj := range tc .services {
235
+ serviceIndexer .Add (obj )
236
+ }
237
+ for _ , obj := range tc .endpoints {
238
+ endpointsIndexer .Add (obj )
239
+ }
240
+
241
+ testServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
242
+ if ! tc .forceDiscoveryFail {
243
+ w .WriteHeader (http .StatusOK )
244
+ }
245
+ w .WriteHeader (http .StatusForbidden )
246
+ }))
247
+ defer testServer .Close ()
219
248
220
- c := AvailableConditionController {
221
- apiServiceClient : fakeClient .Apiregistration (),
222
- apiServiceLister : listers .NewAPIServiceLister (apiServiceIndexer ),
223
- serviceLister : v1listers .NewServiceLister (serviceIndexer ),
224
- endpointsLister : v1listers .NewEndpointsLister (endpointsIndexer ),
225
- }
226
- c .sync (tc .apiServiceName )
249
+ c := AvailableConditionController {
250
+ apiServiceClient : fakeClient .Apiregistration (),
251
+ apiServiceLister : listers .NewAPIServiceLister (apiServiceIndexer ),
252
+ serviceLister : v1listers .NewServiceLister (serviceIndexer ),
253
+ endpointsLister : v1listers .NewEndpointsLister (endpointsIndexer ),
254
+ discoveryClient : testServer .Client (),
255
+ serviceResolver : & fakeServiceResolver {url : testServer .URL },
256
+ }
257
+ c .sync (tc .apiServiceName )
227
258
228
- // ought to have one action writing status
229
- if e , a := 1 , len (fakeClient .Actions ()); e != a {
230
- t .Errorf ("%v expected %v, got %v" , tc .name , e , fakeClient .Actions ())
231
- continue
232
- }
259
+ // ought to have one action writing status
260
+ if e , a := 1 , len (fakeClient .Actions ()); e != a {
261
+ t .Fatalf ("%v expected %v, got %v" , tc .name , e , fakeClient .Actions ())
262
+ }
233
263
234
- action , ok := fakeClient .Actions ()[0 ].(clienttesting.UpdateAction )
235
- if ! ok {
236
- t .Errorf ("%v got %v" , tc .name , ok )
237
- continue
238
- }
264
+ action , ok := fakeClient .Actions ()[0 ].(clienttesting.UpdateAction )
265
+ if ! ok {
266
+ t .Fatalf ("%v got %v" , tc .name , ok )
267
+ }
239
268
240
- if e , a := 1 , len (action .GetObject ().(* apiregistration.APIService ).Status .Conditions ); e != a {
241
- t . Errorf ("%v expected %v, got %v" , tc .name , e , action .GetObject ())
242
- continue
243
- }
244
- condition := action . GetObject ().( * apiregistration. APIService ). Status . Conditions [ 0 ]
245
- if e , a := tc .expectedAvailability . Type , condition . Type ; e != a {
246
- t . Errorf ( "%v expected %v, got %#v" , tc . name , e , condition )
247
- }
248
- if e , a := tc .expectedAvailability . Status , condition . Status ; e != a {
249
- t . Errorf ( "%v expected %v, got %#v" , tc . name , e , condition )
250
- }
251
- if e , a := tc .expectedAvailability . Reason , condition . Reason ; e != a {
252
- t . Errorf ( "%v expected %v, got %#v" , tc . name , e , condition )
253
- }
254
- if e , a := tc .expectedAvailability . Message , condition . Message ; e != a {
255
- t . Errorf ( "%v expected %v, got %#v" , tc . name , e , condition )
256
- }
257
- if condition . LastTransitionTime . IsZero () {
258
- t . Error ( "expected lastTransitionTime to be non-zero" )
259
- }
269
+ if e , a := 1 , len (action .GetObject ().(* apiregistration.APIService ).Status .Conditions ); e != a {
270
+ t . Fatalf ("%v expected %v, got %v" , tc .name , e , action .GetObject ())
271
+ }
272
+ condition := action . GetObject ().( * apiregistration. APIService ). Status . Conditions [ 0 ]
273
+ if e , a := tc . expectedAvailability . Type , condition . Type ; e != a {
274
+ t . Errorf ( "%v expected %v, got %#v" , tc .name , e , condition )
275
+ }
276
+ if e , a := tc . expectedAvailability . Status , condition . Status ; e != a {
277
+ t . Errorf ( "%v expected %v, got %#v" , tc .name , e , condition )
278
+ }
279
+ if e , a := tc . expectedAvailability . Reason , condition . Reason ; e != a {
280
+ t . Errorf ( "%v expected %v, got %#v" , tc .name , e , condition )
281
+ }
282
+ if e , a := tc . expectedAvailability . Message , condition . Message ; ! strings . HasPrefix ( a , e ) {
283
+ t . Errorf ( "%v expected %v, got %#v" , tc .name , e , condition )
284
+ }
285
+ if condition . LastTransitionTime . IsZero () {
286
+ t . Error ( "expected lastTransitionTime to be non-zero" )
287
+ }
288
+ })
260
289
}
261
290
}
262
291
292
+ type fakeServiceResolver struct {
293
+ url string
294
+ }
295
+
296
+ func (f * fakeServiceResolver ) ResolveEndpoint (namespace , name string , port int32 ) (* url.URL , error ) {
297
+ return url .Parse (f .url )
298
+ }
299
+
263
300
func TestUpdateAPIServiceStatus (t * testing.T ) {
264
301
foo := & apiregistration.APIService {Status : apiregistration.APIServiceStatus {Conditions : []apiregistration.APIServiceCondition {{Type : "foo" }}}}
265
302
bar := & apiregistration.APIService {Status : apiregistration.APIServiceStatus {Conditions : []apiregistration.APIServiceCondition {{Type : "bar" }}}}
0 commit comments