Replace deprecated datetime.utcnow()

The datetime.utcnow() is deprecated in Python 3.12.
Replace datetime.utcnow() with oslo_utils.timeutils.utcnow().

Change-Id: I4d3382b142c1c9225cad29f9e33abf539ecf0f30
Signed-off-by: Takashi Natsume <takanattie@gmail.com>
This commit is contained in:
Takashi Natsume 2024-09-28 20:45:53 +09:00
parent 896ef7f935
commit 5df5277756
11 changed files with 108 additions and 163 deletions

View File

@ -21,6 +21,7 @@ import eventlet
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils.excutils import save_and_reraise_exception
from oslo_utils import timeutils
from stevedore import enabled
from blazar import context
@ -236,7 +237,7 @@ class ManagerService(service_utils.RPCServer):
sort_dir='asc',
filters={'status': status.event.UNDONE,
'time': {'op': 'le',
'border': datetime.datetime.utcnow()}}
'border': timeutils.utcnow()}}
)
for batch in self._select_for_execution(events):
@ -252,7 +253,7 @@ class ManagerService(service_utils.RPCServer):
try:
event_fn(lease_id=event['lease_id'], event_id=event['id'])
except common_ex.InvalidStatus:
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
if now < event['time'] + datetime.timedelta(
seconds=CONF.manager.event_max_retries * 10):
# Set the event status UNDONE for retrying the event
@ -284,7 +285,7 @@ class ManagerService(service_utils.RPCServer):
return date
def _parse_lease_dates(self, start_date, end_date):
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
now = datetime.datetime(now.year,
now.month,
now.day,

View File

@ -19,6 +19,7 @@ from oslo_log import log as logging
from oslo_utils.excutils import save_and_reraise_exception
from oslo_utils import netutils
from oslo_utils import strutils
from oslo_utils import timeutils
from blazar import context
from blazar.db import api as db_api
@ -422,7 +423,7 @@ class FloatingIpPlugin(base.BasePlugin):
]
}.
"""
start = datetime.datetime.utcnow()
start = timeutils.utcnow()
end = datetime.date.max
reservations = db_utils.get_reservation_allocations_by_fip_ids(

View File

@ -21,6 +21,7 @@ from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import strutils
from oslo_utils.strutils import bool_from_string
from oslo_utils import timeutils
from blazar import context
from blazar.db import api as db_api
@ -167,7 +168,7 @@ class VirtualInstancePlugin(base.BasePlugin, nova.NovaClientWrapper):
]
}.
"""
start = datetime.datetime.utcnow()
start = timeutils.utcnow()
end = datetime.date.max
# To reduce overhead, this method only executes one query
@ -770,8 +771,7 @@ class VirtualInstancePlugin(base.BasePlugin, nova.NovaClientWrapper):
def _select_host(self, reservation, lease):
"""Returns the alternative host id or None if not found."""
values = {}
values['start_date'] = max(datetime.datetime.utcnow(),
lease['start_date'])
values['start_date'] = max(timeutils.utcnow(), lease['start_date'])
values['end_date'] = lease['end_date']
specs = ['vcpus', 'memory_mb', 'disk_gb', 'affinity', 'amount',
'resource_properties']

View File

@ -22,6 +22,7 @@ from novaclient import exceptions as nova_exceptions
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import strutils
from oslo_utils import timeutils
from blazar.db import api as db_api
from blazar.db import exceptions as db_ex
@ -304,7 +305,7 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
host['service_name'])
# Allocate an alternative host.
start_date = max(datetime.datetime.utcnow(), lease['start_date'])
start_date = max(timeutils.utcnow(), lease['start_date'])
new_hostids = self._matching_hosts(
reservation['hypervisor_properties'],
reservation['resource_properties'],
@ -457,7 +458,7 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
def is_updatable_extra_capability(self, capability, property_name):
reservations = db_utils.get_reservations_by_host_id(
capability['computehost_id'], datetime.datetime.utcnow(),
capability['computehost_id'], timeutils.utcnow(),
datetime.date.max)
for r in reservations:
@ -588,7 +589,7 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
]
}.
"""
start = datetime.datetime.utcnow()
start = timeutils.utcnow()
end = datetime.date.max
# To reduce overhead, this method only executes one query
@ -969,7 +970,7 @@ class PhysicalHostMonitorPlugin(base.BaseMonitorPlugin,
reservation_flags = {}
hosts = db_api.unreservable_host_get_all_by_queries([])
interval_begin = datetime.datetime.utcnow()
interval_begin = timeutils.utcnow()
interval = self.get_healing_interval()
if interval == 0:
interval_end = datetime.date.max

View File

@ -16,6 +16,7 @@
import datetime
import operator
from oslo_utils import timeutils
from oslo_utils import uuidutils
from blazar.db import exceptions as db_exceptions
@ -964,12 +965,12 @@ class SQLAlchemyDBApiTestCase(tests.DBTestCase):
db_api.event_create(_get_fake_event_values(
id='1',
event_type=fake_event_type,
time=datetime.datetime.utcnow()
time=timeutils.utcnow()
))
db_api.event_create(_get_fake_event_values(
id='2',
event_type=fake_event_type,
time=datetime.datetime.utcnow()
time=timeutils.utcnow()
))
filtered_events = db_api.event_get_all_sorted_by_filters(

View File

@ -17,6 +17,8 @@ import datetime
from unittest import mock
import ddt
from oslo_config import cfg
from oslo_utils import timeutils
from blazar import context
from blazar import enforcement
@ -24,8 +26,6 @@ from blazar.enforcement import exceptions
from blazar.enforcement import filters
from blazar import tests
from oslo_config import cfg
def get_fake_host(host_id):
return {
@ -158,9 +158,8 @@ class MaxLeaseDurationTestCase(tests.TestCase):
del new_lease_values['reservations']
ctx = context.current()
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(2014, 1, 1, 1, 1)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2014, 1, 1, 1, 1)
self.enforcement.check_update(
ctx, lease, new_lease_values, current_allocations,
allocation_candidates, reservations, new_reservations)
@ -178,9 +177,8 @@ class MaxLeaseDurationTestCase(tests.TestCase):
del new_lease_values['reservations']
ctx = context.current()
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(2014, 1, 1, 1, 1)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2014, 1, 1, 1, 1)
self.assertRaises(exceptions.MaxLeaseDurationException,
self.enforcement.check_update, ctx, lease,
new_lease_values, current_allocations,
@ -199,9 +197,8 @@ class MaxLeaseDurationTestCase(tests.TestCase):
del new_lease_values['reservations']
ctx = context.current()
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(2014, 1, 1, 1, 50)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2014, 1, 1, 1, 50)
self.enforcement.check_update(
ctx, lease, new_lease_values, current_allocations,
allocation_candidates, reservations, new_reservations)
@ -235,9 +232,8 @@ class MaxLeaseDurationTestCase(tests.TestCase):
del new_lease_values['reservations']
ctx = context.current()
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(2014, 1, 1, 1, 1)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2014, 1, 1, 1, 1)
self.enforcement.check_update(
ctx, lease, new_lease_values, current_allocations,
allocation_candidates, reservations, new_reservations)

View File

@ -16,6 +16,8 @@
import datetime
import ddt
from oslo_utils import timeutils
from blazar import context
from blazar import enforcement
from blazar.enforcement import filters
@ -44,10 +46,10 @@ def get_fake_lease(**kwargs):
fake_lease = {
'id': '1',
'name': 'lease_test',
'start_date': datetime.datetime.utcnow().strftime(
'start_date': timeutils.utcnow().strftime(
service.LEASE_DATE_FORMAT),
'end_date': (
datetime.datetime.utcnow() + datetime.timedelta(days=1)).strftime(
timeutils.utcnow() + datetime.timedelta(days=1)).strftime(
service.LEASE_DATE_FORMAT),
'user_id': '111',
'project_id': '222',

View File

@ -22,6 +22,7 @@ import eventlet
import importlib
from oslo_config import cfg
import oslo_messaging as messaging
from oslo_utils import timeutils
from stevedore import enabled
import testtools
@ -424,10 +425,9 @@ class ServiceTestCase(tests.DBTestCase):
start_lease.side_effect = exceptions.InvalidStatus
event_update = self.patch(self.db_api, 'event_update')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = (self.good_date
+ datetime.timedelta(seconds=1))
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = (self.good_date + datetime.timedelta(
seconds=1))
self.manager._exec_event(event)
start_lease.assert_called_once_with(lease_id=event['lease_id'],
@ -445,10 +445,9 @@ class ServiceTestCase(tests.DBTestCase):
start_lease.side_effect = exceptions.InvalidStatus
event_update = self.patch(self.db_api, 'event_update')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = (self.good_date
+ datetime.timedelta(days=1))
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = (self.good_date + datetime.timedelta(
days=1))
self.manager._exec_event(event)
start_lease.assert_called_once_with(lease_id=event['lease_id'],
@ -701,7 +700,7 @@ class ServiceTestCase(tests.DBTestCase):
def test_create_lease_start_date_in_past(self):
lease_values = self.lease_values.copy()
lease_values['start_date'] = datetime.datetime.strftime(
datetime.datetime.utcnow() - datetime.timedelta(days=1),
timeutils.utcnow() - datetime.timedelta(days=1),
service.LEASE_DATE_FORMAT)
self.assertRaises(
@ -782,10 +781,8 @@ class ServiceTestCase(tests.DBTestCase):
def test_update_lease_completed_lease_rename(self):
lease_values = {'name': 'renamed'}
target = datetime.datetime(2015, 1, 1)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
lease = self.manager.update_lease(lease_id=self.lease_id,
values=lease_values)
self.lease_update.assert_called_once_with(self.lease_id, lease_values)
@ -819,10 +816,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.manager.update_lease(lease_id=self.lease_id,
values=lease_values)
self.fake_plugin.update_reservation.assert_called_with(
@ -875,10 +870,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.manager.update_lease(lease_id=self.lease_id,
values=lease_values)
self.fake_plugin.update_reservation.assert_called_with(
@ -922,10 +915,8 @@ class ServiceTestCase(tests.DBTestCase):
}
]
target = datetime.datetime(2013, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
manager_ex.CantUpdateParameter, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -947,10 +938,8 @@ class ServiceTestCase(tests.DBTestCase):
}
]
target = datetime.datetime(2013, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
manager_ex.MissingParameter, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -979,10 +968,8 @@ class ServiceTestCase(tests.DBTestCase):
}
]
target = datetime.datetime(2013, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
exceptions.InvalidInput, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1013,10 +1000,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 20, 14, 00)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.manager.update_lease(lease_id=self.lease_id,
values=lease_values)
self.fake_plugin.update_reservation.assert_called_with(
@ -1063,10 +1048,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 20, 14, 00)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.manager.update_lease(lease_id=self.lease_id,
values=lease_values)
self.fake_plugin.update_reservation.assert_called_with(
@ -1128,10 +1111,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 20, 14, 00)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.manager.update_lease(lease_id=self.lease_id,
values=lease_values)
self.fake_plugin.update_reservation.assert_called_with(
@ -1198,10 +1179,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 20, 14, 00)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
exceptions.NotAuthorized, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1238,10 +1217,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 20, 14, 00)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
exceptions.NotAuthorized, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1277,10 +1254,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 20, 14, 00)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
manager_ex.InvalidDate, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1298,10 +1273,8 @@ class ServiceTestCase(tests.DBTestCase):
'start_date': '2013-12-20 16:00'
}
target = datetime.datetime(2013, 12, 20, 14, 00)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
exceptions.InvalidInput, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1312,10 +1285,8 @@ class ServiceTestCase(tests.DBTestCase):
'start_date': '2013-12-14 13:00'
}
target = datetime.datetime(2013, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
exceptions.InvalidInput, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1326,10 +1297,8 @@ class ServiceTestCase(tests.DBTestCase):
'end_date': '2013-12-14 13:00'
}
target = datetime.datetime(2013, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
exceptions.InvalidInput, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1340,10 +1309,8 @@ class ServiceTestCase(tests.DBTestCase):
'end_date': '2013-12-15 20:00'
}
target = datetime.datetime(2015, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
exceptions.InvalidInput, self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1378,10 +1345,8 @@ class ServiceTestCase(tests.DBTestCase):
'end_date': '2013-12-25 20:00'
}
target = datetime.datetime(2013, 12, 10)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(exceptions.BlazarException,
self.manager.update_lease,
lease_id=self.lease_id, values=lease_values)
@ -1426,10 +1391,8 @@ class ServiceTestCase(tests.DBTestCase):
event_get = self.patch(db_api, 'event_get_first_sorted_by_filters')
event_get.side_effect = fake_event_get
target = datetime.datetime(2013, 12, 15)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(exceptions.NotAuthorized,
self.manager.update_lease,
@ -1709,10 +1672,8 @@ class ServiceTestCase(tests.DBTestCase):
'prolong_for': '8d'
}
target = datetime.datetime(2013, 12, 14)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
enforcement_ex.MaxLeaseDurationException,
manager.update_lease,
@ -1743,10 +1704,8 @@ class ServiceTestCase(tests.DBTestCase):
'prolong_for': '8d'
}
target = datetime.datetime(2013, 12, 14)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
enforcement.exceptions.ExternalServiceFilterException,
manager.update_lease,
@ -1777,10 +1736,8 @@ class ServiceTestCase(tests.DBTestCase):
'prolong_for': '8d'
}
target = datetime.datetime(2013, 12, 14)
with mock.patch.object(datetime,
'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = target
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = target
self.assertRaises(
manager_ex.ExtraCapabilityTooLong,
manager.update_lease,

View File

@ -19,6 +19,9 @@ import uuid
import ddt
from novaclient import exceptions as nova_exceptions
from oslo_config import cfg
from oslo_config import fixture as conf_fixture
from oslo_utils import timeutils
from blazar import context
from blazar.db import api as db_api
@ -29,8 +32,6 @@ from blazar.plugins.instances import instance_plugin
from blazar.plugins import oshosts
from blazar import tests
from blazar.utils.openstack import nova
from oslo_config import cfg
from oslo_config import fixture as conf_fixture
CONF = cfg.CONF
@ -1417,10 +1418,8 @@ class TestVirtualInstancePlugin(tests.TestCase):
pickup_hosts.return_value = {'added': [new_host['id']], 'removed': []}
alloc_update = self.patch(db_api, 'host_allocation_update')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
2020, 1, 1, 11, 00)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2020, 1, 1, 11, 00)
result = plugin._heal_reservation(
dummy_reservation, list(failed_host.values()))
@ -1476,10 +1475,8 @@ class TestVirtualInstancePlugin(tests.TestCase):
mock_update_reservation_inventory = self.patch(
plugin.placement_client, 'update_reservation_inventory')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
2020, 1, 1, 13, 00)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2020, 1, 1, 13, 00)
result = plugin._heal_reservation(
dummy_reservation, list(failed_host.values()))
@ -1533,10 +1530,8 @@ class TestVirtualInstancePlugin(tests.TestCase):
pickup_hosts.side_effect = mgr_exceptions.NotEnoughHostsAvailable
alloc_destroy = self.patch(db_api, 'host_allocation_destroy')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
2020, 1, 1, 11, 00)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2020, 1, 1, 11, 00)
result = plugin._heal_reservation(
dummy_reservation, list(failed_host.values()))
@ -1579,10 +1574,8 @@ class TestVirtualInstancePlugin(tests.TestCase):
pickup_hosts.return_value = {'added': [new_host['id']], 'removed': []}
alloc_update = self.patch(db_api, 'host_allocation_update')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
2020, 1, 1, 11, 00)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2020, 1, 1, 11, 00)
result = plugin._heal_reservation(
dummy_reservation, list(failed_host.values()))
@ -1642,10 +1635,8 @@ class TestVirtualInstancePlugin(tests.TestCase):
mock_update_reservation_inventory = self.patch(
plugin.placement_client, 'update_reservation_inventory')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
2020, 1, 1, 13, 00)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2020, 1, 1, 13, 00)
result = plugin._heal_reservation(
dummy_reservation, list(failed_host.values()))
@ -1703,10 +1694,8 @@ class TestVirtualInstancePlugin(tests.TestCase):
pickup_hosts.side_effect = mgr_exceptions.NotEnoughHostsAvailable
alloc_destroy = self.patch(db_api, 'host_allocation_destroy')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
2020, 1, 1, 11, 00)
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(2020, 1, 1, 11, 00)
result = plugin._heal_reservation(
dummy_reservation, list(failed_host.values()))

View File

@ -15,6 +15,7 @@
import collections
import datetime
import random
from unittest import mock
import ddt
@ -22,7 +23,7 @@ from novaclient import client as nova_client
from novaclient import exceptions as nova_exceptions
from oslo_config import cfg
from oslo_config import fixture as conf_fixture
import random
from oslo_utils import timeutils
import testtools
from blazar import context
@ -908,7 +909,7 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.assertDictEqual(expected, ret)
def test_create_reservation_no_hosts_available(self):
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
values = {
'lease_id': '018c1b43-e69e-4aef-a543-09681539cf4c',
'min': 1,
@ -2215,9 +2216,8 @@ class PhysicalHostPluginTestCase(tests.TestCase):
matching_hosts.return_value = [new_host['id']]
alloc_update = self.patch(self.db_api, 'host_allocation_update')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(
2020, 1, 1, 11, 00)
result = self.fake_phys_plugin._reallocate(dummy_allocation)
@ -2271,9 +2271,8 @@ class PhysicalHostPluginTestCase(tests.TestCase):
matching_hosts.return_value = [new_host['id']]
alloc_update = self.patch(self.db_api, 'host_allocation_update')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(
2020, 1, 1, 13, 00)
result = self.fake_phys_plugin._reallocate(dummy_allocation)
@ -2329,9 +2328,8 @@ class PhysicalHostPluginTestCase(tests.TestCase):
matching_hosts.return_value = []
alloc_destroy = self.patch(self.db_api, 'host_allocation_destroy')
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = datetime.datetime(
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = datetime.datetime(
2020, 1, 1, 11, 00)
result = self.fake_phys_plugin._reallocate(dummy_allocation)
@ -2902,9 +2900,8 @@ class PhysicalHostMonitorPluginTestCase(tests.TestCase):
self.host_monitor_plugin.healing_handlers = [healing_handler]
start_date = datetime.datetime(2020, 1, 1, 12, 00)
with mock.patch.object(datetime, 'datetime',
mock.Mock(wraps=datetime.datetime)) as patched:
patched.utcnow.return_value = start_date
with mock.patch.object(timeutils, 'utcnow') as patched:
patched.return_value = start_date
result = self.host_monitor_plugin.heal()
healing_handler.assert_called_once_with(

View File

@ -26,7 +26,7 @@ oslo.policy>=4.5.0 # Apache-2.0
oslo.serialization!=2.19.1,>=2.18.0 # Apache-2.0
oslo.service>=1.34.0 # Apache-2.0
oslo.upgradecheck>=1.3.0 # Apache-2.0
oslo.utils>=4.5.0 # Apache-2.0
oslo.utils>=7.0.0 # Apache-2.0
python-neutronclient>=6.0.0 # Apache-2.0
python-novaclient>=9.1.0 # Apache-2.0
netaddr>=0.7.18 # BSD