From 8c7925a018c9c6d86aa6f55537a69d8c6e749908 Mon Sep 17 00:00:00 2001 From: wangqun Date: Thu, 5 Nov 2015 04:44:59 +0000 Subject: [PATCH] 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 --- HACKING.rst | 2 ++ magnum/api/controllers/v1/bay.py | 7 +++---- magnum/api/controllers/v1/baymodel.py | 7 +++---- magnum/api/controllers/v1/certificate.py | 5 ++--- magnum/api/controllers/v1/container.py | 7 +++---- magnum/api/controllers/v1/node.py | 7 +++---- magnum/api/controllers/v1/pod.py | 7 +++---- .../api/controllers/v1/replicationcontroller.py | 7 +++---- magnum/api/controllers/v1/service.py | 7 +++---- magnum/api/controllers/v1/x509keypair.py | 5 ++--- magnum/hacking/checks.py | 14 ++++++++++++++ .../tests/unit/api/controllers/v1/test_base.py | 7 +++---- magnum/tests/unit/objects/test_objects.py | 2 +- magnum/tests/unit/test_hacking.py | 17 +++++++++++++++++ 14 files changed, 62 insertions(+), 39 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index ec8822af63..99d89d30cf 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -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. diff --git a/magnum/api/controllers/v1/bay.py b/magnum/api/controllers/v1/bay.py index b6b6908737..cc386ec452 100644 --- a/magnum/api/controllers/v1/bay.py +++ b/magnum/api/controllers/v1/bay.py @@ -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) diff --git a/magnum/api/controllers/v1/baymodel.py b/magnum/api/controllers/v1/baymodel.py index 167a25eb46..0101a73414 100644 --- a/magnum/api/controllers/v1/baymodel.py +++ b/magnum/api/controllers/v1/baymodel.py @@ -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) diff --git a/magnum/api/controllers/v1/certificate.py b/magnum/api/controllers/v1/certificate.py index eecaaf3e57..c8afec65c7 100644 --- a/magnum/api/controllers/v1/certificate.py +++ b/magnum/api/controllers/v1/certificate.py @@ -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) diff --git a/magnum/api/controllers/v1/container.py b/magnum/api/controllers/v1/container.py index 19e8d58d18..63be1013ee 100644 --- a/magnum/api/controllers/v1/container.py +++ b/magnum/api/controllers/v1/container.py @@ -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) diff --git a/magnum/api/controllers/v1/node.py b/magnum/api/controllers/v1/node.py index 3b7992460c..d989160584 100644 --- a/magnum/api/controllers/v1/node.py +++ b/magnum/api/controllers/v1/node.py @@ -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) diff --git a/magnum/api/controllers/v1/pod.py b/magnum/api/controllers/v1/pod.py index 83ef87e189..3035fabe37 100644 --- a/magnum/api/controllers/v1/pod.py +++ b/magnum/api/controllers/v1/pod.py @@ -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): diff --git a/magnum/api/controllers/v1/replicationcontroller.py b/magnum/api/controllers/v1/replicationcontroller.py index eb526ff244..f5affac0ab 100644 --- a/magnum/api/controllers/v1/replicationcontroller.py +++ b/magnum/api/controllers/v1/replicationcontroller.py @@ -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): diff --git a/magnum/api/controllers/v1/service.py b/magnum/api/controllers/v1/service.py index 730e7ba4a5..fd47258e50 100644 --- a/magnum/api/controllers/v1/service.py +++ b/magnum/api/controllers/v1/service.py @@ -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): diff --git a/magnum/api/controllers/v1/x509keypair.py b/magnum/api/controllers/v1/x509keypair.py index cae63dee17..40cbd947b8 100644 --- a/magnum/api/controllers/v1/x509keypair.py +++ b/magnum/api/controllers/v1/x509keypair.py @@ -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') diff --git a/magnum/hacking/checks.py b/magnum/hacking/checks.py index 87bcc75ec9..b8b6d5c915 100644 --- a/magnum/hacking/checks.py +++ b/magnum/hacking/checks.py @@ -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) diff --git a/magnum/tests/unit/api/controllers/v1/test_base.py b/magnum/tests/unit/api/controllers/v1/test_base.py index e649e98271..d0820069a0 100644 --- a/magnum/tests/unit/api/controllers/v1/test_base.py +++ b/magnum/tests/unit/api/controllers/v1/test_base.py @@ -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' diff --git a/magnum/tests/unit/objects/test_objects.py b/magnum/tests/unit/objects/test_objects.py index 06a16edce0..0213bf9e81 100644 --- a/magnum/tests/unit/objects/test_objects.py +++ b/magnum/tests/unit/objects/test_objects.py @@ -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(), diff --git a/magnum/tests/unit/test_hacking.py b/magnum/tests/unit/test_hacking.py index a8bc863e0e..3a1ebf2abd 100644 --- a/magnum/tests/unit/test_hacking.py +++ b/magnum/tests/unit/test_hacking.py @@ -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)