|
| 1 | +/* |
| 2 | +Copyright 2019 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 schema |
| 18 | + |
| 19 | +import ( |
| 20 | + "math/rand" |
| 21 | + "reflect" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + fuzz "github.com/google/gofuzz" |
| 26 | + |
| 27 | + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" |
| 28 | + apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" |
| 29 | + "k8s.io/apimachinery/pkg/util/diff" |
| 30 | + "k8s.io/apimachinery/pkg/util/json" |
| 31 | +) |
| 32 | + |
| 33 | +func TestStructuralRoundtripOrError(t *testing.T) { |
| 34 | + f := fuzz.New() |
| 35 | + seed := time.Now().UnixNano() |
| 36 | + t.Logf("seed = %v", seed) |
| 37 | + //seed = int64(1549012506261785182) |
| 38 | + f.RandSource(rand.New(rand.NewSource(seed))) |
| 39 | + f.Funcs( |
| 40 | + func(s *apiextensions.JSON, c fuzz.Continue) { |
| 41 | + *s = apiextensions.JSON(map[string]interface{}{"foo": float64(42.2)}) |
| 42 | + }, |
| 43 | + func(s *apiextensions.JSONSchemaPropsOrArray, c fuzz.Continue) { |
| 44 | + c.FuzzNoCustom(s) |
| 45 | + if s.Schema != nil { |
| 46 | + s.JSONSchemas = nil |
| 47 | + } else if s.JSONSchemas == nil { |
| 48 | + s.Schema = &apiextensions.JSONSchemaProps{} |
| 49 | + } |
| 50 | + }, |
| 51 | + func(s *apiextensions.JSONSchemaPropsOrBool, c fuzz.Continue) { |
| 52 | + c.FuzzNoCustom(s) |
| 53 | + if s.Schema != nil { |
| 54 | + s.Allows = false |
| 55 | + } |
| 56 | + }, |
| 57 | + func(s **string, c fuzz.Continue) { |
| 58 | + c.FuzzNoCustom(s) |
| 59 | + if *s != nil && **s == "" { |
| 60 | + *s = nil |
| 61 | + } |
| 62 | + }, |
| 63 | + ) |
| 64 | + |
| 65 | + f.MaxDepth(2) |
| 66 | + f.NilChance(0.5) |
| 67 | + |
| 68 | + for i := 0; i < 10000; i++ { |
| 69 | + // fuzz a random field in JSONSchemaProps |
| 70 | + origSchema := &apiextensions.JSONSchemaProps{} |
| 71 | + x := reflect.ValueOf(origSchema).Elem() |
| 72 | + n := rand.Intn(x.NumField()) |
| 73 | + if name := x.Type().Field(n).Name; name == "Example" || name == "ExternalDocs" { |
| 74 | + // we drop these intentionally |
| 75 | + continue |
| 76 | + } |
| 77 | + f.Fuzz(x.Field(n).Addr().Interface()) |
| 78 | + if origSchema.Nullable { |
| 79 | + // non-empty type for nullable. nullable:true with empty type does not roundtrip because |
| 80 | + // go-openapi does not allow to encode that (we use type slices otherwise). |
| 81 | + origSchema.Type = "string" |
| 82 | + } |
| 83 | + |
| 84 | + // it roundtrips or NewStructural errors out. We should never drop anything |
| 85 | + orig, err := NewStructural(origSchema) |
| 86 | + if err != nil { |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + // roundtrip through go-openapi, JSON, v1beta1 JSONSchemaProp, internal JSONSchemaProp |
| 91 | + goOpenAPI := orig.ToGoOpenAPI() |
| 92 | + bs, err := json.Marshal(goOpenAPI) |
| 93 | + if err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + str := nullTypeRE.ReplaceAllString(string(bs), `"type":"$1","nullable":true`) // unfold nullable type:[<type>,"null"] -> type:<type>,nullable:true |
| 97 | + v1beta1Schema := &apiextensionsv1beta1.JSONSchemaProps{} |
| 98 | + err = json.Unmarshal([]byte(str), v1beta1Schema) |
| 99 | + if err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + internalSchema := &apiextensions.JSONSchemaProps{} |
| 103 | + err = apiextensionsv1beta1.Convert_v1beta1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(v1beta1Schema, internalSchema, nil) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + if !reflect.DeepEqual(origSchema, internalSchema) { |
| 109 | + t.Fatalf("original and result differ: %v", diff.ObjectDiff(origSchema, internalSchema)) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments