Skip to content

Commit ff09d18

Browse files
authored
Feature/unit tests (#44)
* Add rfc3164 optional timestamp before the events * Update configuration file * Remove debug logs * remove development settings * Slight unit testing coverage improvements
1 parent 7b0348b commit ff09d18

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/flowgger/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,11 @@ mod test {
178178
fn test_config_from_path_no_file() {
179179
let _config = Config::from_path("doesnotexist.toml").unwrap();
180180
}
181+
182+
#[test]
183+
fn test_config_clone() {
184+
let config = Config::from_path("tests/resources/good_config.toml").unwrap();
185+
let _config_cloned = config.clone();
186+
assert_eq!(config.config, _config_cloned.config);
187+
}
181188
}

src/flowgger/decoder/rfc3164_decoder.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,23 @@ fn test_rfc3164_decode_with_pri() {
143143
assert_eq!(res.msg, Some(r#"appname 69 42 [origin@123 software="te\st sc\"ript" swVersion="0.0.1"] test message"#.to_string()));
144144
assert_eq!(res.full_msg, Some(msg.to_string()));
145145
}
146+
147+
#[test]
148+
fn test_rfc3164_decode_invalid_event() {
149+
let msg = "test message";
150+
let cfg = Config::from_string("[input]\n[input.ltsv_schema]\nformat = \"rfc3164\"\n").unwrap();
151+
152+
let decoder = RFC3164Decoder::new(&cfg);
153+
let res = decoder.decode(msg);
154+
assert!(res.is_err());
155+
}
156+
157+
#[test]
158+
fn test_rfc3164_decode_invalid_date() {
159+
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"#;
160+
let cfg = Config::from_string("[input]\n[input.ltsv_schema]\nformat = \"rfc3164\"\n").unwrap();
161+
162+
let decoder = RFC3164Decoder::new(&cfg);
163+
let res = decoder.decode(msg);
164+
assert!(res.is_err());
165+
}

src/flowgger/record.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,41 @@ pub const SEVERITY_MISSING: u8 = 0xff;
9292

9393
#[test]
9494
fn test_structured_data_display() {
95-
let expected_string = r#"[someid a="b" c="123456"]"#;
95+
let expected_string = r#"[someid a="a string" b="123456" c="true" d="123.456" e="-123456" f]"#;
96+
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)] }"#;
9697
let data = StructuredData {
9798
sd_id: Some("someid".to_string()),
9899
pairs: vec![
99-
("a".to_string(), SDValue::String("b".to_string())),
100-
("c".to_string(), SDValue::U64(123456)),
100+
("a".to_string(), SDValue::String("a string".to_string())),
101+
("b".to_string(), SDValue::U64(123456)),
102+
("c".to_string(), SDValue::Bool(true)),
103+
("d".to_string(), SDValue::F64(123.456)),
104+
("e".to_string(), SDValue::I64(-123456)),
105+
("_f".to_string(), SDValue::Null),
101106
],
102107
};
103108

109+
// Verify both debug and string conversion
104110
let result = data.to_string();
111+
assert_eq!(format!("{:?}", data), expected_debug);
105112
assert_eq!(result, expected_string);
106113
}
114+
115+
#[test]
116+
fn test_record_display() {
117+
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 }"#;
118+
let record = Record {
119+
ts: 123.456,
120+
hostname: "hostname".to_string(),
121+
facility: Some(3),
122+
severity: Some(8),
123+
appname: Some("app".to_string()),
124+
procid: Some("123".to_string()),
125+
msgid: None,
126+
msg: Some("msg".to_string()),
127+
full_msg: None,
128+
sd: None,
129+
};
130+
131+
assert_eq!(format!("{:?}", record), expected_debug);
132+
}

0 commit comments

Comments
 (0)