]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
Replace deprecated datetime.utcnow() calls 1977/head
authorZack Cerza <zack@redhat.com>
Fri, 12 Jul 2024 23:29:50 +0000 (17:29 -0600)
committerZack Cerza <zack@redhat.com>
Tue, 23 Jul 2024 21:20:03 +0000 (15:20 -0600)
See https://blog.miguelgrinberg.com/post/it-s-time-for-a-change-datetime-utcnow-is-now-deprecated

Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/dispatcher/__init__.py
teuthology/dispatcher/supervisor.py
teuthology/provision/cloud/util.py
teuthology/provision/fog.py
teuthology/provision/test/test_fog.py
teuthology/task/ssh_keys.py
teuthology/test/test_worker.py
teuthology/worker.py

index dee4f55fcadcc4490b4191a7b0247815cc7c2275..fe309731d58d875a1eae78a62e2763138b9f1c63 100644 (file)
@@ -1,3 +1,4 @@
+import datetime
 import logging
 import os
 import psutil
@@ -5,7 +6,6 @@ import subprocess
 import sys
 import yaml
 
-from datetime import datetime
 from typing import Dict, List
 
 from teuthology import (
@@ -26,7 +26,7 @@ from teuthology.lock import ops as lock_ops
 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'
 
@@ -34,7 +34,10 @@ 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
 
 
index 5806bb5473718525e43611e3deba39808439066a..c003a4e6204bfe084ed555ad0b853368694c43fd 100644 (file)
@@ -1,3 +1,4 @@
+import datetime
 import logging
 import os
 import subprocess
@@ -6,7 +7,6 @@ import yaml
 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
@@ -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
@@ -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
index a6f137e941a42bdee092db2e35783cd3993c274e..03ea7796f220d7a4cd4e644401e36e8686677057 100644 (file)
@@ -1,5 +1,4 @@
 import datetime
-import dateutil.tz
 import dateutil.parser
 import json
 import os
@@ -103,7 +102,7 @@ class AuthToken(object):
     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)
 
index 63d53cba3fa9513cbc020d89cde831262b520a49..12914f2960aa13998facce66da9b42565e17c5ff 100644 (file)
@@ -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
 
@@ -227,8 +227,8 @@ class FOG(object):
         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.
index a61c172abe6fbc75121a19e52c88bda114ad4866..3d0baa752c86259ddaa78570145e021835976482 100644 (file)
@@ -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
 
@@ -216,8 +217,10 @@ class TestFOG(object):
         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']\
index f7e0dba32c57d71497cd5def7d9ebe9de0a15dba..9c899e8fa9ac825c6a5bba50a6542f431c17c7b9 100644 (file)
@@ -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
@@ -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):
index 1fe9d57b00bd040cc4aff7f1fe10b63f09a902bc..4e4e2f5512e7d0350edf1021ff4e44516fe0ed53 100644 (file)
@@ -1,7 +1,7 @@
+import datetime
 import os
 
 from unittest.mock import patch, Mock, MagicMock
-from datetime import datetime, timedelta
 
 from teuthology import worker
 
@@ -24,21 +24,19 @@ class TestWorker(object):
 
     @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):
index 2e5a57c543e932e0239bddbd91accb728f3aae36..b11f887f46e0266c66e810afdcef86b5938a105f 100644 (file)
@@ -1,3 +1,4 @@
+import datetime
 import logging
 import os
 import subprocess
@@ -6,8 +7,6 @@ import tempfile
 import time
 import yaml
 
-from datetime import datetime
-
 from teuthology import (
     # non-modules
     setup_log_file,
@@ -24,7 +23,7 @@ from teuthology.config import set_config_attr
 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'
 
@@ -32,7 +31,10 @@ 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:
@@ -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
@@ -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(