]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Change datetime.now() to timeutils.utcnow() from oslo_utils
authorYuriy Nesenenko <ynesenenko@mirantis.com>
Fri, 27 Feb 2015 14:06:03 +0000 (16:06 +0200)
committerYuriy Nesenenko <ynesenenko@mirantis.com>
Thu, 19 Mar 2015 12:12:57 +0000 (14:12 +0200)
We use an UTC time to avoid the difference with time zones.

Change-Id: I15aa3b5d3337b90ccdcc6c4ac5d3c7d78108fe21
Related-Bug: #1288979

18 files changed:
HACKING.rst
cinder/db/sqlalchemy/api.py
cinder/db/sqlalchemy/migrate_repo/versions/021_add_default_quota_class.py
cinder/db/sqlalchemy/migrate_repo/versions/026_add_consistencygroup_quota_class.py
cinder/db/sqlalchemy/migrate_repo/versions/040_add_volume_attachment.py
cinder/hacking/checks.py
cinder/tests/api/contrib/test_volume_host_attribute.py
cinder/tests/api/contrib/test_volume_image_metadata.py
cinder/tests/api/contrib/test_volume_migration_status_attribute.py
cinder/tests/api/contrib/test_volume_tenant_attribute.py
cinder/tests/api/v1/test_snapshots.py
cinder/tests/api/v1/test_volumes.py
cinder/tests/api/v2/test_snapshots.py
cinder/tests/api/v2/test_volumes.py
cinder/tests/db/test_purge.py
cinder/tests/test_backup_tsm.py
cinder/tests/test_hacking.py
cinder/volume/drivers/zfssa/zfssanfs.py

index 750d93047984055e321c04c1be2e4b5e92e95fbe..75f0111e53324cc101a4b44749f45be1a678a35f 100644 (file)
@@ -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
index ff214b70b2860ea785db8c557edb0cf03bd2e45b..5a88fccad1139cadd81035ca646f862dbdda112d 100644 (file)
@@ -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(
index ee69116296936f8e57d6146daddd53d0ea9df7e6..f7fc514a52d7fd959e3b80d9c3b97a0d2c8a549c 100644 (file)
@@ -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):
index 361554c5e49cc34104f14d59d830318c80f3ee81..7d2413eaa7d21e1bb6d9b9ecb757297d60b80abc 100644 (file)
@@ -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):
index 0c05af48e12d2423d64c4ec8a63df71c86fc6827..e5b78c6bdd8228f2db4724a34cca4f74478499c2 100644 (file)
@@ -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):
index 972748658e6f53d2ace9a8e02a980bed52a6259d..3f356cc0ac90ce01101d9d0a1f615752e3de9e48 100644 (file)
@@ -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)
index e675affe180c5cbdecf9f9e7b960497067dda88b..2505fca9a8bca41dfe591dffbfbadd9eaba3b542 100644 (file)
 #   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!',
index 09882ede43985c2ed584caf791ff25701b4abeb3..954245b9d030db59c9475f846837082c49e7972a 100644 (file)
 #   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!',
index ee390cc0e804990361f4d6dd08f2425cff682ada..b99f009c2a123c600069dcd9520c55791200a7fe 100644 (file)
 # 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!',
index cbf558195d339a62d896ec4b95193b736b111151..5c671c2bee84c4fbfa4c3b8e954cdc6d572a6d7a 100644 (file)
 #   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!',
index 476a7a5ec39b3167f8546e5ff21df844e2efda23..bc6d76f824a87852f41571c01bdeb189cac4e3ed 100644 (file)
 #    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', )]
index 3f45952ff7d15768a835afba027acb9170579365..6052a7efabdf31b0728ce6e54e57645af7cf7441 100644 (file)
@@ -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',
index 77c7ffee9bed4998a9b89949573ee07edbf978c7..dde584adc0c6f27f3d2326cd03a8d45526a63dbe 100644 (file)
 #    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',
index 921639890f51840daeb3fafa1fc5dbf5380d909c..f65a26614d681a86b7a412abfd8046fb33ba6c4c 100644 (file)
@@ -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',
index e051bc403ad8477899dfbfb3244a0d22973c5e00..94f4adebba58c5e4141e6c3da99f94aeebc3c77c 100644 (file)
@@ -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)
index 0a552abb7d74ba22e1dec14a786cf342496a0f63..035fa1f00439cddd813316360fac27c03d93a5f6 100644 (file)
 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
index 365152b319d2e51dfb832d25ed3b0e427b30f7cd..91eb70e02aa8c8142390835c7555e3b1c37c61be 100644 (file)
@@ -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
index 31d37f64dd5ef160d3c679b7af1071fb69b96234..721f11c0a507fdb6dd7b40a3e1abfd9bf239f857 100644 (file)
@@ -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."""