1 package kclient
2
3 import (
4 "testing"
5
6 "github.com/go-openapi/jsonpointer"
7 "github.com/go-openapi/jsonreference"
8 "github.com/go-openapi/spec"
9 "github.com/google/go-cmp/cmp"
10 olm "github.com/operator-framework/api/pkg/operators/v1alpha1"
11 )
12
13 func TestGetResourceSpecDefinitionFromSwagger(t *testing.T) {
14 tests := []struct {
15 name string
16 swagger []byte
17 group string
18 version string
19 kind string
20 want *spec.Schema
21 wantErr bool
22 }{
23 {
24 name: "not found CRD",
25 swagger: []byte("{}"),
26 group: "aGroup",
27 version: "aVersion",
28 kind: "aKind",
29 want: nil,
30 wantErr: true,
31 },
32 {
33 name: "found CRD without spec",
34 swagger: []byte(`{
35 "definitions": {
36 "com.dev4devs.postgresql.v1alpha1.Database": {
37 "type": "object",
38 "x-kubernetes-group-version-kind": [
39 {
40 "group": "postgresql.dev4devs.com",
41 "kind": "Database",
42 "version": "v1alpha1"
43 }
44 ]
45 }
46 }
47 }`),
48 group: "postgresql.dev4devs.com",
49 version: "v1alpha1",
50 kind: "Database",
51 want: nil,
52 wantErr: false,
53 },
54 {
55 name: "found CRD with spec",
56 swagger: []byte(`{
57 "definitions": {
58 "com.dev4devs.postgresql.v1alpha1.Database": {
59 "type": "object",
60 "x-kubernetes-group-version-kind": [
61 {
62 "group": "postgresql.dev4devs.com",
63 "kind": "Database",
64 "version": "v1alpha1"
65 }
66 ],
67 "properties": {
68 "spec": {
69 "type": "object"
70 }
71 }
72 }
73 }
74 }`),
75 group: "postgresql.dev4devs.com",
76 version: "v1alpha1",
77 kind: "Database",
78 want: &spec.Schema{
79 SchemaProps: spec.SchemaProps{
80 Type: []string{"object"},
81 },
82 },
83 wantErr: false,
84 },
85 }
86
87 for _, tt := range tests {
88 t.Run(tt.name, func(t *testing.T) {
89 got, gotErr := getResourceSpecDefinitionFromSwagger(tt.swagger, tt.group, tt.version, tt.kind)
90 if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(jsonreference.Ref{}, jsonpointer.Pointer{})); diff != "" {
91 t.Errorf("getResourceSpecDefinitionFromSwagger mismatch (-want +got):\n%s", diff)
92 }
93 if (gotErr != nil) != tt.wantErr {
94 t.Errorf("Expected error %v, got %v", tt.wantErr, gotErr)
95 }
96 })
97 }
98 }
99
100 func TestToOpenAPISpec(t *testing.T) {
101 tests := []struct {
102 name string
103 repr olm.CRDDescription
104 want spec.Schema
105 }{
106 {
107 name: "one-level property",
108 repr: olm.CRDDescription{
109 SpecDescriptors: []olm.SpecDescriptor{
110 {
111 Path: "path1",
112 DisplayName: "name to display 1",
113 Description: "description 1",
114 },
115 },
116 },
117 want: spec.Schema{
118 SchemaProps: spec.SchemaProps{
119 Type: []string{"object"},
120 Properties: map[string]spec.Schema{
121 "path1": {
122 SchemaProps: spec.SchemaProps{
123 Type: []string{"string"},
124 Description: "description 1",
125 Title: "name to display 1",
126 },
127 },
128 },
129 AdditionalProperties: &spec.SchemaOrBool{
130 Allows: false,
131 },
132 },
133 },
134 },
135
136 {
137 name: "multiple-levels property",
138 repr: olm.CRDDescription{
139 SpecDescriptors: []olm.SpecDescriptor{
140 {
141 Path: "subpath1.path1",
142 DisplayName: "name to display 1.1",
143 Description: "description 1.1",
144 },
145 {
146 Path: "subpath1.path2",
147 DisplayName: "name to display 1.2",
148 Description: "description 1.2",
149 },
150 {
151 Path: "subpath2.path1",
152 DisplayName: "name to display 2.1",
153 Description: "description 2.1",
154 },
155 },
156 },
157 want: spec.Schema{
158 SchemaProps: spec.SchemaProps{
159 Type: []string{"object"},
160 Properties: map[string]spec.Schema{
161 "subpath1": {
162 SchemaProps: spec.SchemaProps{
163 Type: []string{"object"},
164 Properties: map[string]spec.Schema{
165 "path1": {
166 SchemaProps: spec.SchemaProps{
167 Type: []string{"string"},
168 Description: "description 1.1",
169 Title: "name to display 1.1",
170 },
171 },
172 "path2": {
173 SchemaProps: spec.SchemaProps{
174 Type: []string{"string"},
175 Description: "description 1.2",
176 Title: "name to display 1.2",
177 },
178 },
179 },
180 },
181 },
182 "subpath2": {
183 SchemaProps: spec.SchemaProps{
184 Type: []string{"object"},
185 Properties: map[string]spec.Schema{
186 "path1": {
187 SchemaProps: spec.SchemaProps{
188 Type: []string{"string"},
189 Description: "description 2.1",
190 Title: "name to display 2.1",
191 },
192 },
193 },
194 },
195 },
196 },
197 AdditionalProperties: &spec.SchemaOrBool{
198 Allows: false,
199 },
200 },
201 },
202 },
203 }
204
205 for _, tt := range tests {
206 t.Run(tt.name, func(t *testing.T) {
207 result := toOpenAPISpec(&tt.repr)
208 if diff := cmp.Diff(tt.want, *result, cmp.AllowUnexported(jsonreference.Ref{}, jsonpointer.Pointer{})); diff != "" {
209 t.Errorf("toOpenAPISpec mismatch (-want +got):\n%s", diff)
210 }
211 })
212 }
213 }
214
View as plain text