Repository navigation
Expand file tree
/
Copy pathmessaging_spec.rb
More file actions
542 lines (442 loc) · 17.8 KB
/
Copy pathmessaging_spec.rb
File metadata and controls
542 lines (442 loc) · 17.8 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
require 'webmock/rspec'
require 'avro_turf/messaging'
describe AvroTurf::Messaging do
let(:registry_url) { "http://registry.example.com" }
let(:client_cert) { "test client cert" }
let(:client_key) { "test client key" }
let(:client_key_pass) { "test client key password" }
let(:logger) { Logger.new(StringIO.new) }
let(:path_prefix) { nil }
let(:avro) {
AvroTurf::Messaging.new(
registry_url: registry_url,
schemas_path: "spec/schemas",
logger: logger,
client_cert: client_cert,
client_key: client_key,
client_key_pass: client_key_pass
)
}
let(:message) { { "full_name" => "John Doe" } }
let(:schema_json) do
<<-AVSC
{
"name": "person",
"type": "record",
"fields": [
{
"type": "string",
"name": "full_name"
}
]
}
AVSC
end
let(:city_message) { { "name" => "Paris" } }
let(:city_schema_json) do
<<-AVSC
{
"name": "city",
"type": "record",
"fields": [
{
"type": "string",
"name": "name"
}
]
}
AVSC
end
let(:city_schema) { Avro::Schema.parse(city_schema_json) }
let(:schema) { Avro::Schema.parse(schema_json) }
before do
FileUtils.mkdir_p("spec/schemas")
end
before do
stub_request(:any, /^#{registry_url}/).to_rack(AuthorizedFakeConfluentSchemaRegistryServer)
AuthorizedFakeConfluentSchemaRegistryServer.clear
end
before do
define_schema "person.avsc", schema_json
define_schema "city.avsc", city_schema_json
end
shared_examples_for "encoding and decoding with the schema from schema store" do
it "encodes and decodes messages" do
data = avro.encode(message, schema_name: "person")
expect(avro.decode(data)).to eq message
end
it "allows specifying a reader's schema" do
data = avro.encode(message, schema_name: "person")
expect(avro.decode(data, schema_name: "person")).to eq message
end
it "caches parsed schemas for decoding" do
data = avro.encode(message, schema_name: "person")
avro.decode(data)
allow(Avro::Schema).to receive(:parse).and_call_original
expect(avro.decode(data)).to eq message
expect(Avro::Schema).not_to have_received(:parse)
end
end
shared_examples_for 'encoding and decoding with the schema from registry' do
before do
registry = AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger, path_prefix: path_prefix)
registry.register('person', schema)
registry.register('people', schema)
end
it 'encodes and decodes messages' do
data = avro.encode(message, subject: 'person', version: 1)
expect(avro.decode(data)).to eq message
end
it "allows specifying a reader's schema by subject and version" do
data = avro.encode(message, subject: 'person', version: 1)
expect(avro.decode(data, schema_name: 'person')).to eq message
end
it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry' do
expect { avro.encode(message, subject: 'missing', version: 1) }.to raise_error(AvroTurf::SchemaNotFoundError)
end
it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry and register_schemas false' do
expect { avro.encode(city_message, schema_name: 'city', register_schemas: false) }.
to raise_error(AvroTurf::SchemaNotFoundError, "Schema with structure: #{city_schema} not found on registry")
end
it 'encodes with register_schemas false when the schema exists on the registry' do
data = avro.encode(message, schema_name: 'person', register_schemas: false)
expect(avro.decode(data, schema_name: 'person')).to eq message
end
it 'caches parsed schemas for decoding' do
data = avro.encode(message, subject: 'person', version: 1)
avro.decode(data)
allow(Avro::Schema).to receive(:parse).and_call_original
expect(avro.decode(data)).to eq message
expect(Avro::Schema).not_to have_received(:parse)
end
end
shared_examples_for 'encoding and decoding with the schema_id from registry' do
before do
registry = AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger, path_prefix: path_prefix)
registry.register('person', schema)
registry.register('people', schema)
end
it 'encodes and decodes messages' do
data = avro.encode(message, schema_id: 0)
expect(avro.decode(data)).to eq message
end
it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry' do
expect { avro.encode(message, schema_id: 5) }.to raise_error(AvroTurf::SchemaNotFoundError)
end
it 'caches parsed schemas for decoding' do
data = avro.encode(message, schema_id: 0)
avro.decode(data)
allow(Avro::Schema).to receive(:parse).and_call_original
expect(avro.decode(data)).to eq message
expect(Avro::Schema).not_to have_received(:parse)
end
end
it_behaves_like "encoding and decoding with the schema from schema store"
it_behaves_like 'encoding and decoding with the schema from registry'
it_behaves_like 'encoding and decoding with the schema_id from registry'
context "with a provided registry" do
let(:registry) { AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) }
let(:avro) do
AvroTurf::Messaging.new(
registry: registry,
schemas_path: "spec/schemas",
logger: logger
)
end
it_behaves_like "encoding and decoding with the schema from schema store"
it_behaves_like 'encoding and decoding with the schema from registry'
it_behaves_like 'encoding and decoding with the schema_id from registry'
it "uses the provided registry" do
allow(registry).to receive(:register).and_call_original
message = { "full_name" => "John Doe" }
avro.encode(message, schema_name: "person")
expect(registry).to have_received(:register).with("person", anything)
end
it "allows specifying a schema registry subject" do
allow(registry).to receive(:register).and_call_original
message = { "full_name" => "John Doe" }
avro.encode(message, schema_name: "person", subject: "people")
expect(registry).to have_received(:register).with("people", anything)
end
end
context "with a provided schema store" do
let(:schema_store) { AvroTurf::SchemaStore.new(path: "spec/schemas") }
let(:avro) do
AvroTurf::Messaging.new(
registry_url: registry_url,
schema_store: schema_store,
logger: logger
)
end
it_behaves_like "encoding and decoding with the schema from schema store"
it "uses the provided schema store" do
allow(schema_store).to receive(:find).and_call_original
avro.encode(message, schema_name: "person")
expect(schema_store).to have_received(:find).with("person", nil)
end
end
describe 'decoding with #decode_message' do
shared_examples_for "encoding and decoding with the schema from schema store" do
it "encodes and decodes messages" do
data = avro.encode(message, schema_name: "person")
result = avro.decode_message(data)
expect(result.message).to eq message
expect(result.schema_id).to eq 0
expect(result.writer_schema).to eq schema
expect(result.reader_schema).to eq nil
end
it "allows specifying a reader's schema" do
data = avro.encode(message, schema_name: "person")
result = avro.decode_message(data, schema_name: "person")
expect(result.message).to eq message
expect(result.writer_schema).to eq schema
expect(result.reader_schema).to eq schema
end
it "caches parsed schemas for decoding" do
data = avro.encode(message, schema_name: "person")
avro.decode_message(data)
allow(Avro::Schema).to receive(:parse).and_call_original
expect(avro.decode_message(data).message).to eq message
expect(Avro::Schema).not_to have_received(:parse)
end
end
shared_examples_for 'encoding and decoding with the schema from registry' do
before do
registry = AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger)
registry.register('person', schema)
registry.register('people', schema)
end
it 'encodes and decodes messages' do
data = avro.encode(message, subject: 'person', version: 1)
result = avro.decode_message(data)
expect(result.message).to eq message
expect(result.schema_id).to eq 0
end
it "allows specifying a reader's schema by subject and version" do
data = avro.encode(message, subject: 'person', version: 1)
expect(avro.decode_message(data, schema_name: 'person').message).to eq message
end
it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry' do
expect { avro.encode(message, subject: 'missing', version: 1) }.to raise_error(AvroTurf::SchemaNotFoundError)
end
it 'caches parsed schemas for decoding' do
data = avro.encode(message, subject: 'person', version: 1)
avro.decode_message(data)
allow(Avro::Schema).to receive(:parse).and_call_original
expect(avro.decode_message(data).message).to eq message
expect(Avro::Schema).not_to have_received(:parse)
end
end
it_behaves_like "encoding and decoding with the schema from schema store"
it_behaves_like 'encoding and decoding with the schema from registry'
context "with a provided registry" do
let(:registry) { AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) }
let(:avro) do
AvroTurf::Messaging.new(
registry: registry,
schemas_path: "spec/schemas",
logger: logger
)
end
it_behaves_like "encoding and decoding with the schema from schema store"
it_behaves_like 'encoding and decoding with the schema from registry'
it "uses the provided registry" do
allow(registry).to receive(:register).and_call_original
message = { "full_name" => "John Doe" }
avro.encode(message, schema_name: "person")
expect(registry).to have_received(:register).with("person", anything)
end
it "allows specifying a schema registry subject" do
allow(registry).to receive(:register).and_call_original
message = { "full_name" => "John Doe" }
avro.encode(message, schema_name: "person", subject: "people")
expect(registry).to have_received(:register).with("people", anything)
end
end
context "with a provided schema store" do
let(:schema_store) { AvroTurf::SchemaStore.new(path: "spec/schemas") }
let(:avro) do
AvroTurf::Messaging.new(
registry_url: registry_url,
schema_store: schema_store,
logger: logger
)
end
it_behaves_like "encoding and decoding with the schema from schema store"
it "uses the provided schema store" do
allow(schema_store).to receive(:find).and_call_original
avro.encode(message, schema_name: "person")
expect(schema_store).to have_received(:find).with("person", nil)
end
end
end
context "validating" do
subject(:encode){ avro.encode(message, schema_name: "person", validate: true) }
context "for correct message" do
it { expect { encode }.not_to raise_error }
end
context "when message has wrong type" do
let(:message) { { "full_name" => 123 } }
it { expect { encode }.to raise_error(Avro::SchemaValidator::ValidationError, /\.full_name expected type string, got int/) }
end
context "when message contains extra fields (typo in key)" do
let(:message) { { "fulll_name" => "John Doe" } }
it { expect { encode }.to raise_error(Avro::SchemaValidator::ValidationError, /extra field 'fulll_name'/) }
end
end
context 'fetching and registering schema' do
let(:schema_store) { AvroTurf::SchemaStore.new(path: "spec/schemas") }
let(:registry) { AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) }
let(:avro) do
AvroTurf::Messaging.new(
registry: registry,
schema_store: schema_store,
logger: logger
)
end
let(:schema_id) { 234 }
context 'using fetch_schema' do
subject { avro.fetch_schema(subject: subj, version: version) }
let(:subj) { 'subject' }
let(:version) { 'version' }
let(:response) { {'id' => schema_id, 'schema' => schema_json} }
before do
allow(registry).to receive(:subject_version).with(subj, version).and_return(response)
end
it 'gets schema from registry' do
expect(subject).to eq([schema, schema_id])
end
context "with an incompatible schema type" do
let(:response) { {'id' => schema_id, 'schema' => 'blah', 'schemaType' => schema_type } }
let(:schema_type) { 'PROTOBUF' }
it 'raises IncompatibleSchemaError' do
expect { subject }.to raise_error(
AvroTurf::IncompatibleSchemaError,
"The #{schema_type} schema for #{subj} is incompatible."
)
end
end
end
context 'using fetch_schema_by_id' do
subject { avro.fetch_schema_by_id(schema_id) }
before do
allow(registry).to receive(:fetch).with(schema_id).and_return(schema_json)
end
it 'gets schema from registry' do
expect(subject).to eq([schema, schema_id])
end
end
context 'using fetch_schema_by_body' do
let(:subject_name) { 'city' }
let(:schema_name) { 'city' }
let(:namespace) { 'namespace' }
let(:city_schema_id) { 125 }
let(:city_schema_data) do
{
"subject" => subject_name,
"version" => 123,
"id" => city_schema_id,
"schema" => city_schema
}
end
subject(:fetch_schema_by_body) do
avro.fetch_schema_by_body(schema_name: schema_name, namespace: namespace, subject: subject_name)
end
before do
allow(schema_store).to receive(:find).with(schema_name, namespace).and_return(city_schema)
allow(registry).to receive(:check).with(subject_name, city_schema).and_return(city_schema_data)
end
it 'gets schema from registry' do
expect(fetch_schema_by_body).to eq([city_schema, city_schema_id])
end
end
context 'using register_schema' do
let(:schema_name) { 'schema_name' }
let(:namespace) { 'namespace' }
before do
allow(schema_store).to receive(:find).with(schema_name, namespace).and_return(schema)
end
context 'when subject is not set' do
subject { avro.register_schema(schema_name: schema_name, namespace: namespace) }
before do
allow(registry).to receive(:register).with(schema.fullname, schema).and_return(schema_id)
end
it 'registers schema in registry' do
expect(subject).to eq([schema, schema_id])
end
end
context 'when subject is set' do
subject { avro.register_schema(schema_name: schema_name, namespace: namespace, subject: subj) }
let(:subj) { 'subject' }
before do
allow(registry).to receive(:register).with(subj, schema).and_return(schema_id)
end
it 'registers schema in registry' do
expect(subject).to eq([schema, schema_id])
end
end
end
end
context 'with a registry path prefix' do
let(:path_prefix) { '/prefix' }
let(:avro) {
AvroTurf::Messaging.new(
registry_path_prefix: path_prefix,
registry_url: registry_url,
schemas_path: "spec/schemas",
logger: logger,
client_cert: client_cert,
client_key: client_key,
client_key_pass: client_key_pass
)
}
before do
stub_request(:any, /^#{registry_url}/).to_rack(AuthorizedFakePrefixedConfluentSchemaRegistryServer)
AuthorizedFakePrefixedConfluentSchemaRegistryServer.clear
end
it_behaves_like "encoding and decoding with the schema from schema store"
it_behaves_like 'encoding and decoding with the schema from registry'
it_behaves_like 'encoding and decoding with the schema_id from registry'
end
context 'with a connect timeout' do
let(:avro) {
AvroTurf::Messaging.new(
registry_url: registry_url,
schemas_path: "spec/schemas",
logger: logger,
client_cert: client_cert,
client_key: client_key,
client_key_pass: client_key_pass,
connect_timeout: 10
)
}
it_behaves_like "encoding and decoding with the schema from schema store"
it_behaves_like 'encoding and decoding with the schema from registry'
it_behaves_like 'encoding and decoding with the schema_id from registry'
it 'passes the connect timeout setting to Excon' do
expect(Excon).to receive(:new).with(anything, hash_including(connect_timeout: 10)).and_call_original
avro
end
end
context 'with a custom domain name resolver' do
let(:resolv_resolver) { Resolv.new([Resolv::Hosts.new, Resolv::DNS.new(nameserver: ['127.0.0.1', '127.0.0.1'])]) }
let(:avro) {
AvroTurf::Messaging.new(
registry_url: registry_url,
schemas_path: "spec/schemas",
logger: logger,
client_cert: client_cert,
client_key: client_key,
client_key_pass: client_key_pass,
resolv_resolver: resolv_resolver
)
}
it_behaves_like "encoding and decoding with the schema from schema store"
it_behaves_like 'encoding and decoding with the schema from registry'
it_behaves_like 'encoding and decoding with the schema_id from registry'
it 'passes the domain name resolver setting to Excon' do
expect(Excon).to receive(:new).with(anything, hash_including(resolv_resolver: resolv_resolver)).and_call_original
avro
end
end
end