-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdiscovery_test.go
More file actions
303 lines (270 loc) · 8.17 KB
/
Copy pathdiscovery_test.go
File metadata and controls
303 lines (270 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package graphql
import (
"testing"
"github.com/operator-framework/operator-registry/alpha/declcfg"
)
func TestFieldNameRemapping_EdgeCases(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"name", "name"},
{"package-name", "packageName"},
{"default_channel", "defaultChannel"},
{"related-images", "relatedImages"},
{"", "value"},
{"123invalid", "field_123invalid"},
{"my.field.name", "myFieldName"},
{"CamelCase", "camelCase"},
{"UPPERCASE", "uppercase"},
{"mixed_case-field.name", "mixedCaseFieldName"},
{"spec.template.spec.containers", "specTemplateSpecContainers"},
{"metadata.annotations.description", "metadataAnnotationsDescription"},
{"operators.operatorframework.io/bundle.channels.v1", "operatorsOperatorframeworkIoBundleChannelsV1"},
{"---", "field_"},
{"123", "field_123"},
{"field@#$%", "field"},
}
for _, tc := range testCases {
result := remapFieldName(tc.input)
if result != tc.expected {
t.Errorf("remapFieldName(%q) = %q, expected %q", tc.input, result, tc.expected)
}
}
}
func TestSanitizeTypeName_EdgeCases(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"olm.package", "OlmPackage"},
{"olm.gvk", "OlmGvk"},
{"some-type", "SomeType"},
{"complex.type-name_here", "ComplexTypeNameHere"},
{"", "Unknown"},
{"123invalid", "Invalid"},
{"operators.operatorframework.io/bundle.channels.v1", "OperatorsOperatorframeworkIoBundleChannelsV1"},
{"@#$%", "Unknown"},
{"_____", "Unknown"},
{"ABC", "Abc"},
{"lowercase", "Lowercase"},
}
for _, tc := range testCases {
result := sanitizeTypeName(tc.input)
if result != tc.expected {
t.Errorf("sanitizeTypeName(%q) = %q, expected %q", tc.input, result, tc.expected)
}
}
}
func TestAnalyzeJSONObject_FieldTypes(t *testing.T) {
testObj := map[string]interface{}{
"name": "test-package",
"version": "1.0.0",
"count": 42,
"active": true,
"tags": []interface{}{"tag1", "tag2"},
"numbers": []interface{}{1, 2, 3},
"nested": map[string]interface{}{"key": "value"},
"nullField": nil,
"emptyArray": []interface{}{},
"floatValue": 3.14,
"mixedArray": []interface{}{"string", 123, true},
}
info := &SchemaInfo{
Fields: make(map[string]*FieldInfo),
}
analyzeJSONObject(testObj, info)
// Check that all fields were discovered
expectedFieldCount := len(testObj)
if len(info.Fields) != expectedFieldCount {
t.Errorf("Expected %d fields discovered, got %d", expectedFieldCount, len(info.Fields))
}
// Check specific field types
testField := func(origName string, shouldBeArray bool) {
graphqlField := remapFieldName(origName)
fieldInfo, exists := info.Fields[graphqlField]
if !exists {
t.Errorf("Field %s (mapped to %s) not discovered", origName, graphqlField)
return
}
if fieldInfo.IsArray != shouldBeArray {
t.Errorf("Field %s array status: expected %v, got %v", graphqlField, shouldBeArray, fieldInfo.IsArray)
}
}
testField("name", false)
testField("count", false)
testField("active", false)
testField("tags", true)
testField("numbers", true)
testField("emptyArray", true)
}
func TestBundlePropertiesAnalysis_ComprehensiveTypes(t *testing.T) {
// Test that properties field is discovered with nested structure
bundleObj := map[string]interface{}{
"name": "test-bundle",
"package": "test-package",
"properties": []interface{}{
map[string]interface{}{
"type": "olm.package",
"value": map[string]interface{}{
"packageName": "test-package",
"version": "1.0.0",
},
},
map[string]interface{}{
"type": "olm.gvk",
"value": map[string]interface{}{
"group": "example.com",
"version": "v1",
"kind": "TestResource",
},
},
},
}
info := &SchemaInfo{
Fields: make(map[string]*FieldInfo),
}
// Use the generic field analysis (not bundle-specific)
analyzeJSONObject(bundleObj, info)
// Check that properties field was discovered
propertiesField, exists := info.Fields[remapFieldName("properties")]
if !exists {
t.Error("properties field not discovered")
return
}
// Verify it's detected as an array
if !propertiesField.IsArray {
t.Error("properties field should be detected as an array")
}
// Verify nested fields were discovered
if propertiesField.NestedFields == nil {
t.Error("properties field should have nested fields discovered")
return
}
// Check for common property fields (type, value)
expectedFields := []string{"type", "value"}
for _, field := range expectedFields {
fieldName := remapFieldName(field)
if _, exists := propertiesField.NestedFields[fieldName]; !exists {
t.Errorf("Expected nested field %s not found in properties", fieldName)
}
}
}
func TestSchemaDiscovery_RealWorldExample(t *testing.T) {
// Test with more realistic catalog data
packageMeta := &declcfg.Meta{
Schema: declcfg.SchemaPackage,
Package: "nginx-ingress-operator",
Name: "nginx-ingress-operator",
Blob: []byte(`{
"defaultChannel": "alpha",
"icon": {
"base64data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==",
"mediatype": "image/png"
},
"name": "nginx-ingress-operator",
"schema": "olm.package"
}`),
}
channelMeta := &declcfg.Meta{
Schema: declcfg.SchemaChannel,
Package: "nginx-ingress-operator",
Name: "alpha",
Blob: []byte(`{
"entries": [
{"name": "nginx-ingress-operator.v0.0.1"},
{"name": "nginx-ingress-operator.v0.0.2", "replaces": "nginx-ingress-operator.v0.0.1"}
],
"name": "alpha",
"package": "nginx-ingress-operator",
"schema": "olm.channel"
}`),
}
bundleMeta := &declcfg.Meta{
Schema: declcfg.SchemaBundle,
Package: "nginx-ingress-operator",
Name: "nginx-ingress-operator.v0.0.2",
Blob: []byte(`{
"image": "quay.io/operatorhubio/nginx-ingress-operator@sha256:abc123",
"name": "nginx-ingress-operator.v0.0.2",
"package": "nginx-ingress-operator",
"properties": [
{
"type": "olm.package",
"value": {
"packageName": "nginx-ingress-operator",
"version": "0.0.2"
}
},
{
"type": "olm.gvk",
"value": {
"group": "k8s.nginx.org",
"kind": "NginxIngress",
"version": "v1"
}
},
{
"type": "olm.bundle.mediatype",
"value": "registry+v1"
}
],
"relatedImages": [
{
"image": "quay.io/operatorhubio/nginx-ingress-operator@sha256:abc123",
"name": "operator"
}
],
"schema": "olm.bundle"
}`),
}
testMetas := []*declcfg.Meta{packageMeta, channelMeta, bundleMeta}
catalogSchema, err := DiscoverSchemaFromMetas(testMetas)
if err != nil {
t.Fatalf("Failed to discover schema: %v", err)
}
// Validate the results
if len(catalogSchema.Schemas) != 3 {
t.Errorf("Expected 3 schemas, got %d", len(catalogSchema.Schemas))
}
// Check bundle property discovery
bundleSchema := catalogSchema.Schemas[declcfg.SchemaBundle]
if bundleSchema == nil {
t.Fatal("Bundle schema not found")
}
// With the schema-agnostic approach, we verify the properties field has nested structure
propertiesField, exists := bundleSchema.Fields[remapFieldName("properties")]
if !exists {
t.Error("properties field not found in bundle schema")
return
}
if !propertiesField.IsArray {
t.Error("properties field should be an array")
}
if len(propertiesField.NestedFields) == 0 {
t.Error("properties field should have nested fields")
return
}
// Verify common property fields
for _, field := range []string{"type", "value"} {
if _, exists := propertiesField.NestedFields[remapFieldName(field)]; !exists {
t.Errorf("Expected field %s not found in properties", field)
}
}
// Validate that complex fields are properly mapped
packageSchema := catalogSchema.Schemas[declcfg.SchemaPackage]
if packageSchema == nil {
t.Fatal("Package schema not found")
}
// Check that icon field exists (it's a complex object)
if _, exists := packageSchema.Fields["icon"]; !exists {
t.Error("Icon field not discovered in package schema")
}
// Validate total object counts
if packageSchema.TotalObjects != 1 {
t.Errorf("Expected 1 package, got %d", packageSchema.TotalObjects)
}
if bundleSchema.TotalObjects != 1 {
t.Errorf("Expected 1 bundle, got %d", bundleSchema.TotalObjects)
}
}