fuzzer.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package fuzzer
  14. import (
  15. fuzz "github.com/google/gofuzz"
  16. runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
  17. "k8s.io/kubernetes/pkg/apis/batch"
  18. )
  19. func newBool(val bool) *bool {
  20. p := new(bool)
  21. *p = val
  22. return p
  23. }
  24. // Funcs returns the fuzzer functions for the batch api group.
  25. var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} {
  26. return []interface{}{
  27. func(j *batch.Job, c fuzz.Continue) {
  28. c.FuzzNoCustom(j) // fuzz self without calling this function again
  29. // match defaulting
  30. if len(j.Labels) == 0 {
  31. j.Labels = j.Spec.Template.Labels
  32. }
  33. },
  34. func(j *batch.JobSpec, c fuzz.Continue) {
  35. c.FuzzNoCustom(j) // fuzz self without calling this function again
  36. completions := int32(c.Rand.Int31())
  37. parallelism := int32(c.Rand.Int31())
  38. backoffLimit := int32(c.Rand.Int31())
  39. j.Completions = &completions
  40. j.Parallelism = &parallelism
  41. j.BackoffLimit = &backoffLimit
  42. if c.Rand.Int31()%2 == 0 {
  43. j.ManualSelector = newBool(true)
  44. } else {
  45. j.ManualSelector = nil
  46. }
  47. },
  48. func(sj *batch.CronJobSpec, c fuzz.Continue) {
  49. c.FuzzNoCustom(sj)
  50. suspend := c.RandBool()
  51. sj.Suspend = &suspend
  52. sds := int64(c.RandUint64())
  53. sj.StartingDeadlineSeconds = &sds
  54. sj.Schedule = c.RandString()
  55. successfulJobsHistoryLimit := int32(c.Rand.Int31())
  56. sj.SuccessfulJobsHistoryLimit = &successfulJobsHistoryLimit
  57. failedJobsHistoryLimit := int32(c.Rand.Int31())
  58. sj.FailedJobsHistoryLimit = &failedJobsHistoryLimit
  59. },
  60. func(cp *batch.ConcurrencyPolicy, c fuzz.Continue) {
  61. policies := []batch.ConcurrencyPolicy{batch.AllowConcurrent, batch.ForbidConcurrent, batch.ReplaceConcurrent}
  62. *cp = policies[c.Rand.Intn(len(policies))]
  63. },
  64. }
  65. }