Skip to content

Commit

Permalink
chore: add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bconn98 committed Mar 3, 2024
1 parent 049e2cc commit 7ae8409
Show file tree
Hide file tree
Showing 6 changed files with 608 additions and 49 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ streaming-stats = "0.2.3"
humantime = "2.1"
tempfile = "3.8"
mock_instant = "0.3"
serde_test = "1.0.176"

[[example]]
name = "json_logger"
Expand Down
98 changes: 98 additions & 0 deletions src/append/rolling_file/policy/compound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,101 @@ impl Deserialize for CompoundPolicyDeserializer {
Ok(Box::new(CompoundPolicy::new(trigger, roller)))
}
}

#[cfg(test)]
mod test {
use self::{roll::delete::DeleteRoller, trigger::size::SizeTrigger};

use super::*;
use tempfile::NamedTempFile;

#[cfg(feature = "config_parsing")]
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};

fn create_policy() -> CompoundPolicy {
let trigger = SizeTrigger::new(1024);
let roller = DeleteRoller::new();
CompoundPolicy::new(Box::new(trigger), Box::new(roller))
}

#[test]
#[cfg(feature = "config_parsing")]
fn test_trigger_deser() {
let mut cfg = vec![
Token::Struct {
name: "Trigger",
len: 2,
},
Token::Str("kind"),
Token::Str("size"),
Token::Str("limit"),
Token::U64(1024),
Token::StructEnd,
];

assert_de_tokens(
&Trigger {
kind: "size".to_owned(),
config: Value::Map({
let mut map = BTreeMap::new();
map.insert(Value::String("limit".to_owned()), Value::U64(1024));
map
}),
},
&cfg,
);

// Intentionally break the config
cfg[1] = Token::Str("knd");
assert_de_tokens_error::<Trigger>(&cfg, "missing field `kind`");
}

#[test]
#[cfg(feature = "config_parsing")]
fn test_roller_deser() {
let mut cfg = vec![
Token::Struct {
name: "Roller",
len: 1,
},
Token::Str("kind"),
Token::Str("delete"),
Token::StructEnd,
];

assert_de_tokens(
&Roller {
kind: "delete".to_owned(),
config: Value::Map(BTreeMap::new()),
},
&cfg,
);

// Intentionally break the config
cfg[1] = Token::Str("knd");
assert_de_tokens_error::<Roller>(&cfg, "missing field `kind`");
}

#[test]
fn test_pre_process() {
let policy = create_policy();
assert!(!policy.is_pre_process());
}

#[test]
fn test_process() {
let policy = create_policy();
// Don't roll then roll
let file_sizes = vec![0, 2048];
let tmp_file = NamedTempFile::new().unwrap();

for file_size in file_sizes {
let mut logfile = LogFile {
writer: &mut None,
path: tmp_file.as_ref(),
len: file_size,
};
assert!(policy.process(&mut logfile).is_ok());
}
}
}
21 changes: 21 additions & 0 deletions src/append/rolling_file/policy/compound/roll/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,24 @@ impl Deserialize for DeleteRollerDeserializer {
Ok(Box::<DeleteRoller>::default())
}
}

#[cfg(test)]
mod test {
use super::*;
use tempfile::NamedTempFile;

#[test]
fn test_roll() {
let roller = DeleteRoller::new();

let tmp_file = NamedTempFile::new().unwrap();
let tmp_file = tmp_file.into_temp_path().keep().unwrap();
// File exists, should be ok
let res = roller.roll(&tmp_file);
assert!(res.is_ok());

// File doesn't exist, should err
let res = roller.roll(&tmp_file);
assert!(res.is_err());
}
}
61 changes: 54 additions & 7 deletions src/append/rolling_file/policy/compound/roll/fixed_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ fn make_temp_file_name<P>(file: P) -> PathBuf
where
P: AsRef<Path>,
{
#[cfg(test)]
let mut n = 0;

#[cfg(not(test))]
let mut n = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap_or_else(|_| std::time::Duration::from_secs(0))
Expand Down Expand Up @@ -354,7 +358,7 @@ mod test {
fn wait_for_roller(_roller: &FixedWindowRoller) {}

#[test]
fn rotation() {
fn test_test_rotation() {
let dir = tempfile::tempdir().unwrap();

let base = dir.path().to_str().unwrap();
Expand Down Expand Up @@ -414,7 +418,7 @@ mod test {
}

#[test]
fn rotation_no_trivial_base() {
fn test_rotation_no_trivial_base() {
let dir = tempfile::tempdir().unwrap();
let base = 3;
let fname = "foo.log";
Expand Down Expand Up @@ -459,7 +463,7 @@ mod test {
}

#[test]
fn create_archive_unvaried() {
fn test_create_archive_unvaried() {
let dir = tempfile::tempdir().unwrap();

let base = dir.path().join("log").join("archive");
Expand Down Expand Up @@ -487,7 +491,7 @@ mod test {
}

#[test]
fn create_archive_varied() {
fn test_create_archive_varied() {
let dir = tempfile::tempdir().unwrap();

let base = dir.path().join("log").join("archive");
Expand Down Expand Up @@ -516,7 +520,7 @@ mod test {

#[test]
#[cfg_attr(feature = "gzip", ignore)]
fn unsupported_gzip() {
fn test_unsupported_gzip() {
let dir = tempfile::tempdir().unwrap();

let pattern = dir.path().join("{}.gz");
Expand All @@ -529,7 +533,7 @@ mod test {
#[cfg_attr(not(feature = "gzip"), ignore)]
// or should we force windows user to install gunzip
#[cfg(not(windows))]
fn supported_gzip() {
fn test_supported_gzip() {
use std::process::Command;

let dir = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -561,7 +565,7 @@ mod test {
}

#[test]
fn roll_with_env_var() {
fn test_roll_with_env_var() {
std::env::set_var("LOG_DIR", "test_log_dir");
let fcontent = b"file1";
let dir = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -596,4 +600,47 @@ mod test {
//Check the new rolled file has the same contents as the old one
assert_eq!(contents, fcontent);
}

#[test]
fn test_invalid_pattern() {
let dir = tempfile::tempdir().unwrap();

let base = dir.path().to_str().unwrap();
let roller = FixedWindowRoller::builder().build(&format!("{}/foo.log", base), 2);

assert_eq!(
format!("{}", roller.unwrap_err()),
"pattern does not contain `{}`"
);
}

#[test]
fn test_rotate_to_del() {
let dir = tempfile::tempdir().unwrap();

let base = dir.path().to_str().unwrap();
let roller = FixedWindowRoller::builder()
.build(&format!("{}/foo.log.{{}}", base), 0)
.unwrap();

let file = dir.path().join("foo.log");
File::create(&file).unwrap().write_all(b"file1").unwrap();

roller.roll(&file).unwrap();
wait_for_roller(&roller);
assert!(!file.exists());
assert!(!dir.path().join("foo.log.0").exists());
}

#[test]
#[cfg(feature = "background_rotation")]
fn test_temp_file_exists() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("foo.log");
let temp_file = dir.path().join("foo.0");
assert_eq!(make_temp_file_name(file.clone()), dir.path().join("foo.0"));

let _ = File::create(temp_file).unwrap();
assert_eq!(make_temp_file_name(file), dir.path().join("foo.1"));
}
}
Loading

0 comments on commit 7ae8409

Please sign in to comment.