Skip to content

Commit

Permalink
test: Update integration tests to iterate over all example log files …
Browse files Browse the repository at this point in the history
…under `examples/logs`. (#21)
  • Loading branch information
LinZhihao-723 authored Dec 16, 2024
1 parent d726cf4 commit 5525327
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 56 deletions.
75 changes: 41 additions & 34 deletions tests/lexer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,54 @@ fn test_lexer_simple() -> Result<()> {
let schema_path = std::path::Path::new(project_root)
.join("examples")
.join("schema.yaml");
let log_path = std::path::Path::new(project_root)
let log_path_dir = std::path::Path::new(project_root)
.join("examples")
.join("logs")
.join("hive-24h.log");
.join("logs");
let log_paths = vec![
log_path_dir.clone().join("hive-24h.log"),
log_path_dir.clone().join("hive-24h_large.log"),
];

let schema_config = SchemaConfig::parse_from_file(schema_path.to_str().unwrap())?;
let mut lexer = Lexer::new(schema_config)?;
let buffered_file_stream = Box::new(BufferedFileStream::new(log_path.to_str().unwrap())?);
lexer.set_input_stream(buffered_file_stream);

let mut tokens = Vec::new();
while let Some(token) = lexer.get_next_token()? {
tokens.push(token);
}
assert_eq!(false, tokens.is_empty());

let mut parsed_lines = Vec::new();
let mut parsed_line = String::new();
let mut curr_line_num = 1usize;
for token in &tokens {
if curr_line_num != token.get_line_num() {
parsed_lines.push(parsed_line.clone());
parsed_line.clear();
curr_line_num += 1;
for path in &log_paths {
let log_path = path.to_str().unwrap();
let buffered_file_stream = Box::new(BufferedFileStream::new(log_path)?);
lexer.set_input_stream(buffered_file_stream);

let mut tokens = Vec::new();
while let Some(token) = lexer.get_next_token()? {
tokens.push(token);
}
parsed_line += &token.get_val().to_string();
println!("{:?}", token);
}
parsed_lines.push(parsed_line.clone());
println!("{:?}", parsed_lines);

let mut expected_lines = Vec::new();
let reader = io::BufReader::new(File::open(log_path).expect("failed to open log file"));
for line in reader.lines() {
let line = line.expect("failed to read line");
expected_lines.push(line.clone() + "\n");
}
assert_eq!(false, tokens.is_empty());

assert_eq!(parsed_lines.len(), expected_lines.len());
assert_eq!(false, parsed_line.is_empty());
assert_eq!(parsed_lines, expected_lines);
let mut parsed_lines = Vec::new();
let mut parsed_line = String::new();
let mut curr_line_num = 1usize;
for token in &tokens {
if curr_line_num != token.get_line_num() {
parsed_lines.push(parsed_line.clone());
parsed_line.clear();
curr_line_num += 1;
}
parsed_line += &token.get_val().to_string();
println!("{:?}", token);
}
parsed_lines.push(parsed_line.clone());
println!("{:?}", parsed_lines);

let mut expected_lines = Vec::new();
let reader = io::BufReader::new(File::open(log_path).expect("failed to open log file"));
for line in reader.lines() {
let line = line.expect("failed to read line");
expected_lines.push(line.clone() + "\n");
}

assert_eq!(parsed_lines.len(), expected_lines.len());
assert_eq!(false, parsed_line.is_empty());
assert_eq!(parsed_lines, expected_lines);
}

Ok(())
}
52 changes: 30 additions & 22 deletions tests/log_parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,42 @@ fn test_lexer_simple() -> Result<()> {
let schema_path = std::path::Path::new(project_root)
.join("examples")
.join("schema.yaml");
let log_path = std::path::Path::new(project_root)
let log_path_dir = std::path::Path::new(project_root)
.join("examples")
.join("logs")
.join("hive-24h.log");
.join("logs");
let log_paths = vec![
log_path_dir.clone().join("hive-24h.log"),
log_path_dir.clone().join("hive-24h_large.log"),
];

let schema_config = SchemaConfig::parse_from_file(schema_path.to_str().unwrap())?;
let mut log_parser = LogParser::new(schema_config)?;
log_parser.set_input_file(log_path.to_str().unwrap())?;

let mut actual = String::new();
let mut last_log_event_line_end = 0;
while let Some(log_event) = log_parser.parse_next_log_event()? {
let (start_line, end_line) = log_event.get_line_range();
assert_eq!(last_log_event_line_end + 1, start_line);
last_log_event_line_end = end_line;
actual += log_event.to_string().as_str();
}

let mut expected = String::new();
let reader = io::BufReader::new(File::open(log_path).expect("failed to open log file"));
for line in reader.lines() {
let line = line.expect("failed to read line");
expected += line.as_str();
expected += "\n";
}
for path in log_paths {
let log_path = path.to_str().unwrap();
log_parser.set_input_file(log_path)?;

let mut actual = String::new();
let mut last_log_event_line_end = 0;
while let Some(log_event) = log_parser.parse_next_log_event()? {
let (start_line, end_line) = log_event.get_line_range();
assert_eq!(last_log_event_line_end + 1, start_line);
assert_eq!(false, log_event.get_log_message_tokens().is_empty());
last_log_event_line_end = end_line;
actual += log_event.to_string().as_str();
}

assert_eq!(false, expected.is_empty());
assert_eq!(actual, expected);
let mut expected = String::new();
let reader = io::BufReader::new(File::open(log_path).expect("failed to open log file"));
for line in reader.lines() {
let line = line.expect("failed to read line");
expected += line.as_str();
expected += "\n";
}

assert_eq!(false, expected.is_empty());
assert_eq!(actual, expected);
}

Ok(())
}

0 comments on commit 5525327

Please sign in to comment.