Clear created attributes when tearing down tests

unittest will keep each test case around after completion pinning any
objects references by attributes on the test case. This can bloat
the memory used during a full test suite run to the point where
the test suite will require much more memory than running all of
the services does. On systems witout lots of memory, test suite
failures will occur as test cases cannot fork to execute programs.

Clearing out __dict__ of any attributes that don't start with a _
(since some are needed by unittest itself) reduces memory
significantly. This does require the super class tearDown to be
called last in some cases however.

Change-Id: I0e660b6c2a77c5613c5a523ba0a64c3d7f6dd128
This commit is contained in:
Johannes Erdfelt 2012-03-06 16:58:03 +00:00
parent 534eff8a6f
commit 8b7fed8c37
4 changed files with 32 additions and 16 deletions

View File

@ -181,18 +181,6 @@ For more information on creating unit tests and utilizing the testing
infrastructure in OpenStack Nova, please read nova/testing/README.rst.
Unit Tests and assertRaises
---------------------------
When asserting that a test should raise an exception, test against the
most specific exception possible. An overly broad exception type (like
Exception) can mask errors in the unit test itself.
Example::
self.assertRaises(exception.InstanceNotFound, db.instance_get_by_uuid,
elevated, instance_uuid)
openstack-common
----------------

View File

@ -177,6 +177,12 @@ class TestCase(unittest.TestCase):
except Exception:
pass
# Delete attributes that don't start with _ so they don't pin
# memory around unnecessarily for the duration of the test
# suite
for key in [k for k in self.__dict__.keys() if k[0] != '_']:
del self.__dict__[key]
def flags(self, **kw):
"""Override flag variables for a test."""
for k, v in kw.iteritems():

View File

@ -33,6 +33,17 @@ Using Fakes
TBD
test.TestCase
-------------
The TestCase class from nova.test (generally imported as test) will
automatically manage self.stubs using the stubout module and self.mox
using the mox module during the setUp step. They will automatically
verify and clean up during the tearDown step.
If using test.TestCase, calling the super class setUp is required and
calling the super class tearDown is required to be last if tearDown
is overriden.
Writing Functional Tests
------------------------
@ -42,3 +53,14 @@ Writing Integration Tests
-------------------------
TBD
Tests and assertRaises
----------------------
When asserting that a test should raise an exception, test against the
most specific exception possible. An overly broad exception type (like
Exception) can mask errors in the unit test itself.
Example::
self.assertRaises(exception.InstanceNotFound, db.instance_get_by_uuid,
elevated, instance_uuid)

View File

@ -135,8 +135,8 @@ class FlatNetworkTestCase(test.TestCase):
is_admin=False)
def tearDown(self):
super(FlatNetworkTestCase, self).tearDown()
self.network.instance_dns_manager.delete_dns_file()
super(FlatNetworkTestCase, self).tearDown()
def test_get_instance_nw_info(self):
fake_get_instance_nw_info = fake_network.fake_get_instance_nw_info
@ -1277,8 +1277,8 @@ class FloatingIPTestCase(test.TestCase):
is_admin=False)
def tearDown(self):
super(FloatingIPTestCase, self).tearDown()
self.network.floating_dns_manager.delete_dns_file()
super(FloatingIPTestCase, self).tearDown()
def test_double_deallocation(self):
instance_ref = db.api.instance_create(self.context,
@ -1480,8 +1480,8 @@ class InstanceDNSTestCase(test.TestCase):
is_admin=False)
def tearDown(self):
super(InstanceDNSTestCase, self).tearDown()
self.network.instance_dns_manager.delete_dns_file()
super(InstanceDNSTestCase, self).tearDown()
def test_dns_domains_private(self):
zone1 = 'testzone'
@ -1525,10 +1525,10 @@ class LdapDNSTestCase(test.TestCase):
self.driver.create_domain(domain2)
def tearDown(self):
super(LdapDNSTestCase, self).tearDown()
self.driver.delete_domain(domain1)
self.driver.delete_domain(domain2)
sys.modules['ldap'] = self.saved_ldap
super(LdapDNSTestCase, self).tearDown()
def test_ldap_dns_domains(self):
domains = self.driver.get_domains()