From 4a6072d89f5803914c9116743032240bd30a642f Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 20 Feb 2025 09:45:22 -0800 Subject: [PATCH] Use randfill, do API renames Kubernetes-commit: e54719bb6674fac228671e0786d19c2cf27b08a3 --- rest/config_test.go | 80 +++++++++++++++++----------------- rest/exec_test.go | 48 ++++++++++---------- tools/cache/controller_test.go | 8 ++-- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/rest/config_test.go b/rest/config_test.go index 6475813f..9323d4cc 100644 --- a/rest/config_test.go +++ b/rest/config_test.go @@ -39,9 +39,9 @@ import ( "k8s.io/client-go/util/flowcontrol" "github.com/google/go-cmp/cmp" - fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "sigs.k8s.io/randfill" ) func TestIsConfigTransportTLS(t *testing.T) { @@ -313,60 +313,60 @@ func (fakeAuthProviderConfigPersister) Persist(map[string]string) error { var fakeAuthProviderConfigPersisterError = errors.New("fakeAuthProviderConfigPersisterError") func TestAnonymousAuthConfig(t *testing.T) { - f := fuzz.New().NilChance(0.0).NumElements(1, 1) + f := randfill.New().NilChance(0.0).NumElements(1, 1) f.Funcs( - func(r *runtime.Codec, f fuzz.Continue) { + func(r *runtime.Codec, f randfill.Continue) { codec := &fakeCodec{} - f.Fuzz(codec) + f.Fill(codec) *r = codec }, - func(r *http.RoundTripper, f fuzz.Continue) { + func(r *http.RoundTripper, f randfill.Continue) { roundTripper := &fakeRoundTripper{} - f.Fuzz(roundTripper) + f.Fill(roundTripper) *r = roundTripper }, - func(fn *func(http.RoundTripper) http.RoundTripper, f fuzz.Continue) { + func(fn *func(http.RoundTripper) http.RoundTripper, f randfill.Continue) { *fn = fakeWrapperFunc }, - func(fn *transport.WrapperFunc, f fuzz.Continue) { + func(fn *transport.WrapperFunc, f randfill.Continue) { *fn = fakeWrapperFunc }, - func(r *runtime.NegotiatedSerializer, f fuzz.Continue) { + func(r *runtime.NegotiatedSerializer, f randfill.Continue) { serializer := &fakeNegotiatedSerializer{} - f.Fuzz(serializer) + f.Fill(serializer) *r = serializer }, - func(r *flowcontrol.RateLimiter, f fuzz.Continue) { + func(r *flowcontrol.RateLimiter, f randfill.Continue) { limiter := &fakeLimiter{} - f.Fuzz(limiter) + f.Fill(limiter) *r = limiter }, - func(h *WarningHandler, f fuzz.Continue) { + func(h *WarningHandler, f randfill.Continue) { *h = &fakeWarningHandler{} }, - func(h *WarningHandlerWithContext, f fuzz.Continue) { + func(h *WarningHandlerWithContext, f randfill.Continue) { *h = &fakeWarningHandlerWithContext{} }, // Authentication does not require fuzzer - func(r *AuthProviderConfigPersister, f fuzz.Continue) {}, - func(r *clientcmdapi.AuthProviderConfig, f fuzz.Continue) { + func(r *AuthProviderConfigPersister, f randfill.Continue) {}, + func(r *clientcmdapi.AuthProviderConfig, f randfill.Continue) { r.Config = map[string]string{} }, - func(r *func(ctx context.Context, network, addr string) (net.Conn, error), f fuzz.Continue) { + func(r *func(ctx context.Context, network, addr string) (net.Conn, error), f randfill.Continue) { *r = fakeDialFunc }, - func(r *func(*http.Request) (*url.URL, error), f fuzz.Continue) { + func(r *func(*http.Request) (*url.URL, error), f randfill.Continue) { *r = fakeProxyFunc }, - func(r *runtime.Object, f fuzz.Continue) { + func(r *runtime.Object, f randfill.Continue) { unknown := &runtime.Unknown{} - f.Fuzz(unknown) + f.Fill(unknown) *r = unknown }, ) for i := 0; i < 20; i++ { original := &Config{} - f.Fuzz(original) + f.Fill(original) actual := AnonymousClientConfig(original) expected := *original @@ -414,58 +414,58 @@ func TestAnonymousAuthConfig(t *testing.T) { } func TestCopyConfig(t *testing.T) { - f := fuzz.New().NilChance(0.0).NumElements(1, 1) + f := randfill.New().NilChance(0.0).NumElements(1, 1) f.Funcs( - func(r *runtime.Codec, f fuzz.Continue) { + func(r *runtime.Codec, f randfill.Continue) { codec := &fakeCodec{} - f.Fuzz(codec) + f.Fill(codec) *r = codec }, - func(r *http.RoundTripper, f fuzz.Continue) { + func(r *http.RoundTripper, f randfill.Continue) { roundTripper := &fakeRoundTripper{} - f.Fuzz(roundTripper) + f.Fill(roundTripper) *r = roundTripper }, - func(fn *func(http.RoundTripper) http.RoundTripper, f fuzz.Continue) { + func(fn *func(http.RoundTripper) http.RoundTripper, f randfill.Continue) { *fn = fakeWrapperFunc }, - func(fn *transport.WrapperFunc, f fuzz.Continue) { + func(fn *transport.WrapperFunc, f randfill.Continue) { *fn = fakeWrapperFunc }, - func(r *runtime.NegotiatedSerializer, f fuzz.Continue) { + func(r *runtime.NegotiatedSerializer, f randfill.Continue) { serializer := &fakeNegotiatedSerializer{} - f.Fuzz(serializer) + f.Fill(serializer) *r = serializer }, - func(r *flowcontrol.RateLimiter, f fuzz.Continue) { + func(r *flowcontrol.RateLimiter, f randfill.Continue) { limiter := &fakeLimiter{} - f.Fuzz(limiter) + f.Fill(limiter) *r = limiter }, - func(h *WarningHandler, f fuzz.Continue) { + func(h *WarningHandler, f randfill.Continue) { *h = &fakeWarningHandler{} }, - func(h *WarningHandlerWithContext, f fuzz.Continue) { + func(h *WarningHandlerWithContext, f randfill.Continue) { *h = &fakeWarningHandlerWithContext{} }, - func(r *AuthProviderConfigPersister, f fuzz.Continue) { + func(r *AuthProviderConfigPersister, f randfill.Continue) { *r = fakeAuthProviderConfigPersister{} }, - func(r *func(ctx context.Context, network, addr string) (net.Conn, error), f fuzz.Continue) { + func(r *func(ctx context.Context, network, addr string) (net.Conn, error), f randfill.Continue) { *r = fakeDialFunc }, - func(r *func(*http.Request) (*url.URL, error), f fuzz.Continue) { + func(r *func(*http.Request) (*url.URL, error), f randfill.Continue) { *r = fakeProxyFunc }, - func(r *runtime.Object, f fuzz.Continue) { + func(r *runtime.Object, f randfill.Continue) { unknown := &runtime.Unknown{} - f.Fuzz(unknown) + f.Fill(unknown) *r = unknown }, ) for i := 0; i < 20; i++ { original := &Config{} - f.Fuzz(original) + f.Fill(original) actual := CopyConfig(original) expected := *original diff --git a/rest/exec_test.go b/rest/exec_test.go index 6ab7bcc9..290b512c 100644 --- a/rest/exec_test.go +++ b/rest/exec_test.go @@ -26,12 +26,12 @@ import ( "testing" "github.com/google/go-cmp/cmp" - fuzz "github.com/google/gofuzz" "k8s.io/apimachinery/pkg/runtime" clientauthenticationapi "k8s.io/client-go/pkg/apis/clientauthentication" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "k8s.io/client-go/transport" "k8s.io/client-go/util/flowcontrol" + "sigs.k8s.io/randfill" ) func TestConfigToExecCluster(t *testing.T) { @@ -211,60 +211,60 @@ func TestConfigToExecCluster(t *testing.T) { func TestConfigToExecClusterRoundtrip(t *testing.T) { t.Parallel() - f := fuzz.New().NilChance(0.5).NumElements(1, 1) + f := randfill.New().NilChance(0.5).NumElements(1, 1) f.Funcs( - func(r *runtime.Codec, f fuzz.Continue) { + func(r *runtime.Codec, f randfill.Continue) { codec := &fakeCodec{} - f.Fuzz(codec) + f.Fill(codec) *r = codec }, - func(r *http.RoundTripper, f fuzz.Continue) { + func(r *http.RoundTripper, f randfill.Continue) { roundTripper := &fakeRoundTripper{} - f.Fuzz(roundTripper) + f.Fill(roundTripper) *r = roundTripper }, - func(fn *func(http.RoundTripper) http.RoundTripper, f fuzz.Continue) { + func(fn *func(http.RoundTripper) http.RoundTripper, f randfill.Continue) { *fn = fakeWrapperFunc }, - func(fn *transport.WrapperFunc, f fuzz.Continue) { + func(fn *transport.WrapperFunc, f randfill.Continue) { *fn = fakeWrapperFunc }, - func(r *runtime.NegotiatedSerializer, f fuzz.Continue) { + func(r *runtime.NegotiatedSerializer, f randfill.Continue) { serializer := &fakeNegotiatedSerializer{} - f.Fuzz(serializer) + f.Fill(serializer) *r = serializer }, - func(r *flowcontrol.RateLimiter, f fuzz.Continue) { + func(r *flowcontrol.RateLimiter, f randfill.Continue) { limiter := &fakeLimiter{} - f.Fuzz(limiter) + f.Fill(limiter) *r = limiter }, - func(h *WarningHandler, f fuzz.Continue) { + func(h *WarningHandler, f randfill.Continue) { *h = &fakeWarningHandler{} }, - func(h *WarningHandlerWithContext, f fuzz.Continue) { + func(h *WarningHandlerWithContext, f randfill.Continue) { *h = &fakeWarningHandlerWithContext{} }, // Authentication does not require fuzzer - func(r *AuthProviderConfigPersister, f fuzz.Continue) {}, - func(r *clientcmdapi.AuthProviderConfig, f fuzz.Continue) { + func(r *AuthProviderConfigPersister, f randfill.Continue) {}, + func(r *clientcmdapi.AuthProviderConfig, f randfill.Continue) { r.Config = map[string]string{} }, - func(r *func(ctx context.Context, network, addr string) (net.Conn, error), f fuzz.Continue) { + func(r *func(ctx context.Context, network, addr string) (net.Conn, error), f randfill.Continue) { *r = fakeDialFunc }, - func(r *func(*http.Request) (*url.URL, error), f fuzz.Continue) { + func(r *func(*http.Request) (*url.URL, error), f randfill.Continue) { *r = fakeProxyFunc }, - func(r *runtime.Object, f fuzz.Continue) { + func(r *runtime.Object, f randfill.Continue) { unknown := &runtime.Unknown{} - f.Fuzz(unknown) + f.Fill(unknown) *r = unknown }, ) for i := 0; i < 100; i++ { expected := &Config{} - f.Fuzz(expected) + f.Fill(expected) // This is the list of known fields that this roundtrip doesn't care about. We should add new // fields to this list if we don't want to roundtrip them on exec cluster conversion. @@ -347,9 +347,9 @@ func TestConfigToExecClusterRoundtrip(t *testing.T) { func TestExecClusterToConfigRoundtrip(t *testing.T) { t.Parallel() - f := fuzz.New().NilChance(0.5).NumElements(1, 1) + f := randfill.New().NilChance(0.5).NumElements(1, 1) f.Funcs( - func(r *runtime.Object, f fuzz.Continue) { + func(r *runtime.Object, f randfill.Continue) { // We don't expect the clientauthentication.Cluster.Config to show up in the Config that // comes back from the roundtrip, so just set it to nil. *r = nil @@ -357,7 +357,7 @@ func TestExecClusterToConfigRoundtrip(t *testing.T) { ) for i := 0; i < 100; i++ { expected := &clientauthenticationapi.Cluster{} - f.Fuzz(expected) + f.Fill(expected) // Manually set URLs so we don't get an error when parsing these during the roundtrip. if expected.Server != "" { diff --git a/tools/cache/controller_test.go b/tools/cache/controller_test.go index 9067fc15..d76cc634 100644 --- a/tools/cache/controller_test.go +++ b/tools/cache/controller_test.go @@ -35,7 +35,7 @@ import ( fcache "k8s.io/client-go/tools/cache/testing" "k8s.io/klog/v2/ktesting" - fuzz "github.com/google/gofuzz" + "sigs.k8s.io/randfill" ) func Example() { @@ -252,12 +252,12 @@ func TestHammerController(t *testing.T) { // Let's add a few objects to the source. currentNames := sets.String{} rs := rand.NewSource(rand.Int63()) - f := fuzz.New().NilChance(.5).NumElements(0, 2).RandSource(rs) + f := randfill.New().NilChance(.5).NumElements(0, 2).RandSource(rs) for i := 0; i < 100; i++ { var name string var isNew bool if currentNames.Len() == 0 || rand.Intn(3) == 1 { - f.Fuzz(&name) + f.Fill(&name) isNew = true } else { l := currentNames.List() @@ -265,7 +265,7 @@ func TestHammerController(t *testing.T) { } pod := &v1.Pod{} - f.Fuzz(pod) + f.Fill(pod) pod.ObjectMeta.Name = name pod.ObjectMeta.Namespace = "default" // Add, update, or delete randomly.