Replace tearDown with addCleanup in magnum unit tests

Infra team has indicated that tearDown methods should be replaced
with addCleanup in unit tests. The reason is that all addCleanup
methods will be executed even if one of them fails, while a failure
in tearDown method can leave the rest of the tearDown un-executed,
which can leave stale state laying around.

Moreover, tearDown methods won't run if an exception raises in setUp
method, while addCleanup will run in such case.

So, we should replace tearDown with addCleanup methods.

Change-Id: I3d16954ef7e2fd229f779d193f24ac01d1d2f641
Closes-Bug: #1476976
This commit is contained in:
Hua Wang 2015-07-16 03:03:43 +08:00
parent 9ef3f748f8
commit 37af2d70f9
2 changed files with 12 additions and 10 deletions

View File

@ -89,14 +89,15 @@ class TestCase(base.BaseTestCase):
objects_base.MagnumObjectRegistry._registry._obj_classes)
self.addCleanup(self._restore_obj_registry)
def reset_pecan():
pecan.set_config({}, overwrite=True)
self.addCleanup(reset_pecan)
def _restore_obj_registry(self):
objects_base.MagnumObjectRegistry._registry._obj_classes \
= self._base_test_obj_backup
def tearDown(self):
super(TestCase, self).tearDown()
pecan.set_config({}, overwrite=True)
def config(self, **kw):
"""Override config options for a test."""
group = kw.pop('group', None)

View File

@ -172,12 +172,13 @@ class TestBayResource(BaseMagnumClient):
self.baymodel = self._create_baymodel('testbay')
def tearDown(self):
super(TestBayResource, self).tearDown()
try:
self.cs.baymodels.delete(self.baymodel.uuid)
except exceptions.BadRequest:
pass
def delete_baymodel():
try:
self.cs.baymodels.delete(self.baymodel.uuid)
except exceptions.BadRequest:
pass
self.addCleanup(delete_baymodel)
def test_bay_create_and_delete(self):
bay = self._create_bay('testbay', self.baymodel.uuid)