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

Compound appender tests #355

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ fnv = "1.0"
humantime = { version = "2.1", optional = true }
log = { version = "0.4.20", features = ["std"] }
log-mdc = { version = "0.1", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
serde = { version = "1.0.196", optional = true, features = ["derive"] }
serde-value = { version = "0.7", optional = true }
thread-id = { version = "4", optional = true }
typemap-ors = { version = "1.0.0", optional = true }
serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.9", optional = true }
toml = { version = "0.8", optional = true }
toml = { version = "<0.8.10", optional = true }
parking_lot = { version = "0.12.0", optional = true }
rand = { version = "0.8", optional = true}
thiserror = "1.0.15"
Expand All @@ -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
100 changes: 99 additions & 1 deletion src/append/rolling_file/policy/compound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Requires the `compound_policy` feature.
#[cfg(feature = "config_parsing")]
use serde::{self, de};
use serde::de;
#[cfg(feature = "config_parsing")]
use serde_value::Value;
#[cfg(feature = "config_parsing")]
Expand Down 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
Loading