Deal correctly with negative elapsed time

Since we're comparing local time to database time, we need to correctly
handle negative offsets to account for clock skew. The datetime.timedelta
class always reports a positive number of seconds, but with a possibly
negative number of *days*. Therefore, take the days into account.

Change-Id: I4422e4cf41cb57cdc89548d01b0abc27cda914ef
Closes-Bug: #1688327
This commit is contained in:
Zane Bitter 2017-05-04 11:41:55 -04:00
parent c44e2058d2
commit f3f05c9a3f
2 changed files with 11 additions and 1 deletions

View File

@ -2096,7 +2096,7 @@ class Stack(collections.Mapping):
start_time = timeutils.round_to_seconds(self.updated_time or
self.created_time)
nowish = timeutils.round_to_seconds(datetime.datetime.utcnow())
return (nowish - start_time).seconds
return int((nowish - start_time).total_seconds())
def time_remaining(self):
"""Time left before stack times out."""

View File

@ -120,6 +120,16 @@ class StackTest(common.HeatTestCase):
10, 10, 0)
self.assertEqual(600, self.stack.time_elapsed())
@mock.patch.object(stack, 'datetime')
def test_time_elapsed_negative(self, mock_dt):
self.stack = stack.Stack(self.ctx, 'test_stack', self.tmpl)
# dummy create time 10:00:00
self.stack.created_time = datetime.datetime(2015, 7, 27, 10, 0, 0)
# mock utcnow set to 09:59:50 (-10s offset)
mock_dt.datetime.utcnow.return_value = datetime.datetime(2015, 7, 27,
9, 59, 50)
self.assertEqual(-10, self.stack.time_elapsed())
@mock.patch.object(stack, 'datetime')
def test_time_elapsed_ms(self, mock_dt):
self.stack = stack.Stack(self.ctx, 'test_stack', self.tmpl)