Some unit tests breaks timeutils.utcnow() usage in next case

Bug #1527593 reported by Tomi Juvonen
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Compute (nova)
Fix Released
Medium
Tomi Juvonen

Bug Description

Especially when making new test case that gets executed after case that has made timeutils.set_time_override(), it can fail if timeutils.utcnow() works unexpectedly. To have that working in new case, one has to call timeutils.clear_time_override() before using timeutils.utcnow(). This is what happens otherwise:

    Traceback (most recent call last):
      File "nova/tests/unit/compute/test_compute_api.py", line 2995, in test_host_statuses
        instances)
      File "nova/compute/api.py", line 3386, in get_instances_host_statuses
        host_status = self.get_instance_host_status(instance)
      File "nova/compute/api.py", line 3374, in get_instance_host_status
        instance["services"][0])
      File "nova/servicegroup/api.py", line 89, in service_is_up
        return self._driver.is_up(member)
      File "nova/servicegroup/drivers/db.py", line 79, in is_up
        elapsed = timeutils.delta_seconds(last_heartbeat, timeutils.utcnow())
      File "/home/tojuvone/nova/.tox/py27/lib/python2.7/site-packages/oslo_utils/timeutils.py", line 297, in delta_seconds
        delta = after - before
    TypeError: can't subtract offset-naive and offset-aware datetimes

Cases using timeutils.set_time_override() should also make timeutils.clear_time_override() at the end.

Tags: testing
Revision history for this message
Tomi Juvonen (tomi-juvonen-q) wrote :

Example here: /nova/nova/tests/unit/compute/test_compute_api.py
Hit that in: https://review.openstack.org/241562

Changed in nova:
assignee: nobody → Tomi Juvonen (tomi-juvonen-q)
tags: added: testing
Revision history for this message
Markus Zoeller (markus_z) (mzoeller) wrote :
Changed in nova:
status: New → Confirmed
importance: Undecided → Medium
status: Confirmed → In Progress
Revision history for this message
Markus Zoeller (markus_z) (mzoeller) wrote :

@Tomi: Thanks for working on that. Since you set yourself as assignee, please provide a patch in the near future for that.

Revision history for this message
Tomi Juvonen (tomi-juvonen-q) wrote :

Thanks Markus, I will.

Revision history for this message
Tomi Juvonen (tomi-juvonen-q) wrote :

Somehow I cannot get reproduced this now. I know in oslo_utils/fixture.py it has the clear_time_override:
class TimeFixture(fixtures.Fixture):
    """A fixture for overriding the time returned by timeutils.utcnow().

    :param override_time: datetime instance or list thereof. If not given,
                          defaults to the current UTC time.

    """

    def __init__(self, override_time=None):
        super(TimeFixture, self).__init__()
        self._override_time = override_time

    def setUp(self):
        super(TimeFixture, self).setUp()
        timeutils.set_time_override(self._override_time)
        self.addCleanup(timeutils.clear_time_override)

Before couple of rebase I had other test case effecting to next, but doesn't occur now. timeutils.utcnow() will show wrong only within one test case method and that is no bug. Didn't find recent changes from timeutils.py nor fixture.py.

Revision history for this message
Hans Lindgren (hanlind) wrote :

See https://review.openstack.org/237616 that merged the same day you filed this bug.

Revision history for this message
Tomi Juvonen (tomi-juvonen-q) wrote :

Yes, this solves the problem.

Changed in nova:
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.