Skip to content

Commit e6b668f

Browse files
committed
Removes unnecessarily complicated local timezone in test
Default with tzinfo=None is local timezone anyway, no need to set it manually.
1 parent 1b18962 commit e6b668f

1 file changed

Lines changed: 22 additions & 32 deletions

File tree

src/borg/testsuite/archiver/prune_cmd_test.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,14 @@ def test_prune_ignore_protected(archivers, request):
266266

267267
class MockArchive:
268268
def __init__(self, ts, id):
269-
self.ts = ts
269+
# Real archive objects have UTC zoned timestamps
270+
self.ts = ts.replace(tzinfo=timezone.utc)
270271
self.id = id
271272

272273
def __repr__(self):
273274
return f"{self.id}: {self.ts.isoformat()}"
274275

275276

276-
# This is the local timezone of the system running the tests.
277-
# We need this e.g. to construct archive timestamps for the prune tests,
278-
# because borg prune operates in the local timezone (it first converts the
279-
# archive timestamp to the local timezone). So, if we want the y/m/d/h/m/s
280-
# values which prune uses to be exactly the ones we give [and NOT shift them
281-
# by tzoffset], we need to give the timestamps in the same local timezone.
282-
# Please note that the timestamps in a real borg archive or manifest are
283-
# stored in UTC timezone.
284-
local_tz = datetime.now(tz=timezone.utc).astimezone(tz=None).tzinfo
285-
286-
287277
@pytest.mark.parametrize(
288278
"rule,num_to_keep,expected_ids",
289279
[
@@ -303,23 +293,23 @@ def subset(lst, ids):
303293

304294
archives = [
305295
# years apart
306-
MockArchive(datetime(2015, 1, 1, 10, 0, 0, tzinfo=local_tz), 1),
307-
MockArchive(datetime(2016, 1, 1, 10, 0, 0, tzinfo=local_tz), 2),
308-
MockArchive(datetime(2017, 1, 1, 10, 0, 0, tzinfo=local_tz), 3),
296+
MockArchive(datetime(2015, 1, 1, 10, 0, 0), 1),
297+
MockArchive(datetime(2016, 1, 1, 10, 0, 0), 2),
298+
MockArchive(datetime(2017, 1, 1, 10, 0, 0), 3),
309299
# months apart
310-
MockArchive(datetime(2017, 2, 1, 10, 0, 0, tzinfo=local_tz), 4),
311-
MockArchive(datetime(2017, 3, 1, 10, 0, 0, tzinfo=local_tz), 5),
300+
MockArchive(datetime(2017, 2, 1, 10, 0, 0), 4),
301+
MockArchive(datetime(2017, 3, 1, 10, 0, 0), 5),
312302
# days apart
313-
MockArchive(datetime(2017, 3, 2, 10, 0, 0, tzinfo=local_tz), 6),
314-
MockArchive(datetime(2017, 3, 3, 10, 0, 0, tzinfo=local_tz), 7),
315-
MockArchive(datetime(2017, 3, 4, 10, 0, 0, tzinfo=local_tz), 8),
303+
MockArchive(datetime(2017, 3, 2, 10, 0, 0), 6),
304+
MockArchive(datetime(2017, 3, 3, 10, 0, 0), 7),
305+
MockArchive(datetime(2017, 3, 4, 10, 0, 0), 8),
316306
# minutes apart
317-
MockArchive(datetime(2017, 10, 1, 9, 45, 0, tzinfo=local_tz), 9),
318-
MockArchive(datetime(2017, 10, 1, 9, 55, 0, tzinfo=local_tz), 10),
307+
MockArchive(datetime(2017, 10, 1, 9, 45, 0), 9),
308+
MockArchive(datetime(2017, 10, 1, 9, 55, 0), 10),
319309
# seconds apart
320-
MockArchive(datetime(2017, 10, 1, 10, 0, 1, tzinfo=local_tz), 11),
321-
MockArchive(datetime(2017, 10, 1, 10, 0, 3, tzinfo=local_tz), 12),
322-
MockArchive(datetime(2017, 10, 1, 10, 0, 5, tzinfo=local_tz), 13),
310+
MockArchive(datetime(2017, 10, 1, 10, 0, 1), 11),
311+
MockArchive(datetime(2017, 10, 1, 10, 0, 3), 12),
312+
MockArchive(datetime(2017, 10, 1, 10, 0, 5), 13),
323313
]
324314
kept_because = {}
325315
keep = prune_split(archives, rule, num_to_keep, None, kept_because)
@@ -335,17 +325,17 @@ def subset(lst, ids):
335325

336326
archives = [
337327
# oldest backup, but not last in its year
338-
MockArchive(datetime(2018, 1, 1, 10, 0, 0, tzinfo=local_tz), 1),
328+
MockArchive(datetime(2018, 1, 1, 10, 0, 0), 1),
339329
# an interim backup
340-
MockArchive(datetime(2018, 12, 30, 10, 0, 0, tzinfo=local_tz), 2),
330+
MockArchive(datetime(2018, 12, 30, 10, 0, 0), 2),
341331
# year-end backups
342-
MockArchive(datetime(2018, 12, 31, 10, 0, 0, tzinfo=local_tz), 3),
343-
MockArchive(datetime(2019, 12, 31, 10, 0, 0, tzinfo=local_tz), 4),
332+
MockArchive(datetime(2018, 12, 31, 10, 0, 0), 3),
333+
MockArchive(datetime(2019, 12, 31, 10, 0, 0), 4),
344334
]
345335

346336
# Keep oldest when retention target can't otherwise be met
347337
kept_because = {}
348-
keep = prune_split(archives, "yearly", 3, kept_because)
338+
keep = prune_split(archives, "yearly", 3, None, kept_because)
349339

350340
assert set(keep) == subset(archives, [1, 3, 4])
351341
assert kept_because[1][0] == "yearly[oldest]"
@@ -354,7 +344,7 @@ def subset(lst, ids):
354344

355345
# Otherwise, prune it
356346
kept_because = {}
357-
keep = prune_split(archives, "yearly", 2, kept_because)
347+
keep = prune_split(archives, "yearly", 2, None, kept_because)
358348

359349
assert set(keep) == subset(archives, [3, 4])
360350
assert kept_because[3][0] == "yearly"
@@ -365,7 +355,7 @@ def test_prune_split_no_archives():
365355
archives = []
366356

367357
kept_because = {}
368-
keep = prune_split(archives, "yearly", 3, kept_because)
358+
keep = prune_split(archives, "yearly", 3, None, kept_because)
369359

370360
assert keep == []
371361
assert kept_because == {}

0 commit comments

Comments
 (0)