Debian Codegraph @

pkg/apis/example/fuzzer/fuzzer.go

raw
1 /*
2 Copyright 2015 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package fuzzer
18
19 import (
20 "fmt"
21
22 "sigs.k8s.io/randfill"
23
24 apitesting "k8s.io/apimachinery/pkg/api/apitesting"
25 "k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
26 "k8s.io/apimachinery/pkg/runtime"
27 runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
28 "k8s.io/apiserver/pkg/apis/example"
29 examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
30 )
31
32 // overrideMetaFuncs override some generic fuzzer funcs from k8s.io/apiserver in order to have more realistic
33 // values in a Kubernetes context.
34 func overrideMetaFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
35 return []interface{}{
36 func(j *runtime.Object, c randfill.Continue) {
37 // TODO: uncomment when round trip starts from a versioned object
38 if true { // c.Bool() {
39 *j = &runtime.Unknown{
40 // We do not set TypeMeta here because it is not carried through a round trip
41 Raw: []byte(`{"apiVersion":"unknown.group/unknown","kind":"Something","someKey":"someValue"}`),
42 ContentType: runtime.ContentTypeJSON,
43 }
44 } else {
45 types := []runtime.Object{&example.Pod{}}
46 t := types[c.Rand.Intn(len(types))]
47 c.Fill(t)
48 *j = t
49 }
50 },
51 func(r *runtime.RawExtension, c randfill.Continue) {
52 // Pick an arbitrary type and fuzz it
53 types := []runtime.Object{&example.Pod{}}
54 obj := types[c.Rand.Intn(len(types))]
55 c.Fill(obj)
56
57 // Convert the object to raw bytes
58 bytes, err := runtime.Encode(apitesting.TestCodec(codecs, examplev1.SchemeGroupVersion), obj)
59 if err != nil {
60 panic(fmt.Sprintf("Failed to encode object: %v", err))
61 }
62
63 // Set the bytes field on the RawExtension
64 r.Raw = bytes
65 },
66 }
67 }
68
69 func exampleFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
70 return []interface{}{
71 func(s *example.PodSpec, c randfill.Continue) {
72 c.FillNoCustom(s)
73 // has a default value
74 ttl := int64(30)
75 if c.Bool() {
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 *example.PodPhase, c randfill.Continue) {
85 statuses := []example.PodPhase{"Pending", "Running", "Succeeded", "Failed", "Unknown"}
86 *j = statuses[c.Rand.Intn(len(statuses))]
87 },
88 func(rp *example.RestartPolicy, c randfill.Continue) {
89 policies := []example.RestartPolicy{"Always", "Never", "OnFailure"}
90 *rp = policies[c.Rand.Intn(len(policies))]
91 },
92 }
93 }
94
95 // Funcs returns the fuzzer functions for the example api group.
96 var Funcs = fuzzer.MergeFuzzerFuncs(
97 overrideMetaFuncs,
98 exampleFuncs,
99 )