@@ -23,6 +23,7 @@ import (
23
23
"io"
24
24
"net"
25
25
"net/http"
26
+ "net/url"
26
27
"path/filepath"
27
28
"reflect"
28
29
"strings"
@@ -32,12 +33,12 @@ import (
32
33
v1 "k8s.io/api/core/v1"
33
34
"k8s.io/apimachinery/pkg/runtime"
34
35
"k8s.io/apimachinery/pkg/runtime/schema"
35
- "k8s.io/apimachinery/pkg/util/diff"
36
36
"k8s.io/client-go/kubernetes/scheme"
37
37
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
38
38
"k8s.io/client-go/transport"
39
39
"k8s.io/client-go/util/flowcontrol"
40
40
41
+ "github.com/google/go-cmp/cmp"
41
42
fuzz "github.com/google/gofuzz"
42
43
"github.com/stretchr/testify/assert"
43
44
)
@@ -274,8 +275,13 @@ func (n *fakeNegotiatedSerializer) DecoderToVersion(serializer runtime.Decoder,
274
275
var fakeDialFunc = func (ctx context.Context , network , addr string ) (net.Conn , error ) {
275
276
return nil , fakeDialerError
276
277
}
278
+
277
279
var fakeDialerError = errors .New ("fakedialer" )
278
280
281
+ func fakeProxyFunc (* http.Request ) (* url.URL , error ) {
282
+ return nil , errors .New ("fakeproxy" )
283
+ }
284
+
279
285
type fakeAuthProviderConfigPersister struct {}
280
286
281
287
func (fakeAuthProviderConfigPersister ) Persist (map [string ]string ) error {
@@ -318,8 +324,12 @@ func TestAnonymousConfig(t *testing.T) {
318
324
func (r * clientcmdapi.AuthProviderConfig , f fuzz.Continue ) {
319
325
r .Config = map [string ]string {}
320
326
},
321
- // Dial does not require fuzzer
322
- func (r * func (ctx context.Context , network , addr string ) (net.Conn , error ), f fuzz.Continue ) {},
327
+ func (r * func (ctx context.Context , network , addr string ) (net.Conn , error ), f fuzz.Continue ) {
328
+ * r = fakeDialFunc
329
+ },
330
+ func (r * func (* http.Request ) (* url.URL , error ), f fuzz.Continue ) {
331
+ * r = fakeProxyFunc
332
+ },
323
333
)
324
334
for i := 0 ; i < 20 ; i ++ {
325
335
original := & Config {}
@@ -350,13 +360,22 @@ func TestAnonymousConfig(t *testing.T) {
350
360
if ! reflect .DeepEqual (expectedError , actualError ) {
351
361
t .Fatalf ("AnonymousClientConfig dropped the Dial field" )
352
362
}
353
- } else {
354
- actual .Dial = nil
355
- expected .Dial = nil
356
363
}
364
+ actual .Dial = nil
365
+ expected .Dial = nil
357
366
358
- if ! reflect .DeepEqual (* actual , expected ) {
359
- t .Fatalf ("AnonymousClientConfig dropped unexpected fields, identify whether they are security related or not: %s" , diff .ObjectGoPrintDiff (expected , actual ))
367
+ if actual .Proxy != nil {
368
+ _ , actualError := actual .Proxy (nil )
369
+ _ , expectedError := expected .Proxy (nil )
370
+ if ! reflect .DeepEqual (expectedError , actualError ) {
371
+ t .Fatalf ("AnonymousClientConfig dropped the Proxy field" )
372
+ }
373
+ }
374
+ actual .Proxy = nil
375
+ expected .Proxy = nil
376
+
377
+ if diff := cmp .Diff (* actual , expected ); diff != "" {
378
+ t .Fatalf ("AnonymousClientConfig dropped unexpected fields, identify whether they are security related or not (-got, +want): %s" , diff )
360
379
}
361
380
}
362
381
}
@@ -396,6 +415,9 @@ func TestCopyConfig(t *testing.T) {
396
415
func (r * func (ctx context.Context , network , addr string ) (net.Conn , error ), f fuzz.Continue ) {
397
416
* r = fakeDialFunc
398
417
},
418
+ func (r * func (* http.Request ) (* url.URL , error ), f fuzz.Continue ) {
419
+ * r = fakeProxyFunc
420
+ },
399
421
)
400
422
for i := 0 ; i < 20 ; i ++ {
401
423
original := & Config {}
@@ -410,10 +432,10 @@ func TestCopyConfig(t *testing.T) {
410
432
// function return the expected object.
411
433
if actual .WrapTransport == nil || ! reflect .DeepEqual (expected .WrapTransport (nil ), & fakeRoundTripper {}) {
412
434
t .Fatalf ("CopyConfig dropped the WrapTransport field" )
413
- } else {
414
- actual .WrapTransport = nil
415
- expected .WrapTransport = nil
416
435
}
436
+ actual .WrapTransport = nil
437
+ expected .WrapTransport = nil
438
+
417
439
if actual .Dial != nil {
418
440
_ , actualError := actual .Dial (context .Background (), "" , "" )
419
441
_ , expectedError := expected .Dial (context .Background (), "" , "" )
@@ -423,6 +445,7 @@ func TestCopyConfig(t *testing.T) {
423
445
}
424
446
actual .Dial = nil
425
447
expected .Dial = nil
448
+
426
449
if actual .AuthConfigPersister != nil {
427
450
actualError := actual .AuthConfigPersister .Persist (nil )
428
451
expectedError := expected .AuthConfigPersister .Persist (nil )
@@ -433,8 +456,18 @@ func TestCopyConfig(t *testing.T) {
433
456
actual .AuthConfigPersister = nil
434
457
expected .AuthConfigPersister = nil
435
458
436
- if ! reflect .DeepEqual (* actual , expected ) {
437
- t .Fatalf ("CopyConfig dropped unexpected fields, identify whether they are security related or not: %s" , diff .ObjectReflectDiff (expected , * actual ))
459
+ if actual .Proxy != nil {
460
+ _ , actualError := actual .Proxy (nil )
461
+ _ , expectedError := expected .Proxy (nil )
462
+ if ! reflect .DeepEqual (expectedError , actualError ) {
463
+ t .Fatalf ("CopyConfig dropped the Proxy field" )
464
+ }
465
+ }
466
+ actual .Proxy = nil
467
+ expected .Proxy = nil
468
+
469
+ if diff := cmp .Diff (* actual , expected ); diff != "" {
470
+ t .Fatalf ("CopyConfig dropped unexpected fields, identify whether they are security related or not (-got, +want): %s" , diff )
438
471
}
439
472
}
440
473
}
@@ -564,10 +597,11 @@ func TestConfigSprint(t *testing.T) {
564
597
RateLimiter : & fakeLimiter {},
565
598
Timeout : 3 * time .Second ,
566
599
Dial : fakeDialFunc ,
600
+ Proxy : fakeProxyFunc ,
567
601
}
568
602
want := fmt .Sprintf (
569
- `&rest.Config{Host:"localhost:8080", APIPath:"v1", ContentConfig:rest.ContentConfig{AcceptContentTypes:"application/json", ContentType:"application/json", GroupVersion:(*schema.GroupVersion)(nil), NegotiatedSerializer:runtime.NegotiatedSerializer(nil)}, Username:"gopher", Password:"--- REDACTED ---", BearerToken:"--- REDACTED ---", BearerTokenFile:"", Impersonate:rest.ImpersonationConfig{UserName:"gopher2", Groups:[]string(nil), Extra:map[string][]string(nil)}, AuthProvider:api.AuthProviderConfig{Name: "gopher", Config: map[string]string{--- REDACTED ---}}, AuthConfigPersister:rest.AuthProviderConfigPersister(--- REDACTED ---), ExecProvider:api.AuthProviderConfig{Command: "sudo", Args: []string{"--- REDACTED ---"}, Env: []ExecEnvVar{--- REDACTED ---}, APIVersion: ""}, TLSClientConfig:rest.sanitizedTLSClientConfig{Insecure:false, ServerName:"", CertFile:"a.crt", KeyFile:"a.key", CAFile:"", CertData:[]uint8{0x2d, 0x2d, 0x2d, 0x20, 0x54, 0x52, 0x55, 0x4e, 0x43, 0x41, 0x54, 0x45, 0x44, 0x20, 0x2d, 0x2d, 0x2d}, KeyData:[]uint8{0x2d, 0x2d, 0x2d, 0x20, 0x52, 0x45, 0x44, 0x41, 0x43, 0x54, 0x45, 0x44, 0x20, 0x2d, 0x2d, 0x2d}, CAData:[]uint8(nil), NextProtos:[]string{"h2", "http/1.1"}}, UserAgent:"gobot", DisableCompression:false, Transport:(*rest.fakeRoundTripper)(%p), WrapTransport:(transport.WrapperFunc)(%p), QPS:1, Burst:2, RateLimiter:(*rest.fakeLimiter)(%p), Timeout:3000000000, Dial:(func(context.Context, string, string) (net.Conn, error))(%p)}` ,
570
- c .Transport , fakeWrapperFunc , c .RateLimiter , fakeDialFunc ,
603
+ `&rest.Config{Host:"localhost:8080", APIPath:"v1", ContentConfig:rest.ContentConfig{AcceptContentTypes:"application/json", ContentType:"application/json", GroupVersion:(*schema.GroupVersion)(nil), NegotiatedSerializer:runtime.NegotiatedSerializer(nil)}, Username:"gopher", Password:"--- REDACTED ---", BearerToken:"--- REDACTED ---", BearerTokenFile:"", Impersonate:rest.ImpersonationConfig{UserName:"gopher2", Groups:[]string(nil), Extra:map[string][]string(nil)}, AuthProvider:api.AuthProviderConfig{Name: "gopher", Config: map[string]string{--- REDACTED ---}}, AuthConfigPersister:rest.AuthProviderConfigPersister(--- REDACTED ---), ExecProvider:api.AuthProviderConfig{Command: "sudo", Args: []string{"--- REDACTED ---"}, Env: []ExecEnvVar{--- REDACTED ---}, APIVersion: ""}, TLSClientConfig:rest.sanitizedTLSClientConfig{Insecure:false, ServerName:"", CertFile:"a.crt", KeyFile:"a.key", CAFile:"", CertData:[]uint8{0x2d, 0x2d, 0x2d, 0x20, 0x54, 0x52, 0x55, 0x4e, 0x43, 0x41, 0x54, 0x45, 0x44, 0x20, 0x2d, 0x2d, 0x2d}, KeyData:[]uint8{0x2d, 0x2d, 0x2d, 0x20, 0x52, 0x45, 0x44, 0x41, 0x43, 0x54, 0x45, 0x44, 0x20, 0x2d, 0x2d, 0x2d}, CAData:[]uint8(nil), NextProtos:[]string{"h2", "http/1.1"}}, UserAgent:"gobot", DisableCompression:false, Transport:(*rest.fakeRoundTripper)(%p), WrapTransport:(transport.WrapperFunc)(%p), QPS:1, Burst:2, RateLimiter:(*rest.fakeLimiter)(%p), Timeout:3000000000, Dial:(func(context.Context, string, string) (net.Conn, error))(%p), Proxy:(func(*http.Request) (*url.URL, error))(%p)}` ,
604
+ c .Transport , fakeWrapperFunc , c .RateLimiter , fakeDialFunc , fakeProxyFunc ,
571
605
)
572
606
573
607
for _ , f := range []string {"%s" , "%v" , "%+v" , "%#v" } {
0 commit comments