Use randfill, do API renames

Kubernetes-commit: e54719bb6674fac228671e0786d19c2cf27b08a3
This commit is contained in:
Tim Hockin 2025-02-20 09:45:22 -08:00 committed by Kubernetes Publisher
parent b3c8744140
commit 4a6072d89f
3 changed files with 68 additions and 68 deletions

View File

@ -39,9 +39,9 @@ import (
"k8s.io/client-go/util/flowcontrol" "k8s.io/client-go/util/flowcontrol"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"sigs.k8s.io/randfill"
) )
func TestIsConfigTransportTLS(t *testing.T) { func TestIsConfigTransportTLS(t *testing.T) {
@ -313,60 +313,60 @@ func (fakeAuthProviderConfigPersister) Persist(map[string]string) error {
var fakeAuthProviderConfigPersisterError = errors.New("fakeAuthProviderConfigPersisterError") var fakeAuthProviderConfigPersisterError = errors.New("fakeAuthProviderConfigPersisterError")
func TestAnonymousAuthConfig(t *testing.T) { 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( f.Funcs(
func(r *runtime.Codec, f fuzz.Continue) { func(r *runtime.Codec, f randfill.Continue) {
codec := &fakeCodec{} codec := &fakeCodec{}
f.Fuzz(codec) f.Fill(codec)
*r = codec *r = codec
}, },
func(r *http.RoundTripper, f fuzz.Continue) { func(r *http.RoundTripper, f randfill.Continue) {
roundTripper := &fakeRoundTripper{} roundTripper := &fakeRoundTripper{}
f.Fuzz(roundTripper) f.Fill(roundTripper)
*r = 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 *fn = fakeWrapperFunc
}, },
func(fn *transport.WrapperFunc, f fuzz.Continue) { func(fn *transport.WrapperFunc, f randfill.Continue) {
*fn = fakeWrapperFunc *fn = fakeWrapperFunc
}, },
func(r *runtime.NegotiatedSerializer, f fuzz.Continue) { func(r *runtime.NegotiatedSerializer, f randfill.Continue) {
serializer := &fakeNegotiatedSerializer{} serializer := &fakeNegotiatedSerializer{}
f.Fuzz(serializer) f.Fill(serializer)
*r = serializer *r = serializer
}, },
func(r *flowcontrol.RateLimiter, f fuzz.Continue) { func(r *flowcontrol.RateLimiter, f randfill.Continue) {
limiter := &fakeLimiter{} limiter := &fakeLimiter{}
f.Fuzz(limiter) f.Fill(limiter)
*r = limiter *r = limiter
}, },
func(h *WarningHandler, f fuzz.Continue) { func(h *WarningHandler, f randfill.Continue) {
*h = &fakeWarningHandler{} *h = &fakeWarningHandler{}
}, },
func(h *WarningHandlerWithContext, f fuzz.Continue) { func(h *WarningHandlerWithContext, f randfill.Continue) {
*h = &fakeWarningHandlerWithContext{} *h = &fakeWarningHandlerWithContext{}
}, },
// Authentication does not require fuzzer // Authentication does not require fuzzer
func(r *AuthProviderConfigPersister, f fuzz.Continue) {}, func(r *AuthProviderConfigPersister, f randfill.Continue) {},
func(r *clientcmdapi.AuthProviderConfig, f fuzz.Continue) { func(r *clientcmdapi.AuthProviderConfig, f randfill.Continue) {
r.Config = map[string]string{} 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 *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 *r = fakeProxyFunc
}, },
func(r *runtime.Object, f fuzz.Continue) { func(r *runtime.Object, f randfill.Continue) {
unknown := &runtime.Unknown{} unknown := &runtime.Unknown{}
f.Fuzz(unknown) f.Fill(unknown)
*r = unknown *r = unknown
}, },
) )
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
original := &Config{} original := &Config{}
f.Fuzz(original) f.Fill(original)
actual := AnonymousClientConfig(original) actual := AnonymousClientConfig(original)
expected := *original expected := *original
@ -414,58 +414,58 @@ func TestAnonymousAuthConfig(t *testing.T) {
} }
func TestCopyConfig(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( f.Funcs(
func(r *runtime.Codec, f fuzz.Continue) { func(r *runtime.Codec, f randfill.Continue) {
codec := &fakeCodec{} codec := &fakeCodec{}
f.Fuzz(codec) f.Fill(codec)
*r = codec *r = codec
}, },
func(r *http.RoundTripper, f fuzz.Continue) { func(r *http.RoundTripper, f randfill.Continue) {
roundTripper := &fakeRoundTripper{} roundTripper := &fakeRoundTripper{}
f.Fuzz(roundTripper) f.Fill(roundTripper)
*r = 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 *fn = fakeWrapperFunc
}, },
func(fn *transport.WrapperFunc, f fuzz.Continue) { func(fn *transport.WrapperFunc, f randfill.Continue) {
*fn = fakeWrapperFunc *fn = fakeWrapperFunc
}, },
func(r *runtime.NegotiatedSerializer, f fuzz.Continue) { func(r *runtime.NegotiatedSerializer, f randfill.Continue) {
serializer := &fakeNegotiatedSerializer{} serializer := &fakeNegotiatedSerializer{}
f.Fuzz(serializer) f.Fill(serializer)
*r = serializer *r = serializer
}, },
func(r *flowcontrol.RateLimiter, f fuzz.Continue) { func(r *flowcontrol.RateLimiter, f randfill.Continue) {
limiter := &fakeLimiter{} limiter := &fakeLimiter{}
f.Fuzz(limiter) f.Fill(limiter)
*r = limiter *r = limiter
}, },
func(h *WarningHandler, f fuzz.Continue) { func(h *WarningHandler, f randfill.Continue) {
*h = &fakeWarningHandler{} *h = &fakeWarningHandler{}
}, },
func(h *WarningHandlerWithContext, f fuzz.Continue) { func(h *WarningHandlerWithContext, f randfill.Continue) {
*h = &fakeWarningHandlerWithContext{} *h = &fakeWarningHandlerWithContext{}
}, },
func(r *AuthProviderConfigPersister, f fuzz.Continue) { func(r *AuthProviderConfigPersister, f randfill.Continue) {
*r = fakeAuthProviderConfigPersister{} *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 *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 *r = fakeProxyFunc
}, },
func(r *runtime.Object, f fuzz.Continue) { func(r *runtime.Object, f randfill.Continue) {
unknown := &runtime.Unknown{} unknown := &runtime.Unknown{}
f.Fuzz(unknown) f.Fill(unknown)
*r = unknown *r = unknown
}, },
) )
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
original := &Config{} original := &Config{}
f.Fuzz(original) f.Fill(original)
actual := CopyConfig(original) actual := CopyConfig(original)
expected := *original expected := *original

View File

@ -26,12 +26,12 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
clientauthenticationapi "k8s.io/client-go/pkg/apis/clientauthentication" clientauthenticationapi "k8s.io/client-go/pkg/apis/clientauthentication"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api" clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/transport" "k8s.io/client-go/transport"
"k8s.io/client-go/util/flowcontrol" "k8s.io/client-go/util/flowcontrol"
"sigs.k8s.io/randfill"
) )
func TestConfigToExecCluster(t *testing.T) { func TestConfigToExecCluster(t *testing.T) {
@ -211,60 +211,60 @@ func TestConfigToExecCluster(t *testing.T) {
func TestConfigToExecClusterRoundtrip(t *testing.T) { func TestConfigToExecClusterRoundtrip(t *testing.T) {
t.Parallel() t.Parallel()
f := fuzz.New().NilChance(0.5).NumElements(1, 1) f := randfill.New().NilChance(0.5).NumElements(1, 1)
f.Funcs( f.Funcs(
func(r *runtime.Codec, f fuzz.Continue) { func(r *runtime.Codec, f randfill.Continue) {
codec := &fakeCodec{} codec := &fakeCodec{}
f.Fuzz(codec) f.Fill(codec)
*r = codec *r = codec
}, },
func(r *http.RoundTripper, f fuzz.Continue) { func(r *http.RoundTripper, f randfill.Continue) {
roundTripper := &fakeRoundTripper{} roundTripper := &fakeRoundTripper{}
f.Fuzz(roundTripper) f.Fill(roundTripper)
*r = 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 *fn = fakeWrapperFunc
}, },
func(fn *transport.WrapperFunc, f fuzz.Continue) { func(fn *transport.WrapperFunc, f randfill.Continue) {
*fn = fakeWrapperFunc *fn = fakeWrapperFunc
}, },
func(r *runtime.NegotiatedSerializer, f fuzz.Continue) { func(r *runtime.NegotiatedSerializer, f randfill.Continue) {
serializer := &fakeNegotiatedSerializer{} serializer := &fakeNegotiatedSerializer{}
f.Fuzz(serializer) f.Fill(serializer)
*r = serializer *r = serializer
}, },
func(r *flowcontrol.RateLimiter, f fuzz.Continue) { func(r *flowcontrol.RateLimiter, f randfill.Continue) {
limiter := &fakeLimiter{} limiter := &fakeLimiter{}
f.Fuzz(limiter) f.Fill(limiter)
*r = limiter *r = limiter
}, },
func(h *WarningHandler, f fuzz.Continue) { func(h *WarningHandler, f randfill.Continue) {
*h = &fakeWarningHandler{} *h = &fakeWarningHandler{}
}, },
func(h *WarningHandlerWithContext, f fuzz.Continue) { func(h *WarningHandlerWithContext, f randfill.Continue) {
*h = &fakeWarningHandlerWithContext{} *h = &fakeWarningHandlerWithContext{}
}, },
// Authentication does not require fuzzer // Authentication does not require fuzzer
func(r *AuthProviderConfigPersister, f fuzz.Continue) {}, func(r *AuthProviderConfigPersister, f randfill.Continue) {},
func(r *clientcmdapi.AuthProviderConfig, f fuzz.Continue) { func(r *clientcmdapi.AuthProviderConfig, f randfill.Continue) {
r.Config = map[string]string{} 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 *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 *r = fakeProxyFunc
}, },
func(r *runtime.Object, f fuzz.Continue) { func(r *runtime.Object, f randfill.Continue) {
unknown := &runtime.Unknown{} unknown := &runtime.Unknown{}
f.Fuzz(unknown) f.Fill(unknown)
*r = unknown *r = unknown
}, },
) )
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
expected := &Config{} 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 // 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. // 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) { func TestExecClusterToConfigRoundtrip(t *testing.T) {
t.Parallel() t.Parallel()
f := fuzz.New().NilChance(0.5).NumElements(1, 1) f := randfill.New().NilChance(0.5).NumElements(1, 1)
f.Funcs( 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 // 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. // comes back from the roundtrip, so just set it to nil.
*r = nil *r = nil
@ -357,7 +357,7 @@ func TestExecClusterToConfigRoundtrip(t *testing.T) {
) )
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
expected := &clientauthenticationapi.Cluster{} 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. // Manually set URLs so we don't get an error when parsing these during the roundtrip.
if expected.Server != "" { if expected.Server != "" {

View File

@ -35,7 +35,7 @@ import (
fcache "k8s.io/client-go/tools/cache/testing" fcache "k8s.io/client-go/tools/cache/testing"
"k8s.io/klog/v2/ktesting" "k8s.io/klog/v2/ktesting"
fuzz "github.com/google/gofuzz" "sigs.k8s.io/randfill"
) )
func Example() { func Example() {
@ -252,12 +252,12 @@ func TestHammerController(t *testing.T) {
// Let's add a few objects to the source. // Let's add a few objects to the source.
currentNames := sets.String{} currentNames := sets.String{}
rs := rand.NewSource(rand.Int63()) 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++ { for i := 0; i < 100; i++ {
var name string var name string
var isNew bool var isNew bool
if currentNames.Len() == 0 || rand.Intn(3) == 1 { if currentNames.Len() == 0 || rand.Intn(3) == 1 {
f.Fuzz(&name) f.Fill(&name)
isNew = true isNew = true
} else { } else {
l := currentNames.List() l := currentNames.List()
@ -265,7 +265,7 @@ func TestHammerController(t *testing.T) {
} }
pod := &v1.Pod{} pod := &v1.Pod{}
f.Fuzz(pod) f.Fill(pod)
pod.ObjectMeta.Name = name pod.ObjectMeta.Name = name
pod.ObjectMeta.Namespace = "default" pod.ObjectMeta.Namespace = "default"
// Add, update, or delete randomly. // Add, update, or delete randomly.