Skip to content

Commit

Permalink
feat(unittests): add vrl as test input (#19107)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hoffmann <mhoffm@posteo.de>
  • Loading branch information
MichaHoffmann authored Nov 14, 2023
1 parent 5a55567 commit 0eb5396
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ pub struct TestInput {

/// The type of the input event.
///
/// Can be either `raw`, `log`, or `metric.
/// Can be either `raw`, `vrl`, `log`, or `metric.
#[serde(default = "default_test_input_type", rename = "type")]
pub type_str: String,

Expand All @@ -515,6 +515,11 @@ pub struct TestInput {
/// event) and when the input type is set to `raw`.
pub value: Option<String>,

/// The vrl expression to generate the input event.
///
/// Only relevant when `type` is `vrl`.
pub source: Option<String>,

/// The set of log fields to use when creating a log input event.
///
/// Only relevant when `type` is `log`.
Expand Down
32 changes: 31 additions & 1 deletion src/config/unit_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests;
mod unit_test_components;

use std::{
collections::{HashMap, HashSet},
collections::{BTreeMap, HashMap, HashSet},
sync::Arc,
};

Expand All @@ -15,6 +15,11 @@ use tokio::sync::{
Mutex,
};
use uuid::Uuid;
use vrl::{
compiler::{state::RuntimeState, Context, TargetValue, TimeZone},
diagnostic::Formatter,
value,
};

pub use self::unit_test_components::{
UnitTestSinkCheck, UnitTestSinkConfig, UnitTestSinkResult, UnitTestSourceConfig,
Expand Down Expand Up @@ -557,6 +562,31 @@ fn build_input_event(input: &TestInput) -> Result<Event, String> {
Some(v) => Ok(Event::Log(LogEvent::from_str_legacy(v.clone()))),
None => Err("input type 'raw' requires the field 'value'".to_string()),
},
"vrl" => {
if let Some(source) = &input.source {
let fns = vrl::stdlib::all();
let result = match vrl::compiler::compile(source, &fns) {
Ok(v) => v,
Err(e) => return Err(Formatter::new(source, e.clone()).to_string()),
};

let mut target = TargetValue {
value: value!({}),
metadata: value::Value::Object(BTreeMap::new()),
secrets: value::Secrets::default(),
};

let mut state = RuntimeState::default();
let timezone = TimeZone::default();
let mut ctx = Context::new(&mut target, &mut state, &timezone);
match result.program.resolve(&mut ctx) {
Ok(value) => Ok(Event::Log(LogEvent::from(value.clone()))),
Err(e) => Err(e.to_string()),
}
} else {
Err("input type 'vrl' requires the field 'source'".to_string())
}
}
"log" => {
if let Some(log_fields) = &input.log_fields {
let mut event = LogEvent::from_str_legacy("");
Expand Down

0 comments on commit 0eb5396

Please sign in to comment.