Excise use of non-timezone aware utcnow()

Replace all instances of `datetime.datetime.utcnow()`,
which is deprecated, with the timezone-aware oslo's
`timeutils.utcnow()` method, across the Ironic project.

Closes-Bug: #2067740
Change-Id: I998681c14f945846f58e723b9be2202dbe8ea12c
This commit is contained in:
cid 2024-06-02 05:43:42 -05:00
parent 2f41bf1a0d
commit b5b7e60c47
10 changed files with 40 additions and 28 deletions

View File

@ -12,12 +12,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import json
import openstack
from openstack.connection import exceptions as openstack_exc
from oslo_log import log
from oslo_utils import timeutils
from ironic.common import context as ironic_context
from ironic.common import exception
@ -164,7 +164,7 @@ def _create_metadata_dictionary(node, action):
"""
label = "ironic_node_%s" % node.uuid
data = {'instance_uuid': node.instance_uuid or node.uuid,
'last_seen': datetime.datetime.utcnow().isoformat(),
'last_seen': timeutils.utcnow().isoformat(),
'last_action': action}
return {label: json.dumps(data)}

View File

@ -12,10 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import jsonschema
from jsonschema import exceptions as json_schema_exc
from oslo_utils import timeutils
from ironic.common import exception
from ironic.common.i18n import _
@ -113,7 +112,7 @@ def update_raid_info(node, raid_config):
one root volume or if node.properties['capabilities'] is malformed.
"""
current = raid_config.copy()
current['last_updated'] = str(datetime.datetime.utcnow())
current['last_updated'] = str(timeutils.utcnow())
node.raid_config = current
# Current RAID configuration can have 0 or 1 root volumes. If there

View File

@ -41,7 +41,6 @@ notifying Neutron of a change, etc.
"""
import collections
import datetime
import queue
import eventlet
@ -50,6 +49,7 @@ from ironic_lib import metrics_utils
from oslo_log import log
import oslo_messaging as messaging
from oslo_utils import excutils
from oslo_utils import timeutils
from oslo_utils import uuidutils
from ironic.common import boot_devices
@ -2602,7 +2602,7 @@ class ConductorManager(base_manager.BaseConductorManager):
message = {'message_id': uuidutils.generate_uuid(),
'instance_uuid': instance_uuid,
'node_uuid': node_uuid,
'timestamp': datetime.datetime.utcnow()}
'timestamp': timeutils.utcnow()}
try:
lock_purpose = 'getting sensors data'
@ -2672,7 +2672,7 @@ class ConductorManager(base_manager.BaseConductorManager):
# populate the message which will be sent to ceilometer
# or other data consumer
message = {'message_id': uuidutils.generate_uuid(),
'timestamp': datetime.datetime.utcnow(),
'timestamp': timeutils.utcnow(),
'hostname': self.host}
try:

View File

@ -19,6 +19,8 @@ import datetime
import hashlib
import json
from oslo_utils import timeutils
from ironic.api.controllers.v1 import allocation as al_controller
from ironic.api.controllers.v1 import chassis as chassis_controller
from ironic.api.controllers.v1 import deploy_template as dt_controller
@ -82,7 +84,7 @@ class FakeMemcache(object):
self.token_expiration = None
def get(self, key):
dt = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
dt = timeutils.utcnow() + datetime.timedelta(minutes=5)
return json.dumps((self._cache.get(key), dt.isoformat()))
def set(self, key, value, time=0, min_compress_len=0):

View File

@ -11,13 +11,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import json
from unittest import mock
from keystoneauth1 import loading as ks_loading
import openstack
from openstack.connection import exceptions as openstack_exc
from oslo_utils import timeutils
from oslo_utils import uuidutils
from ironic.common import cinder
@ -144,12 +144,14 @@ class TestCinderUtils(db_base.DbTestCase):
self.assertIsNone(cinder._get_attachment_id(self.node, unattached))
self.assertIsNone(cinder._get_attachment_id(self.node, no_attachment))
@mock.patch.object(datetime, 'datetime', autospec=True)
def test__create_metadata_dictionary(self, mock_datetime):
@mock.patch.object(timeutils, 'utcnow', autospec=True)
def test__create_metadata_dictionary(self, mock_utcnow):
fake_time = '2017-06-05T00:33:26.574676'
mock_utcnow = mock.Mock()
mock_datetime.utcnow.return_value = mock_utcnow
mock_utcnow.isoformat.return_value = fake_time
mock_datetime = mock.Mock()
mock_datetime.isoformat.return_value = fake_time
mock_utcnow.return_value = mock_datetime
expected_key = ("ironic_node_%s" % self.node.uuid)
expected_data = {
'instance_uuid': self.node.instance_uuid,

View File

@ -235,7 +235,8 @@ class DbConductorTestCase(base.DbTestCase):
h = 'fake-host'
expected = {}
mock_utcnow.return_value = datetime.datetime.utcnow()
mock_utcnow.return_value = datetime.datetime.now(
datetime.timezone.utc).replace(tzinfo=None)
self._create_test_cdr(hostname=h, drivers=[], hardware_types=[])
result = self.dbapi.get_active_hardware_type_dict()
self.assertEqual(expected, result)
@ -246,7 +247,8 @@ class DbConductorTestCase(base.DbTestCase):
ht = 'hardware-type'
expected = {ht: {h}}
mock_utcnow.return_value = datetime.datetime.utcnow()
mock_utcnow.return_value = datetime.datetime.now(
datetime.timezone.utc).replace(tzinfo=None)
self._create_test_cdr(hostname=h, drivers=[], hardware_types=[ht])
result = self.dbapi.get_active_hardware_type_dict()
self.assertEqual(expected, result)
@ -260,7 +262,8 @@ class DbConductorTestCase(base.DbTestCase):
key = '%s:%s' % (group, ht)
expected = {key: {h}}
mock_utcnow.return_value = datetime.datetime.utcnow()
mock_utcnow.return_value = datetime.datetime.now(
datetime.timezone.utc).replace(tzinfo=None)
self._create_test_cdr(hostname=h, drivers=[], hardware_types=[ht],
conductor_group=group)
result = self.dbapi.get_active_hardware_type_dict(use_groups=True)
@ -273,7 +276,8 @@ class DbConductorTestCase(base.DbTestCase):
ht2 = 'another-hardware-type'
expected = {ht1: {h}, ht2: {h}}
mock_utcnow.return_value = datetime.datetime.utcnow()
mock_utcnow.return_value = datetime.datetime.now(
datetime.timezone.utc).replace(tzinfo=None)
self._create_test_cdr(hostname=h, drivers=[],
hardware_types=[ht1, ht2])
result = self.dbapi.get_active_hardware_type_dict()
@ -286,7 +290,8 @@ class DbConductorTestCase(base.DbTestCase):
ht = 'hardware-type'
expected = {ht: {h1, h2}}
mock_utcnow.return_value = datetime.datetime.utcnow()
mock_utcnow.return_value = datetime.datetime.now(
datetime.timezone.utc).replace(tzinfo=None)
self._create_test_cdr(id=1, hostname=h1, drivers=[],
hardware_types=[ht])
self._create_test_cdr(id=2, hostname=h2, drivers=[],
@ -303,7 +308,8 @@ class DbConductorTestCase(base.DbTestCase):
ht2 = 'another-hardware-type'
expected = {ht1: {h1, h2}, ht2: {h1, h2}}
mock_utcnow.return_value = datetime.datetime.utcnow()
mock_utcnow.return_value = datetime.datetime.now(
datetime.timezone.utc).replace(tzinfo=None)
self._create_test_cdr(id=1, hostname=h1, drivers=[],
hardware_types=[ht1, ht2])
self._create_test_cdr(id=2, hostname=h2, drivers=[],

View File

@ -14,6 +14,7 @@
import datetime
from unittest import mock
from oslo_utils import timeutils
import sushy
from ironic.common import exception
@ -314,7 +315,7 @@ class RedfishFirmwareTestCase(db_base.DbTestCase):
bmc_component = {'component': 'bmc', 'url': 'https://bmc/v1.0.1'}
bios_component = {'component': 'bios', 'url': 'https://bios/v1.0.1'}
if add_wait:
wait_start_time = datetime.datetime.utcnow() -\
wait_start_time = timeutils.utcnow() -\
datetime.timedelta(minutes=1)
bmc_component['wait_start_time'] = wait_start_time.isoformat()
bios_component['wait_start_time'] = wait_start_time.isoformat()

View File

@ -16,6 +16,7 @@
import datetime
from unittest import mock
from oslo_utils import timeutils
from oslo_utils import units
import sushy
@ -1281,8 +1282,7 @@ class RedfishManagementTestCase(db_base.DbTestCase):
mock_update_service = mock.Mock()
mock_get_update_service.return_value = mock_update_service
wait_start_time = datetime.datetime.utcnow() -\
datetime.timedelta(minutes=15)
wait_start_time = timeutils.utcnow() - datetime.timedelta(minutes=15)
driver_internal_info = {
'firmware_updates': [
{'task_monitor': '/task/123',
@ -1312,8 +1312,7 @@ class RedfishManagementTestCase(db_base.DbTestCase):
mock_update_service = mock.Mock()
mock_get_update_service.return_value = mock_update_service
wait_start_time = datetime.datetime.utcnow() -\
datetime.timedelta(minutes=1)
wait_start_time = timeutils.utcnow() - datetime.timedelta(minutes=1)
driver_internal_info = {
'firmware_updates': [
{'task_monitor': '/task/123',

View File

@ -13,6 +13,8 @@
import datetime
from unittest import mock
from oslo_utils import timeutils
from ironic.common import exception
from ironic.common import states
from ironic.conductor import task_manager
@ -30,7 +32,7 @@ class AgentPowerTest(db_base.DbTestCase):
self.config(fast_track=True, group='deploy')
self.power = agent_power.AgentPower()
dii = {
'agent_last_heartbeat': datetime.datetime.utcnow().strftime(
'agent_last_heartbeat': timeutils.utcnow().strftime(
"%Y-%m-%dT%H:%M:%S.%f"),
'deployment_reboot': True,
'agent_url': 'http://url',

View File

@ -23,6 +23,7 @@ import time
from unittest import mock
import uuid
from oslo_utils import timeutils
from oslo_utils import uuidutils
from ironic.common import exception
@ -336,7 +337,7 @@ class TestUpdateImages(BaseTest):
touch(self.master_path)
href = 'http://awesomefreeimages.al/img999'
self.img_info = {
'updated_at': datetime.datetime((datetime.datetime.utcnow().year
'updated_at': datetime.datetime((timeutils.utcnow().year
+ 1), 11, 15, 8, 12, 31)
}
res = image_cache._delete_master_path_if_stale(self.master_path, href,