diff --git a/subiquity/models/tests/test_subiquity.py b/subiquity/models/tests/test_subiquity.py index 042ae3dd..b2ded689 100644 --- a/subiquity/models/tests/test_subiquity.py +++ b/subiquity/models/tests/test_subiquity.py @@ -13,6 +13,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import datetime import fnmatch import json import re @@ -264,11 +265,13 @@ class TestSubiquityModel(unittest.IsolatedAsyncioTestCase): self.assertEqual(expected_error, str(ctx.exception)) @mock.patch("subiquity.models.subiquity.lsb_release") - @mock.patch("subiquitycore.file_util.datetime.datetime") + @mock.patch("subiquitycore.file_util.datetime.datetime", wraps=datetime.datetime) def test_cloud_init_files_emits_datasource_config_and_clean_script( - self, datetime, lsb_release + self, m_datetime, lsb_release ): - datetime.utcnow.return_value = "2004-03-05 ..." + m_datetime.now.return_value = datetime.datetime( + 2004, 3, 5, tzinfo=datetime.timezone.utc + ) main_user = IdentityData( username="mainuser", crypted_password="sample_pass", hostname="somehost" ) @@ -296,7 +299,7 @@ grub_dpkg: # NETPLAN_CONFIG_ROOT_READ_ONLY is True "/etc/cloud/cloud.cfg.d/90-installer-network.cfg" ) - header = "# Autogenerated by Subiquity: 2004-03-05 ... UTC\n" + header = "# Autogenerated by Subiquity: 2004-03-05 00:00:00 UTC\n" with self.subTest( "Stable releases Jammy do not disable cloud-init." " NETPLAN_ROOT_READ_ONLY=True uses cloud-init networking" diff --git a/subiquity/server/ubuntu_advantage.py b/subiquity/server/ubuntu_advantage.py index 48abc8fd..640d4637 100644 --- a/subiquity/server/ubuntu_advantage.py +++ b/subiquity/server/ubuntu_advantage.py @@ -17,6 +17,7 @@ helper. """ import asyncio import contextlib +import datetime import json import logging import os @@ -328,10 +329,10 @@ class UAInterface: info = await self.get_subscription_status(token) # Sometimes, a time zone offset of 0 is replaced by the letter Z. This - # is specified in RFC 3339 but not supported by fromisoformat. - # See https://bugs.python.org/issue35829 + # is specified in RFC 3339 but not supported by fromisoformat before + # Python 3.11. See https://bugs.python.org/issue35829 expiration = dt.fromisoformat(info["expires"].replace("Z", "+00:00")) - if expiration.timestamp() <= dt.utcnow().timestamp(): + if expiration <= dt.now(datetime.timezone.utc): raise ExpiredTokenError(token, expires=info["expires"]) def is_activable_service(service: dict) -> bool: diff --git a/subiquitycore/file_util.py b/subiquitycore/file_util.py index c1e48e50..f31509d1 100644 --- a/subiquitycore/file_util.py +++ b/subiquitycore/file_util.py @@ -70,7 +70,7 @@ def write_file(filename, content, **kwargs): def generate_timestamped_header() -> str: - now = datetime.datetime.utcnow() + now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) return f"# Autogenerated by Subiquity: {now} UTC\n"