Skip to content

Commit

Permalink
Merge pull request canonical#1981 from ogayot/utcnow
Browse files Browse the repository at this point in the history
Replace deprecated uses of datetime.datetime.utcnow
  • Loading branch information
ogayot authored Apr 26, 2024
2 parents 1d7c7c0 + c18d434 commit 65f8ef5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
11 changes: 7 additions & 4 deletions subiquity/models/tests/test_subiquity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# 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/>.

import datetime
import fnmatch
import json
import re
Expand Down Expand Up @@ -264,11 +265,13 @@ def test_cloud_init_user_list_merge(self, run_cmd):
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"
)
Expand Down Expand Up @@ -296,7 +299,7 @@ def test_cloud_init_files_emits_datasource_config_and_clean_script(
# 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"
Expand Down
7 changes: 4 additions & 3 deletions subiquity/server/ubuntu_advantage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import asyncio
import contextlib
import datetime
import json
import logging
import os
Expand Down Expand Up @@ -328,10 +329,10 @@ async def get_subscription(self, token: str) -> UbuntuProSubscription:
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:
Expand Down
2 changes: 1 addition & 1 deletion subiquitycore/file_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down

0 comments on commit 65f8ef5

Please sign in to comment.