Debian Codegraph @

pkg/util/proto/openapi_test.go

raw
1 /*
2 Copyright 2017 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 proto_test
18
19 import (
20 "path/filepath"
21 "reflect"
22 "testing"
23
24 openapi_v3 "github.com/google/gnostic-models/openapiv3"
25
26 "k8s.io/kube-openapi/pkg/util/proto"
27 prototesting "k8s.io/kube-openapi/pkg/util/proto/testing"
28 )
29
30 var fakeSchema = prototesting.Fake{Path: filepath.Join("testdata", "swagger.json")}
31 var fakeSchemaNext = prototesting.Fake{Path: filepath.Join("testdata", "swagger_next.json")}
32 var fakeSchemaV300 = prototesting.FakeV3{Path: filepath.Join("testdata", "openapi_v3_0_0")}
33
34 // loadV18Models loads the v1.8 swagger schema and returns proto.Models.
35 func loadV18Models(t *testing.T) proto.Models {
36 t.Helper()
37 s, err := fakeSchema.OpenAPISchema()
38 if err != nil {
39 t.Fatalf("failed to open v1.8 schema: %v", err)
40 }
41 models, err := proto.NewOpenAPIData(s)
42 if err != nil {
43 t.Fatalf("failed to create OpenAPI data: %v", err)
44 }
45 return models
46 }
47
48 // loadV18DeploymentModel loads the v1.8 schema and returns the Deployment Kind.
49 func loadV18DeploymentModel(t *testing.T) (proto.Models, proto.Schema, *proto.Kind) {
50 t.Helper()
51 models := loadV18Models(t)
52 schema := models.LookupModel("io.k8s.api.apps.v1beta1.Deployment")
53 if schema == nil {
54 t.Fatal("model io.k8s.api.apps.v1beta1.Deployment not found")
55 }
56 deployment, ok := schema.(*proto.Kind)
57 if !ok || deployment == nil {
58 t.Fatal("expected schema to be *proto.Kind")
59 }
60 return models, schema, deployment
61 }
62
63 func TestV18Deployment(t *testing.T) {
64 t.Run("should lookup the Schema by its model name", func(t *testing.T) {
65 models := loadV18Models(t)
66 schema := models.LookupModel("io.k8s.api.apps.v1beta1.Deployment")
67 if schema == nil {
68 t.Fatal("model io.k8s.api.apps.v1beta1.Deployment not found")
69 }
70 })
71
72 t.Run("should be a Kind", func(t *testing.T) {
73 _, _, deployment := loadV18DeploymentModel(t)
74 if deployment == nil {
75 t.Fatal("expected deployment to be non-nil")
76 }
77 })
78
79 t.Run("should have a path", func(t *testing.T) {
80 _, _, deployment := loadV18DeploymentModel(t)
81 got := deployment.GetPath().Get()
82 want := []string{"io.k8s.api.apps.v1beta1.Deployment"}
83 if !reflect.DeepEqual(got, want) {
84 t.Errorf("path = %v, want %v", got, want)
85 }
86 })
87
88 t.Run("should have a kind key of type string", func(t *testing.T) {
89 _, _, deployment := loadV18DeploymentModel(t)
90 if _, ok := deployment.Fields["kind"]; !ok {
91 t.Fatal("missing 'kind' field")
92 }
93 key, ok := deployment.Fields["kind"].(*proto.Primitive)
94 if !ok || key == nil {
95 t.Fatal("expected 'kind' to be *proto.Primitive")
96 }
97 if key.Type != "string" {
98 t.Errorf("kind.Type = %q, want %q", key.Type, "string")
99 }
100 got := key.GetPath().Get()
101 want := []string{"io.k8s.api.apps.v1beta1.Deployment", ".kind"}
102 if !reflect.DeepEqual(got, want) {
103 t.Errorf("kind path = %v, want %v", got, want)
104 }
105 })
106
107 t.Run("should have a apiVersion key of type string", func(t *testing.T) {
108 _, _, deployment := loadV18DeploymentModel(t)
109 if _, ok := deployment.Fields["apiVersion"]; !ok {
110 t.Fatal("missing 'apiVersion' field")
111 }
112 key, ok := deployment.Fields["apiVersion"].(*proto.Primitive)
113 if !ok || key == nil {
114 t.Fatal("expected 'apiVersion' to be *proto.Primitive")
115 }
116 if key.Type != "string" {
117 t.Errorf("apiVersion.Type = %q, want %q", key.Type, "string")
118 }
119 got := key.GetPath().Get()
120 want := []string{"io.k8s.api.apps.v1beta1.Deployment", ".apiVersion"}
121 if !reflect.DeepEqual(got, want) {
122 t.Errorf("apiVersion path = %v, want %v", got, want)
123 }
124 })
125
126 t.Run("should have a metadata key of type Reference", func(t *testing.T) {
127 _, _, deployment := loadV18DeploymentModel(t)
128 if _, ok := deployment.Fields["metadata"]; !ok {
129 t.Fatal("missing 'metadata' field")
130 }
131 key, ok := deployment.Fields["metadata"].(proto.Reference)
132 if !ok {
133 t.Fatal("expected 'metadata' to be proto.Reference")
134 }
135 if key.Reference() != "io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta" {
136 t.Errorf("metadata reference = %q, want %q", key.Reference(), "io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta")
137 }
138 subSchema, ok := key.SubSchema().(*proto.Kind)
139 if !ok || subSchema == nil {
140 t.Fatal("expected metadata sub-schema to be *proto.Kind")
141 }
142 })
143
144 t.Run("should have a status key of type Reference", func(t *testing.T) {
145 _, _, deployment := loadV18DeploymentModel(t)
146 if _, ok := deployment.Fields["status"]; !ok {
147 t.Fatal("missing 'status' field")
148 }
149 key, ok := deployment.Fields["status"].(proto.Reference)
150 if !ok {
151 t.Fatal("expected 'status' to be proto.Reference")
152 }
153 if key.Reference() != "io.k8s.api.apps.v1beta1.DeploymentStatus" {
154 t.Errorf("status reference = %q, want %q", key.Reference(), "io.k8s.api.apps.v1beta1.DeploymentStatus")
155 }
156 status, ok := key.SubSchema().(*proto.Kind)
157 if !ok || status == nil {
158 t.Fatal("expected status sub-schema to be *proto.Kind")
159 }
160 })
161
162 t.Run("should have a valid DeploymentStatus", func(t *testing.T) {
163 _, _, deployment := loadV18DeploymentModel(t)
164 statusRef, ok := deployment.Fields["status"].(proto.Reference)
165 if !ok {
166 t.Fatal("expected 'status' to be proto.Reference")
167 }
168 status, ok := statusRef.SubSchema().(*proto.Kind)
169 if !ok || status == nil {
170 t.Fatal("expected status sub-schema to be *proto.Kind")
171 }
172
173 t.Log("having availableReplicas key")
174 if _, ok := status.Fields["availableReplicas"]; !ok {
175 t.Fatal("missing 'availableReplicas' field in status")
176 }
177 replicas, ok := status.Fields["availableReplicas"].(*proto.Primitive)
178 if !ok || replicas == nil {
179 t.Fatal("expected 'availableReplicas' to be *proto.Primitive")
180 }
181 if replicas.Type != "integer" {
182 t.Errorf("availableReplicas.Type = %q, want %q", replicas.Type, "integer")
183 }
184
185 t.Log("having conditions key")
186 if _, ok := status.Fields["conditions"]; !ok {
187 t.Fatal("missing 'conditions' field in status")
188 }
189 conditions, ok := status.Fields["conditions"].(*proto.Array)
190 if !ok || conditions == nil {
191 t.Fatal("expected 'conditions' to be *proto.Array")
192 }
193 wantName := `Array of Reference to "io.k8s.api.apps.v1beta1.DeploymentCondition"`
194 if conditions.GetName() != wantName {
195 t.Errorf("conditions name = %q, want %q", conditions.GetName(), wantName)
196 }
197 wantExt := map[string]interface{}{
198 "x-kubernetes-patch-merge-key": "type",
199 "x-kubernetes-patch-strategy": "merge",
200 }
201 if !reflect.DeepEqual(conditions.GetExtensions(), wantExt) {
202 t.Errorf("conditions extensions = %v, want %v", conditions.GetExtensions(), wantExt)
203 }
204 condition, ok := conditions.SubType.(proto.Reference)
205 if !ok {
206 t.Fatal("expected conditions.SubType to be proto.Reference")
207 }
208 if condition.Reference() != "io.k8s.api.apps.v1beta1.DeploymentCondition" {
209 t.Errorf("condition reference = %q, want %q", condition.Reference(), "io.k8s.api.apps.v1beta1.DeploymentCondition")
210 }
211 })
212
213 t.Run("should have a spec key of type Reference", func(t *testing.T) {
214 _, _, deployment := loadV18DeploymentModel(t)
215 if _, ok := deployment.Fields["spec"]; !ok {
216 t.Fatal("missing 'spec' field")
217 }
218 key, ok := deployment.Fields["spec"].(proto.Reference)
219 if !ok {
220 t.Fatal("expected 'spec' to be proto.Reference")
221 }
222 if key.Reference() != "io.k8s.api.apps.v1beta1.DeploymentSpec" {
223 t.Errorf("spec reference = %q, want %q", key.Reference(), "io.k8s.api.apps.v1beta1.DeploymentSpec")
224 }
225 spec, ok := key.SubSchema().(*proto.Kind)
226 if !ok || spec == nil {
227 t.Fatal("expected spec sub-schema to be *proto.Kind")
228 }
229 })
230
231 t.Run("should have a spec with no gvk", func(t *testing.T) {
232 _, _, deployment := loadV18DeploymentModel(t)
233 specRef, ok := deployment.Fields["spec"].(proto.Reference)
234 if !ok {
235 t.Fatal("expected 'spec' to be proto.Reference")
236 }
237 spec, ok := specRef.SubSchema().(*proto.Kind)
238 if !ok || spec == nil {
239 t.Fatal("expected spec sub-schema to be *proto.Kind")
240 }
241 if _, found := spec.GetExtensions()["x-kubernetes-group-version-kind"]; found {
242 t.Error("spec should not have x-kubernetes-group-version-kind extension")
243 }
244 })
245
246 t.Run("should have a spec with a PodTemplateSpec sub-field", func(t *testing.T) {
247 _, _, deployment := loadV18DeploymentModel(t)
248 specRef, ok := deployment.Fields["spec"].(proto.Reference)
249 if !ok {
250 t.Fatal("expected 'spec' to be proto.Reference")
251 }
252 spec, ok := specRef.SubSchema().(*proto.Kind)
253 if !ok || spec == nil {
254 t.Fatal("expected spec sub-schema to be *proto.Kind")
255 }
256 if _, ok := spec.Fields["template"]; !ok {
257 t.Fatal("missing 'template' field in spec")
258 }
259 key, ok := spec.Fields["template"].(proto.Reference)
260 if !ok {
261 t.Fatal("expected 'template' to be proto.Reference")
262 }
263 if key.Reference() != "io.k8s.api.core.v1.PodTemplateSpec" {
264 t.Errorf("template reference = %q, want %q", key.Reference(), "io.k8s.api.core.v1.PodTemplateSpec")
265 }
266 })
267 }
268
269 func TestV111Deployment(t *testing.T) {
270 s, err := fakeSchemaNext.OpenAPISchema()
271 if err != nil {
272 t.Fatalf("failed to open v1.11 schema: %v", err)
273 }
274 models, err := proto.NewOpenAPIData(s)
275 if err != nil {
276 t.Fatalf("failed to create OpenAPI data: %v", err)
277 }
278
279 t.Run("should lookup the Schema by its model name", func(t *testing.T) {
280 schema := models.LookupModel("io.k8s.api.apps.v1beta1.Deployment")
281 if schema == nil {
282 t.Fatal("model io.k8s.api.apps.v1beta1.Deployment not found")
283 }
284 })
285
286 t.Run("should be a Kind", func(t *testing.T) {
287 schema := models.LookupModel("io.k8s.api.apps.v1beta1.Deployment")
288 if schema == nil {
289 t.Fatal("model not found")
290 }
291 deployment, ok := schema.(*proto.Kind)
292 if !ok || deployment == nil {
293 t.Fatal("expected schema to be *proto.Kind")
294 }
295 })
296 }
297
298 func TestV111ControllerRevision(t *testing.T) {
299 s, err := fakeSchemaNext.OpenAPISchema()
300 if err != nil {
301 t.Fatalf("failed to open v1.11 schema: %v", err)
302 }
303 models, err := proto.NewOpenAPIData(s)
304 if err != nil {
305 t.Fatalf("failed to create OpenAPI data: %v", err)
306 }
307
308 t.Run("should lookup the Schema by its model name", func(t *testing.T) {
309 schema := models.LookupModel("io.k8s.api.apps.v1beta1.ControllerRevision")
310 if schema == nil {
311 t.Fatal("model io.k8s.api.apps.v1beta1.ControllerRevision not found")
312 }
313 })
314
315 t.Run("data property should be map[string]Arbitrary", func(t *testing.T) {
316 schema := models.LookupModel("io.k8s.api.apps.v1beta1.ControllerRevision")
317 if schema == nil {
318 t.Fatal("model not found")
319 }
320 cr, ok := schema.(*proto.Kind)
321 if !ok || cr == nil {
322 t.Fatal("expected schema to be *proto.Kind")
323 }
324 if _, ok := cr.Fields["data"]; !ok {
325 t.Fatal("missing 'data' field")
326 }
327
328 data, ok := cr.Fields["data"].(*proto.Map)
329 if !ok || data == nil {
330 t.Fatal("expected 'data' to be *proto.Map")
331 }
332 wantName := "Map of Arbitrary value (primitive, object or array)"
333 if data.GetName() != wantName {
334 t.Errorf("data name = %q, want %q", data.GetName(), wantName)
335 }
336 wantPath := []string{"io.k8s.api.apps.v1beta1.ControllerRevision", ".data"}
337 if !reflect.DeepEqual(data.GetPath().Get(), wantPath) {
338 t.Errorf("data path = %v, want %v", data.GetPath().Get(), wantPath)
339 }
340
341 arbitrary, ok := data.SubType.(*proto.Arbitrary)
342 if !ok || arbitrary == nil {
343 t.Fatal("expected data.SubType to be *proto.Arbitrary")
344 }
345 wantArbName := "Arbitrary value (primitive, object or array)"
346 if arbitrary.GetName() != wantArbName {
347 t.Errorf("arbitrary name = %q, want %q", arbitrary.GetName(), wantArbName)
348 }
349 if !reflect.DeepEqual(arbitrary.GetPath().Get(), wantPath) {
350 t.Errorf("arbitrary path = %v, want %v", arbitrary.GetPath().Get(), wantPath)
351 }
352 })
353 }
354
355 func TestSubjectAccessReview(t *testing.T) {
356 s, err := fakeSchema.OpenAPISchema()
357 if err != nil {
358 t.Fatalf("failed to open schema: %v", err)
359 }
360 models, err := proto.NewOpenAPIData(s)
361 if err != nil {
362 t.Fatalf("failed to create OpenAPI data: %v", err)
363 }
364
365 t.Run("should lookup the Schema by its model", func(t *testing.T) {
366 schema := models.LookupModel("io.k8s.api.authorization.v1.LocalSubjectAccessReview")
367 if schema == nil {
368 t.Fatal("model io.k8s.api.authorization.v1.LocalSubjectAccessReview not found")
369 }
370 })
371
372 t.Run("should be a Kind and have a spec", func(t *testing.T) {
373 schema := models.LookupModel("io.k8s.api.authorization.v1.LocalSubjectAccessReview")
374 if schema == nil {
375 t.Fatal("model not found")
376 }
377 sar, ok := schema.(*proto.Kind)
378 if !ok || sar == nil {
379 t.Fatal("expected schema to be *proto.Kind")
380 }
381 if _, ok := sar.Fields["spec"]; !ok {
382 t.Fatal("missing 'spec' field")
383 }
384 specRef, ok := sar.Fields["spec"].(proto.Reference)
385 if !ok {
386 t.Fatal("expected 'spec' to be proto.Reference")
387 }
388 if specRef.Reference() != "io.k8s.api.authorization.v1.SubjectAccessReviewSpec" {
389 t.Errorf("spec reference = %q, want %q", specRef.Reference(), "io.k8s.api.authorization.v1.SubjectAccessReviewSpec")
390 }
391 sarspec, ok := specRef.SubSchema().(*proto.Kind)
392 if !ok || sarspec == nil {
393 t.Fatal("expected spec sub-schema to be *proto.Kind")
394 }
395 })
396
397 t.Run("should have a valid SubjectAccessReviewSpec", func(t *testing.T) {
398 schema := models.LookupModel("io.k8s.api.authorization.v1.LocalSubjectAccessReview")
399 if schema == nil {
400 t.Fatal("model not found")
401 }
402 sar, ok := schema.(*proto.Kind)
403 if !ok || sar == nil {
404 t.Fatal("expected schema to be *proto.Kind")
405 }
406 specRef, ok := sar.Fields["spec"].(proto.Reference)
407 if !ok {
408 t.Fatal("expected 'spec' to be proto.Reference")
409 }
410 sarspec, ok := specRef.SubSchema().(*proto.Kind)
411 if !ok || sarspec == nil {
412 t.Fatal("expected spec sub-schema to be *proto.Kind")
413 }
414
415 if _, ok := sarspec.Fields["extra"]; !ok {
416 t.Fatal("missing 'extra' field")
417 }
418 extra, ok := sarspec.Fields["extra"].(*proto.Map)
419 if !ok || extra == nil {
420 t.Fatal("expected 'extra' to be *proto.Map")
421 }
422 if extra.GetName() != "Map of Array of string" {
423 t.Errorf("extra name = %q, want %q", extra.GetName(), "Map of Array of string")
424 }
425 wantExtraPath := []string{"io.k8s.api.authorization.v1.SubjectAccessReviewSpec", ".extra"}
426 if !reflect.DeepEqual(extra.GetPath().Get(), wantExtraPath) {
427 t.Errorf("extra path = %v, want %v", extra.GetPath().Get(), wantExtraPath)
428 }
429
430 array, ok := extra.SubType.(*proto.Array)
431 if !ok || array == nil {
432 t.Fatal("expected extra.SubType to be *proto.Array")
433 }
434 if array.GetName() != "Array of string" {
435 t.Errorf("array name = %q, want %q", array.GetName(), "Array of string")
436 }
437 if !reflect.DeepEqual(array.GetPath().Get(), wantExtraPath) {
438 t.Errorf("array path = %v, want %v", array.GetPath().Get(), wantExtraPath)
439 }
440
441 str, ok := array.SubType.(*proto.Primitive)
442 if !ok || str == nil {
443 t.Fatal("expected array.SubType to be *proto.Primitive")
444 }
445 if str.Type != "string" {
446 t.Errorf("str.Type = %q, want %q", str.Type, "string")
447 }
448 if str.GetName() != "string" {
449 t.Errorf("str name = %q, want %q", str.GetName(), "string")
450 }
451 if !reflect.DeepEqual(str.GetPath().Get(), wantExtraPath) {
452 t.Errorf("str path = %v, want %v", str.GetPath().Get(), wantExtraPath)
453 }
454 })
455 }
456
457 func TestPath(t *testing.T) {
458 t.Run("can be created by NewPath", func(t *testing.T) {
459 path := proto.NewPath("key")
460 if path.String() != "key" {
461 t.Errorf("path.String() = %q, want %q", path.String(), "key")
462 }
463 })
464
465 t.Run("can create and print complex paths", func(t *testing.T) {
466 key := proto.NewPath("key")
467 array := key.ArrayPath(12)
468 field := array.FieldPath("subKey")
469 if field.String() != "key[12].subKey" {
470 t.Errorf("field.String() = %q, want %q", field.String(), "key[12].subKey")
471 }
472 })
473
474 t.Run("has a length", func(t *testing.T) {
475 key := proto.NewPath("key")
476 array := key.ArrayPath(12)
477 field := array.FieldPath("subKey")
478 if field.Len() != 3 {
479 t.Errorf("field.Len() = %d, want %d", field.Len(), 3)
480 }
481 })
482
483 t.Run("can look like an array", func(t *testing.T) {
484 key := proto.NewPath("key")
485 array := key.ArrayPath(12)
486 field := array.FieldPath("subKey")
487 got := field.Get()
488 want := []string{"key", "[12]", ".subKey"}
489 if !reflect.DeepEqual(got, want) {
490 t.Errorf("field.Get() = %v, want %v", got, want)
491 }
492 })
493 }
494
495 // loadV3Deployment loads the v3.0.0 schema and returns the Deployment Kind.
496 func loadV3Deployment(t *testing.T) *proto.Kind {
497 t.Helper()
498 s, err := fakeSchemaV300.OpenAPIV3Schema("apps/v1")
499 if err != nil {
500 t.Fatalf("failed to open v3.0.0 schema: %v", err)
501 }
502 models, err := proto.NewOpenAPIV3Data(s)
503 if err != nil {
504 t.Fatalf("failed to create OpenAPI v3 data: %v", err)
505 }
506 schema := models.LookupModel("io.k8s.api.apps.v1.Deployment")
507 if schema == nil {
508 t.Fatal("model io.k8s.api.apps.v1.Deployment not found")
509 }
510 deployment, ok := schema.(*proto.Kind)
511 if !ok || deployment == nil {
512 t.Fatal("expected schema to be *proto.Kind")
513 }
514 return deployment
515 }
516
517 func TestV3Deployment(t *testing.T) {
518 t.Run("should have a path", func(t *testing.T) {
519 deployment := loadV3Deployment(t)
520 got := deployment.GetPath().Get()
521 want := []string{"io.k8s.api.apps.v1.Deployment"}
522 if !reflect.DeepEqual(got, want) {
523 t.Errorf("path = %v, want %v", got, want)
524 }
525 })
526
527 t.Run("should have a kind key of type string", func(t *testing.T) {
528 deployment := loadV3Deployment(t)
529 if _, ok := deployment.Fields["kind"]; !ok {
530 t.Fatal("missing 'kind' field")
531 }
532 key, ok := deployment.Fields["kind"].(*proto.Primitive)
533 if !ok || key == nil {
534 t.Fatal("expected 'kind' to be *proto.Primitive")
535 }
536 if key.Type != "string" {
537 t.Errorf("kind.Type = %q, want %q", key.Type, "string")
538 }
539 got := key.GetPath().Get()
540 want := []string{"io.k8s.api.apps.v1.Deployment", ".kind"}
541 if !reflect.DeepEqual(got, want) {
542 t.Errorf("kind path = %v, want %v", got, want)
543 }
544 })
545
546 t.Run("should have a apiVersion key of type string", func(t *testing.T) {
547 deployment := loadV3Deployment(t)
548 if _, ok := deployment.Fields["apiVersion"]; !ok {
549 t.Fatal("missing 'apiVersion' field")
550 }
551 key, ok := deployment.Fields["apiVersion"].(*proto.Primitive)
552 if !ok || key == nil {
553 t.Fatal("expected 'apiVersion' to be *proto.Primitive")
554 }
555 if key.Type != "string" {
556 t.Errorf("apiVersion.Type = %q, want %q", key.Type, "string")
557 }
558 got := key.GetPath().Get()
559 want := []string{"io.k8s.api.apps.v1.Deployment", ".apiVersion"}
560 if !reflect.DeepEqual(got, want) {
561 t.Errorf("apiVersion path = %v, want %v", got, want)
562 }
563 })
564
565 t.Run("should have a metadata key of type Reference", func(t *testing.T) {
566 deployment := loadV3Deployment(t)
567 if _, ok := deployment.Fields["metadata"]; !ok {
568 t.Fatal("missing 'metadata' field")
569 }
570 key, ok := deployment.Fields["metadata"].(proto.Reference)
571 if !ok {
572 t.Fatal("expected 'metadata' to be proto.Reference")
573 }
574 if key.Reference() != "io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta" {
575 t.Errorf("metadata reference = %q, want %q", key.Reference(), "io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta")
576 }
577 subSchema, ok := key.SubSchema().(*proto.Kind)
578 if !ok || subSchema == nil {
579 t.Fatal("expected metadata sub-schema to be *proto.Kind")
580 }
581 })
582
583 t.Run("status/should have a valid DeploymentStatus", func(t *testing.T) {
584 deployment := loadV3Deployment(t)
585 if _, ok := deployment.Fields["status"]; !ok {
586 t.Fatal("missing 'status' field")
587 }
588 statusRef, ok := deployment.Fields["status"].(proto.Reference)
589 if !ok {
590 t.Fatal("expected 'status' to be proto.Reference")
591 }
592 if statusRef.Reference() != "io.k8s.api.apps.v1.DeploymentStatus" {
593 t.Errorf("status reference = %q, want %q", statusRef.Reference(), "io.k8s.api.apps.v1.DeploymentStatus")
594 }
595 status, ok := statusRef.SubSchema().(*proto.Kind)
596 if !ok || status == nil {
597 t.Fatal("expected status sub-schema to be *proto.Kind")
598 }
599
600 t.Log("having availableReplicas key")
601 if _, ok := status.Fields["availableReplicas"]; !ok {
602 t.Fatal("missing 'availableReplicas' field in status")
603 }
604 replicas, ok := status.Fields["availableReplicas"].(*proto.Primitive)
605 if !ok || replicas == nil {
606 t.Fatal("expected 'availableReplicas' to be *proto.Primitive")
607 }
608 if replicas.Type != "integer" {
609 t.Errorf("availableReplicas.Type = %q, want %q", replicas.Type, "integer")
610 }
611
612 t.Log("having conditions key")
613 if _, ok := status.Fields["conditions"]; !ok {
614 t.Fatal("missing 'conditions' field in status")
615 }
616 conditions, ok := status.Fields["conditions"].(*proto.Array)
617 if !ok || conditions == nil {
618 t.Fatal("expected 'conditions' to be *proto.Array")
619 }
620 wantName := `Array of Reference to "io.k8s.api.apps.v1.DeploymentCondition"`
621 if conditions.GetName() != wantName {
622 t.Errorf("conditions name = %q, want %q", conditions.GetName(), wantName)
623 }
624 wantExt := map[string]interface{}{
625 "x-kubernetes-patch-merge-key": "type",
626 "x-kubernetes-patch-strategy": "merge",
627 }
628 if !reflect.DeepEqual(conditions.GetExtensions(), wantExt) {
629 t.Errorf("conditions extensions = %v, want %v", conditions.GetExtensions(), wantExt)
630 }
631 condition, ok := conditions.SubType.(proto.Reference)
632 if !ok {
633 t.Fatal("expected conditions.SubType to be proto.Reference")
634 }
635 if condition.Reference() != "io.k8s.api.apps.v1.DeploymentCondition" {
636 t.Errorf("condition reference = %q, want %q", condition.Reference(), "io.k8s.api.apps.v1.DeploymentCondition")
637 }
638 })
639
640 t.Run("spec/should have no gvk", func(t *testing.T) {
641 deployment := loadV3Deployment(t)
642 if _, ok := deployment.Fields["spec"]; !ok {
643 t.Fatal("missing 'spec' field")
644 }
645 specRef, ok := deployment.Fields["spec"].(proto.Reference)
646 if !ok {
647 t.Fatal("expected 'spec' to be proto.Reference")
648 }
649 if specRef.Reference() != "io.k8s.api.apps.v1.DeploymentSpec" {
650 t.Errorf("spec reference = %q, want %q", specRef.Reference(), "io.k8s.api.apps.v1.DeploymentSpec")
651 }
652 spec, ok := specRef.SubSchema().(*proto.Kind)
653 if !ok || spec == nil {
654 t.Fatal("expected spec sub-schema to be *proto.Kind")
655 }
656 if _, found := spec.GetExtensions()["x-kubernetes-group-version-kind"]; found {
657 t.Error("spec should not have x-kubernetes-group-version-kind extension")
658 }
659 })
660
661 t.Run("spec/should have a PodTemplateSpec sub-field", func(t *testing.T) {
662 deployment := loadV3Deployment(t)
663 specRef, ok := deployment.Fields["spec"].(proto.Reference)
664 if !ok {
665 t.Fatal("expected 'spec' to be proto.Reference")
666 }
667 spec, ok := specRef.SubSchema().(*proto.Kind)
668 if !ok || spec == nil {
669 t.Fatal("expected spec sub-schema to be *proto.Kind")
670 }
671 if _, ok := spec.Fields["template"]; !ok {
672 t.Fatal("missing 'template' field in spec")
673 }
674 key, ok := spec.Fields["template"].(proto.Reference)
675 if !ok {
676 t.Fatal("expected 'template' to be proto.Reference")
677 }
678 if key.Reference() != "io.k8s.api.core.v1.PodTemplateSpec" {
679 t.Errorf("template reference = %q, want %q", key.Reference(), "io.k8s.api.core.v1.PodTemplateSpec")
680 }
681 })
682 }
683
684 func TestV3GVKExtension(t *testing.T) {
685 spec := []byte(`{
686 "openapi": "3.0.0",
687 "info": {
688 "title": "Kubernetes",
689 "version": "v1.24.0"
690 },
691 "paths": {
692 "/foo": {
693 "get": {
694 "responses": {
695 "200": {
696 "description": "OK",
697 "content": {
698 "application/json": {
699 "schema": {
700 "$ref": "#/components/schemas/Foo"
701 }
702 }
703 }
704 }
705 }
706 }
707 }
708 },
709 "components": {
710 "schemas": {
711 "Foo": {
712 "type": "object",
713 "properties": {},
714 "x-kubernetes-group-version-kind": [
715 {
716 "group": "foo",
717 "kind": "Foo",
718 "version": "v1"
719 }
720 ]
721 }
722 }
723 }
724 }`)
725
726 document, err := openapi_v3.ParseDocument(spec)
727 if err != nil {
728 t.Fatalf("failed to parse OpenAPI v3 document: %v", err)
729 }
730 models, err := proto.NewOpenAPIV3Data(document)
731 if err != nil {
732 t.Fatalf("failed to create OpenAPI v3 data: %v", err)
733 }
734 schema := models.LookupModel("Foo")
735 if schema == nil {
736 t.Fatal("model Foo not found")
737 }
738
739 t.Run("should have an extension with gvk", func(t *testing.T) {
740 if _, found := schema.GetExtensions()["x-kubernetes-group-version-kind"]; !found {
741 t.Error("expected x-kubernetes-group-version-kind extension to be present")
742 }
743 })
744
745 t.Run("should convert to proto.Kind type", func(t *testing.T) {
746 foo, ok := schema.(*proto.Kind)
747 if !ok {
748 t.Fatal("expected schema to be *proto.Kind")
749 }
750 if foo == nil {
751 t.Fatal("expected foo to be non-nil")
752 }
753 })
754 }