Merge pull request #1981 from ogayot/utcnow

Replace deprecated uses of datetime.datetime.utcnow
This commit is contained in:
Olivier Gayot 2024-04-26 15:58:47 +02:00 committed by GitHub
commit 65f8ef50d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 8 deletions

View File

@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
import fnmatch import fnmatch
import json import json
import re import re
@ -264,11 +265,13 @@ class TestSubiquityModel(unittest.IsolatedAsyncioTestCase):
self.assertEqual(expected_error, str(ctx.exception)) self.assertEqual(expected_error, str(ctx.exception))
@mock.patch("subiquity.models.subiquity.lsb_release") @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( 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( main_user = IdentityData(
username="mainuser", crypted_password="sample_pass", hostname="somehost" username="mainuser", crypted_password="sample_pass", hostname="somehost"
) )
@ -296,7 +299,7 @@ grub_dpkg:
# NETPLAN_CONFIG_ROOT_READ_ONLY is True # NETPLAN_CONFIG_ROOT_READ_ONLY is True
"/etc/cloud/cloud.cfg.d/90-installer-network.cfg" "/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( with self.subTest(
"Stable releases Jammy do not disable cloud-init." "Stable releases Jammy do not disable cloud-init."
" NETPLAN_ROOT_READ_ONLY=True uses cloud-init networking" " NETPLAN_ROOT_READ_ONLY=True uses cloud-init networking"

View File

@ -17,6 +17,7 @@ helper. """
import asyncio import asyncio
import contextlib import contextlib
import datetime
import json import json
import logging import logging
import os import os
@ -328,10 +329,10 @@ class UAInterface:
info = await self.get_subscription_status(token) info = await self.get_subscription_status(token)
# Sometimes, a time zone offset of 0 is replaced by the letter Z. This # Sometimes, a time zone offset of 0 is replaced by the letter Z. This
# is specified in RFC 3339 but not supported by fromisoformat. # is specified in RFC 3339 but not supported by fromisoformat before
# See https://bugs.python.org/issue35829 # Python 3.11. See https://bugs.python.org/issue35829
expiration = dt.fromisoformat(info["expires"].replace("Z", "+00:00")) 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"]) raise ExpiredTokenError(token, expires=info["expires"])
def is_activable_service(service: dict) -> bool: def is_activable_service(service: dict) -> bool:

View File

@ -70,7 +70,7 @@ def write_file(filename, content, **kwargs):
def generate_timestamped_header() -> str: 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" return f"# Autogenerated by Subiquity: {now} UTC\n"