| 1 | /* |
| 2 | Copyright 2020 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 handler_test |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | "reflect" |
| 22 | "testing" |
| 23 | |
| 24 | "k8s.io/kube-openapi/pkg/handler" |
| 25 | |
| 26 | "k8s.io/kube-openapi/pkg/validation/spec" |
| 27 | ) |
| 28 | |
| 29 | func TestDefaultPruning(t *testing.T) { |
| 30 | def := spec.Definitions{ |
| 31 | "foo": spec.Schema{ |
| 32 | SchemaProps: spec.SchemaProps{ |
| 33 | Default: 0, |
| 34 | AllOf: []spec.Schema{{SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}}, |
| 35 | AnyOf: []spec.Schema{{SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}}, |
| 36 | OneOf: []spec.Schema{{SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}}, |
| 37 | Not: &spec.Schema{SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}, |
| 38 | Properties: map[string]spec.Schema{ |
| 39 | "foo": {SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}, |
| 40 | }, |
| 41 | AdditionalProperties: &spec.SchemaOrBool{Schema: &spec.Schema{SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}}, |
| 42 | PatternProperties: map[string]spec.Schema{ |
| 43 | "foo": {SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}, |
| 44 | }, |
| 45 | Dependencies: spec.Dependencies{ |
| 46 | "foo": spec.SchemaOrStringArray{Schema: &spec.Schema{SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}}, |
| 47 | }, |
| 48 | AdditionalItems: &spec.SchemaOrBool{ |
| 49 | Schema: &spec.Schema{SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}, |
| 50 | }, |
| 51 | Definitions: spec.Definitions{ |
| 52 | "bar": spec.Schema{SchemaProps: spec.SchemaProps{Default: "default-string", Title: "Field"}}, |
| 53 | }, |
| 54 | }, |
| 55 | }, |
| 56 | } |
| 57 | jsonDef, err := json.Marshal(def) |
| 58 | if err != nil { |
| 59 | t.Fatalf("Failed to marshal definition: %v", err) |
| 60 | } |
| 61 | wanted := spec.Definitions{ |
| 62 | "foo": spec.Schema{ |
| 63 | SchemaProps: spec.SchemaProps{ |
| 64 | AllOf: []spec.Schema{{SchemaProps: spec.SchemaProps{Title: "Field"}}}, |
| 65 | AnyOf: []spec.Schema{{SchemaProps: spec.SchemaProps{Title: "Field"}}}, |
| 66 | OneOf: []spec.Schema{{SchemaProps: spec.SchemaProps{Title: "Field"}}}, |
| 67 | Not: &spec.Schema{SchemaProps: spec.SchemaProps{Title: "Field"}}, |
| 68 | Properties: map[string]spec.Schema{ |
| 69 | "foo": {SchemaProps: spec.SchemaProps{Title: "Field"}}, |
| 70 | }, |
| 71 | AdditionalProperties: &spec.SchemaOrBool{Schema: &spec.Schema{SchemaProps: spec.SchemaProps{Title: "Field"}}}, |
| 72 | PatternProperties: map[string]spec.Schema{ |
| 73 | "foo": {SchemaProps: spec.SchemaProps{Title: "Field"}}, |
| 74 | }, |
| 75 | Dependencies: spec.Dependencies{ |
| 76 | "foo": spec.SchemaOrStringArray{Schema: &spec.Schema{SchemaProps: spec.SchemaProps{Title: "Field"}}}, |
| 77 | }, |
| 78 | AdditionalItems: &spec.SchemaOrBool{ |
| 79 | Schema: &spec.Schema{SchemaProps: spec.SchemaProps{Title: "Field"}}, |
| 80 | }, |
| 81 | Definitions: spec.Definitions{ |
| 82 | "bar": spec.Schema{SchemaProps: spec.SchemaProps{Title: "Field"}}, |
| 83 | }, |
| 84 | }, |
| 85 | }, |
| 86 | } |
| 87 | |
| 88 | got := handler.PruneDefaults(def) |
| 89 | if !reflect.DeepEqual(got, wanted) { |
| 90 | gotJSON, _ := json.Marshal(got) |
| 91 | wantedJSON, _ := json.Marshal(wanted) |
| 92 | t.Fatalf("got: %v\nwanted %v", string(gotJSON), string(wantedJSON)) |
| 93 | } |
| 94 | // Make sure that def hasn't been changed. |
| 95 | newDef, _ := json.Marshal(def) |
| 96 | if string(newDef) != string(jsonDef) { |
| 97 | t.Fatalf("prune removed defaults from initial config:\nBefore: %v\nAfter: %v", string(jsonDef), string(newDef)) |
| 98 | } |
| 99 | // Make sure that no-op doesn't change the object. |
| 100 | if reflect.ValueOf(handler.PruneDefaults(got)).Pointer() != reflect.ValueOf(got).Pointer() { |
| 101 | t.Fatal("no-op prune returned new object") |
| 102 | } |
| 103 | } |