...

Source file src/k8s.io/kubernetes/pkg/apis/batch/fuzzer/fuzzer.go

Documentation: k8s.io/kubernetes/pkg/apis/batch/fuzzer

     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 = &parallelism
    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  			if c.RandBool() {
    68  				c.Fuzz(j.ManagedBy)
    69  			}
    70  		},
    71  		func(sj *batch.CronJobSpec, c fuzz.Continue) {
    72  			c.FuzzNoCustom(sj)
    73  			suspend := c.RandBool()
    74  			sj.Suspend = &suspend
    75  			sds := int64(c.RandUint64())
    76  			sj.StartingDeadlineSeconds = &sds
    77  			sj.Schedule = c.RandString()
    78  			successfulJobsHistoryLimit := int32(c.Rand.Int31())
    79  			sj.SuccessfulJobsHistoryLimit = &successfulJobsHistoryLimit
    80  			failedJobsHistoryLimit := int32(c.Rand.Int31())
    81  			sj.FailedJobsHistoryLimit = &failedJobsHistoryLimit
    82  		},
    83  		func(cp *batch.ConcurrencyPolicy, c fuzz.Continue) {
    84  			policies := []batch.ConcurrencyPolicy{batch.AllowConcurrent, batch.ForbidConcurrent, batch.ReplaceConcurrent}
    85  			*cp = policies[c.Rand.Intn(len(policies))]
    86  		},
    87  		func(p *batch.PodFailurePolicyOnPodConditionsPattern, c fuzz.Continue) {
    88  			c.FuzzNoCustom(p)
    89  			if p.Status == "" {
    90  				p.Status = api.ConditionTrue
    91  			}
    92  		},
    93  	}
    94  }
    95  

View as plain text