python-3.12: do not use datetime.datetime.utcnow()

This is deprecated in the favor of:
oslo_utils.timeutils.utcnow()

Change-Id: Ic7304aea55258822b0be59ce45c6686182f4ecd0
This commit is contained in:
Thomas Goirand 2023-12-17 17:44:14 +01:00 committed by Rodolfo Alonso Hernandez
parent 3ef02cc2fb
commit bf2f8342d7
8 changed files with 21 additions and 13 deletions

View File

@ -14,7 +14,6 @@
# under the License.
import collections
from datetime import datetime
import itertools
import netaddr
@ -31,6 +30,7 @@ from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging
from oslo_serialization import jsonutils
from oslo_utils import timeutils
from oslo_utils import uuidutils
from neutron.agent import resource_cache
@ -99,7 +99,7 @@ class PluginReportStateAPI(object):
agent_state['uuid'] = uuidutils.generate_uuid()
kwargs = {
'agent_state': {'agent_state': agent_state},
'time': datetime.utcnow().strftime(constants.ISO8601_TIME_FORMAT),
'time': timeutils.utcnow().strftime(constants.ISO8601_TIME_FORMAT),
}
method = cctxt.call if use_call else cctxt.cast
return method(context, 'report_state', **kwargs)

View File

@ -17,6 +17,7 @@ import datetime
from neutron_lib.db import api as db_api
from oslo_db import exception as db_exc
from oslo_utils import timeutils
from neutron.common import utils
from neutron.objects import quota as quota_obj
@ -28,7 +29,7 @@ UNLIMITED_QUOTA = -1
# Wrapper for utcnow - needed for mocking it in unit tests
def utcnow():
return datetime.datetime.utcnow()
return timeutils.utcnow()
class QuotaUsageInfo(collections.namedtuple(

View File

@ -17,6 +17,7 @@ import datetime
from neutron_lib import context
from neutron_lib.db import api as db_api
from oslo_utils import timeutils
from oslo_utils import uuidutils
from neutron.objects import quota
@ -58,8 +59,8 @@ class _ReservationSql(testlib_api.SqlTestCase):
self.assertEqual(res_delta, res.resource_deltas[0])
with db_api.CONTEXT_READER.using(self.context):
res_map = quota.Reservation.get_total_reservations_map(
self.context, datetime.datetime.utcnow(), res.project_id,
resources, True)
self.context, timeutils.utcnow(),
res.project_id, resources, True)
self.assertEqual({'port': 100}, res_map)
self.assertIsInstance(res_map['port'], int)

View File

@ -15,6 +15,7 @@
import datetime
from oslo_utils import timeutils
from oslo_utils import uuidutils
from neutron.agent.common import resource_processing_queue as queue
@ -80,8 +81,8 @@ class TestExclusiveResourceProcessor(base.BaseTestCase):
self.assertEqual(datetime.datetime.min,
primary._get_resource_data_timestamp())
ts1 = datetime.datetime.utcnow() - datetime.timedelta(seconds=10)
ts2 = datetime.datetime.utcnow()
ts1 = timeutils.utcnow() - datetime.timedelta(seconds=10)
ts2 = timeutils.utcnow()
primary.fetched_and_processed(ts2)
self.assertEqual(ts2, primary._get_resource_data_timestamp())

View File

@ -25,9 +25,11 @@ from neutron_lib import rpc as n_rpc
from oslo_config import cfg
from oslo_context import context as oslo_context
from oslo_serialization import jsonutils
from oslo_utils import timeutils
from oslo_utils import uuidutils
from neutron.agent import rpc
from neutron.conf.agent import common as conf_common
from neutron.objects import network
from neutron.objects.port.extensions import port_hints
from neutron.objects import ports
@ -77,6 +79,7 @@ class AgentRPCPluginApi(base.BaseTestCase):
class AgentPluginReportState(base.BaseTestCase):
def test_plugin_report_state_timeout_report_interval(self):
conf_common.register_agent_state_opts_helper(cfg.CONF)
cfg.CONF.set_override('report_interval', 15, 'AGENT')
reportStateAPI = rpc.PluginReportStateAPI('test')
self.assertEqual(reportStateAPI.timeout, 15)
@ -124,9 +127,9 @@ class AgentPluginReportState(base.BaseTestCase):
expected_time = datetime.datetime(2015, 7, 27, 15, 33, 30, 0)
expected_time_str = '2015-07-27T15:33:30.000000'
expected_agent_state = {'agent': 'test'}
with mock.patch('neutron.agent.rpc.datetime') as mock_datetime:
with mock.patch.object(timeutils, 'utcnow',
return_value=expected_time):
reportStateAPI = rpc.PluginReportStateAPI(topic)
mock_datetime.utcnow.return_value = expected_time
with mock.patch.object(reportStateAPI.client, 'call'), \
mock.patch.object(reportStateAPI.client, 'cast'
) as mock_cast, \

View File

@ -31,6 +31,7 @@ from neutron_lib.tests.unit import fake_notifier
from oslo_config import cfg
from oslo_db import exception as db_exc
import oslo_messaging
from oslo_utils import timeutils
from oslo_utils import uuidutils
from webob import exc
@ -199,7 +200,7 @@ class AgentSchedulerTestMixIn(object):
def test_agent_registration_bad_timestamp(self):
callback = agents_db.AgentExtRpcCallback()
delta_time = datetime.datetime.now() - datetime.timedelta(days=1)
delta_time = timeutils.utcnow() - datetime.timedelta(days=1)
str_time = delta_time.strftime('%Y-%m-%dT%H:%M:%S.%f')
callback.report_state(
self.adminContext,
@ -209,7 +210,7 @@ class AgentSchedulerTestMixIn(object):
def test_agent_registration_invalid_timestamp_allowed(self):
callback = agents_db.AgentExtRpcCallback()
utc_time = datetime.datetime.utcnow()
utc_time = timeutils.utcnow()
delta_time = utc_time - datetime.timedelta(seconds=10)
str_time = delta_time.strftime('%Y-%m-%dT%H:%M:%S.%f')
callback.report_state(

View File

@ -15,6 +15,7 @@
import datetime
from neutron_lib.db import api as db_api
from oslo_utils import timeutils
from oslo_utils import uuidutils
from neutron.objects import quota
@ -60,7 +61,7 @@ class ReservationDbObjectTestCase(obj_test_base.BaseDbObjectTestCase,
return reservation
def test_delete_expired(self):
dt = datetime.datetime.utcnow()
dt = timeutils.utcnow()
resources = {'goals': 2, 'assists': 1}
exp_date1 = datetime.datetime(2016, 3, 31, 14, 30)
exp_date2 = datetime.datetime(2015, 3, 31, 14, 30)

View File

@ -40,7 +40,7 @@ oslo.rootwrap>=5.15.0 # Apache-2.0
oslo.serialization>=2.25.0 # Apache-2.0
oslo.service>=2.8.0 # Apache-2.0
oslo.upgradecheck>=1.3.0 # Apache-2.0
oslo.utils>=6.2.0 # Apache-2.0
oslo.utils>=7.0.0 # Apache-2.0
oslo.versionedobjects>=1.35.1 # Apache-2.0
osprofiler>=2.3.0 # Apache-2.0
os-ken>=2.2.0 # Apache-2.0