k8s.io/kubernetes@v1.29.3/pkg/apis/batch/fuzzer/fuzzer.go (about) 1 /* 2 Copyright 2017 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 "math" 21 22 fuzz "github.com/google/gofuzz" 23 runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" 24 "k8s.io/kubernetes/pkg/apis/batch" 25 api "k8s.io/kubernetes/pkg/apis/core" 26 "k8s.io/utils/pointer" 27 ) 28 29 // Funcs returns the fuzzer functions for the batch api group. 30 var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} { 31 return []interface{}{ 32 func(j *batch.Job, c fuzz.Continue) { 33 c.FuzzNoCustom(j) // fuzz self without calling this function again 34 35 // match defaulting 36 if len(j.Labels) == 0 { 37 j.Labels = j.Spec.Template.Labels 38 } 39 }, 40 func(j *batch.JobSpec, c fuzz.Continue) { 41 c.FuzzNoCustom(j) // fuzz self without calling this function again 42 completions := int32(c.Rand.Int31()) 43 parallelism := int32(c.Rand.Int31()) 44 backoffLimit := int32(c.Rand.Int31()) 45 j.Completions = &completions 46 j.Parallelism = ¶llelism 47 j.BackoffLimit = &backoffLimit 48 j.ManualSelector = pointer.Bool(c.RandBool()) 49 mode := batch.NonIndexedCompletion 50 if c.RandBool() { 51 mode = batch.IndexedCompletion 52 j.BackoffLimitPerIndex = pointer.Int32(c.Rand.Int31()) 53 j.MaxFailedIndexes = pointer.Int32(c.Rand.Int31()) 54 } 55 if c.RandBool() { 56 j.BackoffLimit = pointer.Int32(math.MaxInt32) 57 } 58 j.CompletionMode = &mode 59 // We're fuzzing the internal JobSpec type, not the v1 type, so we don't 60 // need to fuzz the nil value. 61 j.Suspend = pointer.Bool(c.RandBool()) 62 podReplacementPolicy := batch.TerminatingOrFailed 63 if c.RandBool() { 64 podReplacementPolicy = batch.Failed 65 } 66 j.PodReplacementPolicy = &podReplacementPolicy 67 }, 68 func(sj *batch.CronJobSpec, c fuzz.Continue) { 69 c.FuzzNoCustom(sj) 70 suspend := c.RandBool() 71 sj.Suspend = &suspend 72 sds := int64(c.RandUint64()) 73 sj.StartingDeadlineSeconds = &sds 74 sj.Schedule = c.RandString() 75 successfulJobsHistoryLimit := int32(c.Rand.Int31()) 76 sj.SuccessfulJobsHistoryLimit = &successfulJobsHistoryLimit 77 failedJobsHistoryLimit := int32(c.Rand.Int31()) 78 sj.FailedJobsHistoryLimit = &failedJobsHistoryLimit 79 }, 80 func(cp *batch.ConcurrencyPolicy, c fuzz.Continue) { 81 policies := []batch.ConcurrencyPolicy{batch.AllowConcurrent, batch.ForbidConcurrent, batch.ReplaceConcurrent} 82 *cp = policies[c.Rand.Intn(len(policies))] 83 }, 84 func(p *batch.PodFailurePolicyOnPodConditionsPattern, c fuzz.Continue) { 85 c.FuzzNoCustom(p) 86 if p.Status == "" { 87 p.Status = api.ConditionTrue 88 } 89 }, 90 } 91 }