make all uses of utcnow use our testable utils.utcnow

This commit is contained in:
Vishvananda Ishaya
2011-06-02 14:23:05 -07:00
parent 679516260f
commit 980cf1fc3c
10 changed files with 24 additions and 30 deletions

View File

@@ -53,7 +53,6 @@
CLI interface for nova management. CLI interface for nova management.
""" """
import datetime
import gettext import gettext
import glob import glob
import json import json
@@ -689,7 +688,7 @@ class ServiceCommands(object):
"""Show a list of all running services. Filter by host & service name. """Show a list of all running services. Filter by host & service name.
args: [host] [service]""" args: [host] [service]"""
ctxt = context.get_admin_context() ctxt = context.get_admin_context()
now = datetime.datetime.utcnow() now = utils.utcnow()
services = db.service_get_all(ctxt) services = db.service_get_all(ctxt)
if host: if host:
services = [s for s in services if s['host'] == host] services = [s for s in services if s['host'] == host]

View File

@@ -11,9 +11,8 @@
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License.import datetime # under the License.
import datetime
import uuid import uuid
from nova import flags from nova import flags
@@ -64,7 +63,7 @@ def notify(publisher_id, event_type, priority, payload):
{'message_id': str(uuid.uuid4()), {'message_id': str(uuid.uuid4()),
'publisher_id': 'compute.host1', 'publisher_id': 'compute.host1',
'timestamp': datetime.datetime.utcnow(), 'timestamp': utils.utcnow(),
'priority': 'WARN', 'priority': 'WARN',
'event_type': 'compute.create_instance', 'event_type': 'compute.create_instance',
'payload': {'instance_id': 12, ... }} 'payload': {'instance_id': 12, ... }}
@@ -79,5 +78,5 @@ def notify(publisher_id, event_type, priority, payload):
event_type=event_type, event_type=event_type,
priority=priority, priority=priority,
payload=payload, payload=payload,
timestamp=str(datetime.datetime.utcnow())) timestamp=str(utils.utcnow()))
driver.notify(msg) driver.notify(msg)

View File

@@ -28,6 +28,7 @@ from nova import exception
from nova import flags from nova import flags
from nova import log as logging from nova import log as logging
from nova import rpc from nova import rpc
from nova import utils
from nova.compute import power_state from nova.compute import power_state
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@@ -61,7 +62,7 @@ class Scheduler(object):
"""Check whether a service is up based on last heartbeat.""" """Check whether a service is up based on last heartbeat."""
last_heartbeat = service['updated_at'] or service['created_at'] last_heartbeat = service['updated_at'] or service['created_at']
# Timestamps in DB are UTC. # Timestamps in DB are UTC.
elapsed = datetime.datetime.utcnow() - last_heartbeat elapsed = utils.utcnow() - last_heartbeat
return elapsed < datetime.timedelta(seconds=FLAGS.service_down_time) return elapsed < datetime.timedelta(seconds=FLAGS.service_down_time)
def hosts_up(self, context, topic): def hosts_up(self, context, topic):

View File

@@ -21,10 +21,9 @@
Simple Scheduler Simple Scheduler
""" """
import datetime
from nova import db from nova import db
from nova import flags from nova import flags
from nova import utils
from nova.scheduler import driver from nova.scheduler import driver
from nova.scheduler import chance from nova.scheduler import chance
@@ -54,7 +53,7 @@ class SimpleScheduler(chance.ChanceScheduler):
# TODO(vish): this probably belongs in the manager, if we # TODO(vish): this probably belongs in the manager, if we
# can generalize this somehow # can generalize this somehow
now = datetime.datetime.utcnow() now = utils.utcnow()
db.instance_update(context, instance_id, {'host': host, db.instance_update(context, instance_id, {'host': host,
'scheduled_at': now}) 'scheduled_at': now})
return host return host
@@ -66,7 +65,7 @@ class SimpleScheduler(chance.ChanceScheduler):
if self.service_is_up(service): if self.service_is_up(service):
# NOTE(vish): this probably belongs in the manager, if we # NOTE(vish): this probably belongs in the manager, if we
# can generalize this somehow # can generalize this somehow
now = datetime.datetime.utcnow() now = utils.utcnow()
db.instance_update(context, db.instance_update(context,
instance_id, instance_id,
{'host': service['host'], {'host': service['host'],
@@ -90,7 +89,7 @@ class SimpleScheduler(chance.ChanceScheduler):
# TODO(vish): this probably belongs in the manager, if we # TODO(vish): this probably belongs in the manager, if we
# can generalize this somehow # can generalize this somehow
now = datetime.datetime.utcnow() now = utils.utcnow()
db.volume_update(context, volume_id, {'host': host, db.volume_update(context, volume_id, {'host': host,
'scheduled_at': now}) 'scheduled_at': now})
return host return host
@@ -103,7 +102,7 @@ class SimpleScheduler(chance.ChanceScheduler):
if self.service_is_up(service): if self.service_is_up(service):
# NOTE(vish): this probably belongs in the manager, if we # NOTE(vish): this probably belongs in the manager, if we
# can generalize this somehow # can generalize this somehow
now = datetime.datetime.utcnow() now = utils.utcnow()
db.volume_update(context, db.volume_update(context,
volume_id, volume_id,
{'host': service['host'], {'host': service['host'],

View File

@@ -23,7 +23,6 @@ inline callbacks.
""" """
import datetime
import functools import functools
import os import os
import shutil import shutil
@@ -37,6 +36,7 @@ from eventlet import greenthread
from nova import fakerabbit from nova import fakerabbit
from nova import flags from nova import flags
from nova import rpc from nova import rpc
from nova import utils
from nova import service from nova import service
from nova import wsgi from nova import wsgi
from nova.virt import fake from nova.virt import fake
@@ -69,7 +69,7 @@ class TestCase(unittest.TestCase):
# NOTE(vish): We need a better method for creating fixtures for tests # NOTE(vish): We need a better method for creating fixtures for tests
# now that we have some required db setup for the system # now that we have some required db setup for the system
# to work properly. # to work properly.
self.start = datetime.datetime.utcnow() self.start = utils.utcnow()
shutil.copyfile(os.path.join(FLAGS.state_path, FLAGS.sqlite_clean_db), shutil.copyfile(os.path.join(FLAGS.state_path, FLAGS.sqlite_clean_db),
os.path.join(FLAGS.state_path, FLAGS.sqlite_db)) os.path.join(FLAGS.state_path, FLAGS.sqlite_db))

View File

@@ -19,7 +19,6 @@
Tests For Compute Tests For Compute
""" """
import datetime
import mox import mox
import stubout import stubout
@@ -217,12 +216,12 @@ class ComputeTestCase(test.TestCase):
instance_ref = db.instance_get(self.context, instance_id) instance_ref = db.instance_get(self.context, instance_id)
self.assertEqual(instance_ref['launched_at'], None) self.assertEqual(instance_ref['launched_at'], None)
self.assertEqual(instance_ref['deleted_at'], None) self.assertEqual(instance_ref['deleted_at'], None)
launch = datetime.datetime.utcnow() launch = utils.utcnow()
self.compute.run_instance(self.context, instance_id) self.compute.run_instance(self.context, instance_id)
instance_ref = db.instance_get(self.context, instance_id) instance_ref = db.instance_get(self.context, instance_id)
self.assert_(instance_ref['launched_at'] > launch) self.assert_(instance_ref['launched_at'] > launch)
self.assertEqual(instance_ref['deleted_at'], None) self.assertEqual(instance_ref['deleted_at'], None)
terminate = datetime.datetime.utcnow() terminate = utils.utcnow()
self.compute.terminate_instance(self.context, instance_id) self.compute.terminate_instance(self.context, instance_id)
self.context = self.context.elevated(True) self.context = self.context.elevated(True)
instance_ref = db.instance_get(self.context, instance_id) instance_ref = db.instance_get(self.context, instance_id)

View File

@@ -20,8 +20,6 @@
Tests For Console proxy. Tests For Console proxy.
""" """
import datetime
from nova import context from nova import context
from nova import db from nova import db
from nova import exception from nova import exception

View File

@@ -16,7 +16,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import datetime
import webob import webob
import webob.dec import webob.dec
import webob.exc import webob.exc

View File

@@ -196,7 +196,7 @@ class ZoneSchedulerTestCase(test.TestCase):
service.topic = 'compute' service.topic = 'compute'
service.id = kwargs['id'] service.id = kwargs['id']
service.availability_zone = kwargs['zone'] service.availability_zone = kwargs['zone']
service.created_at = datetime.datetime.utcnow() service.created_at = utils.utcnow()
return service return service
def test_with_two_zones(self): def test_with_two_zones(self):
@@ -290,7 +290,7 @@ class SimpleDriverTestCase(test.TestCase):
dic['host'] = kwargs.get('host', 'dummy') dic['host'] = kwargs.get('host', 'dummy')
s_ref = db.service_create(self.context, dic) s_ref = db.service_create(self.context, dic)
if 'created_at' in kwargs.keys() or 'updated_at' in kwargs.keys(): if 'created_at' in kwargs.keys() or 'updated_at' in kwargs.keys():
t = datetime.datetime.utcnow() - datetime.timedelta(0) t = utils.utcnow() - datetime.timedelta(0)
dic['created_at'] = kwargs.get('created_at', t) dic['created_at'] = kwargs.get('created_at', t)
dic['updated_at'] = kwargs.get('updated_at', t) dic['updated_at'] = kwargs.get('updated_at', t)
db.service_update(self.context, s_ref['id'], dic) db.service_update(self.context, s_ref['id'], dic)
@@ -401,7 +401,7 @@ class SimpleDriverTestCase(test.TestCase):
FLAGS.compute_manager) FLAGS.compute_manager)
compute1.start() compute1.start()
s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute') s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute')
now = datetime.datetime.utcnow() now = utils.utcnow()
delta = datetime.timedelta(seconds=FLAGS.service_down_time * 2) delta = datetime.timedelta(seconds=FLAGS.service_down_time * 2)
past = now - delta past = now - delta
db.service_update(self.context, s1['id'], {'updated_at': past}) db.service_update(self.context, s1['id'], {'updated_at': past})
@@ -542,7 +542,7 @@ class SimpleDriverTestCase(test.TestCase):
def test_wont_sechedule_if_specified_host_is_down(self): def test_wont_sechedule_if_specified_host_is_down(self):
compute1 = self.start_service('compute', host='host1') compute1 = self.start_service('compute', host='host1')
s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute') s1 = db.service_get_by_args(self.context, 'host1', 'nova-compute')
now = datetime.datetime.utcnow() now = utils.utcnow()
delta = datetime.timedelta(seconds=FLAGS.service_down_time * 2) delta = datetime.timedelta(seconds=FLAGS.service_down_time * 2)
past = now - delta past = now - delta
db.service_update(self.context, s1['id'], {'updated_at': past}) db.service_update(self.context, s1['id'], {'updated_at': past})
@@ -692,7 +692,7 @@ class SimpleDriverTestCase(test.TestCase):
dic = {'instance_id': instance_id, 'size': 1} dic = {'instance_id': instance_id, 'size': 1}
v_ref = db.volume_create(self.context, {'instance_id': instance_id, v_ref = db.volume_create(self.context, {'instance_id': instance_id,
'size': 1}) 'size': 1})
t1 = datetime.datetime.utcnow() - datetime.timedelta(1) t1 = utils.utcnow() - datetime.timedelta(1)
dic = {'created_at': t1, 'updated_at': t1, 'binary': 'nova-volume', dic = {'created_at': t1, 'updated_at': t1, 'binary': 'nova-volume',
'topic': 'volume', 'report_count': 0} 'topic': 'volume', 'report_count': 0}
s_ref = db.service_create(self.context, dic) s_ref = db.service_create(self.context, dic)
@@ -709,7 +709,7 @@ class SimpleDriverTestCase(test.TestCase):
"""Confirms src-compute node is alive.""" """Confirms src-compute node is alive."""
instance_id = self._create_instance() instance_id = self._create_instance()
i_ref = db.instance_get(self.context, instance_id) i_ref = db.instance_get(self.context, instance_id)
t = datetime.datetime.utcnow() - datetime.timedelta(10) t = utils.utcnow() - datetime.timedelta(10)
s_ref = self._create_compute_service(created_at=t, updated_at=t, s_ref = self._create_compute_service(created_at=t, updated_at=t,
host=i_ref['host']) host=i_ref['host'])
@@ -737,7 +737,7 @@ class SimpleDriverTestCase(test.TestCase):
"""Confirms exception raises in case dest host does not exist.""" """Confirms exception raises in case dest host does not exist."""
instance_id = self._create_instance() instance_id = self._create_instance()
i_ref = db.instance_get(self.context, instance_id) i_ref = db.instance_get(self.context, instance_id)
t = datetime.datetime.utcnow() - datetime.timedelta(10) t = utils.utcnow() - datetime.timedelta(10)
s_ref = self._create_compute_service(created_at=t, updated_at=t, s_ref = self._create_compute_service(created_at=t, updated_at=t,
host=i_ref['host']) host=i_ref['host'])
@@ -796,7 +796,7 @@ class SimpleDriverTestCase(test.TestCase):
# mocks for live_migration_common_check() # mocks for live_migration_common_check()
instance_id = self._create_instance() instance_id = self._create_instance()
i_ref = db.instance_get(self.context, instance_id) i_ref = db.instance_get(self.context, instance_id)
t1 = datetime.datetime.utcnow() - datetime.timedelta(10) t1 = utils.utcnow() - datetime.timedelta(10)
s_ref = self._create_compute_service(created_at=t1, updated_at=t1, s_ref = self._create_compute_service(created_at=t1, updated_at=t1,
host=dest) host=dest)

View File

@@ -307,7 +307,7 @@ def get_my_linklocal(interface):
def utcnow(): def utcnow():
"""Overridable version of datetime.datetime.utcnow.""" """Overridable version of utils.utcnow."""
if utcnow.override_time: if utcnow.override_time:
return utcnow.override_time return utcnow.override_time
return datetime.datetime.utcnow() return datetime.datetime.utcnow()