1
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
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
100 if reflect.ValueOf(handler.PruneDefaults(got)).Pointer() != reflect.ValueOf(got).Pointer() {
101 t.Fatal("no-op prune returned new object")
102 }
103 }
104
View as plain text