Skip to content

Commit a2e6694

Browse files
committed
Switch from gofuzz to randfill
Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent fcb7d9e commit a2e6694

File tree

2 files changed

+80
-80
lines changed

2 files changed

+80
-80
lines changed

sdk/apis/apis/fuzzer/fuzzer.go

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package fuzzer
1919
import (
2020
"strings"
2121

22-
fuzz "github.com/google/gofuzz"
22+
"sigs.k8s.io/randfill"
2323

2424
corev1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -32,36 +32,36 @@ import (
3232
// Funcs returns the fuzzer functions for the apiserverinternal api group.
3333
func Funcs(codecs runtimeserializer.CodecFactory) []any {
3434
return []any{
35-
func(r *metav1.ManagedFieldsEntry, c fuzz.Continue) {
35+
func(r *metav1.ManagedFieldsEntry, c randfill.Continue) {
3636
// match the fuzzer default content for runtime.Object
3737
r.APIVersion = "v1"
3838
},
39-
func(r *v1alpha2.APIExport, c fuzz.Continue) {
40-
c.FuzzNoCustom(r)
39+
func(r *v1alpha2.APIExport, c randfill.Continue) {
40+
c.FillNoCustom(r)
4141
r.TypeMeta = metav1.TypeMeta{}
4242
r.Kind = ""
4343
r.APIVersion = ""
4444
},
45-
func(r *v1alpha1.APIExport, c fuzz.Continue) {
46-
c.FuzzNoCustom(r)
45+
func(r *v1alpha1.APIExport, c randfill.Continue) {
46+
c.FillNoCustom(r)
4747
r.TypeMeta = metav1.TypeMeta{}
4848
r.Kind = ""
4949
r.APIVersion = ""
5050
},
51-
func(r *v1alpha1.APIExportSpec, c fuzz.Continue) {
52-
c.FuzzNoCustom(r)
51+
func(r *v1alpha1.APIExportSpec, c randfill.Continue) {
52+
c.FillNoCustom(r)
5353

5454
r.LatestResourceSchemas = []string{
55-
nonEmptyString(c.RandString) + "." + nonEmptyString(c.RandString) + "." + nonEmptyString(c.RandString),
55+
nonEmptyString(c.String) + "." + nonEmptyString(c.String) + "." + nonEmptyString(c.String),
5656
}
5757
},
58-
func(r *v1alpha2.APIExportSpec, c fuzz.Continue) {
59-
c.FuzzNoCustom(r)
58+
func(r *v1alpha2.APIExportSpec, c randfill.Continue) {
59+
c.FillNoCustom(r)
6060
r.Resources = nil
6161
for range c.Intn(5) {
62-
name := nonEmptyString(c.RandString)
63-
group := nonEmptyString(c.RandString)
64-
schema := nonEmptyString(c.RandString) + "." + name + "." + group
62+
name := nonEmptyString(c.String)
63+
group := nonEmptyString(c.String)
64+
schema := nonEmptyString(c.String) + "." + name + "." + group
6565
r.Resources = append(r.Resources, v1alpha2.ResourceSchema{
6666
Group: group,
6767
Name: name,
@@ -73,13 +73,13 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
7373
}
7474
r.PermissionClaims = nil
7575
for range c.Intn(5) {
76-
group := nonEmptyString(c.RandString)
77-
resource := nonEmptyString(c.RandString)
78-
identityHash := nonEmptyString(c.RandString)
76+
group := nonEmptyString(c.String)
77+
resource := nonEmptyString(c.String)
78+
identityHash := nonEmptyString(c.String)
7979
verbs := []string{}
8080
numVerbs := c.Intn(5) + 1 // the lower bound is 0, but 0 verbs is not a valid combination
8181
for range numVerbs {
82-
verbs = append(verbs, nonEmptyString(c.RandString))
82+
verbs = append(verbs, nonEmptyString(c.String))
8383
}
8484
r.PermissionClaims = append(r.PermissionClaims, v1alpha2.PermissionClaim{
8585
GroupResource: v1alpha2.GroupResource{
@@ -91,26 +91,26 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
9191
})
9292
}
9393
},
94-
func(r *v1alpha2.APIBinding, c fuzz.Continue) {
95-
c.FuzzNoCustom(r)
94+
func(r *v1alpha2.APIBinding, c randfill.Continue) {
95+
c.FillNoCustom(r)
9696
r.TypeMeta = metav1.TypeMeta{}
9797
r.Kind = ""
9898
r.APIVersion = ""
9999
},
100-
func(r *v1alpha1.APIBinding, c fuzz.Continue) {
101-
c.FuzzNoCustom(r)
100+
func(r *v1alpha1.APIBinding, c randfill.Continue) {
101+
c.FillNoCustom(r)
102102
r.TypeMeta = metav1.TypeMeta{}
103103
r.Kind = ""
104104
r.APIVersion = ""
105105
},
106-
func(r *v1alpha1.APIBindingSpec, c fuzz.Continue) {
107-
c.FuzzNoCustom(r)
106+
func(r *v1alpha1.APIBindingSpec, c randfill.Continue) {
107+
c.FillNoCustom(r)
108108

109109
r.PermissionClaims = nil
110110
for range c.Intn(5) {
111-
group := nonEmptyString(c.RandString)
112-
resource := nonEmptyString(c.RandString)
113-
identityHash := nonEmptyString(c.RandString)
111+
group := nonEmptyString(c.String)
112+
resource := nonEmptyString(c.String)
113+
identityHash := nonEmptyString(c.String)
114114

115115
apc := v1alpha1.AcceptablePermissionClaim{
116116
PermissionClaim: v1alpha1.PermissionClaim{
@@ -119,81 +119,81 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
119119
Resource: resource,
120120
},
121121
IdentityHash: identityHash,
122-
All: c.RandBool(),
122+
All: c.Bool(),
123123
},
124124
}
125125

126126
if !apc.All {
127127
apc.ResourceSelector = []v1alpha1.ResourceSelector{}
128128
for range c.Intn(5) + 1 {
129-
apc.ResourceSelector = append(apc.ResourceSelector, v1alpha1.ResourceSelector{Name: nonEmptyString(c.RandString), Namespace: nonEmptyString(c.RandString)})
129+
apc.ResourceSelector = append(apc.ResourceSelector, v1alpha1.ResourceSelector{Name: nonEmptyString(c.String), Namespace: nonEmptyString(c.String)})
130130
}
131131
}
132132

133133
r.PermissionClaims = append(r.PermissionClaims, apc)
134134
}
135135
},
136-
func(r *v1alpha1.APIBindingStatus, c fuzz.Continue) {
137-
c.FuzzNoCustom(r)
136+
func(r *v1alpha1.APIBindingStatus, c randfill.Continue) {
137+
c.FillNoCustom(r)
138138
r.AppliedPermissionClaims = nil
139139
for range c.Intn(5) {
140-
group := nonEmptyString(c.RandString)
141-
resource := nonEmptyString(c.RandString)
142-
identityHash := nonEmptyString(c.RandString)
140+
group := nonEmptyString(c.String)
141+
resource := nonEmptyString(c.String)
142+
identityHash := nonEmptyString(c.String)
143143

144144
pc := v1alpha1.PermissionClaim{
145145
GroupResource: v1alpha1.GroupResource{
146146
Group: group,
147147
Resource: resource,
148148
},
149149
IdentityHash: identityHash,
150-
All: c.RandBool(),
150+
All: c.Bool(),
151151
}
152152

153153
if !pc.All {
154154
pc.ResourceSelector = []v1alpha1.ResourceSelector{}
155155
for range c.Intn(5) + 1 {
156-
pc.ResourceSelector = append(pc.ResourceSelector, v1alpha1.ResourceSelector{Name: nonEmptyString(c.RandString), Namespace: nonEmptyString(c.RandString)})
156+
pc.ResourceSelector = append(pc.ResourceSelector, v1alpha1.ResourceSelector{Name: nonEmptyString(c.String), Namespace: nonEmptyString(c.String)})
157157
}
158158
}
159159

160160
r.AppliedPermissionClaims = append(r.AppliedPermissionClaims, pc)
161161
}
162162
r.ExportPermissionClaims = nil
163163
for range c.Intn(5) {
164-
group := nonEmptyString(c.RandString)
165-
resource := nonEmptyString(c.RandString)
166-
identityHash := nonEmptyString(c.RandString)
164+
group := nonEmptyString(c.String)
165+
resource := nonEmptyString(c.String)
166+
identityHash := nonEmptyString(c.String)
167167

168168
pc := v1alpha1.PermissionClaim{
169169
GroupResource: v1alpha1.GroupResource{
170170
Group: group,
171171
Resource: resource,
172172
},
173173
IdentityHash: identityHash,
174-
All: c.RandBool(),
174+
All: c.Bool(),
175175
}
176176

177177
if !pc.All {
178178
pc.ResourceSelector = []v1alpha1.ResourceSelector{}
179179
for range c.Intn(5) + 1 {
180-
pc.ResourceSelector = append(pc.ResourceSelector, v1alpha1.ResourceSelector{Name: nonEmptyString(c.RandString), Namespace: nonEmptyString(c.RandString)})
180+
pc.ResourceSelector = append(pc.ResourceSelector, v1alpha1.ResourceSelector{Name: nonEmptyString(c.String), Namespace: nonEmptyString(c.String)})
181181
}
182182
}
183183
r.ExportPermissionClaims = append(r.ExportPermissionClaims, pc)
184184
}
185185
},
186-
func(r *v1alpha2.APIBindingSpec, c fuzz.Continue) {
187-
c.FuzzNoCustom(r)
186+
func(r *v1alpha2.APIBindingSpec, c randfill.Continue) {
187+
c.FillNoCustom(r)
188188
r.PermissionClaims = nil
189189
for range c.Intn(5) {
190-
group := nonEmptyString(c.RandString)
191-
resource := nonEmptyString(c.RandString)
192-
identityHash := nonEmptyString(c.RandString)
190+
group := nonEmptyString(c.String)
191+
resource := nonEmptyString(c.String)
192+
identityHash := nonEmptyString(c.String)
193193
verbs := []string{}
194194
numVerbs := c.Intn(5) + 1 // the lower bound is 0, but 0 verbs is not a valid combination
195195
for range numVerbs {
196-
verbs = append(verbs, nonEmptyString(c.RandString))
196+
verbs = append(verbs, nonEmptyString(c.String))
197197
}
198198

199199
selector := v1alpha2.PermissionClaimSelector{}
@@ -205,18 +205,18 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
205205
labels := make(map[string]string)
206206
numLabels := c.Intn(5) + 1
207207
for range numLabels {
208-
labels[nonEmptyString(c.RandString)] = nonEmptyString(c.RandString)
208+
labels[nonEmptyString(c.String)] = nonEmptyString(c.String)
209209
}
210210
selector.MatchLabels = labels
211211
default:
212212
numExpressions := c.Intn(5) + 1
213213
expressions := make([]metav1.LabelSelectorRequirement, numExpressions)
214214
for range numExpressions {
215215
expressions = append(expressions, metav1.LabelSelectorRequirement{
216-
Key: nonEmptyString(c.RandString),
216+
Key: nonEmptyString(c.String),
217217
Operator: metav1.LabelSelectorOpIn,
218218
Values: []string{
219-
nonEmptyString(c.RandString),
219+
nonEmptyString(c.String),
220220
},
221221
})
222222
}
@@ -239,17 +239,17 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
239239
})
240240
}
241241
},
242-
func(r *v1alpha2.APIBindingStatus, c fuzz.Continue) {
243-
c.FuzzNoCustom(r)
242+
func(r *v1alpha2.APIBindingStatus, c randfill.Continue) {
243+
c.FillNoCustom(r)
244244
r.AppliedPermissionClaims = nil
245245
for range c.Intn(5) {
246-
group := nonEmptyString(c.RandString)
247-
resource := nonEmptyString(c.RandString)
248-
identityHash := nonEmptyString(c.RandString)
246+
group := nonEmptyString(c.String)
247+
resource := nonEmptyString(c.String)
248+
identityHash := nonEmptyString(c.String)
249249
verbs := []string{}
250250
numVerbs := c.Intn(5) + 1 // the lower bound is 0, but 0 verbs is not a valid combination
251251
for range numVerbs {
252-
verbs = append(verbs, nonEmptyString(c.RandString))
252+
verbs = append(verbs, nonEmptyString(c.String))
253253
}
254254

255255
selector := v1alpha2.PermissionClaimSelector{}
@@ -261,18 +261,18 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
261261
labels := make(map[string]string)
262262
numLabels := c.Intn(5) + 1
263263
for range numLabels {
264-
labels[nonEmptyString(c.RandString)] = nonEmptyString(c.RandString)
264+
labels[nonEmptyString(c.String)] = nonEmptyString(c.String)
265265
}
266266
selector.MatchLabels = labels
267267
default:
268268
numExpressions := c.Intn(5) + 1
269269
expressions := make([]metav1.LabelSelectorRequirement, numExpressions)
270270
for range numExpressions {
271271
expressions = append(expressions, metav1.LabelSelectorRequirement{
272-
Key: nonEmptyString(c.RandString),
272+
Key: nonEmptyString(c.String),
273273
Operator: metav1.LabelSelectorOpIn,
274274
Values: []string{
275-
nonEmptyString(c.RandString),
275+
nonEmptyString(c.String),
276276
},
277277
})
278278
}
@@ -293,13 +293,13 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
293293
}
294294
r.ExportPermissionClaims = nil
295295
for range c.Intn(5) {
296-
group := nonEmptyString(c.RandString)
297-
resource := nonEmptyString(c.RandString)
298-
identityHash := nonEmptyString(c.RandString)
296+
group := nonEmptyString(c.String)
297+
resource := nonEmptyString(c.String)
298+
identityHash := nonEmptyString(c.String)
299299
verbs := []string{}
300300
numVerbs := c.Intn(5) + 1 // the lower bound is 0, but 0 verbs is not a valid combination
301301
for range numVerbs {
302-
verbs = append(verbs, nonEmptyString(c.RandString))
302+
verbs = append(verbs, nonEmptyString(c.String))
303303
}
304304
r.ExportPermissionClaims = append(r.ExportPermissionClaims, v1alpha2.PermissionClaim{
305305
GroupResource: v1alpha2.GroupResource{
@@ -311,25 +311,25 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
311311
})
312312
}
313313
},
314-
func(r *v1alpha1.Identity, c fuzz.Continue) {
315-
c.FuzzNoCustom(r)
314+
func(r *v1alpha1.Identity, c randfill.Continue) {
315+
c.FillNoCustom(r)
316316

317317
r.SecretRef = &corev1.SecretReference{}
318-
c.Fuzz(r.SecretRef)
318+
c.Fill(r.SecretRef)
319319
},
320-
func(r *v1alpha2.APIExportList, c fuzz.Continue) {
321-
c.FuzzNoCustom(r)
320+
func(r *v1alpha2.APIExportList, c randfill.Continue) {
321+
c.FillNoCustom(r)
322322
r.TypeMeta = metav1.TypeMeta{}
323323
r.Kind = ""
324324
r.APIVersion = ""
325325
},
326-
func(r *v1alpha1.APIExportList, c fuzz.Continue) {
327-
c.FuzzNoCustom(r)
326+
func(r *v1alpha1.APIExportList, c randfill.Continue) {
327+
c.FillNoCustom(r)
328328
r.TypeMeta = metav1.TypeMeta{}
329329
r.Kind = ""
330330
r.APIVersion = ""
331331
},
332-
func(r *v1alpha1.APIResourceSchemaSpec, c fuzz.Continue) {
332+
func(r *v1alpha1.APIResourceSchemaSpec, c randfill.Continue) {
333333
r.Conversion = &v1alpha1.CustomResourceConversion{}
334334

335335
none := v1alpha1.ConversionStrategyType("None")
@@ -340,18 +340,18 @@ func Funcs(codecs runtimeserializer.CodecFactory) []any {
340340

341341
if r.Conversion.Strategy == webhook {
342342
r.Conversion.Webhook = &v1alpha1.WebhookConversion{}
343-
c.Fuzz(r.Conversion.Webhook)
343+
c.Fill(r.Conversion.Webhook)
344344
}
345345
},
346346
}
347347
}
348348

349-
// TOODO(mjudeikis): This will go away after we rebase to 1.32 and can use new fuzzer.
350-
func nonEmptyString(f func() string) string {
351-
s := f()
349+
// TODO(mjudeikis): This will go away after we rebase to 1.32 and can use new fuzzer.
350+
// TODO(ntnn): The new fuzzer randfill.Continue.String function can
351+
// contain dots, which breaks the apiversion conversion tests.
352+
func nonEmptyString(f func(n int) string) string {
353+
s := f(0)
352354
switch {
353-
case len(s) == 0:
354-
return nonEmptyString(f)
355355
case strings.Contains(s, "."):
356356
return nonEmptyString(f)
357357
default:

sdk/apis/roundtrip_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"testing"
2323

2424
"github.com/google/go-cmp/cmp"
25-
fuzz "github.com/google/gofuzz"
2625
"github.com/stretchr/testify/require"
26+
"sigs.k8s.io/randfill"
2727

2828
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
2929
"k8s.io/apimachinery/pkg/api/apitesting/roundtrip"
@@ -245,8 +245,8 @@ func removeAnnotations(t *testing.T, object runtime.Object, annotationsToRemove
245245

246246
// fuzzInternalObject fuzzes an arbitrary runtime object using the appropriate
247247
// fuzzer registered with the apitesting package.
248-
func fuzzInternalObject(t *testing.T, fuzzer *fuzz.Fuzzer, object runtime.Object) runtime.Object {
249-
fuzzer.Fuzz(object)
248+
func fuzzInternalObject(t *testing.T, fuzzer *randfill.Filler, object runtime.Object) runtime.Object {
249+
fuzzer.Fill(object)
250250

251251
j, err := apimeta.TypeAccessor(object)
252252
if err != nil {

0 commit comments

Comments
 (0)