-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtests.rs
983 lines (844 loc) · 33.1 KB
/
tests.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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
use crate::sources::docker_logs::*;
#[test]
fn generate_config() {
crate::test_util::test_generate_config::<DockerLogsConfig>();
}
#[test]
fn exclude_self() {
let (tx, _rx) = SourceSender::new_test();
let mut source = DockerLogsSource::new(
DockerLogsConfig::default(),
tx,
ShutdownSignal::noop(),
LogNamespace::Legacy,
)
.unwrap();
source.hostname = Some("451062c59603".to_owned());
assert!(source.exclude_self("451062c59603a1cf0c6af3e74a31c0ae63d8275aa16a5fc78ef31b923baaffc3"));
// hostname too short
source.hostname = Some("a".to_owned());
assert!(!source.exclude_self("a29d569bd46c"));
}
#[cfg(all(test, feature = "docker-logs-integration-tests"))]
mod integration_tests {
use crate::sources::docker_logs::*;
use crate::sources::docker_logs::{CONTAINER, CREATED_AT, IMAGE, NAME};
use crate::{
event::Event,
test_util::{
collect_n, collect_ready,
components::{assert_source_compliance, SOURCE_TAGS},
trace_init,
},
SourceSender,
};
use bollard::{
container::{
Config as ContainerConfig, CreateContainerOptions, KillContainerOptions,
RemoveContainerOptions, StartContainerOptions, WaitContainerOptions,
},
image::{CreateImageOptions, ListImagesOptions},
};
use futures::{stream::TryStreamExt, FutureExt};
use similar_asserts::assert_eq;
use vrl::value;
/// None if docker is not present on the system
async fn source_with<'a, L: Into<Option<&'a str>>>(
names: &[&str],
label: L,
log_namespace: Option<bool>,
) -> impl Stream<Item = Event> {
source_with_config(DockerLogsConfig {
include_containers: Some(names.iter().map(|&s| s.to_owned()).collect()),
include_labels: Some(label.into().map(|l| vec![l.to_owned()]).unwrap_or_default()),
log_namespace,
..DockerLogsConfig::default()
})
.await
}
async fn source_with_config(config: DockerLogsConfig) -> impl Stream<Item = Event> + Unpin {
let (sender, recv) = SourceSender::new_test();
let source = config
.build(SourceContext::new_test(sender, None))
.await
.unwrap();
tokio::spawn(async move { source.await.unwrap() });
recv
}
/// Users should ensure to remove container before exiting.
async fn log_container(
name: &str,
label: Option<&str>,
log: &str,
docker: &Docker,
tty: bool,
) -> String {
cmd_container(name, label, vec!["echo", log], docker, tty).await
}
/// Users should ensure to remove container before exiting.
/// Will resend message every so often.
async fn eternal_container(
name: &str,
label: Option<&str>,
log: &str,
docker: &Docker,
) -> String {
cmd_container(
name,
label,
vec![
"sh",
"-c",
format!("echo before; i=0; while [ $i -le 50 ]; do sleep 0.1; echo {}; i=$((i+1)); done", log).as_str(),
],
docker,
false
).await
}
/// Users should ensure to remove container before exiting.
async fn cmd_container(
name: &str,
label: Option<&str>,
cmd: Vec<&str>,
docker: &Docker,
tty: bool,
) -> String {
if let Some(id) = cmd_container_for_real(name, label, cmd, docker, tty).await {
id
} else {
// Maybe a before created container is present
info!(
message = "Assumes that named container remained from previous tests.",
name = name
);
name.to_owned()
}
}
/// Users should ensure to remove container before exiting.
async fn cmd_container_for_real(
name: &str,
label: Option<&str>,
cmd: Vec<&str>,
docker: &Docker,
tty: bool,
) -> Option<String> {
pull_busybox(docker).await;
trace!("Creating container.");
let options = Some(CreateContainerOptions {
name,
platform: None,
});
let config = ContainerConfig {
image: Some("busybox"),
cmd: Some(cmd),
labels: label.map(|label| vec![(label, "")].into_iter().collect()),
tty: Some(tty),
..Default::default()
};
let container = docker.create_container(options, config).await;
container.ok().map(|c| c.id)
}
async fn pull_busybox(docker: &Docker) {
let mut filters = HashMap::new();
filters.insert("reference", vec!["busybox:latest"]);
let options = Some(ListImagesOptions {
filters,
..Default::default()
});
let images = docker.list_images(options).await.unwrap();
if images.is_empty() {
// If `busybox:latest` not found, pull it
let options = Some(CreateImageOptions {
from_image: "busybox",
tag: "latest",
..Default::default()
});
docker
.create_image(options, None, None)
.for_each(|item| async move {
let info = item.unwrap();
if let Some(error) = info.error {
panic!("{:?}", error);
}
})
.await
}
}
/// Returns once container has started
async fn container_start(id: &str, docker: &Docker) -> Result<(), bollard::errors::Error> {
trace!("Starting container.");
let options = None::<StartContainerOptions<&str>>;
docker.start_container(id, options).await
}
/// Returns once container is done running
async fn container_wait(id: &str, docker: &Docker) -> Result<(), bollard::errors::Error> {
trace!("Waiting for container.");
docker
.wait_container(id, None::<WaitContainerOptions<&str>>)
.try_for_each(|exit| async move {
info!(message = "Container exited with status code.", status_code = ?exit.status_code);
Ok(())
})
.await
}
/// Returns once container is killed
async fn container_kill(id: &str, docker: &Docker) -> Result<(), bollard::errors::Error> {
trace!("Waiting for container to be killed.");
docker
.kill_container(id, None::<KillContainerOptions<&str>>)
.await
}
/// Returns once container is done running
async fn container_run(id: &str, docker: &Docker) -> Result<(), bollard::errors::Error> {
container_start(id, docker).await?;
container_wait(id, docker).await
}
async fn container_remove(id: &str, docker: &Docker) {
trace!("Removing container.");
// Don't panic, as this is unrelated to the test, and there are possibly other containers that need to be removed
_ = docker
.remove_container(id, None::<RemoveContainerOptions>)
.await
.map_err(|e| error!(%e));
}
/// Returns once it's certain that log has been made
/// Expects that this is the only one with a container
async fn container_log_n(
n: usize,
name: &str,
label: Option<&str>,
log: &str,
docker: &Docker,
) -> String {
container_with_optional_tty_log_n(n, name, label, log, docker, false).await
}
async fn container_with_optional_tty_log_n(
n: usize,
name: &str,
label: Option<&str>,
log: &str,
docker: &Docker,
tty: bool,
) -> String {
let id = log_container(name, label, log, docker, tty).await;
for _ in 0..n {
if let Err(error) = container_run(&id, docker).await {
container_remove(&id, docker).await;
panic!("Container failed to start with error: {:?}", error);
}
}
id
}
/// Once function returns, the container has entered into running state.
/// Container must be killed before removed.
async fn running_container(
name: &'static str,
label: Option<&'static str>,
log: &'static str,
docker: &Docker,
) -> String {
let out = source_with(&[name], None, None).await;
let docker = docker.clone();
let id = eternal_container(name, label, log, &docker).await;
if let Err(error) = container_start(&id, &docker).await {
container_remove(&id, &docker).await;
panic!("Container start failed with error: {:?}", error);
}
// Wait for before message
let events = collect_n(out, 1).await;
assert_eq!(
events[0].as_log()[log_schema().message_key().unwrap().to_string()],
"before".into()
);
id
}
fn is_empty<T>(mut rx: impl Stream<Item = T> + Unpin) -> bool {
rx.next().now_or_never().is_none()
}
#[tokio::test]
async fn container_with_tty_vector_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Vector)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "log container_with_tty";
let name = "container_with_tty_namespaced";
let out = source_with(&[name], None, Some(true)).await;
let docker = docker(None, None).unwrap();
let id = container_with_optional_tty_log_n(1, name, None, message, &docker, true).await;
let events = collect_n(out, 1).await;
container_remove(&id, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
assert_eq!(events[0].as_log().get(".").unwrap(), &value!(message));
})
.await;
}
#[tokio::test]
async fn container_with_tty_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "log container_with_tty";
let name = "container_with_tty";
let out = source_with(&[name], None, None).await;
let docker = docker(None, None).unwrap();
let id = container_with_optional_tty_log_n(1, name, None, message, &docker, true).await;
let events = collect_n(out, 1).await;
container_remove(&id, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
assert_eq!(
events[0].as_log()[log_schema().message_key().unwrap().to_string()],
message.into()
);
})
.await;
}
#[tokio::test]
async fn newly_started_vector_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Vector)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "9";
let name = "vector_test_newly_started_namespaced";
let label = "vector_test_label_newly_started";
let out = source_with(&[name], None, Some(true)).await;
let docker = docker(None, None).unwrap();
let id = container_log_n(1, name, Some(label), message, &docker).await;
let events = collect_n(out, 1).await;
container_remove(&id, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
let log = events[0].as_log();
let meta = log.metadata().value();
assert_eq!(log.get(".").unwrap(), &value!(message));
assert_eq!(
meta.get(path!(DockerLogsConfig::NAME, CONTAINER)).unwrap(),
&value!(id)
);
assert!(meta
.get(path!(DockerLogsConfig::NAME, CREATED_AT))
.is_some());
assert_eq!(
meta.get(path!(DockerLogsConfig::NAME, IMAGE)).unwrap(),
&value!("busybox")
);
assert!(meta
.get(path!(DockerLogsConfig::NAME, "labels", label))
.is_some());
assert_eq!(
meta.get(path!(DockerLogsConfig::NAME, NAME)).unwrap(),
&value!(name)
);
assert_eq!(
meta.get(path!("vector", "source_type")).unwrap(),
&value!(DockerLogsConfig::NAME)
);
assert!(meta
.get(path!("vector", "ingest_timestamp"))
.unwrap()
.is_timestamp())
})
.await;
}
#[tokio::test]
async fn newly_started_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "9";
let name = "vector_test_newly_started";
let label = "vector_test_label_newly_started";
let out = source_with(&[name], None, None).await;
let docker = docker(None, None).unwrap();
let id = container_log_n(1, name, Some(label), message, &docker).await;
let events = collect_n(out, 1).await;
container_remove(&id, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
let log = events[0].as_log();
assert_eq!(
log[log_schema().message_key().unwrap().to_string()],
message.into()
);
assert_eq!(log[CONTAINER], id.into());
assert!(log.get(CREATED_AT).is_some());
assert_eq!(log[IMAGE], "busybox".into());
assert!(log.get(format!("label.{}", label).as_str()).is_some());
assert_eq!(events[0].as_log()[&NAME], name.into());
assert_eq!(
events[0].as_log()[log_schema().source_type_key().unwrap().to_string()],
DockerLogsConfig::NAME.into()
);
})
.await;
}
#[tokio::test]
async fn restart_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "10";
let name = "vector_test_restart";
let out = source_with(&[name], None, None).await;
let docker = docker(None, None).unwrap();
let id = container_log_n(2, name, None, message, &docker).await;
let events = collect_n(out, 2).await;
container_remove(&id, &docker).await;
let definition = schema_definitions.unwrap();
definition.assert_valid_for_event(&events[0]);
let message_key = log_schema().message_key().unwrap().to_string();
assert_eq!(events[0].as_log()[&message_key], message.into());
definition.assert_valid_for_event(&events[1]);
assert_eq!(events[1].as_log()[message_key], message.into());
})
.await;
}
#[tokio::test]
async fn include_containers_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "11";
let name0 = "vector_test_include_container_0";
let name1 = "vector_test_include_container_1";
let out = source_with(&[name1], None, None).await;
let docker = docker(None, None).unwrap();
let id0 = container_log_n(1, name0, None, "11", &docker).await;
let id1 = container_log_n(1, name1, None, message, &docker).await;
let events = collect_n(out, 1).await;
container_remove(&id0, &docker).await;
container_remove(&id1, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
assert_eq!(
events[0].as_log()[log_schema().message_key().unwrap().to_string()],
message.into()
);
})
.await;
}
#[tokio::test]
async fn exclude_containers_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let will_be_read = "12";
let prefix = "vector_test_exclude_containers";
let included0 = format!("{}_{}", prefix, "include0");
let included1 = format!("{}_{}", prefix, "include1");
let excluded0 = format!("{}_{}", prefix, "excluded0");
let docker = docker(None, None).unwrap();
let out = source_with_config(DockerLogsConfig {
include_containers: Some(vec![prefix.to_owned()]),
exclude_containers: Some(vec![excluded0.to_owned()]),
..DockerLogsConfig::default()
})
.await;
let id0 = container_log_n(1, &excluded0, None, "will not be read", &docker).await;
let id1 = container_log_n(1, &included0, None, will_be_read, &docker).await;
let id2 = container_log_n(1, &included1, None, will_be_read, &docker).await;
tokio::time::sleep(Duration::from_secs(1)).await;
let events = collect_ready(out).await;
container_remove(&id0, &docker).await;
container_remove(&id1, &docker).await;
container_remove(&id2, &docker).await;
assert_eq!(events.len(), 2);
let definition = schema_definitions.unwrap();
definition.assert_valid_for_event(&events[0]);
let message_key = log_schema().message_key().unwrap().to_string();
assert_eq!(events[0].as_log()[&message_key], will_be_read.into());
definition.assert_valid_for_event(&events[1]);
assert_eq!(events[1].as_log()[message_key], will_be_read.into());
})
.await;
}
#[tokio::test]
async fn include_labels_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "13";
let name0 = "vector_test_include_labels_0";
let name1 = "vector_test_include_labels_1";
let label = "vector_test_include_label";
let out = source_with(&[name0, name1], label, None).await;
let docker = docker(None, None).unwrap();
let id0 = container_log_n(1, name0, None, "13", &docker).await;
let id1 = container_log_n(1, name1, Some(label), message, &docker).await;
let events = collect_n(out, 1).await;
container_remove(&id0, &docker).await;
container_remove(&id1, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
assert_eq!(
events[0].as_log()[log_schema().message_key().unwrap().to_string()],
message.into()
);
})
.await;
}
#[tokio::test]
async fn currently_running_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "14";
let name = "vector_test_currently_running";
let label = "vector_test_label_currently_running";
let docker = docker(None, None).unwrap();
let id = running_container(name, Some(label), message, &docker).await;
let out = source_with(&[name], None, None).await;
let events = collect_n(out, 1).await;
_ = container_kill(&id, &docker).await;
container_remove(&id, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
let log = events[0].as_log();
assert_eq!(
log[log_schema().message_key().unwrap().to_string()],
message.into()
);
assert_eq!(log[CONTAINER], id.into());
assert!(log.get(CREATED_AT).is_some());
assert_eq!(log[IMAGE], "busybox".into());
assert!(log.get(format!("label.{}", label).as_str()).is_some());
assert_eq!(events[0].as_log()[&NAME], name.into());
assert_eq!(
events[0].as_log()[log_schema().source_type_key().unwrap().to_string()],
DockerLogsConfig::NAME.into()
);
})
.await;
}
#[tokio::test]
async fn include_image_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "15";
let name = "vector_test_include_image";
let config = DockerLogsConfig {
include_containers: Some(vec![name.to_owned()]),
include_images: Some(vec!["busybox".to_owned()]),
..DockerLogsConfig::default()
};
let out = source_with_config(config).await;
let docker = docker(None, None).unwrap();
let id = container_log_n(1, name, None, message, &docker).await;
let events = collect_n(out, 1).await;
container_remove(&id, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
assert_eq!(
events[0].as_log()[log_schema().message_key().unwrap().to_string()],
message.into()
);
})
.await;
}
#[tokio::test]
async fn not_include_image_legacy_namespace() {
trace_init();
// No assert_source_compliance here since we aren't including the image
// no events are collected.
let message = "16";
let name = "vector_test_not_include_image";
let config_ex = DockerLogsConfig {
include_images: Some(vec!["some_image".to_owned()]),
..DockerLogsConfig::default()
};
let exclude_out = source_with_config(config_ex).await;
let docker = docker(None, None).unwrap();
let id = container_log_n(1, name, None, message, &docker).await;
container_remove(&id, &docker).await;
assert!(is_empty(exclude_out));
}
#[tokio::test]
async fn not_include_running_image_legacy_namespace() {
trace_init();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "17";
let name = "vector_test_not_include_running_image";
let config_ex = DockerLogsConfig {
include_images: Some(vec!["some_image".to_owned()]),
..DockerLogsConfig::default()
};
let config_in = DockerLogsConfig {
include_containers: Some(vec![name.to_owned()]),
include_images: Some(vec!["busybox".to_owned()]),
..DockerLogsConfig::default()
};
let docker = docker(None, None).unwrap();
let id = running_container(name, None, message, &docker).await;
let exclude_out = source_with_config(config_ex).await;
let include_out = source_with_config(config_in).await;
_ = collect_n(include_out, 1).await;
_ = container_kill(&id, &docker).await;
container_remove(&id, &docker).await;
assert!(is_empty(exclude_out));
})
.await;
}
#[tokio::test]
async fn flat_labels_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let message = "18";
let name = "vector_test_flat_labels";
let label = "vector.test.label.flat.labels";
let docker = docker(None, None).unwrap();
let id = running_container(name, Some(label), message, &docker).await;
let out = source_with(&[name], None, None).await;
let events = collect_n(out, 1).await;
_ = container_kill(&id, &docker).await;
container_remove(&id, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
let log = events[0].as_log();
assert_eq!(
log[log_schema().message_key().unwrap().to_string()],
message.into()
);
assert_eq!(log[CONTAINER], id.into());
assert!(log.get(CREATED_AT).is_some());
assert_eq!(log[IMAGE], "busybox".into());
assert!(log
.get("label")
.unwrap()
.as_object()
.unwrap()
.get(label)
.is_some());
assert_eq!(events[0].as_log()[&NAME], name.into());
assert_eq!(
events[0].as_log()[log_schema().source_type_key().unwrap().to_string()],
DockerLogsConfig::NAME.into()
);
})
.await;
}
#[tokio::test]
async fn log_longer_than_16kb_legacy_namespace() {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone();
assert_source_compliance(&SOURCE_TAGS, async {
let mut message = String::with_capacity(20 * 1024);
for _ in 0..message.capacity() {
message.push('0');
}
let name = "vector_test_log_longer_than_16kb";
let out = source_with(&[name], None, None).await;
let docker = docker(None, None).unwrap();
let id = container_log_n(1, name, None, message.as_str(), &docker).await;
let events = collect_n(out, 1).await;
container_remove(&id, &docker).await;
schema_definitions
.unwrap()
.assert_valid_for_event(&events[0]);
let log = events[0].as_log();
assert_eq!(
log[log_schema().message_key().unwrap().to_string()],
message.into()
);
})
.await;
}
#[tokio::test]
async fn merge_multiline_vector_namespace() {
assert_source_compliance(&SOURCE_TAGS, async {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Vector)
.first()
.unwrap()
.schema_definition
.clone()
.unwrap();
let emitted_messages = vec![
"java.lang.Exception",
" at com.foo.bar(bar.java:123)",
" at com.foo.baz(baz.java:456)",
];
let expected_messages = vec![concat!(
"java.lang.Exception\n",
" at com.foo.bar(bar.java:123)\n",
" at com.foo.baz(baz.java:456)",
)];
let name = "vector_test_merge_multiline_namespaced";
let config = DockerLogsConfig {
include_containers: Some(vec![name.to_owned()]),
include_images: Some(vec!["busybox".to_owned()]),
multiline: Some(MultilineConfig {
start_pattern: "^[^\\s]".to_owned(),
condition_pattern: "^[\\s]+at".to_owned(),
mode: line_agg::Mode::ContinueThrough,
timeout_ms: Duration::from_millis(10),
}),
log_namespace: Some(true),
..DockerLogsConfig::default()
};
let out = source_with_config(config).await;
let docker = docker(None, None).unwrap();
let command = emitted_messages
.into_iter()
.map(|message| format!("echo {:?}", message))
.collect::<Box<_>>()
.join(" && ");
let id = cmd_container(name, None, vec!["sh", "-c", &command], &docker, false).await;
if let Err(error) = container_run(&id, &docker).await {
container_remove(&id, &docker).await;
panic!("Container failed to start with error: {:?}", error);
}
let events = collect_n(out, expected_messages.len()).await;
container_remove(&id, &docker).await;
let actual_messages = events
.into_iter()
.map(|event| {
schema_definitions.assert_valid_for_event(&event);
event
.into_log()
.remove(".")
.unwrap()
.to_string_lossy()
.into_owned()
})
.collect::<Vec<_>>();
assert_eq!(actual_messages, expected_messages);
})
.await;
}
#[tokio::test]
async fn merge_multiline_legacy_namespace() {
assert_source_compliance(&SOURCE_TAGS, async {
trace_init();
let schema_definitions = DockerLogsConfig::default()
.outputs(LogNamespace::Legacy)
.first()
.unwrap()
.schema_definition
.clone()
.unwrap();
let emitted_messages = vec![
"java.lang.Exception",
" at com.foo.bar(bar.java:123)",
" at com.foo.baz(baz.java:456)",
];
let expected_messages = vec![concat!(
"java.lang.Exception\n",
" at com.foo.bar(bar.java:123)\n",
" at com.foo.baz(baz.java:456)",
)];
let name = "vector_test_merge_multiline";
let config = DockerLogsConfig {
include_containers: Some(vec![name.to_owned()]),
include_images: Some(vec!["busybox".to_owned()]),
multiline: Some(MultilineConfig {
start_pattern: "^[^\\s]".to_owned(),
condition_pattern: "^[\\s]+at".to_owned(),
mode: line_agg::Mode::ContinueThrough,
timeout_ms: Duration::from_millis(10),
}),
..DockerLogsConfig::default()
};
let out = source_with_config(config).await;
let docker = docker(None, None).unwrap();
let command = emitted_messages
.into_iter()
.map(|message| format!("echo {:?}", message))
.collect::<Box<_>>()
.join(" && ");
let id = cmd_container(name, None, vec!["sh", "-c", &command], &docker, false).await;
if let Err(error) = container_run(&id, &docker).await {
container_remove(&id, &docker).await;
panic!("Container failed to start with error: {:?}", error);
}
let events = collect_n(out, expected_messages.len()).await;
container_remove(&id, &docker).await;
let actual_messages = events
.into_iter()
.map(|event| {
schema_definitions.assert_valid_for_event(&event);
event
.into_log()
.remove(log_schema().message_key_target_path().unwrap())
.unwrap()
.to_string_lossy()
.into_owned()
})
.collect::<Vec<_>>();
assert_eq!(actual_messages, expected_messages);
})
.await;
}
}