From 5eaa4065e58f6ed55f3f2ab6fc34156e089ed5c7 Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Tue, 2 May 2023 16:50:07 +0200 Subject: Add autopkgtest test case for post-1970 symlink consistency Bug-Ubuntu: https://launchpad.net/bugs/2017999 Signed-off-by: Benjamin Drung --- debian/tests/python | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/debian/tests/python b/debian/tests/python index 4517f59..c6406af 100755 --- a/debian/tests/python +++ b/debian/tests/python @@ -7,11 +7,25 @@ import datetime import os import pathlib +import re import sys import typing import unittest import zoneinfo +ROOT_DIR = pathlib.Path(__file__).parent.parent.parent + + +def read_backwards_links(backwards_file: pathlib.Path) -> dict[str, str]: + """Read backwards compatibility links from the upstream backwards file.""" + backwards_links = {} + for line in backwards_file.read_text(encoding="utf-8").splitlines(): + match = re.match(r"^Link\t(?P\S+)\t+(?P\S+)", line) + if not match: + continue + backwards_links[match.group("link_name")] = match.group("target") + return backwards_links + class TestZoneinfo(unittest.TestCase): """Test timezones using Python's zoneinfo module.""" @@ -82,6 +96,37 @@ class TestZoneinfo(unittest.TestCase): oslo = zoneinfo.ZoneInfo("Europe/Oslo") self.assertEqual(self._hours(date.replace(tzinfo=oslo).utcoffset()), 2) + def _assert_equal_offset( + self, + date: datetime.datetime, + timezone1: zoneinfo.ZoneInfo, + timezone2: zoneinfo.ZoneInfo, + ) -> None: + date1 = date.replace(tzinfo=timezone1) + date2 = date.replace(tzinfo=timezone2) + self.assertEqual(self._hours(date2 - date1), 0) + + def test_post_1970_symlinks_consistency(self) -> None: + """Test that post-1970 symlinks are consistent with pre-1970 timezones. + + Building tzdata with PACKRATDATA=backzone will result in separate + time zones for time zones that differ only before 1970. These time + zones should behave identical after 1970. Building tzdata without + PACKRATDATA=backzone will result in one of the time zones become a + symlink to the other time zone. + """ + links = read_backwards_links(ROOT_DIR / "backward") + # Enderbury is not inhabited any more. See https://launchpad.net/bugs/2017982 + links.pop("Pacific/Enderbury") + for link_name, target in links.items(): + with self.subTest(f"{link_name} -> {target}"): + tz_link = zoneinfo.ZoneInfo(link_name) + tz_target = zoneinfo.ZoneInfo(target) + now = datetime.datetime.now() + self._assert_equal_offset(now, tz_link, tz_target) + future = now + datetime.timedelta(days=30 * 6) + self._assert_equal_offset(future, tz_link, tz_target) + def test_timezones(self) -> None: """Test all zones to load, have a name, and have a reasonable offset.""" for zone in zoneinfo.available_timezones(): -- 2.39.2