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
This commit is contained in:
wangqun 2015-11-05 04:44:59 +00:00
parent d90bc0ef7d
commit 8c7925a018
14 changed files with 62 additions and 39 deletions

View File

@ -9,6 +9,8 @@ Magnum Specific Commandments
---------------------------- ----------------------------
- [M301] policy.enforce_wsgi decorator must be the first decorator on a method. - [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 - [M318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert
like assertIsNone(A) like assertIsNone(A)
- [M322] Method's default argument shouldn't be mutable. - [M322] Method's default argument shouldn't be mutable.

View File

@ -13,8 +13,7 @@
# 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 from oslo_utils import timeutils
import pecan import pecan
from pecan import rest from pecan import rest
import wsme import wsme
@ -149,8 +148,8 @@ class Bay(base.APIBase):
status_reason="CREATE completed successfully", status_reason="CREATE completed successfully",
api_address='172.24.4.3', api_address='172.24.4.3',
node_addresses=['172.24.4.4', '172.24.4.5'], node_addresses=['172.24.4.4', '172.24.4.5'],
created_at=datetime.datetime.utcnow(), created_at=timeutils.utcnow(),
updated_at=datetime.datetime.utcnow()) updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand) return cls._convert_with_links(sample, 'http://localhost:9511', expand)

View File

@ -12,10 +12,9 @@
# 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 glanceclient.exc import glanceclient.exc
import novaclient.exceptions as nova_exc import novaclient.exceptions as nova_exc
from oslo_utils import timeutils
import pecan import pecan
from pecan import rest from pecan import rest
import wsme import wsme
@ -178,8 +177,8 @@ class BayModel(base.APIBase):
https_proxy='https://proxy.com:123', https_proxy='https://proxy.com:123',
no_proxy='192.168.0.1,192.168.0.2,192.168.0.3', no_proxy='192.168.0.1,192.168.0.2,192.168.0.3',
labels={'key1': 'val1', 'key2': 'val2'}, labels={'key1': 'val1', 'key2': 'val2'},
created_at=datetime.datetime.utcnow(), created_at=timeutils.utcnow(),
updated_at=datetime.datetime.utcnow(), updated_at=timeutils.utcnow(),
public=False), public=False),
return cls._convert_with_links(sample, 'http://localhost:9511', expand) return cls._convert_with_links(sample, 'http://localhost:9511', expand)

View File

@ -12,8 +12,7 @@
# 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 from oslo_utils import timeutils
import pecan import pecan
from pecan import rest from pecan import rest
import wsme import wsme
@ -110,7 +109,7 @@ class Certificate(base.APIBase):
@classmethod @classmethod
def sample(cls, expand=True): def sample(cls, expand=True):
sample = cls(bay_uuid='7ae81bb3-dec3-4289-8d6c-da80bd8001ae', sample = cls(bay_uuid='7ae81bb3-dec3-4289-8d6c-da80bd8001ae',
created_at=datetime.datetime.utcnow(), created_at=timeutils.utcnow(),
csr='AAA....AAA') csr='AAA....AAA')
return cls._convert_with_links(sample, 'http://localhost:9511', expand) return cls._convert_with_links(sample, 'http://localhost:9511', expand)

View File

@ -13,9 +13,8 @@
# 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
from oslo_log import log as logging from oslo_log import log as logging
from oslo_utils import timeutils
import pecan import pecan
from pecan import rest from pecan import rest
import wsme import wsme
@ -135,8 +134,8 @@ class Container(base.APIBase):
status='Running', status='Running',
memory='512m', memory='512m',
bay_uuid="fff114da-3bfa-4a0f-a123-c0dffad9718e", bay_uuid="fff114da-3bfa-4a0f-a123-c0dffad9718e",
created_at=datetime.datetime.utcnow(), created_at=timeutils.utcnow(),
updated_at=datetime.datetime.utcnow()) updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand) return cls._convert_with_links(sample, 'http://localhost:9511', expand)

View File

@ -13,8 +13,7 @@
# 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 from oslo_utils import timeutils
import pecan import pecan
from pecan import rest from pecan import rest
import wsme import wsme
@ -90,8 +89,8 @@ class Node(base.APIBase):
type='virt', type='virt',
image_id='Fedora-k8s', image_id='Fedora-k8s',
ironic_node_id='4b6ec4a9-d412-494a-be77-a2fd16361402', ironic_node_id='4b6ec4a9-d412-494a-be77-a2fd16361402',
created_at=datetime.datetime.utcnow(), created_at=timeutils.utcnow(),
updated_at=datetime.datetime.utcnow()) updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand) return cls._convert_with_links(sample, 'http://localhost:9511', expand)

View File

@ -11,8 +11,7 @@
# 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 from oslo_utils import timeutils
import pecan import pecan
from pecan import rest from pecan import rest
import wsme import wsme
@ -114,8 +113,8 @@ class Pod(v1_base.K8sResourceBase):
] ]
} }
}''', }''',
created_at=datetime.datetime.utcnow(), created_at=timeutils.utcnow(),
updated_at=datetime.datetime.utcnow()) updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand) return cls._convert_with_links(sample, 'http://localhost:9511', expand)
def parse_manifest(self): def parse_manifest(self):

View File

@ -12,8 +12,7 @@
# 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 from oslo_utils import timeutils
import pecan import pecan
from pecan import rest from pecan import rest
import wsme import wsme
@ -130,8 +129,8 @@ class ReplicationController(v1_base.K8sResourceBase):
} }
} }
}''', }''',
created_at=datetime.datetime.utcnow(), created_at=timeutils.utcnow(),
updated_at=datetime.datetime.utcnow()) updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand) return cls._convert_with_links(sample, 'http://localhost:9511', expand)
def parse_manifest(self): def parse_manifest(self):

View File

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

View File

@ -12,8 +12,7 @@
# 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 from oslo_utils import timeutils
import pecan import pecan
from pecan import rest from pecan import rest
import wsme import wsme
@ -121,7 +120,7 @@ class X509KeyPair(base.APIBase):
sample = cls(uuid='f978db47-9a37-4e9f-8572-804a10abc0aa', sample = cls(uuid='f978db47-9a37-4e9f-8572-804a10abc0aa',
name='MyX509KeyPair', name='MyX509KeyPair',
bay_uuid='7ae81bb3-dec3-4289-8d6c-da80bd8001ae', bay_uuid='7ae81bb3-dec3-4289-8d6c-da80bd8001ae',
created_at=datetime.datetime.utcnow(), created_at=timeutils.utcnow(),
ca_cert='AAA....AAA', ca_cert='AAA....AAA',
certificate='BBB....BBB', certificate='BBB....BBB',
private_key='CCC....CCC') private_key='CCC....CCC')

View File

@ -123,6 +123,19 @@ def assert_equal_in(logical_line):
"contents.") "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): def factory(register):
register(check_policy_enforce_decorator) register(check_policy_enforce_decorator)
register(no_mutable_default_args) register(no_mutable_default_args)
@ -131,3 +144,4 @@ def factory(register):
register(assert_equal_not_none) register(assert_equal_not_none)
register(assert_true_isinstance) register(assert_true_isinstance)
register(assert_equal_in) register(assert_equal_in)
register(use_timeutils_utcnow)

View File

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

View File

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

View File

@ -187,3 +187,20 @@ class HackingTestCase(base.TestCase):
code = "self.assertTrue()" code = "self.assertTrue()"
self._assert_has_no_errors(code, check) 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)