@@ -3,10 +3,12 @@ package test
33import (
44 "encoding/json"
55 "errors"
6+ "fmt"
67 "io"
78 "net/http"
89 "net/http/httptest"
910 "path/filepath"
11+ "strings"
1012 "testing"
1113
1214 "github.com/stretchr/testify/require"
@@ -186,7 +188,10 @@ WaitForStreams:
186188 return ctx , nil
187189}
188190
189- type DiscoveryClientHandler struct {}
191+ type DiscoveryClientHandler struct {
192+ V1Resources []string
193+ Groups []string
194+ }
190195
191196var _ http.Handler = (* DiscoveryClientHandler )(nil )
192197
@@ -200,14 +205,24 @@ func (h *DiscoveryClientHandler) ServeHTTP(w http.ResponseWriter, req *http.Requ
200205 // Request Performed by DiscoveryClient to Kube API (Get API Groups)
201206 if req .URL .Path == "/apis" {
202207 w .Header ().Set ("Content-Type" , "application/json" )
203- _ , _ = w .Write ([]byte (`{"kind":"APIGroupList","apiVersion":"v1","groups":[]}` ))
208+ _ , _ = fmt .Fprintf (w , `{"kind":"APIGroupList","apiVersion":"v1","groups":[%s]}` , strings .Join (append (h .Groups ,
209+ `{"name":"apps","versions":[{"groupVersion":"apps/v1","version":"v1"}],"preferredVersion":{"groupVersion":"apps/v1","version":"v1"}}` ,
210+ ), "," ))
204211 return
205212 }
206213 // Request Performed by DiscoveryClient to Kube API (Get API Resources)
207214 if req .URL .Path == "/api/v1" {
208215 w .Header ().Set ("Content-Type" , "application/json" )
209- _ , _ = w .Write ([]byte (`{"kind":"APIResourceList","apiVersion":"v1","resources":[
210- {"name":"pods","singularName":"","namespaced":true,"kind":"Pod","verbs":["get","list","watch","create","update","patch","delete"]}
216+ _ , _ = fmt .Fprintf (w , `{"kind":"APIResourceList","apiVersion":"v1","resources":[%s]}` , strings .Join (append (h .V1Resources ,
217+ `{"name":"nodes","singularName":"","namespaced":false,"kind":"Node","verbs":["get","list","watch"]}` ,
218+ `{"name":"pods","singularName":"","namespaced":true,"kind":"Pod","verbs":["get","list","watch","create","update","patch","delete"]}` ,
219+ ), "," ))
220+ return
221+ }
222+ if req .URL .Path == "/apis/apps/v1" {
223+ w .Header ().Set ("Content-Type" , "application/json" )
224+ _ , _ = w .Write ([]byte (`{"kind":"APIResourceList","apiVersion":"v1","groupVersion":"apps/v1","resources":[
225+ {"name":"deployments","singularName":"","namespaced":true,"kind":"Deployment","verbs":["get","list","watch","create","update","patch","delete"]}
211226 ]}` ))
212227 return
213228 }
@@ -264,12 +279,27 @@ const tokenReviewSuccessful = `
264279 }`
265280
266281type TokenReviewHandler struct {
282+ DiscoveryClientHandler
267283 TokenReviewed bool
268284}
269285
270286var _ http.Handler = (* TokenReviewHandler )(nil )
271287
288+ func NewTokenReviewHandler () * TokenReviewHandler {
289+ trh := & TokenReviewHandler {}
290+ trh .Groups = []string {
291+ `{"name":"authentication.k8s.io","versions":[{"groupVersion":"authentication.k8s.io/v1","version":"v1"}],"preferredVersion":{"groupVersion":"authentication.k8s.io/v1","version":"v1"}}` ,
292+ }
293+ return trh
294+ }
295+
272296func (h * TokenReviewHandler ) ServeHTTP (w http.ResponseWriter , req * http.Request ) {
297+ h .DiscoveryClientHandler .ServeHTTP (w , req )
298+ if req .URL .EscapedPath () == "/apis/authentication.k8s.io/v1" {
299+ w .Header ().Set ("Content-Type" , "application/json" )
300+ _ , _ = w .Write ([]byte (`{"kind":"APIResourceList","apiVersion":"v1","groupVersion":"authentication.k8s.io/v1","resources":[{"name":"tokenreviews","singularName":"","namespaced":false,"kind":"TokenReview","verbs":["create"]}]}` ))
301+ return
302+ }
273303 if req .URL .EscapedPath () == "/apis/authentication.k8s.io/v1/tokenreviews" {
274304 w .Header ().Set ("Content-Type" , "application/json" )
275305 _ , _ = w .Write ([]byte (tokenReviewSuccessful ))
0 commit comments