...
1
16
17 package openapitest_test
18
19 import (
20 "testing"
21
22 "k8s.io/client-go/openapi/openapitest"
23 "k8s.io/kube-openapi/pkg/spec3"
24 kjson "sigs.k8s.io/json"
25 )
26
27 func TestOpenAPIEmbeddedTest(t *testing.T) {
28 client := openapitest.NewEmbeddedFileClient()
29
30
31 paths, err := client.Paths()
32 if err != nil {
33 t.Fatalf("error fetching paths: %v", err)
34 }
35 if len(paths) == 0 {
36 t.Error("empty paths")
37 }
38
39
40 expectedPaths := []string{
41 "api/v1",
42 "apis/apps/v1",
43 "apis/batch/v1",
44 "apis/networking.k8s.io/v1alpha1",
45 "apis/discovery.k8s.io/v1",
46 }
47 for _, p := range expectedPaths {
48 if _, ok := paths[p]; !ok {
49 t.Fatalf("expected %s", p)
50 }
51 }
52
53
54 for path, gv := range paths {
55 data, err := gv.Schema("application/json")
56 if err != nil {
57 t.Fatalf("error reading schema for %v: %v", path, err)
58 }
59 o := &spec3.OpenAPI{}
60 stricterrs, err := kjson.UnmarshalStrict(data, o)
61 if err != nil {
62 t.Fatalf("error unmarshaling schema for %v: %v", path, err)
63 }
64 if len(stricterrs) > 0 {
65 t.Fatalf("strict errors unmarshaling schema for %v: %v", path, stricterrs)
66 }
67 }
68 }
69
70 func TestOpenAPITest(t *testing.T) {
71 client := openapitest.NewFileClient("testdata")
72
73
74 paths, err := client.Paths()
75 if err != nil {
76 t.Fatalf("error fetching paths: %v", err)
77 }
78 if len(paths) == 0 {
79 t.Error("empty paths")
80 }
81
82
83 expectedPaths := []string{
84 "api/v1",
85 "apis/apps/v1",
86 "apis/batch/v1",
87 "apis/networking.k8s.io/v1alpha1",
88 "apis/discovery.k8s.io/v1",
89 }
90 for _, p := range expectedPaths {
91 if _, ok := paths[p]; !ok {
92 t.Fatalf("expected %s", p)
93 }
94 }
95
96
97 for path, gv := range paths {
98 data, err := gv.Schema("application/json")
99 if err != nil {
100 t.Fatalf("error reading schema for %v: %v", path, err)
101 }
102 o := &spec3.OpenAPI{}
103 stricterrs, err := kjson.UnmarshalStrict(data, o)
104 if err != nil {
105 t.Fatalf("error unmarshaling schema for %v: %v", path, err)
106 }
107 if len(stricterrs) > 0 {
108 t.Fatalf("strict errors unmarshaling schema for %v: %v", path, stricterrs)
109 }
110 }
111 }
112
View as plain text