fix tests so that they are as before the splitup

This commit is contained in:
Thomas Waldmann 2025-05-21 22:20:12 +02:00
parent 5545af1fd0
commit 881eaca08a
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
3 changed files with 74 additions and 57 deletions

View file

@ -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",
[

View file

@ -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

View file

@ -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",
"<foo",
"|foo",
'foo"bar',
"foo?",
"*bar",
"foo\nbar",
"foo\0bar",
# leading/trailing blanks
" foo",
"bar ",
# contains surrogate-escapes
"foo\udc80bar",
"foo\udcffbar",
],
)
def test_archivename_invalid(name):
@ -337,7 +319,7 @@ def test_archivename_invalid(name):
archivename_validator(name)
@pytest.mark.parametrize("text", ["foo", "bar", "baz"])
@pytest.mark.parametrize("text", ["", "single line", "multi\nline\ncomment"])
def test_text_ok(text):
assert text_validator(name="text", max_length=100)(text) == text
@ -345,19 +327,12 @@ def test_text_ok(text):
@pytest.mark.parametrize(
"text",
[
"", # empty
"foo\0bar", # contains null byte
"foo\nbar", # contains newline
"foo\rbar", # contains carriage return
"foo\tbar", # contains tab
"foo\x01bar", # contains control character
"foo\x02bar", # contains control character
"foo\x03bar", # contains control character
"foo\x04bar", # contains control character
"foo\x05bar", # contains control character
"foo\x06bar", # contains control character
"foo\x07bar", # contains control character
"foo\x08bar", # contains control character
"x" * 101, # too long
# invalid chars:
"foo\0bar",
# contains surrogate-escapes
"foo\udc80bar",
"foo\udcffbar",
],
)
def test_text_invalid(text):