|
1 | 1 | import re |
2 | | -from datetime import datetime |
| 2 | +from datetime import datetime, timezone, timedelta |
| 3 | + |
| 4 | +import pytest |
3 | 5 |
|
4 | 6 | from ...constants import * # NOQA |
| 7 | +from ...archiver.prune_cmd import prune_split, prune_within |
5 | 8 | from . import cmd, RK_ENCRYPTION, src_dir, generate_archiver_tests |
| 9 | +from ...helpers import interval |
6 | 10 |
|
7 | 11 | pytest_generate_tests = lambda metafunc: generate_archiver_tests(metafunc, kinds="local,remote,binary") # NOQA |
8 | 12 |
|
@@ -257,3 +261,142 @@ def test_prune_ignore_protected(archivers, request): |
257 | 261 | output = cmd(archiver, "repo-list") |
258 | 262 | assert "archive1" in output # @PROT protected archive1 from deletion |
259 | 263 | assert "archive3" in output # last one |
| 264 | + |
| 265 | + |
| 266 | +class MockArchive: |
| 267 | + def __init__(self, ts, id): |
| 268 | + self.ts = ts |
| 269 | + self.id = id |
| 270 | + |
| 271 | + def __repr__(self): |
| 272 | + return f"{self.id}: {self.ts.isoformat()}" |
| 273 | + |
| 274 | + |
| 275 | +# This is the local timezone of the system running the tests. |
| 276 | +# We need this e.g. to construct archive timestamps for the prune tests, |
| 277 | +# because borg prune operates in the local timezone (it first converts the |
| 278 | +# archive timestamp to the local timezone). So, if we want the y/m/d/h/m/s |
| 279 | +# values which prune uses to be exactly the ones we give [and NOT shift them |
| 280 | +# by tzoffset], we need to give the timestamps in the same local timezone. |
| 281 | +# Please note that the timestamps in a real borg archive or manifest are |
| 282 | +# stored in UTC timezone. |
| 283 | +local_tz = datetime.now(tz=timezone.utc).astimezone(tz=None).tzinfo |
| 284 | + |
| 285 | + |
| 286 | +def test_prune_within(): |
| 287 | + def subset(lst, indices): |
| 288 | + return {lst[i] for i in indices} |
| 289 | + |
| 290 | + def dotest(test_archives, within, indices): |
| 291 | + for ta in test_archives, reversed(test_archives): |
| 292 | + kept_because = {} |
| 293 | + keep = prune_within(ta, interval(within), kept_because) |
| 294 | + assert set(keep) == subset(test_archives, indices) |
| 295 | + assert all("within" == kept_because[a.id][0] for a in keep) |
| 296 | + |
| 297 | + # 1 minute, 1.5 hours, 2.5 hours, 3.5 hours, 25 hours, 49 hours |
| 298 | + test_offsets = [60, 90 * 60, 150 * 60, 210 * 60, 25 * 60 * 60, 49 * 60 * 60] |
| 299 | + now = datetime.now(timezone.utc) |
| 300 | + test_dates = [now - timedelta(seconds=s) for s in test_offsets] |
| 301 | + test_archives = [MockArchive(date, i) for i, date in enumerate(test_dates)] |
| 302 | + |
| 303 | + dotest(test_archives, "15S", []) |
| 304 | + dotest(test_archives, "2M", [0]) |
| 305 | + dotest(test_archives, "1H", [0]) |
| 306 | + dotest(test_archives, "2H", [0, 1]) |
| 307 | + dotest(test_archives, "3H", [0, 1, 2]) |
| 308 | + dotest(test_archives, "24H", [0, 1, 2, 3]) |
| 309 | + dotest(test_archives, "26H", [0, 1, 2, 3, 4]) |
| 310 | + dotest(test_archives, "2d", [0, 1, 2, 3, 4]) |
| 311 | + dotest(test_archives, "50H", [0, 1, 2, 3, 4, 5]) |
| 312 | + dotest(test_archives, "3d", [0, 1, 2, 3, 4, 5]) |
| 313 | + dotest(test_archives, "1w", [0, 1, 2, 3, 4, 5]) |
| 314 | + dotest(test_archives, "1m", [0, 1, 2, 3, 4, 5]) |
| 315 | + dotest(test_archives, "1y", [0, 1, 2, 3, 4, 5]) |
| 316 | + |
| 317 | + |
| 318 | +@pytest.mark.parametrize( |
| 319 | + "rule,num_to_keep,expected_ids", |
| 320 | + [ |
| 321 | + ("yearly", 3, (13, 2, 1)), |
| 322 | + ("monthly", 3, (13, 8, 4)), |
| 323 | + ("weekly", 2, (13, 8)), |
| 324 | + ("daily", 3, (13, 8, 7)), |
| 325 | + ("hourly", 3, (13, 10, 8)), |
| 326 | + ("minutely", 3, (13, 10, 9)), |
| 327 | + ("secondly", 4, (13, 12, 11, 10)), |
| 328 | + ("daily", 0, []), |
| 329 | + ], |
| 330 | +) |
| 331 | +def test_prune_split(rule, num_to_keep, expected_ids): |
| 332 | + def subset(lst, ids): |
| 333 | + return {i for i in lst if i.id in ids} |
| 334 | + |
| 335 | + archives = [ |
| 336 | + # years apart |
| 337 | + MockArchive(datetime(2015, 1, 1, 10, 0, 0, tzinfo=local_tz), 1), |
| 338 | + MockArchive(datetime(2016, 1, 1, 10, 0, 0, tzinfo=local_tz), 2), |
| 339 | + MockArchive(datetime(2017, 1, 1, 10, 0, 0, tzinfo=local_tz), 3), |
| 340 | + # months apart |
| 341 | + MockArchive(datetime(2017, 2, 1, 10, 0, 0, tzinfo=local_tz), 4), |
| 342 | + MockArchive(datetime(2017, 3, 1, 10, 0, 0, tzinfo=local_tz), 5), |
| 343 | + # days apart |
| 344 | + MockArchive(datetime(2017, 3, 2, 10, 0, 0, tzinfo=local_tz), 6), |
| 345 | + MockArchive(datetime(2017, 3, 3, 10, 0, 0, tzinfo=local_tz), 7), |
| 346 | + MockArchive(datetime(2017, 3, 4, 10, 0, 0, tzinfo=local_tz), 8), |
| 347 | + # minutes apart |
| 348 | + MockArchive(datetime(2017, 10, 1, 9, 45, 0, tzinfo=local_tz), 9), |
| 349 | + MockArchive(datetime(2017, 10, 1, 9, 55, 0, tzinfo=local_tz), 10), |
| 350 | + # seconds apart |
| 351 | + MockArchive(datetime(2017, 10, 1, 10, 0, 1, tzinfo=local_tz), 11), |
| 352 | + MockArchive(datetime(2017, 10, 1, 10, 0, 3, tzinfo=local_tz), 12), |
| 353 | + MockArchive(datetime(2017, 10, 1, 10, 0, 5, tzinfo=local_tz), 13), |
| 354 | + ] |
| 355 | + kept_because = {} |
| 356 | + keep = prune_split(archives, rule, num_to_keep, kept_because) |
| 357 | + |
| 358 | + assert set(keep) == subset(archives, expected_ids) |
| 359 | + for item in keep: |
| 360 | + assert kept_because[item.id][0] == rule |
| 361 | + |
| 362 | + |
| 363 | +def test_prune_split_keep_oldest(): |
| 364 | + def subset(lst, ids): |
| 365 | + return {i for i in lst if i.id in ids} |
| 366 | + |
| 367 | + archives = [ |
| 368 | + # oldest backup, but not last in its year |
| 369 | + MockArchive(datetime(2018, 1, 1, 10, 0, 0, tzinfo=local_tz), 1), |
| 370 | + # an interim backup |
| 371 | + MockArchive(datetime(2018, 12, 30, 10, 0, 0, tzinfo=local_tz), 2), |
| 372 | + # year-end backups |
| 373 | + MockArchive(datetime(2018, 12, 31, 10, 0, 0, tzinfo=local_tz), 3), |
| 374 | + MockArchive(datetime(2019, 12, 31, 10, 0, 0, tzinfo=local_tz), 4), |
| 375 | + ] |
| 376 | + |
| 377 | + # Keep oldest when retention target can't otherwise be met |
| 378 | + kept_because = {} |
| 379 | + keep = prune_split(archives, "yearly", 3, kept_because) |
| 380 | + |
| 381 | + assert set(keep) == subset(archives, [1, 3, 4]) |
| 382 | + assert kept_because[1][0] == "yearly[oldest]" |
| 383 | + assert kept_because[3][0] == "yearly" |
| 384 | + assert kept_because[4][0] == "yearly" |
| 385 | + |
| 386 | + # Otherwise, prune it |
| 387 | + kept_because = {} |
| 388 | + keep = prune_split(archives, "yearly", 2, kept_because) |
| 389 | + |
| 390 | + assert set(keep) == subset(archives, [3, 4]) |
| 391 | + assert kept_because[3][0] == "yearly" |
| 392 | + assert kept_because[4][0] == "yearly" |
| 393 | + |
| 394 | + |
| 395 | +def test_prune_split_no_archives(): |
| 396 | + archives = [] |
| 397 | + |
| 398 | + kept_because = {} |
| 399 | + keep = prune_split(archives, "yearly", 3, kept_because) |
| 400 | + |
| 401 | + assert keep == [] |
| 402 | + assert kept_because == {} |
0 commit comments