Skip to content

Commit

Permalink
enhancement(unit tests): Enable population of event metadata by a VRL…
Browse files Browse the repository at this point in the history
… unit test source (vectordotdev#19729)

* Enable population of event metadata by a VRL source

At the moment, there seems to be no way to produce an event with
non-trivial metadata for a unit test, which makes it hard to test
configurations that have log_namespace = true.

Reference: vectordotdev#19639.

* Add changelog snippet

* Fix changelog snippet name

* Fix unit test transform name clash
  • Loading branch information
GreyTeardrop authored Jan 30, 2024
1 parent 60f5fe0 commit c1a39e4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog.d/19639_unit_test_vrl_metadata.enhancement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Unit tests can now populate event metadata with the `% = ...` syntax.

authors: GreyTeardrop
9 changes: 7 additions & 2 deletions src/config/unit_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
self, loading, ComponentKey, Config, ConfigBuilder, ConfigPath, SinkOuter, SourceOuter,
TestDefinition, TestInput, TestInputValue, TestOutput,
},
event::{Event, LogEvent, Value},
event::{Event, EventMetadata, LogEvent, Value},
signal,
topology::{builder::TopologyPieces, RunningTopology},
};
Expand Down Expand Up @@ -581,7 +581,12 @@ fn build_input_event(input: &TestInput) -> Result<Event, String> {
result
.program
.resolve(&mut ctx)
.map(|v| Event::Log(LogEvent::from(v.clone())))
.map(|_| {
Event::Log(LogEvent::from_parts(
target.value.clone(),
EventMetadata::default_with_value(target.metadata.clone()),
))
})
.map_err(|e| e.to_string())
} else {
Err("input type 'vrl' requires the field 'source'".to_string())
Expand Down
29 changes: 29 additions & 0 deletions tests/behavior/transforms/vrl_test_input.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,32 @@
[[tests.outputs.conditions]]
type = "vrl"
source = """.a.b == "c" && is_timestamp(.d)"""

##------------------------------------------------------------------------------

[transforms.remap_test_metadata]
inputs = []
type = "remap"
source = '''
.event = .
.metadata = %
'''

[[tests]]
name = "remap_test_metadata"
[[tests.inputs]]
insert_at = "remap_test_metadata"
type = "vrl"
source = '''
. = "Event"
% = "Metadata"
'''

[[tests.outputs]]
extract_from = "remap_test_metadata"
[[tests.outputs.conditions]]
type = "vrl"
source = '''
assert_eq!(.event, "Event")
assert_eq!(.metadata, "Metadata")
'''

0 comments on commit c1a39e4

Please sign in to comment.