]>
Commit | Line | Data |
---|---|---|
0c875cf3 DM |
1 | use failure::*; |
2 | use std::path::PathBuf; | |
3 | ||
4 | use proxmox_backup::backup::*; | |
5 | ||
6 | const TESTDIR: &str = "/tmp/prunetest"; | |
7 | ||
8 | fn get_prune_list( | |
9 | list: Vec<BackupInfo>, | |
10 | keep_last: Option<u64>, | |
11 | keep_daily: Option<u64>, | |
12 | keep_weekly: Option<u64>, | |
13 | keep_monthly: Option<u64>, | |
14 | keep_yearly: Option<u64>, | |
15 | ) -> Vec<PathBuf> { | |
16 | ||
17 | let remove_list = BackupGroup::compute_prune_list( | |
18 | list, keep_last, keep_daily, keep_weekly, keep_monthly, keep_yearly).unwrap(); | |
19 | ||
20 | remove_list | |
21 | .iter() | |
22 | .map(|d| d.backup_dir.relative_path()) | |
23 | .collect() | |
24 | } | |
25 | ||
26 | #[test] | |
27 | fn test_prune_simple() -> Result<(), Error> { | |
28 | ||
29 | let _ = std::fs::remove_dir_all(TESTDIR); | |
30 | std::fs::create_dir(TESTDIR)?; | |
31 | ||
32 | std::fs::create_dir_all(format!("{}/{}", TESTDIR, "host/elsa/2019-12-02T11:59:15Z"))?; | |
33 | std::fs::create_dir_all(format!("{}/{}", TESTDIR, "host/elsa/2019-12-03T11:59:15Z"))?; | |
34 | std::fs::create_dir_all(format!("{}/{}", TESTDIR, "host/elsa/2019-12-04T11:59:15Z"))?; | |
35 | std::fs::create_dir_all(format!("{}/{}", TESTDIR, "host/elsa/2019-12-04T12:59:15Z"))?; | |
36 | ||
37 | ||
38 | let orig_list = BackupInfo::list_backups(std::path::Path::new(TESTDIR))?; | |
39 | ||
40 | // keep-last tests | |
41 | ||
42 | let list = orig_list.clone(); | |
43 | let remove_list = get_prune_list(list, Some(4), None, None, None, None); | |
44 | let expect: Vec<PathBuf> = Vec::new(); | |
45 | assert_eq!(remove_list, expect); | |
46 | ||
47 | let list = orig_list.clone(); | |
48 | let remove_list = get_prune_list(list, Some(3), None, None, None, None); | |
49 | let expect: Vec<PathBuf> = vec![ | |
50 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
51 | ]; | |
52 | assert_eq!(remove_list, expect); | |
53 | ||
54 | let list = orig_list.clone(); | |
55 | let remove_list = get_prune_list(list, Some(2), None, None, None, None); | |
56 | let expect: Vec<PathBuf> = vec![ | |
57 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
58 | PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), | |
59 | ]; | |
60 | assert_eq!(remove_list, expect); | |
61 | ||
62 | let list = orig_list.clone(); | |
63 | let remove_list = get_prune_list(list, Some(1), None, None, None, None); | |
64 | let expect: Vec<PathBuf> = vec![ | |
65 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
66 | PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), | |
67 | PathBuf::from("host/elsa/2019-12-04T11:59:15Z"), | |
68 | ]; | |
69 | assert_eq!(remove_list, expect); | |
70 | ||
71 | let list = orig_list.clone(); | |
72 | let remove_list = get_prune_list(list, Some(0), None, None, None, None); | |
73 | let expect: Vec<PathBuf> = vec![ | |
74 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
75 | PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), | |
76 | PathBuf::from("host/elsa/2019-12-04T11:59:15Z"), | |
77 | PathBuf::from("host/elsa/2019-12-04T12:59:15Z"), | |
78 | ]; | |
79 | assert_eq!(remove_list, expect); | |
80 | ||
81 | // keep-last, keep-daily mixed | |
82 | let list = orig_list.clone(); | |
83 | let remove_list = get_prune_list(list, Some(2), Some(2), None, None, None); | |
84 | let expect: Vec<PathBuf> = vec![ | |
85 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
86 | ]; | |
87 | assert_eq!(remove_list, expect); | |
88 | ||
89 | // keep-daily test | |
90 | ||
91 | let list = orig_list.clone(); | |
92 | let remove_list = get_prune_list(list, None, Some(3), None, None, None); | |
93 | let expect: Vec<PathBuf> = vec![PathBuf::from("host/elsa/2019-12-04T11:59:15Z")]; | |
94 | assert_eq!(remove_list, expect); | |
95 | ||
96 | // keep-daily test | |
97 | ||
98 | let list = orig_list.clone(); | |
99 | let remove_list = get_prune_list(list, None, Some(2), None, None, None); | |
100 | let expect: Vec<PathBuf> = vec![ | |
101 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
102 | PathBuf::from("host/elsa/2019-12-04T11:59:15Z"), | |
103 | ]; | |
104 | assert_eq!(remove_list, expect); | |
105 | ||
106 | // keep-weekly | |
107 | let list = orig_list.clone(); | |
108 | let remove_list = get_prune_list(list, None, None, Some(5), None, None); | |
109 | let expect: Vec<PathBuf> = vec![ | |
110 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
111 | PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), | |
112 | PathBuf::from("host/elsa/2019-12-04T11:59:15Z"), | |
113 | ]; | |
114 | assert_eq!(remove_list, expect); | |
115 | ||
116 | // keep-monthly | |
117 | let list = orig_list.clone(); | |
118 | let remove_list = get_prune_list(list, None, None, None, Some(6), None); | |
119 | let expect: Vec<PathBuf> = vec![ | |
120 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
121 | PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), | |
122 | PathBuf::from("host/elsa/2019-12-04T11:59:15Z"), | |
123 | ]; | |
124 | assert_eq!(remove_list, expect); | |
125 | ||
126 | // keep-yearly | |
127 | let list = orig_list.clone(); | |
128 | let remove_list = get_prune_list(list, None, None, None, None, Some(7)); | |
129 | let expect: Vec<PathBuf> = vec![ | |
130 | PathBuf::from("host/elsa/2019-12-02T11:59:15Z"), | |
131 | PathBuf::from("host/elsa/2019-12-03T11:59:15Z"), | |
132 | PathBuf::from("host/elsa/2019-12-04T11:59:15Z"), | |
133 | ]; | |
134 | assert_eq!(remove_list, expect); | |
135 | ||
136 | Ok(()) | |
137 | } |