timeutils.utcnow should be used instead of datetime.datetime.utcnow

Developer should use timeutils.utcnow to replace the
datetime.datetime.utcnow.

Change-Id: Ifaca72e286a805632cd17406464175ba6819a9b0
Closes-Bug: #1513308
changes/71/241871/3
wangqun 7 years ago
parent d90bc0ef7d
commit 8c7925a018
  1. 2
      HACKING.rst
  2. 7
      magnum/api/controllers/v1/bay.py
  3. 7
      magnum/api/controllers/v1/baymodel.py
  4. 5
      magnum/api/controllers/v1/certificate.py
  5. 7
      magnum/api/controllers/v1/container.py
  6. 7
      magnum/api/controllers/v1/node.py
  7. 7
      magnum/api/controllers/v1/pod.py
  8. 7
      magnum/api/controllers/v1/replicationcontroller.py
  9. 7
      magnum/api/controllers/v1/service.py
  10. 5
      magnum/api/controllers/v1/x509keypair.py
  11. 14
      magnum/hacking/checks.py
  12. 7
      magnum/tests/unit/api/controllers/v1/test_base.py
  13. 2
      magnum/tests/unit/objects/test_objects.py
  14. 17
      magnum/tests/unit/test_hacking.py

@ -9,6 +9,8 @@ Magnum Specific Commandments
----------------------------
- [M301] policy.enforce_wsgi decorator must be the first decorator on a method.
- [M310] timeutils.utcnow() wrapper must be used instead of direct calls to
datetime.datetime.utcnow() to make it easy to override its return value.
- [M318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert
like assertIsNone(A)
- [M322] Method's default argument shouldn't be mutable.

@ -13,8 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -149,8 +148,8 @@ class Bay(base.APIBase):
status_reason="CREATE completed successfully",
api_address='172.24.4.3',
node_addresses=['172.24.4.4', '172.24.4.5'],
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand)

@ -12,10 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import glanceclient.exc
import novaclient.exceptions as nova_exc
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -178,8 +177,8 @@ class BayModel(base.APIBase):
https_proxy='https://proxy.com:123',
no_proxy='192.168.0.1,192.168.0.2,192.168.0.3',
labels={'key1': 'val1', 'key2': 'val2'},
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow(),
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow(),
public=False),
return cls._convert_with_links(sample, 'http://localhost:9511', expand)

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -110,7 +109,7 @@ class Certificate(base.APIBase):
@classmethod
def sample(cls, expand=True):
sample = cls(bay_uuid='7ae81bb3-dec3-4289-8d6c-da80bd8001ae',
created_at=datetime.datetime.utcnow(),
created_at=timeutils.utcnow(),
csr='AAA....AAA')
return cls._convert_with_links(sample, 'http://localhost:9511', expand)

@ -13,9 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_log import log as logging
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -135,8 +134,8 @@ class Container(base.APIBase):
status='Running',
memory='512m',
bay_uuid="fff114da-3bfa-4a0f-a123-c0dffad9718e",
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand)

@ -13,8 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -90,8 +89,8 @@ class Node(base.APIBase):
type='virt',
image_id='Fedora-k8s',
ironic_node_id='4b6ec4a9-d412-494a-be77-a2fd16361402',
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand)

@ -11,8 +11,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -114,8 +113,8 @@ class Pod(v1_base.K8sResourceBase):
]
}
}''',
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
def parse_manifest(self):

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -130,8 +129,8 @@ class ReplicationController(v1_base.K8sResourceBase):
}
}
}''',
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
def parse_manifest(self):

@ -10,8 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -121,8 +120,8 @@ class Service(v1_base.K8sResourceBase):
}
}
}''',
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
def parse_manifest(self):

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from oslo_utils import timeutils
import pecan
from pecan import rest
import wsme
@ -121,7 +120,7 @@ class X509KeyPair(base.APIBase):
sample = cls(uuid='f978db47-9a37-4e9f-8572-804a10abc0aa',
name='MyX509KeyPair',
bay_uuid='7ae81bb3-dec3-4289-8d6c-da80bd8001ae',
created_at=datetime.datetime.utcnow(),
created_at=timeutils.utcnow(),
ca_cert='AAA....AAA',
certificate='BBB....BBB',
private_key='CCC....CCC')

@ -123,6 +123,19 @@ def assert_equal_in(logical_line):
"contents.")
def use_timeutils_utcnow(logical_line, filename):
# tools are OK to use the standard datetime module
if "/tools/" in filename:
return
msg = "M310: timeutils.utcnow() must be used instead of datetime.%s()"
datetime_funcs = ['now', 'utcnow']
for f in datetime_funcs:
pos = logical_line.find('datetime.%s' % f)
if pos != -1:
yield (pos, msg % f)
def factory(register):
register(check_policy_enforce_decorator)
register(no_mutable_default_args)
@ -131,3 +144,4 @@ def factory(register):
register(assert_equal_not_none)
register(assert_true_isinstance)
register(assert_equal_in)
register(use_timeutils_utcnow)

@ -9,9 +9,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
from mock import patch
from oslo_utils import timeutils
from magnum.api.controllers.v1 import base as api_base
from magnum.tests import base
@ -22,8 +21,8 @@ class TestK8sResourceBase(base.BaseTestCase):
super(TestK8sResourceBase, self).setUp()
self.resource_base = api_base.K8sResourceBase(
uuid='fe78db47-9a37-4e9f-8572-804a10abc0aa',
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow())
def test_get_manifest_with_manifest(self):
expected_manifest = 'expected_manifest'

@ -96,7 +96,7 @@ class TestSubclassedObject(MyObj):
class TestUtils(test_base.TestCase):
def test_datetime_or_none(self):
naive_dt = datetime.datetime.now()
naive_dt = timeutils.utcnow()
dt = timeutils.parse_isotime(timeutils.isotime(naive_dt))
self.assertEqual(dt, utils.datetime_or_none(dt))
self.assertEqual(naive_dt.replace(tzinfo=iso8601.iso8601.Utc(),

@ -187,3 +187,20 @@ class HackingTestCase(base.TestCase):
code = "self.assertTrue()"
self._assert_has_no_errors(code, check)
def test_use_timeunitls_utcow(self):
errors = [(1, 0, "M310")]
check = checks.use_timeutils_utcnow
filename = "magnum/api/controller/v1/baymodel.py"
code = "datetime.now"
self._assert_has_errors(code, check, errors, filename)
code = "datetime.utcnow"
self._assert_has_errors(code, check, errors, filename)
code = "datetime.aa"
self._assert_has_no_errors(code, check, filename)
code = "aaa"
self._assert_has_no_errors(code, check, filename)

Loading…
Cancel
Save