tests/prune.rs: add more tests

This commit is contained in:
Dietmar Maurer 2019-12-05 19:01:51 +01:00
parent 6f47dd8a0f
commit a096eecb5f

View File

@ -5,6 +5,7 @@ use proxmox_backup::backup::*;
fn get_prune_list( fn get_prune_list(
list: Vec<BackupInfo>, list: Vec<BackupInfo>,
return_kept: bool,
keep_last: Option<u64>, keep_last: Option<u64>,
keep_daily: Option<u64>, keep_daily: Option<u64>,
keep_weekly: Option<u64>, keep_weekly: Option<u64>,
@ -20,7 +21,7 @@ fn get_prune_list(
prune_info prune_info
.iter() .iter()
.filter_map(|(info, keep)| { .filter_map(|(info, keep)| {
if *keep { if *keep != return_kept {
None None
} else { } else {
Some(info.backup_dir.relative_path()) Some(info.backup_dir.relative_path())
@ -44,6 +45,63 @@ fn create_info(
BackupInfo { backup_dir, files } BackupInfo { backup_dir, files }
} }
#[test]
fn test_prune_simple2() -> Result<(), Error> {
let mut orig_list = Vec::new();
orig_list.push(create_info("host/elsa/2018-11-15T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-11-15T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-11-21T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-11-22T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-11-29T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-12-01T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-12-02T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-12-03T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-12-04T11:59:15Z", false));
let list = orig_list.clone();
let remove_list = get_prune_list(list, true, Some(1), None, None, None, None);
let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
];
assert_eq!(remove_list, expect);
let list = orig_list.clone();
let remove_list = get_prune_list(list, true, Some(1), Some(1), None, None, None);
let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
];
assert_eq!(remove_list, expect);
let list = orig_list.clone();
let remove_list = get_prune_list(list, true, None, Some(1), Some(1), None, None);
let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-01T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
];
assert_eq!(remove_list, expect);
let list = orig_list.clone();
let remove_list = get_prune_list(list, true, None, Some(1), Some(1), Some(1), None);
let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-11-22T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-01T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
];
assert_eq!(remove_list, expect);
let list = orig_list.clone();
let remove_list = get_prune_list(list, true, None, None, None, Some(1), Some(1));
let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2018-11-15T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
];
assert_eq!(remove_list, expect);
Ok(())
}
#[test] #[test]
fn test_prune_simple() -> Result<(), Error> { fn test_prune_simple() -> Result<(), Error> {
@ -55,23 +113,22 @@ fn test_prune_simple() -> Result<(), Error> {
orig_list.push(create_info("host/elsa/2019-12-04T11:59:15Z", false)); orig_list.push(create_info("host/elsa/2019-12-04T11:59:15Z", false));
orig_list.push(create_info("host/elsa/2019-12-04T12:59:15Z", false)); orig_list.push(create_info("host/elsa/2019-12-04T12:59:15Z", false));
// keep-last tests // keep-last tests
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, Some(4), None, None, None, None); let remove_list = get_prune_list(list, false, Some(4), None, None, None, None);
let expect: Vec<PathBuf> = Vec::new(); let expect: Vec<PathBuf> = Vec::new();
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, Some(3), None, None, None, None); let remove_list = get_prune_list(list, false, Some(3), None, None, None, None);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
]; ];
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, Some(2), None, None, None, None); let remove_list = get_prune_list(list, false, Some(2), None, None, None, None);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
@ -79,7 +136,7 @@ fn test_prune_simple() -> Result<(), Error> {
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, Some(1), None, None, None, None); let remove_list = get_prune_list(list, false, Some(1), None, None, None, None);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
@ -88,7 +145,7 @@ fn test_prune_simple() -> Result<(), Error> {
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, Some(0), None, None, None, None); let remove_list = get_prune_list(list, false, Some(0), None, None, None, None);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
@ -99,19 +156,19 @@ fn test_prune_simple() -> Result<(), Error> {
// keep-last, keep-daily mixed // keep-last, keep-daily mixed
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, Some(2), Some(2), None, None, None); let remove_list = get_prune_list(list, false, Some(2), Some(2), None, None, None);
let expect: Vec<PathBuf> = vec![]; let expect: Vec<PathBuf> = vec![];
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
// keep-daily test // keep-daily test
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, None, Some(3), None, None, None); let remove_list = get_prune_list(list, false, None, Some(3), None, None, None);
let expect: Vec<PathBuf> = vec![PathBuf::from("host/elsa/2019-12-04T11:59:15Z")]; let expect: Vec<PathBuf> = vec![PathBuf::from("host/elsa/2019-12-04T11:59:15Z")];
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
// keep-daily test // keep-daily test
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, None, Some(2), None, None, None); let remove_list = get_prune_list(list, false, None, Some(2), None, None, None);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"), PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
@ -120,7 +177,7 @@ fn test_prune_simple() -> Result<(), Error> {
// keep-weekly // keep-weekly
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, None, None, Some(5), None, None); let remove_list = get_prune_list(list, false, None, None, Some(5), None, None);
// all backup are within the same week, so we only keep a single file // all backup are within the same week, so we only keep a single file
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
@ -131,7 +188,7 @@ fn test_prune_simple() -> Result<(), Error> {
// keep-daily + keep-weekly // keep-daily + keep-weekly
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, None, Some(1), Some(5), None, None); let remove_list = get_prune_list(list, false, None, Some(1), Some(5), None, None);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), PathBuf::from("host/elsa/2019-12-03T11:59:15Z"),
@ -141,7 +198,7 @@ fn test_prune_simple() -> Result<(), Error> {
// keep-monthly // keep-monthly
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, None, None, None, Some(6), None); let remove_list = get_prune_list(list, false, None, None, None, Some(6), None);
// all backup are within the same month, so we only keep a single file // all backup are within the same month, so we only keep a single file
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
@ -152,7 +209,7 @@ fn test_prune_simple() -> Result<(), Error> {
// keep-yearly // keep-yearly
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, None, None, None, None, Some(7)); let remove_list = get_prune_list(list, false, None, None, None, None, Some(7));
// all backup are within the same year, so we only keep a single file // all backup are within the same year, so we only keep a single file
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
@ -163,7 +220,7 @@ fn test_prune_simple() -> Result<(), Error> {
// keep-weekly + keep-monthly + keep-yearly // keep-weekly + keep-monthly + keep-yearly
let list = orig_list.clone(); let list = orig_list.clone();
let remove_list = get_prune_list(list, None, None, Some(5), Some(6), Some(7)); let remove_list = get_prune_list(list, false, None, None, Some(5), Some(6), Some(7));
// all backup are within one week, so we only keep a single file // all backup are within one week, so we only keep a single file
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),