From c1a39e4067362d6e699573c4be4a92cef044766f Mon Sep 17 00:00:00 2001 From: Mykola Rybak Date: Tue, 30 Jan 2024 21:44:37 +0200 Subject: [PATCH] enhancement(unit tests): Enable population of event metadata by a VRL unit test source (#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: https://github.com/vectordotdev/vector/issues/19639. * Add changelog snippet * Fix changelog snippet name * Fix unit test transform name clash --- ...9639_unit_test_vrl_metadata.enhancement.md | 3 ++ src/config/unit_test/mod.rs | 9 ++++-- tests/behavior/transforms/vrl_test_input.toml | 29 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 changelog.d/19639_unit_test_vrl_metadata.enhancement.md diff --git a/changelog.d/19639_unit_test_vrl_metadata.enhancement.md b/changelog.d/19639_unit_test_vrl_metadata.enhancement.md new file mode 100644 index 0000000000000..dc03333a5f3be --- /dev/null +++ b/changelog.d/19639_unit_test_vrl_metadata.enhancement.md @@ -0,0 +1,3 @@ +Unit tests can now populate event metadata with the `% = ...` syntax. + +authors: GreyTeardrop diff --git a/src/config/unit_test/mod.rs b/src/config/unit_test/mod.rs index 78148b356825e..f5402af37f9cb 100644 --- a/src/config/unit_test/mod.rs +++ b/src/config/unit_test/mod.rs @@ -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}, }; @@ -581,7 +581,12 @@ fn build_input_event(input: &TestInput) -> Result { 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()) diff --git a/tests/behavior/transforms/vrl_test_input.toml b/tests/behavior/transforms/vrl_test_input.toml index fab976d8029ed..ef148a29dfeb9 100644 --- a/tests/behavior/transforms/vrl_test_input.toml +++ b/tests/behavior/transforms/vrl_test_input.toml @@ -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") + '''