Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/unit tests #44

Merged
merged 6 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/flowgger/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,11 @@ mod test {
fn test_config_from_path_no_file() {
let _config = Config::from_path("doesnotexist.toml").unwrap();
}

#[test]
fn test_config_clone() {
let config = Config::from_path("tests/resources/good_config.toml").unwrap();
let _config_cloned = config.clone();
assert_eq!(config.config, _config_cloned.config);
}
}
20 changes: 20 additions & 0 deletions src/flowgger/decoder/rfc3164_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,23 @@ fn test_rfc3164_decode_with_pri() {
assert_eq!(res.msg, Some(r#"appname 69 42 [origin@123 software="te\st sc\"ript" swVersion="0.0.1"] test message"#.to_string()));
assert_eq!(res.full_msg, Some(msg.to_string()));
}

#[test]
fn test_rfc3164_decode_invalid_event() {
let msg = "test message";
let cfg = Config::from_string("[input]\n[input.ltsv_schema]\nformat = \"rfc3164\"\n").unwrap();

let decoder = RFC3164Decoder::new(&cfg);
let res = decoder.decode(msg);
assert!(res.is_err());
}

#[test]
fn test_rfc3164_decode_invalid_date() {
let msg = r#"Aug 36 11:15:24 testhostname appname 69 42 [origin@123 software="te\st sc\"ript" swVersion="0.0.1"] test message"#;
let cfg = Config::from_string("[input]\n[input.ltsv_schema]\nformat = \"rfc3164\"\n").unwrap();

let decoder = RFC3164Decoder::new(&cfg);
let res = decoder.decode(msg);
assert!(res.is_err());
}
32 changes: 29 additions & 3 deletions src/flowgger/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,41 @@ pub const SEVERITY_MISSING: u8 = 0xff;

#[test]
fn test_structured_data_display() {
let expected_string = r#"[someid a="b" c="123456"]"#;
let expected_string = r#"[someid a="a string" b="123456" c="true" d="123.456" e="-123456" f]"#;
let expected_debug = r#"StructuredData { sd_id: Some("someid"), pairs: [("a", String("a string")), ("b", U64(123456)), ("c", Bool(true)), ("d", F64(123.456)), ("e", I64(-123456)), ("_f", Null)] }"#;
let data = StructuredData {
sd_id: Some("someid".to_string()),
pairs: vec![
("a".to_string(), SDValue::String("b".to_string())),
("c".to_string(), SDValue::U64(123456)),
("a".to_string(), SDValue::String("a string".to_string())),
("b".to_string(), SDValue::U64(123456)),
("c".to_string(), SDValue::Bool(true)),
("d".to_string(), SDValue::F64(123.456)),
("e".to_string(), SDValue::I64(-123456)),
("_f".to_string(), SDValue::Null),
],
};

// Verify both debug and string conversion
let result = data.to_string();
assert_eq!(format!("{:?}", data), expected_debug);
assert_eq!(result, expected_string);
}

#[test]
fn test_record_display() {
let expected_debug = r#"Record { ts: 123.456, hostname: "hostname", facility: Some(3), severity: Some(8), appname: Some("app"), procid: Some("123"), msgid: None, msg: Some("msg"), full_msg: None, sd: None }"#;
let record = Record {
ts: 123.456,
hostname: "hostname".to_string(),
facility: Some(3),
severity: Some(8),
appname: Some("app".to_string()),
procid: Some("123".to_string()),
msgid: None,
msg: Some("msg".to_string()),
full_msg: None,
sd: None,
};

assert_eq!(format!("{:?}", record), expected_debug);
}