Changes all uses of utcnow to use the version in utils. This is a simple wrapper for datetime.datetime.utcnow that allows us to use fake values for tests.

There are still a few places in the Zone code that is using datetime.now(), I'd prefer to move this to utils.utcnow() as well but I want to chat with sandy first to make sure that there won't be any issues.
This commit is contained in:
Vishvananda Ishaya 2011-06-03 15:11:01 +00:00 committed by Tarmac
commit 2b8863d0da
31 changed files with 77 additions and 95 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
@ -690,7 +689,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

@ -21,7 +21,6 @@ Admin API controller, exposed through http via the api worker.
""" """
import base64 import base64
import datetime
from nova import db from nova import db
from nova import exception from nova import exception
@ -305,7 +304,7 @@ class AdminController(object):
* Volume Count * Volume Count
""" """
services = db.service_get_all(context, False) services = db.service_get_all(context, False)
now = datetime.datetime.utcnow() now = utils.utcnow()
hosts = [] hosts = []
rv = [] rv = []
for host in [service['host'] for service in services]: for host in [service['host'] for service in services]:

View File

@ -23,7 +23,6 @@ datastore.
""" """
import base64 import base64
import datetime
import IPy import IPy
import os import os
import urllib import urllib
@ -235,7 +234,7 @@ class CloudController(object):
'zoneState': 'available'}]} 'zoneState': 'available'}]}
services = db.service_get_all(context, False) services = db.service_get_all(context, False)
now = datetime.datetime.utcnow() now = utils.utcnow()
hosts = [] hosts = []
for host in [service['host'] for service in services]: for host in [service['host'] for service in services]:
if not host in hosts: if not host in hosts:
@ -595,7 +594,7 @@ class CloudController(object):
instance_id = ec2utils.ec2_id_to_id(ec2_id) instance_id = ec2utils.ec2_id_to_id(ec2_id)
output = self.compute_api.get_console_output( output = self.compute_api.get_console_output(
context, instance_id=instance_id) context, instance_id=instance_id)
now = datetime.datetime.utcnow() now = utils.utcnow()
return {"InstanceId": ec2_id, return {"InstanceId": ec2_id,
"Timestamp": now, "Timestamp": now,
"output": base64.b64encode(output)} "output": base64.b64encode(output)}

View File

@ -13,9 +13,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 hashlib import hashlib
import time import time
@ -127,7 +126,7 @@ class AuthMiddleware(wsgi.Middleware):
except exception.NotFound: except exception.NotFound:
return None return None
if token: if token:
delta = datetime.datetime.utcnow() - token['created_at'] delta = utils.utcnow() - token['created_at']
if delta.days >= 2: if delta.days >= 2:
self.db.auth_token_destroy(ctxt, token['token_hash']) self.db.auth_token_destroy(ctxt, token['token_hash'])
else: else:

View File

@ -13,7 +13,7 @@
# 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.
"""Contrib contains extensions that are shipped with nova. """Contrib contains extensions that are shipped with nova.

View File

@ -11,7 +11,7 @@
# 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.
""" """
Module dedicated functions/classes dealing with rate limiting requests. Module dedicated functions/classes dealing with rate limiting requests.

View File

@ -13,7 +13,7 @@
# 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.
"""Rate limiting of arbitrary actions.""" """Rate limiting of arbitrary actions."""

View File

@ -18,7 +18,6 @@
"""Handles all requests relating to instances (guest vms).""" """Handles all requests relating to instances (guest vms)."""
import datetime
import eventlet import eventlet
import re import re
import time import time
@ -407,7 +406,7 @@ class API(base.Base):
instance['id'], instance['id'],
state_description='terminating', state_description='terminating',
state=0, state=0,
terminated_at=datetime.datetime.utcnow()) terminated_at=utils.utcnow())
host = instance['host'] host = instance['host']
if host: if host:

View File

@ -35,7 +35,6 @@ terminating it.
""" """
import datetime
import os import os
import socket import socket
import sys import sys
@ -159,7 +158,7 @@ class ComputeManager(manager.SchedulerDependentManager):
def _update_launched_at(self, context, instance_id, launched_at=None): def _update_launched_at(self, context, instance_id, launched_at=None):
"""Update the launched_at parameter of the given instance.""" """Update the launched_at parameter of the given instance."""
data = {'launched_at': launched_at or datetime.datetime.utcnow()} data = {'launched_at': launched_at or utils.utcnow()}
self.db.instance_update(context, instance_id, data) self.db.instance_update(context, instance_id, data)
def _update_image_ref(self, context, instance_id, image_ref): def _update_image_ref(self, context, instance_id, image_ref):

View File

@ -86,7 +86,7 @@ RRD_VALUES = {
]} ]}
utcnow = datetime.datetime.utcnow utcnow = utils.utcnow
LOG = logging.getLogger('nova.compute.monitor') LOG = logging.getLogger('nova.compute.monitor')

View File

@ -18,7 +18,6 @@
"""RequestContext: context for requests that persist through all of nova.""" """RequestContext: context for requests that persist through all of nova."""
import datetime
import random import random
from nova import exception from nova import exception

View File

@ -19,7 +19,6 @@
Implementation of SQLAlchemy backend. Implementation of SQLAlchemy backend.
""" """
import datetime
import warnings import warnings
from nova import db from nova import db
@ -674,7 +673,7 @@ def fixed_ip_disassociate_all_by_timeout(_context, host, time):
filter_by(allocated=0).\ filter_by(allocated=0).\
update({'instance_id': None, update({'instance_id': None,
'leased': 0, 'leased': 0,
'updated_at': datetime.datetime.utcnow()}, 'updated_at': utils.utcnow()},
synchronize_session='fetch') synchronize_session='fetch')
return result return result
@ -820,17 +819,17 @@ def instance_destroy(context, instance_id):
session.query(models.Instance).\ session.query(models.Instance).\
filter_by(id=instance_id).\ filter_by(id=instance_id).\
update({'deleted': True, update({'deleted': True,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
session.query(models.SecurityGroupInstanceAssociation).\ session.query(models.SecurityGroupInstanceAssociation).\
filter_by(instance_id=instance_id).\ filter_by(instance_id=instance_id).\
update({'deleted': True, update({'deleted': True,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
session.query(models.InstanceMetadata).\ session.query(models.InstanceMetadata).\
filter_by(instance_id=instance_id).\ filter_by(instance_id=instance_id).\
update({'deleted': True, update({'deleted': True,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
@ -1123,7 +1122,7 @@ def key_pair_destroy_all_by_user(context, user_id):
session.query(models.KeyPair).\ session.query(models.KeyPair).\
filter_by(user_id=user_id).\ filter_by(user_id=user_id).\
update({'deleted': 1, update({'deleted': 1,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
@ -1655,7 +1654,7 @@ def volume_destroy(context, volume_id):
session.query(models.Volume).\ session.query(models.Volume).\
filter_by(id=volume_id).\ filter_by(id=volume_id).\
update({'deleted': 1, update({'deleted': 1,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
session.query(models.ExportDevice).\ session.query(models.ExportDevice).\
filter_by(volume_id=volume_id).\ filter_by(volume_id=volume_id).\
@ -1813,7 +1812,7 @@ def snapshot_destroy(context, snapshot_id):
session.query(models.Snapshot).\ session.query(models.Snapshot).\
filter_by(id=snapshot_id).\ filter_by(id=snapshot_id).\
update({'deleted': 1, update({'deleted': 1,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
@ -1968,17 +1967,17 @@ def security_group_destroy(context, security_group_id):
session.query(models.SecurityGroup).\ session.query(models.SecurityGroup).\
filter_by(id=security_group_id).\ filter_by(id=security_group_id).\
update({'deleted': 1, update({'deleted': 1,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
session.query(models.SecurityGroupInstanceAssociation).\ session.query(models.SecurityGroupInstanceAssociation).\
filter_by(security_group_id=security_group_id).\ filter_by(security_group_id=security_group_id).\
update({'deleted': 1, update({'deleted': 1,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
session.query(models.SecurityGroupIngressRule).\ session.query(models.SecurityGroupIngressRule).\
filter_by(group_id=security_group_id).\ filter_by(group_id=security_group_id).\
update({'deleted': 1, update({'deleted': 1,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
@ -1989,11 +1988,11 @@ def security_group_destroy_all(context, session=None):
with session.begin(): with session.begin():
session.query(models.SecurityGroup).\ session.query(models.SecurityGroup).\
update({'deleted': 1, update({'deleted': 1,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
session.query(models.SecurityGroupIngressRule).\ session.query(models.SecurityGroupIngressRule).\
update({'deleted': 1, update({'deleted': 1,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
@ -2627,7 +2626,7 @@ def instance_metadata_delete(context, instance_id, key):
filter_by(key=key).\ filter_by(key=key).\
filter_by(deleted=False).\ filter_by(deleted=False).\
update({'deleted': True, update({'deleted': True,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
@ -2638,7 +2637,7 @@ def instance_metadata_delete_all(context, instance_id):
filter_by(instance_id=instance_id).\ filter_by(instance_id=instance_id).\
filter_by(deleted=False).\ filter_by(deleted=False).\
update({'deleted': True, update({'deleted': True,
'deleted_at': datetime.datetime.utcnow(), 'deleted_at': utils.utcnow(),
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})

View File

@ -17,7 +17,7 @@
from sqlalchemy import Boolean, Column, DateTime, Integer from sqlalchemy import Boolean, Column, DateTime, Integer
from sqlalchemy import MetaData, String, Table from sqlalchemy import MetaData, String, Table
import datetime from nova import utils
meta = MetaData() meta = MetaData()
@ -35,9 +35,9 @@ def old_style_quotas_table(name):
return Table(name, meta, return Table(name, meta,
Column('id', Integer(), primary_key=True), Column('id', Integer(), primary_key=True),
Column('created_at', DateTime(), Column('created_at', DateTime(),
default=datetime.datetime.utcnow), default=utils.utcnow),
Column('updated_at', DateTime(), Column('updated_at', DateTime(),
onupdate=datetime.datetime.utcnow), onupdate=utils.utcnow),
Column('deleted_at', DateTime()), Column('deleted_at', DateTime()),
Column('deleted', Boolean(), default=False), Column('deleted', Boolean(), default=False),
Column('project_id', Column('project_id',
@ -57,9 +57,9 @@ def new_style_quotas_table(name):
return Table(name, meta, return Table(name, meta,
Column('id', Integer(), primary_key=True), Column('id', Integer(), primary_key=True),
Column('created_at', DateTime(), Column('created_at', DateTime(),
default=datetime.datetime.utcnow), default=utils.utcnow),
Column('updated_at', DateTime(), Column('updated_at', DateTime(),
onupdate=datetime.datetime.utcnow), onupdate=utils.utcnow),
Column('deleted_at', DateTime()), Column('deleted_at', DateTime()),
Column('deleted', Boolean(), default=False), Column('deleted', Boolean(), default=False),
Column('project_id', Column('project_id',

View File

@ -19,8 +19,6 @@
SQLAlchemy models for nova data. SQLAlchemy models for nova data.
""" """
import datetime
from sqlalchemy.orm import relationship, backref, object_mapper from sqlalchemy.orm import relationship, backref, object_mapper
from sqlalchemy import Column, Integer, String, schema from sqlalchemy import Column, Integer, String, schema
from sqlalchemy import ForeignKey, DateTime, Boolean, Text from sqlalchemy import ForeignKey, DateTime, Boolean, Text
@ -33,6 +31,7 @@ from nova.db.sqlalchemy.session import get_session
from nova import auth from nova import auth
from nova import exception from nova import exception
from nova import flags from nova import flags
from nova import utils
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@ -43,8 +42,8 @@ class NovaBase(object):
"""Base class for Nova Models.""" """Base class for Nova Models."""
__table_args__ = {'mysql_engine': 'InnoDB'} __table_args__ = {'mysql_engine': 'InnoDB'}
__table_initialized__ = False __table_initialized__ = False
created_at = Column(DateTime, default=datetime.datetime.utcnow) created_at = Column(DateTime, default=utils.utcnow)
updated_at = Column(DateTime, onupdate=datetime.datetime.utcnow) updated_at = Column(DateTime, onupdate=utils.utcnow)
deleted_at = Column(DateTime) deleted_at = Column(DateTime)
deleted = Column(Boolean, default=False) deleted = Column(Boolean, default=False)
@ -64,7 +63,7 @@ class NovaBase(object):
def delete(self, session=None): def delete(self, session=None):
"""Delete this object.""" """Delete this object."""
self.deleted = True self.deleted = True
self.deleted_at = datetime.datetime.utcnow() self.deleted_at = utils.utcnow()
self.save(session=session) self.save(session=session)
def __setitem__(self, key, value): def __setitem__(self, key, value):

View File

@ -235,7 +235,7 @@ class NetworkManager(manager.SchedulerDependentManager):
inst_addr = instance_ref['mac_address'] inst_addr = instance_ref['mac_address']
raise exception.Error(_('IP %(address)s leased to bad mac' raise exception.Error(_('IP %(address)s leased to bad mac'
' %(inst_addr)s vs %(mac)s') % locals()) ' %(inst_addr)s vs %(mac)s') % locals())
now = datetime.datetime.utcnow() now = utils.utcnow()
self.db.fixed_ip_update(context, self.db.fixed_ip_update(context,
fixed_ip_ref['address'], fixed_ip_ref['address'],
{'leased': True, {'leased': True,

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

@ -17,16 +17,17 @@
ZoneManager oversees all communications with child Zones. ZoneManager oversees all communications with child Zones.
""" """
import datetime
import novaclient import novaclient
import thread import thread
import traceback import traceback
from datetime import datetime
from eventlet import greenpool from eventlet import greenpool
from nova import db from nova import db
from nova import flags from nova import flags
from nova import log as logging from nova import log as logging
from nova import utils
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
flags.DEFINE_integer('zone_db_check_interval', 60, flags.DEFINE_integer('zone_db_check_interval', 60,
@ -42,7 +43,7 @@ class ZoneState(object):
self.name = None self.name = None
self.capabilities = None self.capabilities = None
self.attempt = 0 self.attempt = 0
self.last_seen = datetime.min self.last_seen = datetime.datetime.min
self.last_exception = None self.last_exception = None
self.last_exception_time = None self.last_exception_time = None
@ -56,7 +57,7 @@ class ZoneState(object):
def update_metadata(self, zone_metadata): def update_metadata(self, zone_metadata):
"""Update zone metadata after successful communications with """Update zone metadata after successful communications with
child zone.""" child zone."""
self.last_seen = datetime.now() self.last_seen = utils.utcnow()
self.attempt = 0 self.attempt = 0
self.name = zone_metadata.get("name", "n/a") self.name = zone_metadata.get("name", "n/a")
self.capabilities = ", ".join(["%s=%s" % (k, v) self.capabilities = ", ".join(["%s=%s" % (k, v)
@ -72,7 +73,7 @@ class ZoneState(object):
"""Something went wrong. Check to see if zone should be """Something went wrong. Check to see if zone should be
marked as offline.""" marked as offline."""
self.last_exception = exception self.last_exception = exception
self.last_exception_time = datetime.now() self.last_exception_time = utils.utcnow()
api_url = self.api_url api_url = self.api_url
logging.warning(_("'%(exception)s' error talking to " logging.warning(_("'%(exception)s' error talking to "
"zone %(api_url)s") % locals()) "zone %(api_url)s") % locals())
@ -104,7 +105,7 @@ def _poll_zone(zone):
class ZoneManager(object): class ZoneManager(object):
"""Keeps the zone states updated.""" """Keeps the zone states updated."""
def __init__(self): def __init__(self):
self.last_zone_db_check = datetime.min self.last_zone_db_check = datetime.datetime.min
self.zone_states = {} # { <zone_id> : ZoneState } self.zone_states = {} # { <zone_id> : ZoneState }
self.service_states = {} # { <host> : { <service> : { cap k : v }}} self.service_states = {} # { <host> : { <service> : { cap k : v }}}
self.green_pool = greenpool.GreenPool() self.green_pool = greenpool.GreenPool()
@ -158,10 +159,10 @@ class ZoneManager(object):
def ping(self, context=None): def ping(self, context=None):
"""Ping should be called periodically to update zone status.""" """Ping should be called periodically to update zone status."""
diff = datetime.now() - self.last_zone_db_check diff = utils.utcnow() - self.last_zone_db_check
if diff.seconds >= FLAGS.zone_db_check_interval: if diff.seconds >= FLAGS.zone_db_check_interval:
logging.debug(_("Updating zone cache from db.")) logging.debug(_("Updating zone cache from db."))
self.last_zone_db_check = datetime.now() self.last_zone_db_check = utils.utcnow()
self._refresh_from_db(context) self._refresh_from_db(context)
self._poll_zones(context) self._poll_zones(context)

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

@ -16,7 +16,6 @@
# under the License. # under the License.
import copy import copy
import datetime
import json import json
import random import random
import string import string
@ -256,7 +255,7 @@ class FakeAuthDatabase(object):
@staticmethod @staticmethod
def auth_token_create(context, token): def auth_token_create(context, token):
fake_token = FakeToken(created_at=datetime.datetime.now(), **token) fake_token = FakeToken(created_at=utils.utcnow(), **token)
FakeAuthDatabase.data[fake_token.token_hash] = fake_token FakeAuthDatabase.data[fake_token.token_hash] = fake_token
FakeAuthDatabase.data['id_%i' % fake_token.id] = fake_token FakeAuthDatabase.data['id_%i' % fake_token.id] = fake_token
return fake_token return fake_token

View File

@ -22,7 +22,6 @@ and as a WSGI layer
import copy import copy
import json import json
import datetime
import os import os
import shutil import shutil
import tempfile import tempfile

View File

@ -16,7 +16,6 @@
# under the License. # under the License.
import base64 import base64
import datetime
import json import json
import unittest import unittest
from xml.dom import minidom from xml.dom import minidom
@ -29,6 +28,7 @@ from nova import db
from nova import exception from nova import exception
from nova import flags from nova import flags
from nova import test from nova import test
from nova import utils
import nova.api.openstack import nova.api.openstack
from nova.api.openstack import servers from nova.api.openstack import servers
import nova.compute.api import nova.compute.api
@ -115,9 +115,9 @@ def stub_instance(id, user_id=1, private_address=None, public_addresses=None,
"user_data": "", "user_data": "",
"reservation_id": "", "reservation_id": "",
"mac_address": "", "mac_address": "",
"scheduled_at": datetime.datetime.now(), "scheduled_at": utils.utcnow(),
"launched_at": datetime.datetime.now(), "launched_at": utils.utcnow(),
"terminated_at": datetime.datetime.now(), "terminated_at": utils.utcnow(),
"availability_zone": "", "availability_zone": "",
"display_name": "server%s" % id, "display_name": "server%s" % id,
"display_description": "", "display_description": "",

View File

@ -197,7 +197,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):
@ -291,7 +291,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)
@ -402,7 +402,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})
@ -543,7 +543,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})
@ -693,7 +693,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)
@ -710,7 +710,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'])
@ -738,7 +738,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'])
@ -797,7 +797,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

@ -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

@ -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()

View File

@ -51,13 +51,13 @@ A fake XenAPI SDK.
""" """
import datetime
import uuid import uuid
from pprint import pformat from pprint import pformat
from nova import exception from nova import exception
from nova import log as logging from nova import log as logging
from nova import utils
_CLASSES = ['host', 'network', 'session', 'SR', 'VBD', _CLASSES = ['host', 'network', 'session', 'SR', 'VBD',
@ -540,7 +540,7 @@ class SessionBase(object):
except Failure, exc: except Failure, exc:
task['error_info'] = exc.details task['error_info'] = exc.details
task['status'] = 'failed' task['status'] = 'failed'
task['finished'] = datetime.datetime.now() task['finished'] = utils.utcnow()
return task_ref return task_ref
def _check_session(self, params): def _check_session(self, params):

View File

@ -20,14 +20,13 @@
Handles all requests relating to volumes. Handles all requests relating to volumes.
""" """
import datetime
from nova import db
from nova import exception 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 quota from nova import quota
from nova import rpc from nova import rpc
from nova import utils
from nova.db import base from nova.db import base
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@ -78,7 +77,7 @@ class API(base.Base):
volume = self.get(context, volume_id) volume = self.get(context, volume_id)
if volume['status'] != "available": if volume['status'] != "available":
raise exception.ApiError(_("Volume status must be available")) raise exception.ApiError(_("Volume status must be available"))
now = datetime.datetime.utcnow() now = utils.utcnow()
self.db.volume_update(context, volume_id, {'status': 'deleting', self.db.volume_update(context, volume_id, {'status': 'deleting',
'terminated_at': now}) 'terminated_at': now})
host = volume['host'] host = volume['host']

View File

@ -42,8 +42,6 @@ intact.
""" """
import datetime
from nova import context from nova import context
from nova import exception from nova import exception
@ -127,7 +125,7 @@ class VolumeManager(manager.SchedulerDependentManager):
volume_ref['id'], {'status': 'error'}) volume_ref['id'], {'status': 'error'})
raise raise
now = datetime.datetime.utcnow() now = utils.utcnow()
self.db.volume_update(context, self.db.volume_update(context,
volume_ref['id'], {'status': 'available', volume_ref['id'], {'status': 'available',
'launched_at': now}) 'launched_at': now})