From 881eaca08aa27734b22d295765c36285d1d5673d Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 21 May 2025 22:20:12 +0200 Subject: [PATCH] fix tests so that they are as before the splitup --- src/borg/testsuite/archiver/prune_cmd_test.py | 37 ++++++++- src/borg/testsuite/helpers/fs_test.py | 11 ++- .../testsuite/helpers/parseformat_test.py | 83 +++++++------------ 3 files changed, 74 insertions(+), 57 deletions(-) diff --git a/src/borg/testsuite/archiver/prune_cmd_test.py b/src/borg/testsuite/archiver/prune_cmd_test.py index 3141dfbde..9dda19a0b 100644 --- a/src/borg/testsuite/archiver/prune_cmd_test.py +++ b/src/borg/testsuite/archiver/prune_cmd_test.py @@ -1,11 +1,12 @@ import re -from datetime import datetime, timezone +from datetime import datetime, timezone, timedelta import pytest from ...constants import * # NOQA -from ...archiver.prune_cmd import prune_split +from ...archiver.prune_cmd import prune_split, prune_within from . import cmd, RK_ENCRYPTION, src_dir, generate_archiver_tests +from ...helpers import interval pytest_generate_tests = lambda metafunc: generate_archiver_tests(metafunc, kinds="local,remote,binary") # NOQA @@ -282,6 +283,38 @@ class MockArchive: local_tz = datetime.now(tz=timezone.utc).astimezone(tz=None).tzinfo +def test_prune_within(): + def subset(lst, indices): + return {lst[i] for i in indices} + + def dotest(test_archives, within, indices): + for ta in test_archives, reversed(test_archives): + kept_because = {} + keep = prune_within(ta, interval(within), kept_because) + assert set(keep) == subset(test_archives, indices) + assert all("within" == kept_because[a.id][0] for a in keep) + + # 1 minute, 1.5 hours, 2.5 hours, 3.5 hours, 25 hours, 49 hours + test_offsets = [60, 90 * 60, 150 * 60, 210 * 60, 25 * 60 * 60, 49 * 60 * 60] + now = datetime.now(timezone.utc) + test_dates = [now - timedelta(seconds=s) for s in test_offsets] + test_archives = [MockArchive(date, i) for i, date in enumerate(test_dates)] + + dotest(test_archives, "15S", []) + dotest(test_archives, "2M", [0]) + dotest(test_archives, "1H", [0]) + dotest(test_archives, "2H", [0, 1]) + dotest(test_archives, "3H", [0, 1, 2]) + dotest(test_archives, "24H", [0, 1, 2, 3]) + dotest(test_archives, "26H", [0, 1, 2, 3, 4]) + dotest(test_archives, "2d", [0, 1, 2, 3, 4]) + dotest(test_archives, "50H", [0, 1, 2, 3, 4, 5]) + dotest(test_archives, "3d", [0, 1, 2, 3, 4, 5]) + dotest(test_archives, "1w", [0, 1, 2, 3, 4, 5]) + dotest(test_archives, "1m", [0, 1, 2, 3, 4, 5]) + dotest(test_archives, "1y", [0, 1, 2, 3, 4, 5]) + + @pytest.mark.parametrize( "rule,num_to_keep,expected_ids", [ diff --git a/src/borg/testsuite/helpers/fs_test.py b/src/borg/testsuite/helpers/fs_test.py index c11c28f39..eb5b63973 100644 --- a/src/borg/testsuite/helpers/fs_test.py +++ b/src/borg/testsuite/helpers/fs_test.py @@ -270,7 +270,16 @@ def test_safe_unlink_is_safe_ENOSPC(tmpdir, monkeypatch): @pytest.mark.parametrize( "original_path, expected_path", - [("foo", "foo"), ("foo/bar", "foo/bar"), ("/foo/bar", "foo/bar"), ("../foo/bar", "foo/bar")], + [ + (".", "."), + ("..", "."), + ("/", "."), + ("//", "."), + ("foo", "foo"), + ("foo/bar", "foo/bar"), + ("/foo/bar", "foo/bar"), + ("../foo/bar", "foo/bar"), + ], ) def test_remove_dotdot_prefixes(original_path, expected_path): assert remove_dotdot_prefixes(original_path) == expected_path diff --git a/src/borg/testsuite/helpers/parseformat_test.py b/src/borg/testsuite/helpers/parseformat_test.py index 2a62ca160..d7cd002d0 100644 --- a/src/borg/testsuite/helpers/parseformat_test.py +++ b/src/borg/testsuite/helpers/parseformat_test.py @@ -281,21 +281,9 @@ class TestLocationWithoutEnv: @pytest.mark.parametrize( "name", [ - "foo", - "foo bar", - "foo_bar", - "foo-bar", - "foo.bar", - "foo[bar]", - "foo@2020-01-01T12:34:56", - "foo{now}", - "foo{now:%Y-%m-%d}", - "foo{hostname}", - "foo{hostname}-{now}", - "foo{hostname}-{now:%Y-%m-%d}", - "foo{hostname}-{now:%Y-%m-%d}@{now:%H:%M:%S}", - "foo{hostname}-{now:%Y-%m-%d}@{now:%H:%M:%S}", - "foo{hostname}-{now:%Y-%m-%d}@{now:%H:%M:%S}", + "foobar", + # placeholders + "foobar-{now}", ], ) def test_archivename_ok(name): @@ -305,31 +293,25 @@ def test_archivename_ok(name): @pytest.mark.parametrize( "name", [ - "", # empty name - " ", # just a space - " foo", # leading space - "foo ", # trailing space - "foo/bar", # / not allowed - "foo\\bar", # \ not allowed - "foo\nbar", # \n not allowed - "foo\rbar", # \r not allowed - "foo\tbar", # \t not allowed - "foo\0bar", # \0 not allowed - "foo\x01bar", # \x01 not allowed - "foo\x02bar", # \x02 not allowed - "foo\x03bar", # \x03 not allowed - "foo\x04bar", # \x04 not allowed - "foo\x05bar", # \x05 not allowed - "foo\x06bar", # \x06 not allowed - "foo\x07bar", # \x07 not allowed - "foo\x08bar", # \x08 not allowed - "foo\x09bar", # \x09 not allowed - "foo\x0abar", # \x0a not allowed - "foo\x0bbar", # \x0b not allowed - "foo\x0cbar", # \x0c not allowed - "foo\x0dbar", # \x0d not allowed - "foo\x0ebar", # \x0e not allowed - "foo\x0fbar", # \x0f not allowed + "", # too short + "x" * 201, # too long + # invalid chars: + "foo/bar", + "foo\\bar", + ">foo", + "