...
1
16
17 package fuzzer
18
19 import (
20 "fmt"
21
22 "github.com/google/gofuzz"
23
24 apitesting "k8s.io/apimachinery/pkg/api/apitesting"
25 "k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
26 "k8s.io/apimachinery/pkg/apis/testapigroup"
27 "k8s.io/apimachinery/pkg/apis/testapigroup/v1"
28 "k8s.io/apimachinery/pkg/runtime"
29 runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
30 )
31
32
33
34 func overrideMetaFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
35 return []interface{}{
36 func(j *runtime.Object, c fuzz.Continue) {
37
38 if true {
39 *j = &runtime.Unknown{
40
41 Raw: []byte(`{"apiVersion":"unknown.group/unknown","kind":"Something","someKey":"someValue"}`),
42 ContentType: runtime.ContentTypeJSON,
43 }
44 } else {
45 types := []runtime.Object{&testapigroup.Carp{}}
46 t := types[c.Rand.Intn(len(types))]
47 c.Fuzz(t)
48 *j = t
49 }
50 },
51 func(r *runtime.RawExtension, c fuzz.Continue) {
52
53 types := []runtime.Object{&testapigroup.Carp{}}
54 obj := types[c.Rand.Intn(len(types))]
55 c.Fuzz(obj)
56
57
58 bytes, err := runtime.Encode(apitesting.TestCodec(codecs, v1.SchemeGroupVersion), obj)
59 if err != nil {
60 panic(fmt.Sprintf("Failed to encode object: %v", err))
61 }
62
63
64 r.Raw = bytes
65 },
66 }
67 }
68
69 func testapigroupFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
70 return []interface{}{
71 func(s *testapigroup.CarpSpec, c fuzz.Continue) {
72 c.FuzzNoCustom(s)
73
74 ttl := int64(30)
75 if c.RandBool() {
76 ttl = int64(c.Uint32())
77 }
78 s.TerminationGracePeriodSeconds = &ttl
79
80 if s.SchedulerName == "" {
81 s.SchedulerName = "default-scheduler"
82 }
83 },
84 func(j *testapigroup.CarpPhase, c fuzz.Continue) {
85 statuses := []testapigroup.CarpPhase{"Pending", "Running", "Succeeded", "Failed", "Unknown"}
86 *j = statuses[c.Rand.Intn(len(statuses))]
87 },
88 func(rp *testapigroup.RestartPolicy, c fuzz.Continue) {
89 policies := []testapigroup.RestartPolicy{"Always", "Never", "OnFailure"}
90 *rp = policies[c.Rand.Intn(len(policies))]
91 },
92 }
93 }
94
95
96 var Funcs = fuzzer.MergeFuzzerFuncs(
97 overrideMetaFuncs,
98 testapigroupFuncs,
99 )
100
View as plain text