unit test contextmanager: trying to delete resource hides original errors

Bug #1295887 reported by Akihiro Motoki
14
This bug affects 2 people
Affects Status Importance Assigned to Milestone
neutron
Fix Released
Low
Akihiro Motoki

Bug Description

In unit tests, resource contextmanagers such as network(), subnet() try to delete themselves after returning from yield even if an exception occurs. However when an exception occurs, there is a case where deletion fails. In this case original exception will be hidden and it makes difficult to debug test failures.

Before each time starts, resources like database entries will be recreated, so there is no need to try to delete resources even when an exception occurs.

For example, there is a test with programming error below:

    def test_create_dummy(self):
        with self.network() as network:
            port_res = self._create_port(self.fmt, network['network']['id'])
            port = self.deserialize(self.fmt, port_res)
            # --> Some programming error!!!!
            self.assertEqual(20, hoge)
            self._delete('ports', port['port']['id'])

When running this unit tests, we will get the following error. It is hard to understand.

Traceback (most recent call last):
  File "neutron/tests/unit/test_db_plugin.py", line 816, in test_create_dummy
    self._delete('ports', port['port']['id'])
  File "/usr/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "neutron/tests/unit/test_db_plugin.py", line 534, in network
    self._delete('networks', network['network']['id'])
  File "neutron/tests/unit/test_db_plugin.py", line 450, in _delete
    self.assertEqual(res.status_int, expected_code)
  File "/home/ubuntu/neutron/.tox/py27/local/lib/python2.7/site-packages/testtools/testcase.py", line 321, in assertEqual
    self.assertThat(observed, matcher, message)
  File "/home/ubuntu/neutron/.tox/py27/local/lib/python2.7/site-packages/testtools/testcase.py", line 406, in assertThat
    raise mismatch_error
MismatchError: 409 != 204

It is better we have the original exception:

Traceback (most recent call last):
  File "neutron/tests/unit/test_db_plugin.py", line 809, in test_create_dummy
    self.assertEqual(20, hoge)
NameError: global name 'hoge' is not defined

Tags: unittest
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to neutron (master)

Fix proposed to branch: master
Review: https://review.openstack.org/82259

Changed in neutron:
assignee: nobody → Akihiro Motoki (amotoki)
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to neutron (master)

Reviewed: https://review.openstack.org/82259
Committed: https://git.openstack.org/cgit/openstack/neutron/commit/?id=9730c185847e1cc3fe0ced10f98a14a112e23a07
Submitter: Jenkins
Branch: master

commit 9730c185847e1cc3fe0ced10f98a14a112e23a07
Author: Akihiro Motoki <email address hidden>
Date: Sat Mar 22 08:17:20 2014 +0900

    UT: do not hide an original error in test resource ctxtmgr

    In unit tests, resource contextmanagers such as network(), subnet()
    try to delete themselves after returning from yield even if an
    exception occurs. However when an exception occurs, there is a case
    where deletion fails. In this case original exception will be hidden
    and it makes difficult to debug test failures.

    Before each test starts, resources like database entries will be
    recreated, so there is no need to try to delete resources even
    when an exception occurs. This commit removes try-finally clause
    from resource contextmanagers to make original errors visible.

    Closes-Bug: #1295887
    Change-Id: Ia844d2aa2c9fc036e643068c5284f64798963ee3

Changed in neutron:
status: In Progress → Fix Committed
Akihiro Motoki (amotoki)
Changed in neutron:
milestone: none → icehouse-rc1
Thierry Carrez (ttx)
Changed in neutron:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in neutron:
milestone: icehouse-rc1 → 2014.1
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

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