-
Notifications
You must be signed in to change notification settings - Fork 1
/
schemaregistry_client_test.go
263 lines (228 loc) · 8.61 KB
/
schemaregistry_client_test.go
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
/**
* Copyright 2022 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package schemaregistry
import (
"fmt"
"io/ioutil"
"log"
"sort"
"strconv"
"testing"
)
var schemaTests = [][]string{
{"./test/avro/complex.avsc"},
{"./test/avro/union.avsc"},
{"./test/avro/null.avsc"},
{"./test/avro/bool.avsc"},
{"./test/avro/int.avsc"},
{"./test/avro/long.avsc"},
{"./test/avro/float.avsc"},
{"./test/avro/double.avsc"},
{"./test/avro/advanced.avsc", "./test/avro/advanced-2.avsc"},
{"./test/avro/string.avsc"},
}
func testRegister(subject string, schema SchemaInfo) (id int) {
id, err := srClient.Register(subject, schema, false)
maybeFail(subject, err)
return id
}
func testGetBySubjectAndID(subject string, id int) SchemaInfo {
schema, err := srClient.GetBySubjectAndID(subject, id)
maybeFail(strconv.Itoa(id), err)
return schema
}
func testGetBySubjectAndIDNotFound(subject string, id int) {
_, err := srClient.GetBySubjectAndID(subject, id)
if err == nil {
maybeFail("testGetBySubjectAndIDNotFound", fmt.Errorf("Expected error, found nil"))
}
}
func testGetID(subject string, schema SchemaInfo, expected int) int {
actual, err := srClient.GetID(subject, schema, false)
maybeFail(subject, err, expect(actual, expected))
return actual
}
func testGetIDNotFound(subject string, schema SchemaInfo) {
_, err := srClient.GetID(subject, schema, false)
if err == nil {
maybeFail("testGetIDNotFound", fmt.Errorf("Expected error, found nil"))
}
}
func testGetLatestSchemaMetadata(subject string) {
_, err := srClient.GetLatestSchemaMetadata(subject)
maybeFail(subject, err)
}
func testGetSchemaMetadata(subject string, versionID int, expected string) {
actual, err := srClient.GetSchemaMetadata(subject, versionID)
// avoid nil pointer dereference
maybeFail(subject, err)
maybeFail(subject, expect(expected, actual.Schema))
}
func testGetVersion(subject string, schema SchemaInfo) (version int) {
actual, err := srClient.GetVersion(subject, schema, false)
maybeFail(subject, err)
return actual
}
func testGetVersionNotFound(subject string, schema SchemaInfo) {
_, err := srClient.GetVersion(subject, schema, false)
if err == nil {
maybeFail("testGetVersionNotFound", fmt.Errorf("Expected error, found nil"))
}
}
func testGetAllVersions(subject string, expected []int) {
actual, err := srClient.GetAllVersions(subject)
sort.Ints(actual)
sort.Ints(expected)
maybeFail(subject, err, expect(actual, expected))
}
func testGetCompatibility(subject string, expected Compatibility) {
actual, err := srClient.GetCompatibility(subject)
maybeFail(subject, err, expect(actual, expected))
}
func testUpdateCompatibility(subject string, update Compatibility, expected Compatibility) {
actual, err := srClient.UpdateCompatibility(subject, update)
maybeFail(subject, err, expect(actual, expected))
}
func testGetDefaultCompatibility(expected Compatibility) {
actual, err := srClient.GetDefaultCompatibility()
maybeFail("Default Compatibility", err, expect(actual, expected))
}
func testUpdateDefaultCompatibility(update Compatibility, expected Compatibility) {
actual, err := srClient.UpdateDefaultCompatibility(update)
maybeFail("Default Compatibility", err, expect(actual, expected))
}
func testGetAllSubjects(expected []string) {
actual, err := srClient.GetAllSubjects()
sort.Strings(actual)
sort.Strings(expected)
maybeFail("All Subjects", err, expect(actual, expected))
}
func testDeleteSubject(subject string, permanent bool, expected []int, ids []int, schemas []SchemaInfo) {
actual, err := srClient.DeleteSubject(subject, permanent)
sort.Ints(actual)
sort.Ints(expected)
maybeFail(subject, err, expect(actual, expected))
for i := range expected {
if permanent {
testGetBySubjectAndIDNotFound(subject, ids[i])
} else {
testGetBySubjectAndID(subject, ids[i])
}
testGetIDNotFound(subject, schemas[i])
testGetVersionNotFound(subject, schemas[i])
}
}
func testDeleteSubjectVersion(subject string, permanent bool, version int, expected int, id int, schema SchemaInfo) {
actual, err := srClient.DeleteSubjectVersion(subject, version, permanent)
maybeFail(subject, err, expect(actual, expected))
if permanent {
testGetBySubjectAndIDNotFound(subject, id)
} else {
testGetBySubjectAndID(subject, id)
}
testGetIDNotFound(subject, schema)
testGetVersionNotFound(subject, schema)
}
func testTestCompatibility(subject string, version int, schema SchemaInfo, expected bool) {
actual, err := srClient.TestCompatibility(subject, version, schema)
maybeFail(subject, err, expect(actual, expected))
}
func testRemainingVersions(subjects []string, schemas [][]SchemaInfo, ids [][]int, versions [][]int) {
for i := range subjects {
for j := range schemas[i] {
testGetID(subjects[i], schemas[i][j], ids[i][j])
foundVersion := testGetVersion(subjects[i], schemas[i][j])
maybeFail("testRemainingVersions", expect(foundVersion, versions[i][j]))
}
}
}
func TestClient(t *testing.T) {
maybeFail = initFailFunc(t)
url := testconf.getString("SchemaRegistryURL")
if url == "" {
url = "mock://"
}
conf := NewConfig(url)
var err error
srClient, err = NewClient(conf)
maybeFail("schema registry client instantiation ", err)
var subjects = make([]string, len(schemaTests))
var ids = make([][]int, len(schemaTests))
var versions = make([][]int, len(schemaTests))
var schemas = make([][]SchemaInfo, len(schemaTests))
var version int
for idx, schemaTestVersions := range schemaTests {
var currentVersions = make([]int, 0)
subject := fmt.Sprintf("schema%d-key", idx)
srClient.DeleteSubject(subject, false)
srClient.DeleteSubject(subject, true)
subjects[idx] = subject
for _, schemaTest := range schemaTestVersions {
buff, err := ioutil.ReadFile(schemaTest)
if err != nil {
panic(err)
}
schema := SchemaInfo{
Schema: string(buff),
}
id := testRegister(subject, schema)
version = testGetVersion(subject, schema)
// The schema registry will return a normalized Avro Schema so we can't directly compare the two
// To work around this we retrieve a normalized schema from the Schema registry first for comparison
normalized := testGetBySubjectAndID(subject, id)
testGetSchemaMetadata(subject, version, normalized.Schema)
testGetLatestSchemaMetadata(subject)
testUpdateCompatibility(subject, Forward, Forward)
testGetCompatibility(subject, Forward)
testUpdateDefaultCompatibility(None, None)
testGetDefaultCompatibility(None)
currentVersions = append(currentVersions, version)
testGetAllVersions(subject, currentVersions)
ids[idx] = append(ids[idx], id)
versions[idx] = append(versions[idx], version)
schemas[idx] = append(schemas[idx], schema)
}
}
lastSubject := len(subjects) - 1
secondToLastSubject := len(subjects) - 2
testDeleteSubject(subjects[lastSubject], false, versions[lastSubject], ids[lastSubject], schemas[lastSubject])
testDeleteSubjectVersion(subjects[secondToLastSubject], false, versions[secondToLastSubject][0], versions[secondToLastSubject][0], ids[secondToLastSubject][0], schemas[secondToLastSubject][0])
// Second to last subject now has only one version
initialVersionsSecondToLastSubject := versions[secondToLastSubject][0:]
initialSchemasSecondToLastSubject := schemas[secondToLastSubject][0:]
initialIdsSecondToLastSubject := ids[secondToLastSubject][0:]
versions[secondToLastSubject] = versions[secondToLastSubject][1:]
schemas[secondToLastSubject] = schemas[secondToLastSubject][1:]
ids[secondToLastSubject] = ids[secondToLastSubject][1:]
// Only last subject has been removed completely
testGetAllSubjects(subjects[:lastSubject])
remainingSubjects := subjects[:lastSubject]
testRemainingVersions(remainingSubjects, schemas, ids, versions)
// Cleanup subjects
for i := range remainingSubjects {
testDeleteSubject(remainingSubjects[i], false, versions[i], ids[i], schemas[i])
if i == secondToLastSubject {
testDeleteSubject(remainingSubjects[i], true, initialVersionsSecondToLastSubject, initialIdsSecondToLastSubject, initialSchemasSecondToLastSubject)
} else {
testDeleteSubject(remainingSubjects[i], true, versions[i], ids[i], schemas[i])
}
}
}
func init() {
if !testconfRead() {
log.Print("WARN: Missing testconf.json, using mock client")
}
}