Skip to content

Commit

Permalink
Merge pull request #1977 from ceph/utcnow
Browse files Browse the repository at this point in the history
Replace deprecated datetime.utcnow() calls
  • Loading branch information
zmc authored Jul 24, 2024
2 parents a8aed60 + 47c4eff commit 55b2d2b
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
python: "3.10"
- os: ubuntu-22.04
python: "3.11"
- os: ubuntu-24.04
python: "3.12"
steps:
- uses: actions/checkout@v2
- name: Add deadsnakes PPA
Expand Down
9 changes: 6 additions & 3 deletions teuthology/dispatcher/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import datetime
import logging
import os
import psutil
import subprocess
import sys
import yaml

from datetime import datetime
from typing import Dict, List

from teuthology import (
Expand All @@ -26,15 +26,18 @@
from teuthology import safepath

log = logging.getLogger(__name__)
start_time = datetime.utcnow()
start_time = datetime.datetime.now(datetime.timezone.utc)
restart_file_path = '/tmp/teuthology-restart-dispatcher'
stop_file_path = '/tmp/teuthology-stop-dispatcher'


def sentinel(path):
if not os.path.exists(path):
return False
file_mtime = datetime.utcfromtimestamp(os.path.getmtime(path))
file_mtime = datetime.datetime.fromtimestamp(
os.path.getmtime(path),
datetime.timezone.utc,
)
return file_mtime > start_time


Expand Down
6 changes: 3 additions & 3 deletions teuthology/dispatcher/supervisor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import logging
import os
import subprocess
Expand All @@ -6,7 +7,6 @@
import requests

from urllib.parse import urljoin
from datetime import datetime

from teuthology import exporter, kill, report, safepath
from teuthology.config import config as teuth_config
Expand Down Expand Up @@ -275,7 +275,7 @@ def unlock_targets(job_config):


def run_with_watchdog(process, job_config):
job_start_time = datetime.utcnow()
job_start_time = datetime.datetime.now(datetime.timezone.utc)

# Only push the information that's relevant to the watchdog, to save db
# load
Expand All @@ -289,7 +289,7 @@ def run_with_watchdog(process, job_config):
hit_max_timeout = False
while process.poll() is None:
# Kill jobs that have been running longer than the global max
run_time = datetime.utcnow() - job_start_time
run_time = datetime.datetime.now(datetime.timezone.utc) - job_start_time
total_seconds = run_time.days * 60 * 60 * 24 + run_time.seconds
if total_seconds > teuth_config.max_job_time:
hit_max_timeout = True
Expand Down
3 changes: 1 addition & 2 deletions teuthology/provision/cloud/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import dateutil.tz
import dateutil.parser
import json
import os
Expand Down Expand Up @@ -103,7 +102,7 @@ def write(self, value, expires, endpoint):
def expired(self):
if self.expires is None:
return True
utcnow = datetime.datetime.now(dateutil.tz.tzutc())
utcnow = datetime.datetime.now(datetime.timezone.utc)
offset = datetime.timedelta(minutes=30)
return self.expires < (utcnow + offset)

Expand Down
6 changes: 3 additions & 3 deletions teuthology/provision/fog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import datetime
import json
import logging
import requests
import socket
import re

from datetime import datetime
from paramiko import SSHException
from paramiko.ssh_exception import NoValidConnectionsError

Expand Down Expand Up @@ -227,8 +227,8 @@ def schedule_deploy_task(self, host_id):
for task in host_tasks:
timestamp = task['createdTime']
time_delta = (
datetime.utcnow() - datetime.strptime(
timestamp, self.timestamp_format)
datetime.datetime.now(datetime.timezone.utc) - datetime.datetime.strptime(
timestamp, self.timestamp_format).replace(tzinfo=datetime.timezone.utc)
).total_seconds()
# There should only be one deploy task matching our host. Just in
# case there are multiple, select a very recent one.
Expand Down
9 changes: 6 additions & 3 deletions teuthology/provision/test/test_fog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime

from copy import deepcopy
from datetime import datetime
from mock import patch, DEFAULT, PropertyMock
from pytest import raises, mark

Expand Down Expand Up @@ -216,8 +217,10 @@ def test_schedule_deploy_task(self):
tasktype_result = dict(tasktypes=[dict(name='deploy', id=tasktype_id)])
schedule_result = dict()
host_tasks = [dict(
createdTime=datetime.strftime(
datetime.utcnow(), self.klass.timestamp_format),
createdTime=datetime.datetime.strftime(
datetime.datetime.now(datetime.timezone.utc),
self.klass.timestamp_format
),
id=task_id,
)]
self.mocks['m_requests_Session_send']\
Expand Down
4 changes: 2 additions & 2 deletions teuthology/task/ssh_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
Ssh-key key handlers and associated routines
"""
import contextlib
import datetime
import logging
import paramiko
import re
from datetime import datetime

from io import StringIO
from teuthology import contextutil
Expand All @@ -21,7 +21,7 @@ def timestamp(format_='%Y-%m-%d_%H:%M:%S:%f'):
"""
Return a UTC timestamp suitable for use in filenames
"""
return datetime.utcnow().strftime(format_)
return datetime.datetime.now(datetime.timezone.utc).strftime(format_)


def backup_file(remote, path, sudo=False):
Expand Down
20 changes: 9 additions & 11 deletions teuthology/test/test_worker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import os

from unittest.mock import patch, Mock, MagicMock
from datetime import datetime, timedelta

from teuthology import worker

Expand All @@ -24,21 +24,19 @@ def test_restart_file_path_doesnt_exist(self, m_exists):

@patch("os.path.getmtime")
@patch("os.path.exists")
@patch("teuthology.worker.datetime")
def test_needs_restart(self, m_datetime, m_exists, m_getmtime):
def test_needs_restart(self, m_exists, m_getmtime):
m_exists.return_value = True
m_datetime.utcfromtimestamp.return_value = datetime.utcnow() + timedelta(days=1)
result = worker.sentinel(worker.restart_file_path)
assert result
now = datetime.datetime.now(datetime.timezone.utc)
m_getmtime.return_value = (now + datetime.timedelta(days=1)).timestamp()
assert worker.sentinel(worker.restart_file_path)

@patch("os.path.getmtime")
@patch("os.path.exists")
@patch("teuthology.worker.datetime")
def test_does_not_need_restart(self, m_datetime, m_exists, getmtime):
def test_does_not_need_restart(self, m_exists, m_getmtime):
m_exists.return_value = True
m_datetime.utcfromtimestamp.return_value = datetime.utcnow() - timedelta(days=1)
result = worker.sentinel(worker.restart_file_path)
assert not result
now = datetime.datetime.now(datetime.timezone.utc)
m_getmtime.return_value = (now - datetime.timedelta(days=1)).timestamp()
assert not worker.sentinel(worker.restart_file_path)

@patch("os.symlink")
def test_symlink_success(self, m_symlink):
Expand Down
14 changes: 8 additions & 6 deletions teuthology/worker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import logging
import os
import subprocess
Expand All @@ -6,8 +7,6 @@
import time
import yaml

from datetime import datetime

from teuthology import (
# non-modules
setup_log_file,
Expand All @@ -24,15 +23,18 @@
from teuthology.exceptions import BranchNotFoundError, CommitNotFoundError, SkipJob, MaxWhileTries

log = logging.getLogger(__name__)
start_time = datetime.utcnow()
start_time = datetime.datetime.now(datetime.timezone.utc)
restart_file_path = '/tmp/teuthology-restart-workers'
stop_file_path = '/tmp/teuthology-stop-workers'


def sentinel(path):
if not os.path.exists(path):
return False
file_mtime = datetime.utcfromtimestamp(os.path.getmtime(path))
file_mtime = datetime.datetime.fromtimestamp(
os.path.getmtime(path),
datetime.timezone.utc,
)
if file_mtime > start_time:
return True
else:
Expand Down Expand Up @@ -325,7 +327,7 @@ def run_job(job_config, teuth_bin_path, archive_dir, verbose):


def run_with_watchdog(process, job_config):
job_start_time = datetime.utcnow()
job_start_time = datetime.datetime.now(datetime.timezone.utc)

# Only push the information that's relevant to the watchdog, to save db
# load
Expand All @@ -339,7 +341,7 @@ def run_with_watchdog(process, job_config):
symlink_worker_log(job_config['worker_log'], job_config['archive_path'])
while process.poll() is None:
# Kill jobs that have been running longer than the global max
run_time = datetime.utcnow() - job_start_time
run_time = datetime.datetime.now(datetime.timezone.utc) - job_start_time
total_seconds = run_time.days * 60 * 60 * 24 + run_time.seconds
if total_seconds > teuth_config.max_job_time:
log.warning("Job ran longer than {max}s. Killing...".format(
Expand Down

0 comments on commit 55b2d2b

Please sign in to comment.