github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/testapigroup/fuzzer/fuzzer.go (about)

     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  	"github.com/google/gofuzz"
    23  
    24  	apitesting "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/api/apitesting"
    25  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/api/apitesting/fuzzer"
    26  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/testapigroup"
    27  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/testapigroup/v1"
    28  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime"
    29  	runtimeserializer "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/serializer"
    30  )
    31  
    32  // overrideMetaFuncs override some generic fuzzer funcs from k8s.io/apimachinery 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 fuzz.Continue) {
    37  			// TODO: uncomment when round trip starts from a versioned object
    38  			if true { //c.RandBool() {
    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{&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  			// Pick an arbitrary type and fuzz it
    53  			types := []runtime.Object{&testapigroup.Carp{}}
    54  			obj := types[c.Rand.Intn(len(types))]
    55  			c.Fuzz(obj)
    56  
    57  			// Convert the object to raw bytes
    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  			// Set the bytes field on the RawExtension
    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  			// has a default value
    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  // Funcs returns the fuzzer functions for the testapigroup.
    96  var Funcs = fuzzer.MergeFuzzerFuncs(
    97  	overrideMetaFuncs,
    98  	testapigroupFuncs,
    99  )