-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest.rs
More file actions
410 lines (379 loc) · 13.4 KB
/
test.rs
File metadata and controls
410 lines (379 loc) · 13.4 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
use anyhow::{Context, Result};
use apache_avro::{types::Value as AvroValue, Schema as AvroSchema};
use base64::engine::general_purpose::STANDARD as base64;
use base64::Engine;
use bigdecimal::BigDecimal;
use core::str;
use rdkafka::{
admin::{AdminClient, AdminOptions},
consumer::{BaseConsumer, Consumer},
ClientConfig, Message, TopicPartitionList,
};
use serde_json::{json, Map};
use std::collections::HashMap;
use std::sync::Once;
use time::{format_description, OffsetDateTime};
const BOOTSTRAP_SERVERS: &str = "localhost:9092";
const SCHEMA_REGISTRY_ENDPOINT: &str = "http://localhost:8081";
static KAFKA_INTERNAL_TOPICS: [&str; 4] = [
"__consumer_offsets",
"__amazon_msk_canary",
"_schemas",
"__transaction_state",
];
#[test]
fn test_spec() {
let output = std::process::Command::new("flowctl")
.args([
"raw",
"spec",
"--source",
"tests/test.flow.yaml",
"--name",
"acmeCo/materialize-kafka/avro",
])
.output()
.unwrap();
assert!(output.status.success());
let got: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
insta::assert_snapshot!(serde_json::to_string_pretty(&got).unwrap());
}
#[tokio::test]
async fn test_materialization() {
ensure_services_up();
drop_topics().await;
for name in [
"acmeCo/materialize-kafka/avro",
"acmeCo/materialize-kafka/json",
] {
let output = std::process::Command::new("flowctl")
.args([
"preview",
"--source",
"tests/test.flow.yaml",
"--fixture",
"tests/fixture.json",
"--name",
name,
])
.output()
.unwrap();
println!("{}", str::from_utf8(&output.stderr).unwrap());
assert!(output.status.success());
}
insta::assert_snapshot!(snapshot_topics().await);
}
fn ensure_services_up() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let _ = std::process::Command::new("docker")
.args(["network", "create", "flow-test"])
.output();
let status = std::process::Command::new("docker")
.args(["compose", "-f", "docker-compose.yaml", "up", "--wait"])
.status()
.expect("failed to invoke docker compose");
if !status.success() {
let logs = std::process::Command::new("docker")
.args(["compose", "-f", "docker-compose.yaml", "logs"])
.output();
if let Ok(logs) = logs {
eprintln!("--- docker compose logs ---");
eprintln!("{}", String::from_utf8_lossy(&logs.stdout));
eprintln!("{}", String::from_utf8_lossy(&logs.stderr));
}
panic!("docker compose up --wait failed");
}
});
}
async fn drop_topics() {
let admin: AdminClient<_> = ClientConfig::new()
.set("bootstrap.servers", BOOTSTRAP_SERVERS)
.create()
.unwrap();
let topics = list_topics().await;
let topics = topics.iter().map(String::as_str).collect::<Vec<&str>>();
if !topics.is_empty() {
admin
.delete_topics(&topics, &AdminOptions::default())
.await
.unwrap();
}
}
async fn list_topics() -> Vec<String> {
let consumer: BaseConsumer = ClientConfig::new()
.set("bootstrap.servers", BOOTSTRAP_SERVERS)
.create()
.unwrap();
let meta = consumer.fetch_metadata(None, None).unwrap();
let mut topics = meta
.topics()
.iter()
.filter(|t| !KAFKA_INTERNAL_TOPICS.contains(&t.name()))
.map(|t| t.name().to_string())
.collect::<Vec<_>>();
topics.sort();
topics
}
async fn snapshot_topics() -> String {
let mut out = String::new();
for topic in list_topics().await {
if !out.is_empty() {
out.push_str("\n");
}
out.push_str("************\n");
out.push_str(format!("Topic: {}\n", topic).as_str());
out.push_str("************\n");
out.push_str(&snapshot_topic(&topic).await);
out.push_str("\n");
}
out
}
async fn snapshot_topic(topic: &str) -> String {
let consumer: BaseConsumer = ClientConfig::new()
.set("bootstrap.servers", BOOTSTRAP_SERVERS)
.set("enable.auto.commit", "false")
.set("group.id", "materialize-kafka")
.set("auto.offset.reset", "earliest")
.set("enable.partition.eof", "true")
.create()
.unwrap();
let meta = consumer.fetch_metadata(Some(topic), None).unwrap();
assert!(meta.topics()[0].partitions().len() != 0);
let assignment =
meta.topics()[0]
.partitions()
.iter()
.fold(TopicPartitionList::new(), |mut tpl, p| {
tpl.add_partition(topic, p.id());
tpl
});
consumer.assign(&assignment).unwrap();
let mut n = consumer.assignment().unwrap().count();
let mut schema_cache: HashMap<u32, apache_avro::Schema> = HashMap::new();
let mut rows = Vec::new();
for msg in consumer.iter() {
match msg {
Ok(msg) => {
let key = parse_datum(msg.key().unwrap(), &mut schema_cache).await;
let value = parse_datum(msg.payload().unwrap(), &mut schema_cache).await;
let row = format!("key: {}, value: {}", key, value);
rows.push(row);
}
_ => {
n -= 1;
if n == 0 {
break;
}
}
}
}
rows.sort();
rows.join("\n")
}
async fn parse_datum(datum: &[u8], schema_cache: &mut HashMap<u32, apache_avro::Schema>) -> String {
match datum[0] {
0 => {
let schema_id = u32::from_be_bytes(datum[1..5].try_into().unwrap());
if !schema_cache.contains_key(&schema_id) {
schema_cache.insert(schema_id, fetch_schema(schema_id).await);
}
let schema = schema_cache.get(&schema_id).unwrap();
let avro_value = apache_avro::from_avro_datum(schema, &mut &datum[5..], None).unwrap();
avro_to_json(avro_value, schema).unwrap().to_string()
}
_ => str::from_utf8(datum).unwrap().to_owned(),
}
}
async fn fetch_schema(id: u32) -> apache_avro::Schema {
let schema_string = reqwest::get(format!(
"{SCHEMA_REGISTRY_ENDPOINT}/schemas/ids/{id}/schema"
))
.await
.unwrap()
.error_for_status()
.unwrap()
.text()
.await
.unwrap();
apache_avro::Schema::parse_str(&schema_string).unwrap()
}
// TODO(whb): This is copied entirely from source-kafka. Consider a better way
// to share code between the two connectors.
fn avro_to_json(value: AvroValue, schema: &AvroSchema) -> Result<serde_json::Value> {
Ok(match value {
AvroValue::Null => json!(null),
AvroValue::Boolean(v) => json!(v),
AvroValue::Int(v) => json!(v),
AvroValue::Long(v) => json!(v),
AvroValue::Float(v) => match v.is_nan() || v.is_infinite() {
true => json!(v.to_string()),
false => json!(v),
},
AvroValue::Double(v) => match v.is_nan() || v.is_infinite() {
true => json!(v.to_string()),
false => json!(v),
},
AvroValue::Bytes(v) => json!(base64.encode(v)),
AvroValue::String(v) => json!(v),
AvroValue::Fixed(_, v) => json!(base64.encode(v)),
AvroValue::Enum(_, v) => json!(v),
AvroValue::Union(idx, v) => match schema {
AvroSchema::Union(s) => avro_to_json(*v, &s.variants()[idx as usize])
.context("failed to decode union value")?,
_ => anyhow::bail!(
"expected a union schema for a union value but got {}",
schema
),
},
AvroValue::Array(v) => match schema {
AvroSchema::Array(s) => json!(v
.into_iter()
.map(|v| avro_to_json(v, &s.items))
.collect::<Result<Vec<_>>>()?),
_ => anyhow::bail!(
"expected an array schema for an array value but got {}",
schema
),
},
AvroValue::Map(v) => match schema {
AvroSchema::Map(s) => json!(v
.into_iter()
.map(|(k, v)| Ok((k, avro_to_json(v, &s.types)?)))
.collect::<Result<Map<_, _>>>()?),
_ => anyhow::bail!("expected a map schema for a map value but got {}", schema),
},
AvroValue::Record(v) => match schema {
AvroSchema::Record(s) => json!(v
.into_iter()
.zip(s.fields.iter())
.map(|((k, v), field)| {
if k != field.name {
anyhow::bail!(
"expected record field value with name '{}' but schema had name '{}'",
k,
field.name,
)
}
Ok((k, avro_to_json(v, &field.schema)?))
})
.collect::<Result<Map<_, _>>>()?),
_ => anyhow::bail!(
"expected a record schema for a record value but got {}",
schema
),
},
AvroValue::Date(v) => {
let date = OffsetDateTime::UNIX_EPOCH + time::Duration::days(v.into());
json!(format!(
"{}-{:02}-{:02}",
date.year(),
date.month() as u8,
date.day()
))
}
AvroValue::Decimal(v) => match schema {
AvroSchema::Decimal(s) => json!(BigDecimal::new(v.into(), s.scale as i64).to_string()),
_ => anyhow::bail!(
"expected a decimal schema for a decimal value but got {}",
schema
),
},
AvroValue::BigDecimal(v) => json!(v.to_string()),
AvroValue::TimeMillis(v) => {
let time = OffsetDateTime::UNIX_EPOCH + time::Duration::milliseconds(v as i64);
json!(format!(
"{:02}:{:02}:{:02}.{:03}",
time.hour(),
time.minute(),
time.second(),
time.millisecond()
))
}
AvroValue::TimeMicros(v) => {
let time = OffsetDateTime::UNIX_EPOCH + time::Duration::microseconds(v);
json!(format!(
"{:02}:{:02}:{:02}.{:06}",
time.hour(),
time.minute(),
time.second(),
time.microsecond()
))
}
AvroValue::TimestampMillis(v) => {
let time = OffsetDateTime::UNIX_EPOCH + time::Duration::milliseconds(v);
json!(time
.format(&format_description::well_known::Rfc3339)
.unwrap())
}
AvroValue::TimestampMicros(v) => {
let time = OffsetDateTime::UNIX_EPOCH + time::Duration::microseconds(v);
json!(time
.format(&format_description::well_known::Rfc3339)
.unwrap())
}
AvroValue::TimestampNanos(v) => {
let time = OffsetDateTime::UNIX_EPOCH + time::Duration::nanoseconds(v);
json!(time
.format(&format_description::well_known::Rfc3339)
.unwrap())
}
AvroValue::LocalTimestampMillis(v) => {
let time = OffsetDateTime::UNIX_EPOCH + time::Duration::milliseconds(v);
json!(time
.format(&format_description::well_known::Rfc3339)
.unwrap())
}
AvroValue::LocalTimestampMicros(v) => {
let time = OffsetDateTime::UNIX_EPOCH + time::Duration::microseconds(v);
json!(time
.format(&format_description::well_known::Rfc3339)
.unwrap())
}
AvroValue::LocalTimestampNanos(v) => {
let time = OffsetDateTime::UNIX_EPOCH + time::Duration::nanoseconds(v);
json!(time
.format(&format_description::well_known::Rfc3339)
.unwrap())
}
AvroValue::Duration(v) => {
json!(duration_to_duration_string(
v.months().into(),
v.days().into(),
v.millis().into()
))
}
AvroValue::Uuid(v) => json!(v.to_string()),
})
}
fn duration_to_duration_string(months: u32, days: u32, total_milliseconds: u32) -> String {
let total_seconds = total_milliseconds / 1000;
let hours = total_seconds / 3600;
let minutes = (total_seconds % 3600) / 60;
let seconds = total_seconds % 60;
let milliseconds = total_milliseconds % 1000;
let mut duration = String::from("P");
if months > 0 {
duration.push_str(&format!("{}M", months));
}
if days > 0 {
duration.push_str(&format!("{}D", days));
}
if hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0 {
duration.push('T');
if hours > 0 {
duration.push_str(&format!("{}H", hours));
}
if minutes > 0 {
duration.push_str(&format!("{}M", minutes));
}
if seconds > 0 || milliseconds > 0 {
if milliseconds > 0 {
duration.push_str(&format!("{}.{:03}S", seconds, milliseconds));
} else {
duration.push_str(&format!("{}S", seconds));
}
}
}
duration
}