From: Yuriy Nesenenko Date: Fri, 27 Feb 2015 14:06:03 +0000 (+0200) Subject: Change datetime.now() to timeutils.utcnow() from oslo_utils X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=931c34d38b1839bd48500055fff9003f4885143d;p=openstack-build%2Fcinder-build.git Change datetime.now() to timeutils.utcnow() from oslo_utils We use an UTC time to avoid the difference with time zones. Change-Id: I15aa3b5d3337b90ccdcc6c4ac5d3c7d78108fe21 Related-Bug: #1288979 --- diff --git a/HACKING.rst b/HACKING.rst index 750d93047..75f0111e5 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -15,6 +15,7 @@ Cinder Specific Commandments - [N327] assert_called_once is not a valid Mock method. - [N333] Ensure that oslo namespaces are used for namespaced libraries. - [N339] Prevent use of deprecated contextlib.nested. +- [C301] timeutils.utcnow() from oslo_utils should be used instead of datetime.now(). General diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index ff214b70b..5a88fccad 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -3571,7 +3571,7 @@ def purge_deleted_rows(context, age_in_days): LOG.info(_LI('Purging deleted rows older than age=%(age)d days ' 'from table=%(table)s'), {'age': age_in_days, 'table': table}) - deleted_age = dt.datetime.now() - dt.timedelta(days=age_in_days) + deleted_age = timeutils.utcnow() - dt.timedelta(days=age_in_days) try: with session.begin(): result = session.execute( diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/021_add_default_quota_class.py b/cinder/db/sqlalchemy/migrate_repo/versions/021_add_default_quota_class.py index ee6911629..f7fc514a5 100644 --- a/cinder/db/sqlalchemy/migrate_repo/versions/021_add_default_quota_class.py +++ b/cinder/db/sqlalchemy/migrate_repo/versions/021_add_default_quota_class.py @@ -31,7 +31,7 @@ CONF.import_opt('quota_gigabytes', 'cinder.quota') LOG = logging.getLogger(__name__) CLASS_NAME = 'default' -CREATED_AT = datetime.datetime.now() +CREATED_AT = datetime.datetime.now() # noqa def upgrade(migrate_engine): diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/026_add_consistencygroup_quota_class.py b/cinder/db/sqlalchemy/migrate_repo/versions/026_add_consistencygroup_quota_class.py index 361554c5e..7d2413eaa 100644 --- a/cinder/db/sqlalchemy/migrate_repo/versions/026_add_consistencygroup_quota_class.py +++ b/cinder/db/sqlalchemy/migrate_repo/versions/026_add_consistencygroup_quota_class.py @@ -29,7 +29,7 @@ CONF.import_opt('quota_consistencygroups', 'cinder.quota') LOG = logging.getLogger(__name__) CLASS_NAME = 'default' -CREATED_AT = datetime.datetime.now() +CREATED_AT = datetime.datetime.now() # noqa def upgrade(migrate_engine): diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/040_add_volume_attachment.py b/cinder/db/sqlalchemy/migrate_repo/versions/040_add_volume_attachment.py index 0c05af48e..e5b78c6bd 100644 --- a/cinder/db/sqlalchemy/migrate_repo/versions/040_add_volume_attachment.py +++ b/cinder/db/sqlalchemy/migrate_repo/versions/040_add_volume_attachment.py @@ -25,7 +25,7 @@ from cinder.i18n import _LE LOG = logging.getLogger(__name__) -CREATED_AT = datetime.datetime.now() +CREATED_AT = datetime.datetime.now() # noqa def upgrade(migrate_engine): diff --git a/cinder/hacking/checks.py b/cinder/hacking/checks.py index 972748658..3f356cc0a 100644 --- a/cinder/hacking/checks.py +++ b/cinder/hacking/checks.py @@ -147,6 +147,16 @@ def check_no_contextlib_nested(logical_line): yield(0, msg) +def check_datetime_now(logical_line, noqa): + if noqa: + return + + msg = ("C301: Found datetime.now(). " + "Please use timeutils.utcnow() from oslo_utils.") + if 'datetime.now' in logical_line: + yield(0, msg) + + def factory(register): register(no_vi_headers) register(no_translate_debug_logs) @@ -156,3 +166,4 @@ def factory(register): register(check_assert_called_once) register(check_oslo_namespace_imports) register(check_no_contextlib_nested) + register(check_datetime_now) diff --git a/cinder/tests/api/contrib/test_volume_host_attribute.py b/cinder/tests/api/contrib/test_volume_host_attribute.py index e675affe1..2505fca9a 100644 --- a/cinder/tests/api/contrib/test_volume_host_attribute.py +++ b/cinder/tests/api/contrib/test_volume_host_attribute.py @@ -12,11 +12,11 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime import json import uuid from lxml import etree +from oslo_utils import timeutils import webob from cinder import context @@ -33,7 +33,7 @@ def fake_volume_get(*args, **kwargs): 'status': 'available', 'size': 5, 'availability_zone': 'somewhere', - 'created_at': datetime.datetime.now(), + 'created_at': timeutils.utcnow(), 'attach_status': None, 'display_name': 'anothervolume', 'display_description': 'Just another volume!', diff --git a/cinder/tests/api/contrib/test_volume_image_metadata.py b/cinder/tests/api/contrib/test_volume_image_metadata.py index 09882ede4..954245b9d 100644 --- a/cinder/tests/api/contrib/test_volume_image_metadata.py +++ b/cinder/tests/api/contrib/test_volume_image_metadata.py @@ -12,11 +12,11 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime import json import uuid from xml.dom import minidom +from oslo_utils import timeutils import webob from cinder.api import common @@ -34,7 +34,7 @@ def fake_volume_get(*args, **kwargs): 'status': 'available', 'size': 5, 'availability_zone': 'somewhere', - 'created_at': datetime.datetime.now(), + 'created_at': timeutils.utcnow(), 'attach_status': None, 'display_name': 'anothervolume', 'display_description': 'Just another volume!', diff --git a/cinder/tests/api/contrib/test_volume_migration_status_attribute.py b/cinder/tests/api/contrib/test_volume_migration_status_attribute.py index ee390cc0e..b99f009c2 100644 --- a/cinder/tests/api/contrib/test_volume_migration_status_attribute.py +++ b/cinder/tests/api/contrib/test_volume_migration_status_attribute.py @@ -12,11 +12,11 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime import json import uuid from lxml import etree +from oslo_utils import timeutils import webob from cinder import context @@ -32,7 +32,7 @@ def fake_volume_get(*args, **kwargs): 'status': 'available', 'size': 5, 'availability_zone': 'somewhere', - 'created_at': datetime.datetime.now(), + 'created_at': timeutils.utcnow(), 'attach_status': None, 'display_name': 'anothervolume', 'display_description': 'Just another volume!', diff --git a/cinder/tests/api/contrib/test_volume_tenant_attribute.py b/cinder/tests/api/contrib/test_volume_tenant_attribute.py index cbf558195..5c671c2be 100644 --- a/cinder/tests/api/contrib/test_volume_tenant_attribute.py +++ b/cinder/tests/api/contrib/test_volume_tenant_attribute.py @@ -12,11 +12,11 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime import json import uuid from lxml import etree +from oslo_utils import timeutils import webob from cinder import context @@ -35,7 +35,7 @@ def fake_volume_get(*args, **kwargs): 'status': 'available', 'size': 5, 'availability_zone': 'somewhere', - 'created_at': datetime.datetime.now(), + 'created_at': timeutils.utcnow(), 'attach_status': None, 'display_name': 'anothervolume', 'display_description': 'Just another volume!', diff --git a/cinder/tests/api/v1/test_snapshots.py b/cinder/tests/api/v1/test_snapshots.py index 476a7a5ec..bc6d76f82 100644 --- a/cinder/tests/api/v1/test_snapshots.py +++ b/cinder/tests/api/v1/test_snapshots.py @@ -13,11 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime - from lxml import etree import mock from oslo_log import log as logging +from oslo_utils import timeutils import webob from cinder.api.v1 import snapshots @@ -455,7 +454,7 @@ class SnapshotSerializerTest(test.TestCase): id='snap_id', status='snap_status', size=1024, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), display_name='snap_name', display_description='snap_desc', volume_id='vol_id', ) @@ -470,14 +469,14 @@ class SnapshotSerializerTest(test.TestCase): raw_snapshots = [dict(id='snap1_id', status='snap1_status', size=1024, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), display_name='snap1_name', display_description='snap1_desc', volume_id='vol1_id', ), dict(id='snap2_id', status='snap2_status', size=1024, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), display_name='snap2_name', display_description='snap2_desc', volume_id='vol2_id', )] diff --git a/cinder/tests/api/v1/test_volumes.py b/cinder/tests/api/v1/test_volumes.py index 3f45952ff..6052a7efa 100644 --- a/cinder/tests/api/v1/test_volumes.py +++ b/cinder/tests/api/v1/test_volumes.py @@ -18,6 +18,7 @@ import datetime from lxml import etree import mock from oslo_config import cfg +from oslo_utils import timeutils import webob from cinder.api import extensions @@ -835,7 +836,7 @@ class VolumeSerializerTest(test.TestCase): size=1024, availability_zone='vol_availability', bootable='false', - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), attachments=[dict(id='vol_id', volume_id='vol_id', server_id='instance_uuid', @@ -860,7 +861,7 @@ class VolumeSerializerTest(test.TestCase): size=1024, availability_zone='vol1_availability', bootable='true', - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), attachments=[dict(id='vol1_id', volume_id='vol1_id', server_id='instance_uuid', @@ -877,7 +878,7 @@ class VolumeSerializerTest(test.TestCase): size=1024, availability_zone='vol2_availability', bootable='true', - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), attachments=[dict(id='vol2_id', volume_id='vol2_id', server_id='instance_uuid', diff --git a/cinder/tests/api/v2/test_snapshots.py b/cinder/tests/api/v2/test_snapshots.py index 77c7ffee9..dde584adc 100644 --- a/cinder/tests/api/v2/test_snapshots.py +++ b/cinder/tests/api/v2/test_snapshots.py @@ -13,11 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime - from lxml import etree import mock from oslo_log import log as logging +from oslo_utils import timeutils import webob from cinder.api.v2 import snapshots @@ -484,7 +483,7 @@ class SnapshotSerializerTest(test.TestCase): id='snap_id', status='snap_status', size=1024, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), name='snap_name', description='snap_desc', display_description='snap_desc', @@ -503,7 +502,7 @@ class SnapshotSerializerTest(test.TestCase): id='snap1_id', status='snap1_status', size=1024, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), name='snap1_name', description='snap1_desc', volume_id='vol1_id', @@ -512,7 +511,7 @@ class SnapshotSerializerTest(test.TestCase): id='snap2_id', status='snap2_status', size=1024, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), name='snap2_name', description='snap2_desc', volume_id='vol2_id', diff --git a/cinder/tests/api/v2/test_volumes.py b/cinder/tests/api/v2/test_volumes.py index 921639890..f65a26614 100644 --- a/cinder/tests/api/v2/test_volumes.py +++ b/cinder/tests/api/v2/test_volumes.py @@ -19,6 +19,7 @@ import datetime from lxml import etree import mock from oslo_config import cfg +from oslo_utils import timeutils import six import six.moves.urllib.parse as urlparse import webob @@ -1601,7 +1602,7 @@ class VolumeSerializerTest(test.TestCase): size=1024, availability_zone='vol_availability', bootable=False, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), attachments=[ dict( id='vol_id', @@ -1635,7 +1636,7 @@ class VolumeSerializerTest(test.TestCase): size=1024, availability_zone='vol1_availability', bootable=True, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), attachments=[ dict( id='vol1_id', @@ -1657,7 +1658,7 @@ class VolumeSerializerTest(test.TestCase): size=1024, availability_zone='vol2_availability', bootable=False, - created_at=datetime.datetime.now(), + created_at=timeutils.utcnow(), attachments=[dict(id='vol2_id', volume_id='vol2_id', server_id='instance_uuid', diff --git a/cinder/tests/db/test_purge.py b/cinder/tests/db/test_purge.py index e051bc403..94f4adebb 100644 --- a/cinder/tests/db/test_purge.py +++ b/cinder/tests/db/test_purge.py @@ -19,6 +19,7 @@ import datetime import uuid from oslo_log import log as logging +from oslo_utils import timeutils from cinder import context from cinder import db @@ -55,8 +56,8 @@ class PurgeDeletedTest(test.TestCase): ins_stmt = self.vm.insert().values(volume_id=uuidstr) self.conn.execute(ins_stmt) # Set 4 of them deleted, 2 are 60 days ago, 2 are 20 days ago - old = datetime.datetime.now() - datetime.timedelta(days=20) - older = datetime.datetime.now() - datetime.timedelta(days=60) + old = timeutils.utcnow() - datetime.timedelta(days=20) + older = timeutils.utcnow() - datetime.timedelta(days=60) make_old = self.volumes.update().\ where(self.volumes.c.id.in_(self.uuidstrs[1:3]))\ .values(deleted_at=old) diff --git a/cinder/tests/test_backup_tsm.py b/cinder/tests/test_backup_tsm.py index 0a552abb7..035fa1f00 100644 --- a/cinder/tests/test_backup_tsm.py +++ b/cinder/tests/test_backup_tsm.py @@ -17,13 +17,13 @@ Tests for volume backup to IBM Tivoli Storage Manager (TSM). """ -import datetime import json import os import posix from oslo_concurrency import processutils as putils from oslo_log import log as logging +from oslo_utils import timeutils from cinder.backup.drivers import tsm from cinder import context @@ -75,7 +75,7 @@ class TSMBackupSimulator: self._backup_list[path] = [] else: self._backup_list[path][-1]['active'] = False - date = datetime.datetime.now() + date = timeutils.utcnow() datestr = date.strftime("%m/%d/%Y %H:%M:%S") self._backup_list[path].append({'date': datestr, 'active': True}) retcode = 0 diff --git a/cinder/tests/test_hacking.py b/cinder/tests/test_hacking.py index 365152b31..91eb70e02 100644 --- a/cinder/tests/test_hacking.py +++ b/cinder/tests/test_hacking.py @@ -169,3 +169,13 @@ class HackingTestCase(test.TestCase): "with contextlib.nested(")))) self.assertEqual(0, len(list(checks.check_no_contextlib_nested( "with foo as bar")))) + + def test_check_datetime_now(self): + self.assertEqual(1, len(list(checks.check_datetime_now( + "datetime.now", False)))) + self.assertEqual(0, len(list(checks.check_datetime_now( + "timeutils.utcnow", False)))) + + def test_check_datetime_now_noqa(self): + self.assertEqual(0, len(list(checks.check_datetime_now( + "datetime.now() # noqa", True)))) \ No newline at end of file diff --git a/cinder/volume/drivers/zfssa/zfssanfs.py b/cinder/volume/drivers/zfssa/zfssanfs.py index 31d37f64d..721f11c0a 100644 --- a/cinder/volume/drivers/zfssa/zfssanfs.py +++ b/cinder/volume/drivers/zfssa/zfssanfs.py @@ -263,7 +263,9 @@ class ZFSSANFSDriver(nfs.NfsDriver): def _create_snapshot_name(self): """Creates a snapshot name from the date and time.""" - return 'cinder-zfssa-nfs-snapshot-%s' % dt.datetime.now().isoformat() + + return ('cinder-zfssa-nfs-snapshot-%s' % + dt.datetime.utcnow().isoformat()) def _get_share_capacity_info(self): """Get available and used capacity info for the NFS share."""