]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Replace tearDown with addCleanup - Part 3
authorJulia Varlamova <jvarlamova@mirantis.com>
Tue, 18 Feb 2014 11:17:44 +0000 (15:17 +0400)
committerJulia Varlamova <jvarlamova@mirantis.com>
Thu, 3 Jul 2014 11:49:56 +0000 (15:49 +0400)
Replace tearDown with addCleanup in some cinder unit tests.

Infra team has indicated that tearDown should not be used and should
be replaced with addCleanup in all places.

Implements blueprint replace-teardown-with-addcleanup

Change-Id: I8469ffb5c896d40f9979cd6d1f8da81be763ec14

cinder/tests/api/v2/test_volumes.py
cinder/tests/test_backup_swift.py
cinder/tests/test_create_volume_flow.py
cinder/tests/test_emc_smis.py
cinder/tests/test_hds.py
cinder/tests/test_iscsi.py
cinder/tests/test_volume_utils.py

index 7263eedcc2d758e5e8d0db62b75df79c4e84fc84..42c1acca28a6bbee7e4856a6ae4cc9a54698f5a3 100644 (file)
@@ -59,6 +59,7 @@ def stub_snapshot_get(self, context, snapshot_id):
 class VolumeApiTest(test.TestCase):
     def setUp(self):
         super(VolumeApiTest, self).setUp()
+        self.addCleanup(fake_notifier.reset)
         self.ext_mgr = extensions.ExtensionManager()
         self.ext_mgr.extensions = {}
         fake_image.stub_out_image_service(self.stubs)
@@ -66,17 +67,12 @@ class VolumeApiTest(test.TestCase):
 
         self.flags(host='fake',
                    notification_driver=[fake_notifier.__name__])
-
         self.stubs.Set(db, 'volume_get_all', stubs.stub_volume_get_all)
         self.stubs.Set(volume_api.API, 'delete', stubs.stub_volume_delete)
         self.stubs.Set(db, 'service_get_all_by_topic',
                        stubs.stub_service_get_all_by_topic)
         self.maxDiff = None
 
-    def tearDown(self):
-        super(VolumeApiTest, self).tearDown()
-        fake_notifier.reset()
-
     def test_volume_create(self):
         self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get)
         self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)
index 202d175e5dc575564e6f00ea303e1db13f694731..b94ffa6cd1a743923d7b49e4739babe0a50d3936 100644 (file)
@@ -71,13 +71,10 @@ class BackupSwiftTestCase(test.TestCase):
 
         self._create_volume_db_entry()
         self.volume_file = tempfile.NamedTemporaryFile()
+        self.addCleanup(self.volume_file.close)
         for i in xrange(0, 128):
             self.volume_file.write(os.urandom(1024))
 
-    def tearDown(self):
-        self.volume_file.close()
-        super(BackupSwiftTestCase, self).tearDown()
-
     def test_backup_uncompressed(self):
         self._create_backup_db_entry()
         self.flags(backup_compression_algorithm='none')
index 13a3b8691b324664f988295d8b2a71992b99f5f0..a4e4babd76e865fa91ed2bd59eef273bc82b3f74 100644 (file)
@@ -104,7 +104,3 @@ class CreateVolumeFlowTestCase(test.TestCase):
             fake_db())
 
         task._cast_create_volume(self.ctxt, spec, props)
-
-    def tearDown(self):
-        self.stubs.UnsetAll()
-        super(CreateVolumeFlowTestCase, self).tearDown()
index 8946e6205a81c2d5d4dd433049c92708057f7259..b3ab58a99bbe31c864dc486789cf082fbfbb256a 100644 (file)
@@ -899,9 +899,11 @@ class EMCSMISISCSIDriverTestCase(test.TestCase):
         self.data = EMCSMISCommonData()
 
         self.tempdir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, self.tempdir)
         super(EMCSMISISCSIDriverTestCase, self).setUp()
         self.config_file_path = None
         self.create_fake_config_file()
+        self.addCleanup(os.remove, self.config_file_path)
 
         configuration = mock.Mock()
         configuration.cinder_emc_config_file = self.config_file_path
@@ -1089,16 +1091,6 @@ class EMCSMISISCSIDriverTestCase(test.TestCase):
                           self.data.failed_extend_vol,
                           '10')
 
-    def _cleanup(self):
-        bExists = os.path.exists(self.config_file_path)
-        if bExists:
-            os.remove(self.config_file_path)
-        shutil.rmtree(self.tempdir)
-
-    def tearDown(self):
-        self._cleanup()
-        super(EMCSMISISCSIDriverTestCase, self).tearDown()
-
 
 class EMCSMISFCDriverTestCase(test.TestCase):
 
index 290da4b21a45d52a494e9eca8b76b2ee363efcfb..e2cd2d64004e5dddc95741b8ad0be4abcf4c5087 100644 (file)
@@ -167,6 +167,7 @@ class HUSiSCSIDriverTest(test.TestCase):
     def setUp(self):
         super(HUSiSCSIDriverTest, self).setUp()
         (handle, self.config_file) = tempfile.mkstemp('.xml')
+        self.addCleanup(os.remove, self.config_file)
         os.write(handle, CONF)
         os.close(handle)
         SimulatedHusBackend.alloc_lun = []
@@ -179,11 +180,7 @@ class HUSiSCSIDriverTest(test.TestCase):
         self.configuration = mox.MockObject(conf.Configuration)
         self.configuration.hds_cinder_config_file = self.config_file
         self.driver = hds.HUSDriver(configuration=self.configuration)
-
-    def tearDown(self):
-        os.remove(self.config_file)
-        self.mox.UnsetStubs()
-        super(HUSiSCSIDriverTest, self).tearDown()
+        self.addCleanup(self.mox.UnsetStubs)
 
     def test_get_volume_stats(self):
         stats = self.driver.get_volume_stats(True)
index d24989be203f7d992c14034989b3b91fe25acbfe..169738851156d894ea0a7caf9accff46da93db48 100644 (file)
@@ -49,6 +49,8 @@ class TargetAdminTestCase(object):
                        self.fake_verify_backing_lun)
         self.driver = driver.ISCSIDriver()
         self.flags(iscsi_target_prefix='iqn.2011-09.org.foo.bar:')
+        self.persist_tempdir = tempfile.mkdtemp()
+        self.addCleanup(self._cleanup, self.persist_tempdir)
 
     def fake_verify_backing_lun(obj, iqn, tid):
         return True
@@ -103,13 +105,18 @@ class TargetAdminTestCase(object):
         self.run_commands()
         self.verify()
 
+    def _cleanup(self, persist_tempdir):
+        try:
+            shutil.rmtree(persist_tempdir)
+        except OSError:
+            pass
+
 
 class TgtAdmTestCase(test.TestCase, TargetAdminTestCase):
 
     def setUp(self):
         super(TgtAdmTestCase, self).setUp()
         TargetAdminTestCase.setUp(self)
-        self.persist_tempdir = tempfile.mkdtemp()
         self.flags(iscsi_helper='tgtadm')
         self.flags(volumes_dir=self.persist_tempdir)
         self.script_template = "\n".join([
@@ -119,13 +126,6 @@ class TgtAdmTestCase(test.TestCase, TargetAdminTestCase):
             '--delete %(target_name)s',
             'tgtadm --lld iscsi --op show --mode target'])
 
-    def tearDown(self):
-        try:
-            shutil.rmtree(self.persist_tempdir)
-        except OSError:
-            pass
-        super(TgtAdmTestCase, self).tearDown()
-
 
 class IetAdmTestCase(test.TestCase, TargetAdminTestCase):
 
@@ -196,7 +196,6 @@ class LioAdmTestCase(test.TestCase, TargetAdminTestCase):
     def setUp(self):
         super(LioAdmTestCase, self).setUp()
         TargetAdminTestCase.setUp(self)
-        self.persist_tempdir = tempfile.mkdtemp()
         self.flags(iscsi_helper='lioadm')
         self.script_template = "\n".join([
             'cinder-rtstool create '
index 5d0d68bc585bf5ff39af90d6bf438304e7cc9408..af5c590023875e1a07391c78851f1d443d74e5fb 100644 (file)
@@ -48,6 +48,7 @@ class UsageInfoTestCase(test.TestCase):
 
     def setUp(self):
         super(UsageInfoTestCase, self).setUp()
+        self.addCleanup(fake_notifier.reset)
         self.flags(host='fake', notification_driver=["test"])
         self.volume = importutils.import_object(CONF.volume_manager)
         self.user_id = 'fake'
@@ -56,10 +57,6 @@ class UsageInfoTestCase(test.TestCase):
         self.volume_size = 0
         self.context = context.RequestContext(self.user_id, self.project_id)
 
-    def tearDown(self):
-        super(UsageInfoTestCase, self).tearDown()
-        fake_notifier.reset()
-
     def _create_volume(self, params={}):
         """Create a test volume."""
         vol = {}