1
16
17 package resource
18
19 import (
20 "path/filepath"
21 "testing"
22
23 openapi_v2 "github.com/google/gnostic-models/openapiv2"
24
25 "k8s.io/apimachinery/pkg/runtime/schema"
26 openapitesting "k8s.io/kube-openapi/pkg/util/proto/testing"
27 )
28
29 var fakeSchema = openapitesting.Fake{Path: filepath.Join("..", "..", "artifacts", "openapi", "swagger.json")}
30 var fakeSchemaSharedParams = openapitesting.Fake{Path: filepath.Join("..", "..", "artifacts", "openapi", "swagger-with-shared-parameters.json")}
31
32 func TestSupportsQueryParam(t *testing.T) {
33 docInlineParams, err := fakeSchema.OpenAPISchema()
34 if err != nil {
35 t.Fatalf("Failed to get OpenAPI Schema: %v", err)
36 }
37 docSharedParams, err := fakeSchemaSharedParams.OpenAPISchema()
38 if err != nil {
39 t.Fatalf("Failed to get OpenAPI Schema: %v", err)
40 }
41
42 tests := []struct {
43 gvk schema.GroupVersionKind
44 success bool
45 supports bool
46 queryParam VerifiableQueryParam
47 }{
48 {
49 gvk: schema.GroupVersionKind{
50 Group: "",
51 Version: "v1",
52 Kind: "Pod",
53 },
54 success: true,
55 supports: true,
56 queryParam: QueryParamFieldValidation,
57 },
58 {
59 gvk: schema.GroupVersionKind{
60 Group: "",
61 Version: "v1",
62 Kind: "UnknownKind",
63 },
64 success: false,
65 supports: false,
66 queryParam: QueryParamFieldValidation,
67 },
68 {
69 gvk: schema.GroupVersionKind{
70 Group: "",
71 Version: "v1",
72 Kind: "NodeProxyOptions",
73 },
74 success: true,
75 supports: false,
76 queryParam: QueryParamFieldValidation,
77 },
78 {
79 gvk: schema.GroupVersionKind{
80 Group: "",
81 Version: "v1",
82 Kind: "List",
83 },
84 success: false,
85 supports: false,
86 queryParam: QueryParamFieldValidation,
87 },
88 }
89
90 for name, doc := range map[string]*openapi_v2.Document{
91 "inline parameters": docInlineParams,
92 "shared parameters": docSharedParams,
93 } {
94 t.Run(name, func(t *testing.T) {
95 for _, test := range tests {
96 supports, err := supportsQueryParam(doc, test.gvk, test.queryParam)
97 if supports != test.supports || ((err == nil) != test.success) {
98 errStr := "nil"
99 if test.success == false {
100 errStr = "err"
101 }
102 t.Errorf("SupportsQueryParam(doc, %v, %v) = (%v, %v), expected (%v, %v)",
103 test.gvk, test.queryParam,
104 supports, err,
105 test.supports, errStr,
106 )
107 }
108 }
109 })
110 }
111 }
112
113 func TestFieldValidationVerifier(t *testing.T) {
114 fieldValidationVerifier := QueryParamVerifier{
115 finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
116 return []schema.GroupKind{
117 {
118 Group: "crd.com",
119 Kind: "MyCRD",
120 },
121 {
122 Group: "crd.com",
123 Kind: "MyNewCRD",
124 },
125 }, nil
126 }),
127 openAPIGetter: &fakeSchema,
128 queryParam: QueryParamFieldValidation,
129 }
130
131 err := fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "NodeProxyOptions"})
132 if err == nil {
133 t.Fatalf("NodeProxyOptions doesn't support fieldValidation, yet no error found")
134 }
135
136 err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})
137 if err != nil {
138 t.Fatalf("Pod should support fieldValidation: %v", err)
139 }
140
141 err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "MyCRD"})
142 if err != nil {
143 t.Fatalf("MyCRD should support fieldValidation: %v", err)
144 }
145
146 err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "Random"})
147 if err == nil {
148 t.Fatalf("Random doesn't support fieldValidation, yet no error found")
149 }
150
151 err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"})
152 if err == nil {
153 t.Fatalf("List does not support fieldValidation, yet no error found")
154 }
155 }
156
157 type EmptyOpenAPI struct{}
158
159 func (EmptyOpenAPI) OpenAPISchema() (*openapi_v2.Document, error) {
160 return &openapi_v2.Document{}, nil
161 }
162
163 func TestFieldValidationVerifierNoOpenAPI(t *testing.T) {
164 fieldValidationVerifier := QueryParamVerifier{
165 finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
166 return []schema.GroupKind{
167 {
168 Group: "crd.com",
169 Kind: "MyCRD",
170 },
171 {
172 Group: "crd.com",
173 Kind: "MyNewCRD",
174 },
175 }, nil
176 }),
177 openAPIGetter: EmptyOpenAPI{},
178 queryParam: QueryParamFieldValidation,
179 }
180
181 err := fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})
182 if err == nil {
183 t.Fatalf("Pod doesn't support fieldValidation, yet no error found")
184 }
185
186 err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "MyCRD"})
187 if err == nil {
188 t.Fatalf("MyCRD doesn't support fieldValidation, yet no error found")
189 }
190
191 err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"})
192 if err == nil {
193 t.Fatalf("List does not support fieldValidation, yet no error found")
194 }
195 }
196
View as plain text