diff --git a/neutron/agent/rpc.py b/neutron/agent/rpc.py index 8a520dc1cc4..1bb9733e233 100644 --- a/neutron/agent/rpc.py +++ b/neutron/agent/rpc.py @@ -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) diff --git a/neutron/db/quota/api.py b/neutron/db/quota/api.py index 4bb7b7a9029..e5d482557e8 100644 --- a/neutron/db/quota/api.py +++ b/neutron/db/quota/api.py @@ -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( diff --git a/neutron/tests/functional/objects/test_quota.py b/neutron/tests/functional/objects/test_quota.py index b9485f9b4eb..3ba7e93b997 100644 --- a/neutron/tests/functional/objects/test_quota.py +++ b/neutron/tests/functional/objects/test_quota.py @@ -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) diff --git a/neutron/tests/unit/agent/common/test_resource_processing_queue.py b/neutron/tests/unit/agent/common/test_resource_processing_queue.py index 7ec218f4e2c..dac7a3f7e99 100644 --- a/neutron/tests/unit/agent/common/test_resource_processing_queue.py +++ b/neutron/tests/unit/agent/common/test_resource_processing_queue.py @@ -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()) diff --git a/neutron/tests/unit/agent/test_rpc.py b/neutron/tests/unit/agent/test_rpc.py index 474e57ead10..bd03a7349c8 100644 --- a/neutron/tests/unit/agent/test_rpc.py +++ b/neutron/tests/unit/agent/test_rpc.py @@ -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, \ diff --git a/neutron/tests/unit/db/test_agentschedulers_db.py b/neutron/tests/unit/db/test_agentschedulers_db.py index df4edfaa9b0..e7815dd684b 100644 --- a/neutron/tests/unit/db/test_agentschedulers_db.py +++ b/neutron/tests/unit/db/test_agentschedulers_db.py @@ -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( diff --git a/neutron/tests/unit/objects/test_quota.py b/neutron/tests/unit/objects/test_quota.py index 619ef33cde1..5f3d3eb2819 100644 --- a/neutron/tests/unit/objects/test_quota.py +++ b/neutron/tests/unit/objects/test_quota.py @@ -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) diff --git a/requirements.txt b/requirements.txt index 687503daa7a..7fa16745ca0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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