-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathquery.rs
658 lines (562 loc) · 23.1 KB
/
query.rs
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
extern crate chrono;
extern crate serde_json;
extern crate aw_query;
// TODO: Move me to an appropriate place
#[macro_export]
macro_rules! json_map {
{ $( $key:literal : $value:expr),* } => {{
use serde_json::Value;
use serde_json::map::Map;
#[allow(unused_mut)]
let mut map : Map<String, Value> = Map::new();
$(
map.insert( $key.to_string(), json!($value) );
)*
map
}};
}
#[cfg(test)]
mod query_tests {
use chrono;
use chrono::Duration;
use serde_json::json;
use std::convert::TryFrom;
use aw_query::DataType;
use aw_query::QueryError;
use aw_datastore::Datastore;
use aw_models::Bucket;
use aw_models::BucketMetadata;
use aw_models::Event;
use aw_models::TimeInterval;
static TIME_INTERVAL: &str = "1980-01-01T00:00:00Z/2080-01-02T00:00:00Z";
static BUCKET_ID: &str = "testid";
fn setup_datastore_empty() -> Datastore {
return Datastore::new_in_memory(false);
}
fn setup_datastore_with_bucket() -> Datastore {
let ds = setup_datastore_empty();
// Create bucket
let bucket = Bucket {
bid: None,
id: BUCKET_ID.to_string(),
_type: "testtype".to_string(),
client: "testclient".to_string(),
hostname: "testhost".to_string(),
created: Some(chrono::Utc::now()),
data: json_map! {},
metadata: BucketMetadata::default(),
events: None,
last_updated: None,
};
ds.create_bucket(&bucket).unwrap();
return ds;
}
fn setup_datastore_populated() -> Datastore {
let ds = setup_datastore_with_bucket();
// Insert events
let e1 = Event {
id: None,
timestamp: chrono::Utc::now(),
duration: Duration::seconds(0),
data: json_map! {"key": json!("value")},
};
let mut e2 = e1.clone();
e2.timestamp = chrono::Utc::now();
let mut e_replace = e2.clone();
e_replace.data = json_map! {"key": json!("value2")};
e_replace.duration = Duration::seconds(2);
let mut event_list = Vec::new();
event_list.push(e1.clone());
event_list.push(e2.clone());
ds.insert_events(&BUCKET_ID, &event_list).unwrap();
return ds;
}
macro_rules! assert_err_type {
($v:expr, $p:pat) => {
match $v {
Ok(_) => panic!("Expected an error, got {:?}", $v),
Err(e) => match e {
$p => (),
_ => panic!("Expected an error of another type, got {:?}", e),
},
}
};
}
#[test]
fn test_bool() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("True;False;a=True;return True;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Bool(b) => assert_eq!(b, true),
ref data => panic!("Wrong datatype, {:?}", data),
};
}
#[test]
fn test_number() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("1;1.;return 1.1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 1.1),
ref data => panic!("Wrong datatype, {:?}", data),
};
}
#[test]
fn test_equals() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
// number comparison true
let code = String::from("return 1==1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Bool(b) => assert_eq!(b, true),
ref data => panic!("Wrong datatype, {:?}", data),
};
// number comparison false
let code = String::from("return 2==1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Bool(b) => assert_eq!(b, false),
ref data => panic!("Wrong datatype, {:?}", data),
};
// string comparison true
let code = String::from(r#"return "a"=="a";"#);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Bool(b) => assert_eq!(b, true),
ref data => panic!("Wrong datatype, {:?}", data),
};
// string comparison false
let code = String::from(r#"return "a"=="b";"#);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Bool(b) => assert_eq!(b, false),
ref data => panic!("Wrong datatype, {:?}", data),
};
// bool comparison true
let code = String::from("return True==True;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Bool(b) => assert_eq!(b, true),
ref data => panic!("Wrong datatype, {:?}", data),
};
// bool comparison false
let code = String::from("return False==True;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Bool(b) => assert_eq!(b, false),
ref data => panic!("Wrong datatype, {:?}", data),
};
// different types comparison (should raise an error)
let code = String::from("return True==1;");
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidType(_));
}
#[test]
fn test_return() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("return 1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 1.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
// Test with old RETURN method
let code = String::from("RETURN=1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 1.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
let code = String::from("return 1+1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 2.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
}
#[test]
fn test_if() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
// Test hardcoded True
let code = String::from(
"
n=1;
if True { n=2; }
return n;",
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 2.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
// Test hardcoded False
let code = String::from(
"
n=1;
if False { n=2; }
return n;",
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 1.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
// Test expression True
let code = String::from(
"
a=True; n=1;
if &a { n=2; }
return n;",
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 2.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
// Test expression False
let code = String::from(
"
a=False; n=1;
if a { n=2; }
return n;",
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 1.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
// Test if else
let code = String::from(
"
a=False; n=1;
if a { }
else { n=3; }
return n;",
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 3.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
// Test if else if
let code = String::from(
"
a=False; b=True; n=1;
if &a { n=2; }
elif &b { n=3; }
return n;",
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 3.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
// Test if else if else
let code = String::from(
"
a=False; b=True; n=1;
if &a { n=2; }
elif &a { n=3; }
else { n=4; }
return n;",
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 4.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
// Test if inside if
let code = String::from(
"
a=True; n=1;
if &a { if &a { n = 2; } }
return n;",
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::Number(n) => assert_eq!(n, 2.0),
ref data => panic!("Wrong datatype, {:?}", data),
};
}
#[test]
fn test_function() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("return print(1);");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return print(1, 2);");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return no_such_function(1);");
match aw_query::query(&code, &interval, &ds) {
Ok(ok) => panic!(format!("Expected QueryError, got {:?}", ok)),
Err(e) => match e {
QueryError::VariableNotDefined(qe) => assert_eq!(qe, "no_such_function"),
qe => panic!(format!(
"Expected QueryError::VariableNotDefined, got {:?}",
qe
)),
},
}
let code = String::from("invalid_type=1; return invalid_type(1);");
match aw_query::query(&code, &interval, &ds) {
Ok(ok) => panic!(format!("Expected QueryError, got {:?}", ok)),
Err(e) => match e {
QueryError::InvalidType(qe) => assert_eq!(qe, "invalid_type"),
qe => panic!(format!(
"Expected QueryError::VariableNotDefined, got {:?}",
qe
)),
},
}
}
#[test]
fn test_all_functions() {
let ds = setup_datastore_populated();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("return query_bucket(\"testid\");");
aw_query::query(&code, &interval, &ds).unwrap();
let code = format!(
r#"
events = query_bucket(find_bucket("{}"));
events = flood(events);
events = sort_by_duration(events);
events = limit_events(events, 10000);
events = sort_by_timestamp(events);
events = concat(events, query_bucket("{}"));
events = categorize(events, [[["test"], {{ "type": "regex", "regex": "value$" }}], [["test", "testing"], {{ "type": "regex", "regex": "value$" }}]]);
events = tag(events, [["testtag", {{ "type": "regex", "regex": "test$" }}], ["another testtag", {{ "type": "regex", "regex": "test-pat$" }}]]);
total_duration = sum_durations(&events);
bucketnames = query_bucket_names();
print("test", "test2");
events = split_url_events (events);
events = filter_period_intersect(&events, &events);
events = filter_keyvals(events, "$category", [["Uncategorized"]]);
events = filter_keyvals_regex(events, "key", "regex");
events = chunk_events_by_key(events, "key");
events = merge_events_by_keys(events, ["key"]);
return events;"#,
"testid", "testid"
);
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::List(l) => l,
ref data => panic!("Wrong datatype, {:?}", data),
};
// TODO: assert_eq result
}
#[test]
fn test_categorize() {
let ds = setup_datastore_populated();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = format!(
r#"
events = query_bucket("{}");
events = categorize(events, [[["Test", "Subtest"], {{ "type": "regex", "regex": "^value$" }}]]);
return events;"#,
"testid"
);
let result: DataType = aw_query::query(&code, &interval, &ds).unwrap();
let events: Vec<Event> = Vec::try_from(&result).unwrap();
let event = events.first().unwrap();
let cats = event.data.get("$category").unwrap();
assert_eq!(cats, &serde_json::json!(vec!["Test", "Subtest"]));
}
#[test]
fn test_tag() {
let ds = setup_datastore_populated();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = format!(
r#"
events = query_bucket("{}");
events = tag(events, [["testtag", {{ "type": "regex", "regex": "value$" }}], ["another testtag", {{ "type": "regex", "regex": "value$" }}]]);
return events;"#,
"testid"
);
let result: DataType = aw_query::query(&code, &interval, &ds).unwrap();
let events: Vec<Event> = Vec::try_from(&result).unwrap();
let event = events.first().unwrap();
let tags = event.data.get("$tags").unwrap().as_array().unwrap();
assert_eq!(tags.len(), 2);
}
#[test]
fn test_rule_parsing() {
let ds = setup_datastore_populated();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
// Test rule where rule is not a dict
let code = r#"
events = [];
events = tag(events, ["test", false]);
return events;"#;
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
// Test rule without type
let code = r#"
events = [];
events = tag(events, [["testtag", { }]]);
return events;"#;
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
// Test invalid rule type
let code = r#"
events = [];
events = tag(events, [["testtag", { "type": false }]]);
return events;"#;
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
// Test invalid rule name
let code = r#"
events = [];
events = tag(events, [["testtag", { "type": "rgex" }]]);
return events;"#;
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
// Test "none" rule type
let code = r#"
events = [];
events = tag(events, [["testtag", { "type": "none" }]]);
return events;"#;
aw_query::query(&code, &interval, &ds).unwrap();
// Test regex rule where regex field has wrong type
let code = r#"
events = [];
events = tag(events, [["testtag", { "type": "regex", "regex": true }]]);
return events;"#;
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
// Test regex rule where regex field is not set
let code = r#"
events = [];
events = tag(events, [["testtag", { "type": "regex" }]]);
return events;"#;
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
// Test regex rule with ignore_case field
let code = r#"
events = [];
events = tag(events, [["testtag", { "type": "regex", "regex": "test", "ignore_case": false }]]);
return events;"#;
aw_query::query(&code, &interval, &ds).unwrap();
// Test regex rule where ignore_case field is of invalid type
let code = r#"
events = [];
events = tag(events, [["testtag", { "type": "regex", "regex": "test", "ignore_case": "" }]]);
return events;"#;
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
// Test regex rule where uncompilable regex is supplied
let code = r#"
events = [];
events = tag(events, [["testtag", { "type": "regex", "regex": "!#¤%&/(=" }]]);
return events;"#;
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::RegexCompileError(_));
}
#[test]
fn test_string() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("return \"test \\\" with escaped quote\";");
match aw_query::query(&code, &interval, &ds).unwrap() {
aw_query::DataType::String(s) => assert_eq!(s, "test \" with escaped quote"),
_ => panic!("Wrong datatype"),
}
}
#[test]
fn test_list() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("return [];");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return [1];");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return [1+1];");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return [1,1];");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return [1,1+2];");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return [1,1+1,1+2+3,4/3,[1+2]];");
aw_query::query(&code, &interval, &ds).unwrap();
}
#[test]
fn test_comment() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("return 1;# testing 123");
aw_query::query(&code, &interval, &ds).unwrap();
}
#[test]
fn test_dict() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("return {};");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return {\"test\": 2};");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return {\"test\": 2, \"test2\": \"teststr\"};");
aw_query::query(&code, &interval, &ds).unwrap();
let code = String::from("return {\"test\": {\"test\": \"test\"}};");
aw_query::query(&code, &interval, &ds).unwrap();
}
#[test]
fn test_concat() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
// Append lists
let code = String::from("return [1]+[2];");
let res = aw_query::query(&code, &interval, &ds).unwrap();
let mut v = Vec::new();
v.push(DataType::Number(1.0));
v.push(DataType::Number(2.0));
assert_eq!(res, DataType::List(v));
// Append strings
let code = String::from(r#"return "a"+"b";"#);
let res = aw_query::query(&code, &interval, &ds).unwrap();
assert_eq!(res, DataType::String("ab".to_string()));
}
#[test]
fn test_contains() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
// test list true
let code = String::from(r#"a = ["b", "a"]; return contains(a, "a");"#);
let res = aw_query::query(&code, &interval, &ds).unwrap();
assert_eq!(res, DataType::Bool(true));
// test list false
let code = String::from(r#"a = ["b", "a"]; return contains(a, "c");"#);
let res = aw_query::query(&code, &interval, &ds).unwrap();
assert_eq!(res, DataType::Bool(false));
// test dict true
let code = String::from(r#"a = {"a": 1}; return contains(a, "a");"#);
let res = aw_query::query(&code, &interval, &ds).unwrap();
assert_eq!(res, DataType::Bool(true));
// test dict false
let code = String::from(r#"a = {"b": 1}; return contains(a, "a");"#);
let res = aw_query::query(&code, &interval, &ds).unwrap();
assert_eq!(res, DataType::Bool(false));
}
#[test]
fn test_math() {
let ds = setup_datastore_empty();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();
let code = String::from("return 1+1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
DataType::Number(n) => assert_eq!(n, 2.0),
num => panic!("Expected number, got {:?}", num),
};
let code = String::from("return 1-1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
DataType::Number(n) => assert_eq!(n, 0.0),
num => panic!("Expected number, got {:?}", num),
};
let code = String::from("return 3*5;");
match aw_query::query(&code, &interval, &ds).unwrap() {
DataType::Number(n) => assert_eq!(n, 15.0),
num => panic!("Expected number, got {:?}", num),
};
let code = String::from("return 4/2;");
match aw_query::query(&code, &interval, &ds).unwrap() {
DataType::Number(n) => assert_eq!(n, 2.0),
num => panic!("Expected number, got {:?}", num),
};
let code = String::from("return 1/0;");
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::MathError(_));
let code = String::from("return 2.5%1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
DataType::Number(n) => assert_eq!(n, 0.5),
num => panic!("Expected number, got {:?}", num),
};
let code = String::from("return 1+1+0+1;");
match aw_query::query(&code, &interval, &ds).unwrap() {
DataType::Number(n) => assert_eq!(n, 3.0),
num => panic!("Expected number, got {:?}", num),
};
}
}