]>
git.proxmox.com Git - proxmox-backup.git/blob - tests/prune.rs
1 use std
::path
::PathBuf
;
5 use pbs_api_types
::PruneJobOptions
;
6 use pbs_datastore
::manifest
::MANIFEST_BLOB_NAME
;
7 use pbs_datastore
::prune
::compute_prune_info
;
8 use pbs_datastore
::{BackupDir, BackupInfo}
;
11 list
: Vec
<BackupInfo
>,
13 options
: &PruneJobOptions
,
15 let mut prune_info
= compute_prune_info(list
, &options
.keep
).unwrap();
21 .filter_map(|(info
, mark
)| {
22 if mark
.keep() != return_kept
{
25 Some(info
.backup_dir
.relative_path())
31 fn create_info(snapshot
: &str, partial
: bool
) -> BackupInfo
{
32 let backup_dir
= BackupDir
::new_test(snapshot
.parse().unwrap());
34 let mut files
= Vec
::new();
37 files
.push(String
::from(MANIFEST_BLOB_NAME
));
47 fn create_info_protected(snapshot
: &str, partial
: bool
) -> BackupInfo
{
48 let mut info
= create_info(snapshot
, partial
);
49 info
.protected
= true;
54 fn test_prune_protected() -> Result
<(), Error
> {
56 create_info_protected(
57 "host/elsa/2019-11-15T09:39:15Z",
60 create_info("host/elsa/2019-11-15T10:39:15Z", false),
61 create_info("host/elsa/2019-11-15T10:49:15Z", false),
62 create_info_protected(
63 "host/elsa/2019-11-15T10:59:15Z",
68 eprintln
!("{:?}", orig_list
);
70 let mut options
= PruneJobOptions
::default();
71 options
.keep
.keep_last
= Some(1);
72 let remove_list
= get_prune_list(orig_list
.clone(), false, &options
);
73 let expect
: Vec
<PathBuf
> = vec
![PathBuf
::from("host/elsa/2019-11-15T10:39:15Z")];
74 assert_eq
!(remove_list
, expect
);
76 let mut options
= PruneJobOptions
::default();
77 options
.keep
.keep_hourly
= Some(1);
78 let remove_list
= get_prune_list(orig_list
, false, &options
);
79 let expect
: Vec
<PathBuf
> = vec
![PathBuf
::from("host/elsa/2019-11-15T10:39:15Z")];
80 assert_eq
!(remove_list
, expect
);
85 fn test_prune_hourly() -> Result
<(), Error
> {
87 create_info("host/elsa/2019-11-15T09:39:15Z", false),
88 create_info("host/elsa/2019-11-15T10:49:15Z", false),
89 create_info("host/elsa/2019-11-15T10:59:15Z", false),
90 create_info("host/elsa/2019-11-15T11:39:15Z", false),
91 create_info("host/elsa/2019-11-15T11:49:15Z", false),
92 create_info("host/elsa/2019-11-15T11:59:15Z", false),
95 let list
= orig_list
.clone();
96 let mut options
= PruneJobOptions
::default();
97 options
.keep
.keep_hourly
= Some(3);
98 let remove_list
= get_prune_list(list
, false, &options
);
99 let expect
: Vec
<PathBuf
> = vec
![
100 PathBuf
::from("host/elsa/2019-11-15T10:49:15Z"),
101 PathBuf
::from("host/elsa/2019-11-15T11:39:15Z"),
102 PathBuf
::from("host/elsa/2019-11-15T11:49:15Z"),
104 assert_eq
!(remove_list
, expect
);
106 let list
= orig_list
;
107 let mut options
= PruneJobOptions
::default();
108 options
.keep
.keep_hourly
= Some(2);
109 let remove_list
= get_prune_list(list
, true, &options
);
110 let expect
: Vec
<PathBuf
> = vec
![
111 PathBuf
::from("host/elsa/2019-11-15T10:59:15Z"),
112 PathBuf
::from("host/elsa/2019-11-15T11:59:15Z"),
114 assert_eq
!(remove_list
, expect
);
120 fn test_prune_simple2() -> Result
<(), Error
> {
121 let orig_list
= vec
![
122 create_info("host/elsa/2018-11-15T11:59:15Z", false),
123 create_info("host/elsa/2019-11-15T11:59:15Z", false),
124 create_info("host/elsa/2019-11-21T11:59:15Z", false),
125 create_info("host/elsa/2019-11-22T11:59:15Z", false),
126 create_info("host/elsa/2019-11-29T11:59:15Z", false),
127 create_info("host/elsa/2019-12-01T11:59:15Z", false),
128 create_info("host/elsa/2019-12-02T11:59:15Z", false),
129 create_info("host/elsa/2019-12-03T11:59:15Z", false),
130 create_info("host/elsa/2019-12-04T11:59:15Z", false),
133 let list
= orig_list
.clone();
134 let mut options
= PruneJobOptions
::default();
135 options
.keep
.keep_daily
= Some(1);
136 let remove_list
= get_prune_list(list
, true, &options
);
137 let expect
: Vec
<PathBuf
> = vec
![PathBuf
::from("host/elsa/2019-12-04T11:59:15Z")];
138 assert_eq
!(remove_list
, expect
);
140 let list
= orig_list
.clone();
141 let mut options
= PruneJobOptions
::default();
142 options
.keep
.keep_last
= Some(1);
143 options
.keep
.keep_daily
= Some(1);
144 let remove_list
= get_prune_list(list
, true, &options
);
145 let expect
: Vec
<PathBuf
> = vec
![
146 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
147 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
149 assert_eq
!(remove_list
, expect
);
151 let list
= orig_list
.clone();
152 let mut options
= PruneJobOptions
::default();
153 options
.keep
.keep_daily
= Some(1);
154 options
.keep
.keep_weekly
= Some(1);
155 let remove_list
= get_prune_list(list
, true, &options
);
156 let expect
: Vec
<PathBuf
> = vec
![
157 PathBuf
::from("host/elsa/2019-12-01T11:59:15Z"),
158 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
160 assert_eq
!(remove_list
, expect
);
162 let list
= orig_list
.clone();
163 let mut options
= PruneJobOptions
::default();
164 options
.keep
.keep_daily
= Some(1);
165 options
.keep
.keep_weekly
= Some(1);
166 options
.keep
.keep_monthly
= Some(1);
167 let remove_list
= get_prune_list(list
, true, &options
);
168 let expect
: Vec
<PathBuf
> = vec
![
169 PathBuf
::from("host/elsa/2019-11-22T11:59:15Z"),
170 PathBuf
::from("host/elsa/2019-12-01T11:59:15Z"),
171 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
173 assert_eq
!(remove_list
, expect
);
175 let list
= orig_list
;
176 let mut options
= PruneJobOptions
::default();
177 options
.keep
.keep_monthly
= Some(1);
178 options
.keep
.keep_yearly
= Some(1);
179 let remove_list
= get_prune_list(list
, true, &options
);
180 let expect
: Vec
<PathBuf
> = vec
![
181 PathBuf
::from("host/elsa/2018-11-15T11:59:15Z"),
182 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
184 assert_eq
!(remove_list
, expect
);
190 fn test_prune_simple() -> Result
<(), Error
> {
191 let orig_list
= vec
![
192 create_info("host/elsa/2019-12-02T11:59:15Z", false),
193 create_info("host/elsa/2019-12-03T11:59:15Z", false),
194 create_info("host/elsa/2019-12-04T11:59:15Z", false),
195 create_info("host/elsa/2019-12-04T12:59:15Z", false),
200 let list
= orig_list
.clone();
201 let mut options
= PruneJobOptions
::default();
202 options
.keep
.keep_last
= Some(4);
203 let remove_list
= get_prune_list(list
, false, &options
);
204 let expect
: Vec
<PathBuf
> = Vec
::new();
205 assert_eq
!(remove_list
, expect
);
207 let list
= orig_list
.clone();
208 let mut options
= PruneJobOptions
::default();
209 options
.keep
.keep_last
= Some(3);
210 let remove_list
= get_prune_list(list
, false, &options
);
211 let expect
: Vec
<PathBuf
> = vec
![PathBuf
::from("host/elsa/2019-12-02T11:59:15Z")];
212 assert_eq
!(remove_list
, expect
);
214 let list
= orig_list
.clone();
215 let mut options
= PruneJobOptions
::default();
216 options
.keep
.keep_last
= Some(2);
217 let remove_list
= get_prune_list(list
, false, &options
);
218 let expect
: Vec
<PathBuf
> = vec
![
219 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
220 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
222 assert_eq
!(remove_list
, expect
);
224 let list
= orig_list
.clone();
225 let mut options
= PruneJobOptions
::default();
226 options
.keep
.keep_last
= Some(1);
227 let remove_list
= get_prune_list(list
, false, &options
);
228 let expect
: Vec
<PathBuf
> = vec
![
229 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
230 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
231 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
233 assert_eq
!(remove_list
, expect
);
235 let list
= orig_list
.clone();
236 let mut options
= PruneJobOptions
::default();
237 options
.keep
.keep_last
= Some(0);
238 let remove_list
= get_prune_list(list
, false, &options
);
239 let expect
: Vec
<PathBuf
> = vec
![
240 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
241 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
242 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
243 PathBuf
::from("host/elsa/2019-12-04T12:59:15Z"),
245 assert_eq
!(remove_list
, expect
);
247 // keep-last, keep-daily mixed
248 let list
= orig_list
.clone();
249 let mut options
= PruneJobOptions
::default();
250 options
.keep
.keep_last
= Some(2);
251 options
.keep
.keep_daily
= Some(2);
252 let remove_list
= get_prune_list(list
, false, &options
);
253 let expect
: Vec
<PathBuf
> = vec
![];
254 assert_eq
!(remove_list
, expect
);
257 let list
= orig_list
.clone();
258 let mut options
= PruneJobOptions
::default();
259 options
.keep
.keep_daily
= Some(3);
260 let remove_list
= get_prune_list(list
, false, &options
);
261 let expect
: Vec
<PathBuf
> = vec
![PathBuf
::from("host/elsa/2019-12-04T11:59:15Z")];
262 assert_eq
!(remove_list
, expect
);
265 let list
= orig_list
.clone();
266 let mut options
= PruneJobOptions
::default();
267 options
.keep
.keep_daily
= Some(2);
268 let remove_list
= get_prune_list(list
, false, &options
);
269 let expect
: Vec
<PathBuf
> = vec
![
270 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
271 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
273 assert_eq
!(remove_list
, expect
);
276 let list
= orig_list
.clone();
277 let mut options
= PruneJobOptions
::default();
278 options
.keep
.keep_weekly
= Some(5);
279 let remove_list
= get_prune_list(list
, false, &options
);
280 // all backup are within the same week, so we only keep a single file
281 let expect
: Vec
<PathBuf
> = vec
![
282 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
283 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
284 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
286 assert_eq
!(remove_list
, expect
);
288 // keep-daily + keep-weekly
289 let list
= orig_list
.clone();
290 let mut options
= PruneJobOptions
::default();
291 options
.keep
.keep_daily
= Some(1);
292 options
.keep
.keep_weekly
= Some(5);
293 let remove_list
= get_prune_list(list
, false, &options
);
294 let expect
: Vec
<PathBuf
> = vec
![
295 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
296 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
297 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
299 assert_eq
!(remove_list
, expect
);
302 let list
= orig_list
.clone();
303 let mut options
= PruneJobOptions
::default();
304 options
.keep
.keep_monthly
= Some(6);
305 let remove_list
= get_prune_list(list
, false, &options
);
306 // all backup are within the same month, so we only keep a single file
307 let expect
: Vec
<PathBuf
> = vec
![
308 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
309 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
310 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
312 assert_eq
!(remove_list
, expect
);
315 let list
= orig_list
.clone();
316 let mut options
= PruneJobOptions
::default();
317 options
.keep
.keep_yearly
= Some(7);
318 let remove_list
= get_prune_list(list
, false, &options
);
319 // all backup are within the same year, so we only keep a single file
320 let expect
: Vec
<PathBuf
> = vec
![
321 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
322 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
323 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
325 assert_eq
!(remove_list
, expect
);
327 // keep-weekly + keep-monthly + keep-yearly
328 let list
= orig_list
;
329 let mut options
= PruneJobOptions
::default();
330 options
.keep
.keep_weekly
= Some(5);
331 options
.keep
.keep_monthly
= Some(6);
332 options
.keep
.keep_yearly
= Some(7);
333 let remove_list
= get_prune_list(list
, false, &options
);
334 // all backup are within one week, so we only keep a single file
335 let expect
: Vec
<PathBuf
> = vec
![
336 PathBuf
::from("host/elsa/2019-12-02T11:59:15Z"),
337 PathBuf
::from("host/elsa/2019-12-03T11:59:15Z"),
338 PathBuf
::from("host/elsa/2019-12-04T11:59:15Z"),
340 assert_eq
!(remove_list
, expect
);