1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package webhook
16
17 import (
18 "strings"
19 "testing"
20
21 apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
22 )
23
24 func TestAllowAllKnownFields(t *testing.T) {
25 obj := map[string]interface{}{
26 "integer-key": 2,
27 "map-key": map[string]interface{}{
28 "nested-key": false,
29 },
30 "unvalidated-map-key": map[string]interface{}{
31 "unvalidated-key": true,
32 },
33 "array-key": []interface{}{
34 map[string]interface{}{
35 "map-inside-array-key": "map-inside-array-val",
36 },
37 },
38 }
39 schema := &apiextensions.JSONSchemaProps{
40 Type: "object",
41 Properties: map[string]apiextensions.JSONSchemaProps{
42 "integer-key": {Type: "integer"},
43 "unused-key": {Type: "boolean"},
44 "map-key": {
45 Type: "object",
46 Properties: map[string]apiextensions.JSONSchemaProps{
47 "nested-key": {Type: "boolean"},
48 },
49 },
50 "unvalidated-map-key": {Type: "object"},
51 "array-key": {
52 Type: "array",
53 Items: &apiextensions.JSONSchemaPropsOrArray{
54 Schema: &apiextensions.JSONSchemaProps{
55 Type: "object",
56 Properties: map[string]apiextensions.JSONSchemaProps{
57 "unused-key": {Type: "integer"},
58 "map-inside-array-key": {Type: "string"},
59 },
60 },
61 },
62 },
63 },
64 }
65 if err := validateNoUnknownFields(schema, obj, ""); err != nil {
66 t.Fatalf("expected object to be valid but got error: %v", err)
67 }
68 }
69
70 func TestDenyUnknownKeyInMap(t *testing.T) {
71 obj := map[string]interface{}{
72 "valid-key": "valid-val",
73 "unknown-key": 0,
74 }
75 schema := &apiextensions.JSONSchemaProps{
76 Type: "object",
77 Properties: map[string]apiextensions.JSONSchemaProps{
78 "valid-key": {Type: "string"},
79 },
80 }
81 err := validateNoUnknownFields(schema, obj, "")
82 if err == nil {
83 t.Fatalf("expected object to not be allowed")
84 }
85 if !strings.Contains(err.Error(), `"unknown-key"`) {
86 t.Fatalf("expected error to contain unknown key; error: %v", err)
87 }
88 }
89
90 func TestDenyUnknownKeyInNestedMap(t *testing.T) {
91 obj := map[string]interface{}{
92 "map-key": map[string]interface{}{
93 "valid-key": "valid-val",
94 "unknown-key": 0,
95 },
96 }
97 schema := &apiextensions.JSONSchemaProps{
98 Type: "object",
99 Properties: map[string]apiextensions.JSONSchemaProps{
100 "map-key": {
101 Type: "object",
102 Properties: map[string]apiextensions.JSONSchemaProps{
103 "valid-key": {Type: "string"},
104 },
105 },
106 },
107 }
108 err := validateNoUnknownFields(schema, obj, "")
109 if err == nil {
110 t.Fatalf("expected object to not be allowed")
111 }
112 if !strings.Contains(err.Error(), `"map-key.unknown-key"`) {
113 t.Fatalf("expected error to contain unknown key; error: %v", err)
114 }
115 }
116
117 func TestDenyUnknownKeyInMapInsideArray(t *testing.T) {
118 obj := map[string]interface{}{
119 "array-key": []interface{}{
120 map[string]interface{}{
121 "valid-key": "valid-val",
122 "unknown-key": 0,
123 },
124 },
125 }
126 schema := &apiextensions.JSONSchemaProps{
127 Type: "object",
128 Properties: map[string]apiextensions.JSONSchemaProps{
129 "array-key": {
130 Type: "array",
131 Items: &apiextensions.JSONSchemaPropsOrArray{
132 Schema: &apiextensions.JSONSchemaProps{
133 Type: "object",
134 Properties: map[string]apiextensions.JSONSchemaProps{
135 "valid-key": {Type: "string"},
136 },
137 },
138 },
139 },
140 },
141 }
142 err := validateNoUnknownFields(schema, obj, "")
143 if err == nil {
144 t.Fatalf("expected object to not be allowed")
145 }
146 if !strings.Contains(err.Error(), `"array-key[0].unknown-key"`) {
147 t.Fatalf("expected error to contain unknown key; error: %v", err)
148 }
149 }
150
View as plain text