...

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

Documentation: k8s.io/kubernetes/pkg/apis/storage/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  	"fmt"
    21  
    22  	fuzz "github.com/google/gofuzz"
    23  
    24  	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
    25  	api "k8s.io/kubernetes/pkg/apis/core"
    26  	"k8s.io/kubernetes/pkg/apis/storage"
    27  )
    28  
    29  // Funcs returns the fuzzer functions for the storage api group.
    30  var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} {
    31  	return []interface{}{
    32  		func(obj *storage.StorageClass, c fuzz.Continue) {
    33  			c.FuzzNoCustom(obj) // fuzz self without calling this function again
    34  			reclamationPolicies := []api.PersistentVolumeReclaimPolicy{api.PersistentVolumeReclaimDelete, api.PersistentVolumeReclaimRetain}
    35  			obj.ReclaimPolicy = &reclamationPolicies[c.Rand.Intn(len(reclamationPolicies))]
    36  			bindingModes := []storage.VolumeBindingMode{storage.VolumeBindingImmediate, storage.VolumeBindingWaitForFirstConsumer}
    37  			obj.VolumeBindingMode = &bindingModes[c.Rand.Intn(len(bindingModes))]
    38  		},
    39  		func(obj *storage.CSIDriver, c fuzz.Continue) {
    40  			c.FuzzNoCustom(obj) // fuzz self without calling this function again
    41  
    42  			// Custom fuzzing for volume modes.
    43  			switch c.Rand.Intn(7) {
    44  			case 0:
    45  				obj.Spec.VolumeLifecycleModes = nil
    46  			case 1:
    47  				obj.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{}
    48  			case 2:
    49  				// Invalid mode.
    50  				obj.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
    51  					storage.VolumeLifecycleMode(fmt.Sprintf("%d", c.Rand.Int31())),
    52  				}
    53  			case 3:
    54  				obj.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
    55  					storage.VolumeLifecyclePersistent,
    56  				}
    57  			case 4:
    58  				obj.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
    59  					storage.VolumeLifecycleEphemeral,
    60  				}
    61  			case 5:
    62  				obj.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
    63  					storage.VolumeLifecyclePersistent,
    64  					storage.VolumeLifecycleEphemeral,
    65  				}
    66  			case 6:
    67  				obj.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
    68  					storage.VolumeLifecycleEphemeral,
    69  					storage.VolumeLifecyclePersistent,
    70  				}
    71  			}
    72  
    73  			// match defaulting
    74  			if obj.Spec.AttachRequired == nil {
    75  				obj.Spec.AttachRequired = new(bool)
    76  				*(obj.Spec.AttachRequired) = true
    77  			}
    78  			if obj.Spec.PodInfoOnMount == nil {
    79  				obj.Spec.PodInfoOnMount = new(bool)
    80  				*(obj.Spec.PodInfoOnMount) = false
    81  			}
    82  			if obj.Spec.StorageCapacity == nil {
    83  				obj.Spec.StorageCapacity = new(bool)
    84  				*(obj.Spec.StorageCapacity) = false
    85  			}
    86  			if obj.Spec.FSGroupPolicy == nil {
    87  				obj.Spec.FSGroupPolicy = new(storage.FSGroupPolicy)
    88  				*obj.Spec.FSGroupPolicy = storage.ReadWriteOnceWithFSTypeFSGroupPolicy
    89  			}
    90  			if obj.Spec.RequiresRepublish == nil {
    91  				obj.Spec.RequiresRepublish = new(bool)
    92  				*(obj.Spec.RequiresRepublish) = false
    93  			}
    94  			if len(obj.Spec.VolumeLifecycleModes) == 0 {
    95  				obj.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
    96  					storage.VolumeLifecyclePersistent,
    97  				}
    98  			}
    99  			if obj.Spec.SELinuxMount == nil {
   100  				obj.Spec.SELinuxMount = new(bool)
   101  				*(obj.Spec.SELinuxMount) = false
   102  			}
   103  		},
   104  	}
   105  }
   106  

View as plain text