summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn L. Villalovos <openstack.org@sodarock.com>2018-02-05 22:29:38 -0800
committerJohn L. Villalovos <openstack.org@sodarock.com>2018-02-06 21:10:08 -0800
commit1e0570b2a598b43a85faa28e07ab97d56bf5ba5e (patch)
tree2703a8e3187cccb1c52ac15db19a54e6d1f63984
parenta449545a196c6940e56fd1e319b96f85f588ba0d (diff)
downloadoslo-utils-1e0570b2a598b43a85faa28e07ab97d56bf5ba5e.tar.gz
Fix breaking unit tests due to iso8601 changes
The move from iso8601===0.1.11 to iso8601===0.1.12 broke unit tests in oslo.utils. iso8601 used to do: from datetime import datetime But now they call datetime.datetime(): import datetime datetime.datetime() Unfortunately the unit tests that mocked datetime.datetime() are now mocking the one in iso8601. This causes a failure in the unit tests. Fix this by using the 'wraps' argument to mock. So that the calls will get passed through to datetime.datetime. Also changed to using the decorator style of mock. In addition Python 3 unit tests were broken due to changing how the UTC time zone is represented from 'UTC' to 'UTC+00:00'. Closes-Bug: #1747575 Closes-Bug: #1744160 Change-Id: Ia80ffb5e571cc5366bef2bc1a32c457a3c16843d (cherry picked from commit 010fe3b1023871740b57dbc450f80e6c0c0f6e43)
-rw-r--r--oslo_utils/tests/test_timeutils.py52
-rw-r--r--oslo_utils/timeutils.py9
2 files changed, 31 insertions, 30 deletions
diff --git a/oslo_utils/tests/test_timeutils.py b/oslo_utils/tests/test_timeutils.py
index b1b1f8a..77b1aa1 100644
--- a/oslo_utils/tests/test_timeutils.py
+++ b/oslo_utils/tests/test_timeutils.py
@@ -87,20 +87,18 @@ class TimeUtilsTest(test_base.BaseTestCase):
t = timeutils.parse_strtime(s)
self.assertEqual(orig_t, t)
- def _test_is_older_than(self, fn):
- strptime = datetime.datetime.strptime
- with mock.patch('datetime.datetime') as datetime_mock:
- datetime_mock.utcnow.return_value = self.skynet_self_aware_time
- datetime_mock.strptime = strptime
- expect_true = timeutils.is_older_than(fn(self.one_minute_before),
- 59)
- self.assertTrue(expect_true)
- expect_false = timeutils.is_older_than(fn(self.one_minute_before),
- 60)
- self.assertFalse(expect_false)
- expect_false = timeutils.is_older_than(fn(self.one_minute_before),
- 61)
- self.assertFalse(expect_false)
+ @mock.patch('datetime.datetime', wraps=datetime.datetime)
+ def _test_is_older_than(self, fn, datetime_mock):
+ datetime_mock.utcnow.return_value = self.skynet_self_aware_time
+ expect_true = timeutils.is_older_than(fn(self.one_minute_before),
+ 59)
+ self.assertTrue(expect_true)
+ expect_false = timeutils.is_older_than(fn(self.one_minute_before),
+ 60)
+ self.assertFalse(expect_false)
+ expect_false = timeutils.is_older_than(fn(self.one_minute_before),
+ 61)
+ self.assertFalse(expect_false)
def test_is_older_than_datetime(self):
self._test_is_older_than(lambda x: x)
@@ -118,20 +116,18 @@ class TimeUtilsTest(test_base.BaseTestCase):
tzinfo=iso8601.iso8601.FixedOffset(1, 0, 'foo')).replace(
hour=7))
- def _test_is_newer_than(self, fn):
- strptime = datetime.datetime.strptime
- with mock.patch('datetime.datetime') as datetime_mock:
- datetime_mock.utcnow.return_value = self.skynet_self_aware_time
- datetime_mock.strptime = strptime
- expect_true = timeutils.is_newer_than(fn(self.one_minute_after),
- 59)
- self.assertTrue(expect_true)
- expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
- 60)
- self.assertFalse(expect_false)
- expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
- 61)
- self.assertFalse(expect_false)
+ @mock.patch('datetime.datetime', wraps=datetime.datetime)
+ def _test_is_newer_than(self, fn, datetime_mock):
+ datetime_mock.utcnow.return_value = self.skynet_self_aware_time
+ expect_true = timeutils.is_newer_than(fn(self.one_minute_after),
+ 59)
+ self.assertTrue(expect_true)
+ expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
+ 60)
+ self.assertFalse(expect_false)
+ expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
+ 61)
+ self.assertFalse(expect_false)
def test_is_newer_than_datetime(self):
self._test_is_newer_than(lambda x: x)
diff --git a/oslo_utils/timeutils.py b/oslo_utils/timeutils.py
index d467972..e8eb990 100644
--- a/oslo_utils/timeutils.py
+++ b/oslo_utils/timeutils.py
@@ -55,7 +55,8 @@ def isotime(at=None, subsecond=False):
if not subsecond
else _ISO8601_TIME_FORMAT_SUBSECOND)
tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
- st += ('Z' if tz == 'UTC' else tz)
+ # Need to handle either iso8601 or python UTC format
+ st += ('Z' if tz in ('UTC', 'UTC+00:00') else tz)
return st
@@ -256,7 +257,9 @@ def marshall_now(now=None):
minute=now.minute, second=now.second,
microsecond=now.microsecond)
if now.tzinfo:
- d['tzname'] = now.tzinfo.tzname(None)
+ # Need to handle either iso8601 or python UTC format
+ tzname = now.tzinfo.tzname(None)
+ d['tzname'] = 'UTC' if tzname == 'UTC+00:00' else tzname
return d
@@ -283,6 +286,8 @@ def unmarshall_time(tyme):
microsecond=tyme['microsecond'])
tzname = tyme.get('tzname')
if tzname:
+ # Need to handle either iso8601 or python UTC format
+ tzname = 'UTC' if tzname == 'UTC+00:00' else tzname
tzinfo = pytz.timezone(tzname)
dt = tzinfo.localize(dt)
return dt